Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
6c43ee8fd1 | |||
db970b4c69 | |||
a2c17d4be3 | |||
28b21b8e12 |
@ -40,6 +40,7 @@ abstract class BaseController extends AbstractController
|
|||||||
|
|
||||||
foreach ($configuration as $config) {
|
foreach ($configuration as $config) {
|
||||||
$name = $config['name'];
|
$name = $config['name'];
|
||||||
|
// Excluding the checkbox type from checking, because it will be null if not checked
|
||||||
if (null !== $result[$name] || CheckboxType::class == $config['type']) {
|
if (null !== $result[$name] || CheckboxType::class == $config['type']) {
|
||||||
$param->set($name, $result[$name]);
|
$param->set($name, $result[$name]);
|
||||||
}
|
}
|
||||||
|
41
Controller/ParameterController.php
Normal file
41
Controller/ParameterController.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ardent\ParameterBundle\Controller;
|
||||||
|
|
||||||
|
|
||||||
|
use Ardent\ParameterBundle\Service\ParameterService;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
|
||||||
|
class ParameterController extends BaseController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Route("/{category}", name="parameter_bundle_parameters")
|
||||||
|
*/
|
||||||
|
public function parameters($category,
|
||||||
|
ParameterService $param,
|
||||||
|
Request $request)
|
||||||
|
{
|
||||||
|
$configs = $param->getConfig();
|
||||||
|
|
||||||
|
// Handle all special cases for the name
|
||||||
|
/*if ('list' === $name) { // List all the categories
|
||||||
|
$routes = [];
|
||||||
|
foreach ($configs as $key => $config) {
|
||||||
|
$routes[] = ['route' => 'parameters', 'params' => ['name' => $key], 'title' => $key];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('list.routes.twig', ['routes' => $routes]);
|
||||||
|
} else*/if ('all' === $category) { // Show all parameters from all categories
|
||||||
|
$allConfigs = [];
|
||||||
|
foreach ($configs as $config) {
|
||||||
|
$allConfigs = array_merge($allConfigs, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::baseIndex($param, $request, $allConfigs);
|
||||||
|
} else { // Show the parameters from one category
|
||||||
|
return parent::baseIndex($param, $request, $configs[$category]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Ardent\ParameterBundle\DependencyInjection;
|
namespace Ardent\ParameterBundle\DependencyInjection;
|
||||||
|
|
||||||
use Ardent\ParameterBundle\Entity\Parameter;
|
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Extension\Extension;
|
use Symfony\Component\DependencyInjection\Extension\Extension;
|
||||||
@ -14,8 +13,14 @@ class ArdentParameterExtension extends Extension
|
|||||||
{
|
{
|
||||||
$loader = new YamlFileLoader(
|
$loader = new YamlFileLoader(
|
||||||
$container,
|
$container,
|
||||||
new FileLocator(__DIR__.'/../Resources/config')
|
new FileLocator(dirname(__DIR__).'/Resources/config')
|
||||||
);
|
);
|
||||||
$loader->load('services.yaml');
|
$loader->load('services.yaml');
|
||||||
|
|
||||||
|
$configuration = new Configuration();
|
||||||
|
$config = $this->processConfiguration($configuration, $configs);
|
||||||
|
|
||||||
|
$def = $container->findDefinition('ardent.parameter');
|
||||||
|
$def->setArgument('$config', $config['parameters']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
38
DependencyInjection/Configuration.php
Normal file
38
DependencyInjection/Configuration.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Ardent\ParameterBundle\DependencyInjection;
|
||||||
|
|
||||||
|
|
||||||
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||||
|
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||||
|
|
||||||
|
class Configuration implements ConfigurationInterface
|
||||||
|
{
|
||||||
|
public const TYPE_TEXT = 'text';
|
||||||
|
public const TYPE_NUMBER = 'number';
|
||||||
|
|
||||||
|
public function getConfigTreeBuilder()
|
||||||
|
{
|
||||||
|
$treeBuilder = new TreeBuilder('ardent_parameter');
|
||||||
|
|
||||||
|
$treeBuilder->getRootNode()
|
||||||
|
->children()
|
||||||
|
->arrayNode('parameters')
|
||||||
|
->arrayPrototype()
|
||||||
|
->arrayPrototype()
|
||||||
|
->children()
|
||||||
|
->scalarNode('name')->end()
|
||||||
|
->enumNode('type')
|
||||||
|
->values([self::TYPE_TEXT, self::TYPE_NUMBER])
|
||||||
|
->defaultValue(self::TYPE_TEXT)
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end();
|
||||||
|
|
||||||
|
return $treeBuilder;
|
||||||
|
}
|
||||||
|
}
|
@ -4,4 +4,9 @@ services:
|
|||||||
|
|
||||||
Ardent\ParameterBundle\Service\ParameterService:
|
Ardent\ParameterBundle\Service\ParameterService:
|
||||||
public: true
|
public: true
|
||||||
|
autowire: true
|
||||||
|
|
||||||
|
Ardent\ParameterBundle\Controller\:
|
||||||
|
resource: '../../Controller'
|
||||||
|
tags: ['controller.service_arguments']
|
||||||
autowire: true
|
autowire: true
|
@ -2,9 +2,12 @@
|
|||||||
|
|
||||||
namespace Ardent\ParameterBundle\Service;
|
namespace Ardent\ParameterBundle\Service;
|
||||||
|
|
||||||
|
use Ardent\ParameterBundle\DependencyInjection\Configuration;
|
||||||
use Ardent\ParameterBundle\Entity\Parameter;
|
use Ardent\ParameterBundle\Entity\Parameter;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
|
||||||
class ParameterService
|
class ParameterService
|
||||||
{
|
{
|
||||||
@ -13,21 +16,25 @@ class ParameterService
|
|||||||
|
|
||||||
/** @var ParameterBagInterface */
|
/** @var ParameterBagInterface */
|
||||||
private $parameter;
|
private $parameter;
|
||||||
|
private $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ParameterService constructor.
|
* ParameterService constructor.
|
||||||
*
|
*
|
||||||
* @param EntityManagerInterface $em
|
* @param EntityManagerInterface $em
|
||||||
|
* @param ParameterBagInterface $parameter
|
||||||
|
* @param $config
|
||||||
*/
|
*/
|
||||||
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameter)
|
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameter, $config)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->parameter = $parameter;
|
$this->parameter = $parameter;
|
||||||
|
$this->config = $this->supplementConfig($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param bool $parse
|
* @param bool $parse
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
@ -75,4 +82,41 @@ class ParameterService
|
|||||||
$this->em->persist($parameter);
|
$this->em->persist($parameter);
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the types in the config by their classes
|
||||||
|
*
|
||||||
|
* @param $config
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function supplementConfig($config)
|
||||||
|
{
|
||||||
|
foreach ($config as $groupName => &$group) {
|
||||||
|
foreach ($group as &$value) {
|
||||||
|
switch ($value['type']) {
|
||||||
|
default:
|
||||||
|
case Configuration::TYPE_TEXT:
|
||||||
|
$class = TextType::class;
|
||||||
|
break;
|
||||||
|
case Configuration::TYPE_NUMBER:
|
||||||
|
$class = NumberType::class;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value['name'] = sprintf('%s_%s', $groupName, $value['name']);
|
||||||
|
$value['type'] = $class;
|
||||||
|
unset($value);
|
||||||
|
}
|
||||||
|
unset($group);
|
||||||
|
}
|
||||||
|
return $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getConfig()
|
||||||
|
{
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ardent/parameter-bundle",
|
"name": "ardent/parameter-bundle",
|
||||||
"description": "Simple bundle for storing parameters in the database",
|
"description": "Simple bundle for storing parameters in a database",
|
||||||
"type": "symfony-bundle",
|
"type": "symfony-bundle",
|
||||||
"require": {
|
"require": {
|
||||||
"doctrine/orm": "^2.7",
|
"doctrine/orm": "^2.7",
|
||||||
|
Reference in New Issue
Block a user