diff --git a/Command/ContainerInvalidateCommand.php b/Command/ContainerInvalidateCommand.php deleted file mode 100644 index ba36dd9..0000000 --- a/Command/ContainerInvalidateCommand.php +++ /dev/null @@ -1,43 +0,0 @@ -setName('unifik:container:invalidate') - ->setDescription('Invalidate the container cache') - ->setHelp(<<unifik:container:invalidate invalidate (clear) the container cache. - -php app/console unifik:container:invalidate --env=dev -EOF - ) - ; - } - - /** - * @see Command - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $kernel = $this->getContainer()->get('kernel'); - $output->writeln(sprintf('Invalidating container for the %s environment', $kernel->getEnvironment())); - - $this->getContainer()->get('unifik_database_config.container_invalidator')->invalidate(); - } - -} diff --git a/Controller/ConfiguratorController.php b/Controller/ConfiguratorController.php index 9a76da0..b07f287 100644 --- a/Controller/ConfiguratorController.php +++ b/Controller/ConfiguratorController.php @@ -14,30 +14,25 @@ use Unifik\DatabaseConfigBundle\Form\ConfiguratorType; /** - * Locale Controller + * Configurator Controller + * + * @package Unifik.DatabaseConfigBundle.Controller + * + * @author Guillaume Petit */ class ConfiguratorController extends Controller { - /** - * @return Response - */ - public function indexAction() - { - return $this->render('UnifikDatabaseConfigBundle::index.html.twig', array( - 'bundles' => $this->getConfiguratorEnabledBundles() - )); - } - /** * Display a form to edit the configuration of a bundle * - * @param Request $request - * @param string $bundleName + * @param Request $request the request + * @param string $bundleName the bundle name to be configured + * @param string $namespace the namespace of the extension * * @return Response */ - public function editAction(Request $request, $bundleName) + public function editAction(Request $request, $bundleName, $namespace) { $extensionRepository = $this->getDoctrine()->getRepository('UnifikDatabaseConfigBundle:Extension'); $configRepository = $this->getDoctrine()->getRepository('UnifikDatabaseConfigBundle:Config'); @@ -45,12 +40,18 @@ public function editAction(Request $request, $bundleName) $manager = $this->getDoctrine()->getManager(); $bundles = $this->get('kernel')->getBundles(); - $tree = $this->getConfigurationTree($bundles[$bundleName]); - $extension = $extensionRepository->findOneByName($tree->getName()); + $tree = $this->get('unifik_database_config.services.configuration')->getContainerConfigurationTree($bundles[$bundleName]); + $extension = $extensionRepository->findOneBy( + array( + 'name' => $tree->getName(), + 'namespace' => $namespace, + ) + ); if (false == $extension) { $extension = new Extension(); $extension->setName($tree->getName()); + $extension->setNamespace($namespace); } $form = $this->createForm(new ConfiguratorType(), $extension, array('tree' => $tree)); @@ -66,67 +67,21 @@ public function editAction(Request $request, $bundleName) $manager->persist($extension); $manager->flush($extension); - - $this->get('unifik_database_config.container_invalidator')->invalidate(); - } - } - - return $this->render('UnifikDatabaseConfigBundle::edit.html.twig', array( - 'form' => $form->createView(), - 'bundles' => $this->getConfiguratorEnabledBundles() - )); - } - - /** - * Check each bundle currently loaded in the kernel and validate configurator support - * - * @return array - */ - protected function getConfiguratorEnabledBundles() - { - $enabledBundles = array(); - $bundles = $this->get('kernel')->getBundles(); - - foreach ($bundles as $name => $bundle) { - try { - if ($tree = $this->getConfigurationTree($bundle)) { - if ($tree && $this->isConfiguratorEnabledNode($tree)) { - $enabledBundles[] = $name; - } - } - } catch (\Exception $e) { - // skip error'd bundle - } - } - - return $enabledBundles; - } - - /** - * Return the configuration tree of a bundle or false if not defined - * - * @param BundleInterface $bundle - * - * @return mixed - */ - protected function getConfigurationTree(BundleInterface $bundle) - { - $extension = $bundle->getContainerExtension(); - - if ($extension) { - $configuration = $extension->getConfiguration(array(), new ContainerBuilder()); - if ($configuration) { - return $configuration->getConfigTreeBuilder()->buildTree(); } } - return false; + return $this->render( + 'UnifikDatabaseConfigBundle::edit.html.twig', + array( + 'form' => $form->createView(), + ) + ); } /** * Check if a tree node is configuration enabled * - * @param NodeInterface $arrayNode + * @param NodeInterface $arrayNode a node * * @return bool */ diff --git a/DependencyInjection/Compiler/ContainerBuilder.php b/DependencyInjection/Compiler/ContainerBuilder.php deleted file mode 100644 index 42544a5..0000000 --- a/DependencyInjection/Compiler/ContainerBuilder.php +++ /dev/null @@ -1,230 +0,0 @@ -setProxyInstantiator(new RuntimeInstantiator()); - } - } - - /** - * Compiles the container. - * - * This method adds parameters and configs from the database before calling compiler passes - */ - public function compile() - { - try { - $this->initConnection(); - $this->addDbParameters(); - $this->addDbConfig(); - $this->closeConnection(); - } catch (\PDOException $e) { - parent::compile(); - return; - } - - parent::compile(); - } - - /** - * Initializes the database connection - */ - protected function initConnection() - { - $configs = $this->getExtensionConfig('doctrine'); - - $mergedConfig = array(); - foreach ($configs as $config) { - $mergedConfig = array_merge($mergedConfig, $config); - } - - $mergedConfig = $this->getParameterBag()->resolveValue($mergedConfig); - - $params = $mergedConfig['dbal']; - - if (array_key_exists('connections', $params)) { - $defaultEntityManager = $mergedConfig['orm']['default_entity_manager']; - $defaultConnection = $mergedConfig['entity_managers'][$defaultEntityManager]['connection']; - $params = $params['connections'][$defaultConnection]; - } - - $connection_factory = new ConnectionFactory(array()); - $this->databaseConnection = $connection_factory->createConnection($params); - $this->databaseConnection->connect(); - } - - /** - * Closes the database connection - */ - protected function closeConnection() - { - if ($this->databaseConnection->isConnected()) { - $this->databaseConnection->close(); - } - } - - /** - * Check if a given table name exist in the database - * - * @param string $table - * - * @return bool - */ - protected function checkTableExist($table) - { - $queryBuilder = $this->databaseConnection->createQueryBuilder(); - $queryBuilder->select('*'); - $queryBuilder->from($table, 't'); - - try { - $this->databaseConnection->query($queryBuilder); - } catch (DBALException $e) { - return false; - } - - return true; - } - - /** - * Returns the query used to get the configs from the database - * - * @return QueryBuilder - */ - protected function createConfigQuery() - { - $queryBuilder = $this->databaseConnection->createQueryBuilder(); - - $queryBuilder - ->select('e.id AS extension_id, e.name AS extension_name, c.parent_id, p.name AS parent_name, c.id, c.name, c.value') - ->from('container_config', 'c') - ->innerJoin('c', 'container_extension', 'e', 'e.id = c.extension_id') - ->leftJoin('c', 'container_config', 'p', 'p.id = c.parent_id') - ->orderBy('e.id') - ->addOrderBy('c.parent_id') - ->addOrderBy('c.id'); - - return $queryBuilder; - } - - /** - * Adds configs from the database to the current configs - */ - protected function addDbConfig() - { - if (false === $this->checkTableExist('container_config')) { - return; - } - - $query = $this->databaseConnection->query($this->createConfigQuery()); - - $currentExtension = null; - $extensions = array(); - $configs = array(); - - while (false !== $result = $query->fetchObject()) { - - if ($currentExtension != $result->extension_id) { - // The current extension has changed. We have to create a new Extension - $currentExtension = $result->extension_id; - $extension = new Extension(); - $extension->setName($result->extension_name); - $extensions[$currentExtension] = $extension; - } - - // New Config object - $config = new Config(); - $config->setName($result->name); - $config->setValue($result->value); - - - if (null !== $result->parent_id) { - // The current config has a parent. We set the parent and the child - $parentConfig = $configs[$result->parent_id]; - $parentConfig->addChildren($config); - $config->setParent($parentConfig); - } else { - // The current config has no parent so we link it to the extension. - // (We should always link the config to an extension even if it has a parent but it makes it easier to build the config tree that way) - $config->setExtension($extensions[$currentExtension]); - $extensions[$currentExtension]->addConfig($config); - } - - // Store the new config in the configs array to keep it for further use if it has children - $configs[$result->id] = $config; - } - - foreach ($extensions as $extension) { - $values = array(); - - // Loop through configs without parent to get their config trees - foreach ($extension->getConfigs() as $config) { - $values[$config->getName()] = $config->getConfigTree(); - } - - // Adds the new config loaded from the database to the config of the extension - $this->loadFromExtension($extension->getName(), $values); - } - } - - /** - * Returns the query used to get parameters from the database - * - * @return QueryBuilder - */ - protected function createParametersQuery() - { - $queryBuilder = $this->databaseConnection->createQueryBuilder(); - - $queryBuilder - ->select('p.name, p.value') - ->from('container_parameter', 'p'); - - return $queryBuilder; - } - - /** - * Adds the parameters from the database to the container's parameterBag - */ - protected function addDbParameters() - { - if (false === $this->checkTableExist('container_parameter')) { - return; - } - - $query = $this->databaseConnection->query($this->createParametersQuery()); - - while (false !== $result = $query->fetchObject()) { - $this->setParameter($result->name, $result->value); - } - } - -} diff --git a/Entity/Config.php b/Entity/Config.php index 16e9062..2cd2732 100644 --- a/Entity/Config.php +++ b/Entity/Config.php @@ -1,11 +1,14 @@ */ class Config { @@ -47,11 +50,11 @@ public function __construct() { $this->children = new \Doctrine\Common\Collections\ArrayCollection(); } - + /** * Get id * - * @return integer + * @return integer */ public function getId() { @@ -61,20 +64,21 @@ public function getId() /** * Set name * - * @param string $name + * @param string $name the config item name + * * @return Config */ public function setName($name) { $this->name = $name; - + return $this; } /** * Get name * - * @return string + * @return string */ public function getName() { @@ -84,20 +88,21 @@ public function getName() /** * Set value * - * @param string $value + * @param string $value the config item value + * * @return Config */ public function setValue($value) { $this->value = $value; - + return $this; } /** * Get value * - * @return string + * @return string */ public function getValue() { @@ -107,20 +112,23 @@ public function getValue() /** * Add children * - * @param \Unifik\DatabaseConfigBundle\Entity\Config $children + * @param \Unifik\DatabaseConfigBundle\Entity\Config $children the child to add + * * @return Config */ public function addChildren(\Unifik\DatabaseConfigBundle\Entity\Config $children) { $this->children[] = $children; - + return $this; } /** * Remove children * - * @param \Unifik\DatabaseConfigBundle\Entity\Config $children + * @param \Unifik\DatabaseConfigBundle\Entity\Config $children the child to remove + * + * @return void */ public function removeChildren(\Unifik\DatabaseConfigBundle\Entity\Config $children) { @@ -130,7 +138,7 @@ public function removeChildren(\Unifik\DatabaseConfigBundle\Entity\Config $child /** * Get children * - * @return \Doctrine\Common\Collections\Collection + * @return \Doctrine\Common\Collections\Collection */ public function getChildren() { @@ -140,13 +148,14 @@ public function getChildren() /** * Set parent * - * @param \Unifik\DatabaseConfigBundle\Entity\Config $parent + * @param \Unifik\DatabaseConfigBundle\Entity\Config $parent the parent to set + * * @return Config */ public function setParent(\Unifik\DatabaseConfigBundle\Entity\Config $parent = null) { $this->parent = $parent; - + return $this; } @@ -163,13 +172,14 @@ public function getParent() /** * Set extension * - * @param \Unifik\DatabaseConfigBundle\Entity\Extension $extension + * @param \Unifik\DatabaseConfigBundle\Entity\Extension $extension the extension to set + * * @return Config */ public function setExtension(\Unifik\DatabaseConfigBundle\Entity\Extension $extension = null) { $this->extension = $extension; - + return $this; } @@ -183,7 +193,13 @@ public function getExtension() return $this->extension; } - public function getConfigTree() { + /** + * Return the configuration tree (associative array) + * + * @return multitype:array |string + */ + public function getConfigTree() + { if (count($this->children) > 0) { $configArray = array(); foreach ($this->children as $child) { @@ -199,4 +215,26 @@ public function getConfigTree() { return $this->value; } -} \ No newline at end of file + + /** + * Get config child by name + * + * @param string $configName the config name + * + * @return Config|NULL + */ + public function get($configName) + { + foreach ($this->getChildren() as $config) { + if ($config->getName() == $configName) { + if ($config->getValue() != '') { + return $config->getValue(); + } else { + return $config; + } + } + } + return null; + } + +} diff --git a/Entity/ConfigRepository.php b/Entity/ConfigRepository.php index 8219f39..4196aed 100644 --- a/Entity/ConfigRepository.php +++ b/Entity/ConfigRepository.php @@ -5,8 +5,21 @@ use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Mapping as ORM; +/** ConfigRepository + * + * @package Unifik.DatabaseConfigBundle.Entity + * @author Guillaume Petit + * + */ class ConfigRepository extends EntityRepository { + /** + * Delete configuration by extension + * + * @param integer $extensionId the extension id + * + * @return void + */ public function deleteByExtension($extensionId) { $builder = $this->createQueryBuilder('e') diff --git a/Entity/Extension.php b/Entity/Extension.php index 0834218..3764c80 100644 --- a/Entity/Extension.php +++ b/Entity/Extension.php @@ -6,7 +6,11 @@ use Doctrine\ORM\Mapping as ORM; /** - * Extension + * Extension entity + * + * @package Unifik\DatabaseConfigBundle\Entity + * + * @author Guillaume Petit */ class Extension { @@ -21,6 +25,11 @@ class Extension */ private $name; + /** + * @var string namespace defines the scope of the extension, allowing multiple configurations for an extension + */ + private $namespace; + /** * @var \Doctrine\Common\Collections\Collection */ @@ -34,11 +43,11 @@ public function __construct() { $this->configs = new \Doctrine\Common\Collections\ArrayCollection(); } - + /** * Get id * - * @return integer + * @return integer */ public function getId() { @@ -48,20 +57,21 @@ public function getId() /** * Set name * - * @param string $name + * @param string $name the extension name + * * @return Extension */ public function setName($name) { $this->name = $name; - + return $this; } /** * Get name * - * @return string + * @return string */ public function getName() { @@ -71,7 +81,8 @@ public function getName() /** * Add configs * - * @param \Unifik\DatabaseConfigBundle\Entity\Config $config + * @param \Unifik\DatabaseConfigBundle\Entity\Config $config the root configuration node attached to the extension + * * @return Extension */ public function addConfig(\Unifik\DatabaseConfigBundle\Entity\Config $config) @@ -79,14 +90,16 @@ public function addConfig(\Unifik\DatabaseConfigBundle\Entity\Config $config) $config->setExtension($this); $this->configs[] = $config; - + return $this; } /** * Remove configs * - * @param \Unifik\DatabaseConfigBundle\Entity\Config $configs + * @param \Unifik\DatabaseConfigBundle\Entity\Config $configs the root node of the configuration to remove + * + * @return void */ public function removeConfig(\Unifik\DatabaseConfigBundle\Entity\Config $configs) { @@ -96,7 +109,7 @@ public function removeConfig(\Unifik\DatabaseConfigBundle\Entity\Config $configs /** * Get configs * - * @return \Doctrine\Common\Collections\Collection + * @return \Doctrine\Common\Collections\Collection */ public function getConfigs() { @@ -122,10 +135,55 @@ public function getRootConfigs() } /** - * @param \Doctrine\Common\Collections\Collection $configs + * Set configurations + * + * @param \Doctrine\Common\Collections\Collection $configs the collection of configurations to attach the extension + * + * @return void */ public function setConfigs($configs) { $this->configs = $configs; } + + /** + * Get Namespace + * + * @return string + */ + public function getNamespace() + { + return $this->namespace; + } + + /** + * Set the namespace + * + * @param string $namespace the namespace + * + * @return Config + */ + public function setNamespace($namespace) + { + $this->namespace = $namespace; + + return $this; + } + + /** + * Get a config by its name + * + * @param string $configName the config name + * @return Ambigous <\Doctrine\Common\Collections\ArrayCollection, unknown>|NULL + */ + public function get($configName) + { + foreach ($this->getRootConfigs() as $config) { + if ($config->getName() == $configName) { + return $config; + } + } + return null; + } + } \ No newline at end of file diff --git a/Entity/ExtensionRepository.php b/Entity/ExtensionRepository.php index 7fb74e8..254cf6d 100644 --- a/Entity/ExtensionRepository.php +++ b/Entity/ExtensionRepository.php @@ -5,6 +5,13 @@ use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Mapping as ORM; +/** + * ExtensionRepository + * + * @package Unifik\DatabaseConfigBundle\Entity + * @author Guillaume Petit + * + */ class ExtensionRepository extends EntityRepository { } \ No newline at end of file diff --git a/Entity/Parameter.php b/Entity/Parameter.php deleted file mode 100644 index 8e0f9b8..0000000 --- a/Entity/Parameter.php +++ /dev/null @@ -1,83 +0,0 @@ -id; - } - - /** - * Set name - * - * @param string $name - * @return Parameter - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** - * Get name - * - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * Set value - * - * @param string $value - * @return Parameter - */ - public function setValue($value) - { - $this->value = $value; - - return $this; - } - - /** - * Get value - * - * @return string - */ - public function getValue() - { - return $this->value; - } -} \ No newline at end of file diff --git a/Form/ConfiguratorType.php b/Form/ConfiguratorType.php index d17827c..fbd1dff 100644 --- a/Form/ConfiguratorType.php +++ b/Form/ConfiguratorType.php @@ -18,6 +18,7 @@ use Symfony\Component\Validator\Constraints\NotBlank; use Unifik\DatabaseConfigBundle\Form\DataTransformer\ArrayEntityTransformer; +use Unifik\DatabaseConfigBundle\Form\DataTransformer\BooleanTransformer; /** * This is only a PARTIAL and EXPERIMENTAL implementation of all the features available in the Symfony configuration tree. @@ -107,8 +108,11 @@ protected function nodeToField(NodeInterface $node, FormBuilderInterface $builde 'attr' => array() ); + $transformers = array(); + if ($node instanceof BooleanNode) { $type = 'checkbox'; + $transformers[] = new BooleanTransformer(); } elseif ($node instanceof IntegerNode) { $type = 'number'; } elseif ($node instanceof FloatNode) { @@ -139,7 +143,15 @@ protected function nodeToField(NodeInterface $node, FormBuilderInterface $builde $options['attr']['alt'] = $infos; - $builder->add($node->getName(), $type, $options); + $field = $builder->create($node->getName(), $type, $options); + + foreach ($transformers as $transformer) { + $field->addModelTransformer($transformer); + } + + $builder->add($field); + + } /** diff --git a/Form/DataTransformer/BooleanTransformer.php b/Form/DataTransformer/BooleanTransformer.php new file mode 100644 index 0000000..f79e4a8 --- /dev/null +++ b/Form/DataTransformer/BooleanTransformer.php @@ -0,0 +1,41 @@ + + * + */ +class BooleanTransformer implements DataTransformerInterface +{ + + /** (non-PHPdoc) + * + * @param string $value the value coming from the model + * + * @see \Symfony\Component\Form\DataTransformerInterface::transform() + * @return the value to be used in the view + * + */ + public function transform($value) + { + return (bool)$value; + } + + /** (non-PHPdoc) + * + * @param string $value the value coming from the view + * + * @see \Symfony\Component\Form\DataTransformerInterface::reverseTransform() + * @return the value to store in the model + */ + public function reverseTransform($value) + { + return $value ? '1' : '0'; + } + +} diff --git a/Lib/ContainerInvalidator.php b/Lib/ContainerInvalidator.php deleted file mode 100644 index 951431c..0000000 --- a/Lib/ContainerInvalidator.php +++ /dev/null @@ -1,70 +0,0 @@ -kernel = $kernel; - } - - /** - * Invalidate the routing cache - */ - public function invalidate() - { - $regex = '/app' . $this->getKernel()->getEnvironment() . '(.*)ProjectContainer(.*)/i'; - - foreach ($this->getFiles($regex)->in($this->getKernel()->getCacheDir()) as $file) { - unlink($file); - } - } - - /** - * Return the kernel. - * @return Kernel - */ - protected function getKernel() - { - return $this->kernel; - } - - /** - * Return the cache directory. - * @return string - */ - protected function getKernelCacheDir() - { - return $this->kernel->getCacheDir(); - } - - /** - * @param $regex - * @return Finder - */ - protected function getFiles($regex) - { - return $this->getFinder()->files()->name($regex); - } - - /** - * @return Finder - */ - protected function getFinder() - { - return new Finder(); - } - -} diff --git a/Resources/config/doctrine/Config.orm.yml b/Resources/config/doctrine/Config.orm.yml index 990f31b..46c2d21 100644 --- a/Resources/config/doctrine/Config.orm.yml +++ b/Resources/config/doctrine/Config.orm.yml @@ -1,6 +1,6 @@ Unifik\DatabaseConfigBundle\Entity\Config: type: entity - table: container_config + table: config_configuration fields: id: type: integer diff --git a/Resources/config/doctrine/Extension.orm.yml b/Resources/config/doctrine/Extension.orm.yml index 31ba799..70229b9 100644 --- a/Resources/config/doctrine/Extension.orm.yml +++ b/Resources/config/doctrine/Extension.orm.yml @@ -1,6 +1,6 @@ Unifik\DatabaseConfigBundle\Entity\Extension: type: entity - table: container_extension + table: config_extension fields: id: type: integer @@ -10,6 +10,9 @@ Unifik\DatabaseConfigBundle\Entity\Extension: name: type: string length: 255 + namespace: + type: string + length: 255 lifecycleCallbacks: { } repositoryClass: Unifik\DatabaseConfigBundle\Entity\ExtensionRepository oneToMany: diff --git a/Resources/config/doctrine/Parameter.orm.yml b/Resources/config/doctrine/Parameter.orm.yml deleted file mode 100644 index 948bc60..0000000 --- a/Resources/config/doctrine/Parameter.orm.yml +++ /dev/null @@ -1,16 +0,0 @@ -Unifik\DatabaseConfigBundle\Entity\Parameter: - type: entity - table: container_parameter - fields: - id: - type: integer - id: true - generator: - strategy: AUTO - name: - type: string - length: 255 - value: - type: string - length: 255 - lifecycleCallbacks: { } diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 27262c9..472ded2 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -1,10 +1,5 @@ # Configurator -unifik_configurator: - pattern: /configurator - defaults: { _controller: "UnifikDatabaseConfigBundle:Configurator:index" } - options: { do_not_remove: true } - unifik_configurator_edit: - pattern: /configurator/{bundleName} + pattern: /configurator/{bundleName}/{namespace} defaults: { _controller: "UnifikDatabaseConfigBundle:Configurator:edit" } options: { do_not_remove: true } \ No newline at end of file diff --git a/Resources/config/services.yml b/Resources/config/services.yml index aa69701..ccdf736 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,7 +1,11 @@ -parameters: - unifik_database_config.container_invalidator.class: Unifik\DatabaseConfigBundle\Lib\ContainerInvalidator - services: - unifik_database_config.container_invalidator: - class: %unifik_database_config.container_invalidator.class% - arguments: [ @kernel ] \ No newline at end of file + unifik_database_config.repositories.extension: + class: Doctrine\ORM\EntityRepository + factory_service: doctrine.orm.entity_manager + factory_method: getRepository + arguments: + - Unifik\DatabaseConfigBundle\Entity\Extension + + unifik_database_config.services.configuration: + class: Unifik\DatabaseConfigBundle\Service\ConfigurationService + arguments: [ "@kernel", "@unifik_database_config.repositories.extension", "@logger" ] \ No newline at end of file diff --git a/Resources/views/edit.html.twig b/Resources/views/edit.html.twig index 3a07973..99f2504 100644 --- a/Resources/views/edit.html.twig +++ b/Resources/views/edit.html.twig @@ -1,16 +1,12 @@ -{% extends 'UnifikDatabaseConfigBundle::layout.html.twig' %} {% form_theme form 'UnifikDatabaseConfigBundle::form_theme.html.twig' %} -{% block content %} +{{ form_start(form, { attr: {'novalidate': 'novalidate'} }) }} - {{ form_start(form, { attr: {'novalidate': 'novalidate'} }) }} + {{ form_widget(form) }} - {{ form_widget(form) }} + {% block buttons %} + + {% endblock %} - {% block buttons %} - - {% endblock %} +{{ form_end(form) }} - {{ form_end(form) }} - -{% endblock %} diff --git a/Service/ConfigurationService.php b/Service/ConfigurationService.php new file mode 100644 index 0000000..1a04cae --- /dev/null +++ b/Service/ConfigurationService.php @@ -0,0 +1,168 @@ + + * + */ +class ConfigurationService +{ + /** + * @var ExtensionRepository the repository to the extension doctrine entity + */ + private $extensionRepository; + + /** + * @var AppKernel + */ + private $kernel; + + /** + * @var Logger the logger + */ + private $logger; + + /** + * Constructor + * + * @param \AppKernel $kernel the application kernel + * @param ExtensionRepository $extensionRepository the repository for the extension entity + * @param Logger $logger the logger + * + * @return void + */ + public function __construct(\AppKernel $kernel, ExtensionRepository $extensionRepository, Logger $logger) + { + $this->kernel = $kernel; + $this->extensionRepository = $extensionRepository; + $this->logger = $logger; + } + + /** + * Get configuration value either from database or bundle config + * + * @param string $bundleName the bundle name that defines the default configuration + * @param string $extension the extension name + * @param string $namespace the namespace linked to the extension + * @param string $key the configuration key + * + * @throws \InvalidArgumentException when the configuration key is not found + * @return mixed string|boolean the configuration value or false if it doesn't exists + */ + public function getConfigurationValue($bundleName, $extension, $namespace, $key) + { + $value = ''; + $path = explode('.', $key); + $node = $this->getDefaultConfigurationNode($bundleName, $path); + + if ($node === null) { + throw new \InvalidArgumentException('Configuration key not found: ' . $key); + } + + if (null !== $value = $this->getConfigurationFromDatabase($extension, $namespace, $path)) { + if ($node instanceof BooleanNode) { + $value = (boolean) $value; + } elseif ($node instanceof IntegerNode) { + $value = (integer) $value; + } elseif ($node instanceof FloatNode) { + $value = (float) $value; + } + } else { + $value = $node->getDefaultValue(); + } + return $value; + } + + /** + * Get configuration value from database + * + * @param string $extensionName the extension name + * @param string $namespace the namespace linked to the extension + * @param string $path the configuration path + * + * @return string|null + */ + protected function getConfigurationFromDatabase($extensionName, $namespace, $path) + { + $value = null; + $extension = $this->extensionRepository->findOneBy( + array( + 'name' => $extensionName, + 'namespace' => $namespace, + ) + ); + if ($extension) { + $value = $extension; + foreach ($path as $pathElement) { + $value = $value->get($pathElement); + if ($value == null) { + break; + } + } + } + return $value; + } + + /** + * Get configuration value from default bundle configuration + * + * @param string $bundleName the bundle name that defines the configuration + * @param string $path the path of the configuration key + * + * @return string|null + */ + protected function getDefaultConfigurationNode($bundleName, $path) + { + + $tree = $this->getContainerConfigurationTree($this->kernel->getBundle($bundleName)); + + foreach ($path as $pathElement) { + foreach ($tree->getChildren() as $node) { + if ($node->getName() == $pathElement) { + if ($node instanceof ArrayNode) { + $tree = $node; + } else { + return $node; + } + } + } + } + return null; + } + + /** + * Return the configuration tree of a bundle or false if not defined + * + * @param BundleInterface $bundle a bundle + * + * @return mixed boolean|array + */ + public function getContainerConfigurationTree(BundleInterface $bundle) + { + $extension = $bundle->getContainerExtension(); + + if ($extension) { + $configuration = $extension->getConfiguration(array(), new ContainerBuilder()); + if ($configuration) { + return $configuration->getConfigTreeBuilder()->buildTree(); + } + } + + return false; + } + +} diff --git a/composer.json b/composer.json index 94243aa..bc09062 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "unifik/database-config-bundle", + "name": "evoucher/database-config-bundle", "type": "symfony-bundle", "description": "Allows management of Symfony's container configuration from the database", "keywords": ["unifik", "symfony", "database", "container", "config"], - "homepage": "https://github.com/egzakt/UnifikDatabaseConfigBundle", + "homepage": "https://github.com/evoucher/UnifikDatabaseConfigBundle", "license": "MIT", "authors": [ { @@ -13,6 +13,10 @@ { "name": "Frédéric Hamelin", "email": "fredhamelin91@gmail.com" + }, + { + "name": "Guillaume Petit", + "email": "guillaume.petit@sword-group.com" } ], "require": {