From fb4ce1e0983920fd0147c2d65b81a685adf7bad7 Mon Sep 17 00:00:00 2001 From: Aaron Scherer Date: Mon, 19 Mar 2018 11:58:08 -0700 Subject: [PATCH 1/4] Adding normalization to enabled --- src/DependencyInjection/Configuration.php | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 60b3951..70e4e41 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -12,6 +12,7 @@ namespace Cache\CacheBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\Config\Definition\Builder\NodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -46,6 +47,30 @@ public function getConfigTreeBuilder() return $treeBuilder; } + /** + * {@inheritdoc} + */ + private function normalizeEnabled(NodeDefinition $node) + { + $node->beforeNormalization() + ->always() + ->then( + function ($v) { + if (is_string($v['enabled'])) { + $v['enabled'] = $v['enabled'] === 'true'; + } + if (is_int($v['enabled'])) { + $v['enabled'] = $v['enabled'] === 1; + } + + return $v; + } + ) + ->end(); + + return $this; + } + /** * Configure the "cache.session" section. * @@ -66,6 +91,8 @@ private function addSessionSupportSection() ->scalarNode('ttl')->end() ->end(); + $this->normalizeEnabled($node); + return $node; } @@ -88,6 +115,8 @@ private function addSerializerSection() ->scalarNode('prefix')->defaultValue('')->end() ->end(); + $this->normalizeEnabled($node); + return $node; } @@ -110,6 +139,8 @@ private function addValidationSection() ->scalarNode('prefix')->defaultValue('')->end() ->end(); + $this->normalizeEnabled($node); + return $node; } @@ -132,6 +163,8 @@ private function addAnnotationSection() ->scalarNode('prefix')->defaultValue('')->end() ->end(); + $this->normalizeEnabled($node); + return $node; } @@ -148,8 +181,11 @@ private function addLoggingSection() ->addDefaultsIfNotSet() ->children() ->scalarNode('logger')->defaultValue('logger')->end() + ->scalarNode('level')->defaultValue('info')->end() ->end(); + $this->normalizeEnabled($node); + return $node; } @@ -172,6 +208,8 @@ private function addDoctrineSection() ->end() ->end(); + $this->normalizeEnabled($node); + $types = ['metadata', 'result', 'query']; foreach ($types as $type) { $node->children() @@ -234,6 +272,8 @@ private function addRouterSection() ->scalarNode('prefix')->defaultValue('')->end() ->end(); + $this->normalizeEnabled($node); + return $node; } @@ -246,10 +286,14 @@ private function addDataCollectorSection() $node = $tree->root('data_collector'); $node + ->canBeEnabled() + ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->end() ->end(); + $this->normalizeEnabled($node); + return $node; } } From ed6da2244d7af08f6ab2cdcece89fbba884d55f3 Mon Sep 17 00:00:00 2001 From: Aaron Scherer Date: Mon, 19 Mar 2018 12:01:57 -0700 Subject: [PATCH 2/4] Adding docblock --- src/DependencyInjection/Configuration.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 70e4e41..38b77dc 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -48,7 +48,11 @@ public function getConfigTreeBuilder() } /** - * {@inheritdoc} + * Normalizes the enabled field to be truthy + * + * @param NodeDefinition $node + * + * @return Configuration */ private function normalizeEnabled(NodeDefinition $node) { From 694cab2d2b93782e864888f6f382f11f2e0aa819 Mon Sep 17 00:00:00 2001 From: Aaron Scherer Date: Mon, 19 Mar 2018 13:13:10 -0700 Subject: [PATCH 3/4] Registering CacheFlushCommand --- src/DependencyInjection/CacheExtension.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/DependencyInjection/CacheExtension.php b/src/DependencyInjection/CacheExtension.php index 3c8e363..3e4aab9 100644 --- a/src/DependencyInjection/CacheExtension.php +++ b/src/DependencyInjection/CacheExtension.php @@ -13,6 +13,7 @@ use Cache\Bridge\Doctrine\DoctrineCacheBridge; use Cache\CacheBundle\Bridge\SymfonyValidatorBridge; +use Cache\CacheBundle\Command\CacheFlushCommand; use Cache\CacheBundle\Factory\DoctrineBridgeFactory; use Cache\CacheBundle\Factory\RouterFactory; use Cache\CacheBundle\Factory\SessionHandlerFactory; @@ -21,6 +22,7 @@ use Cache\SessionHandler\Psr6SessionHandler; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -69,6 +71,9 @@ public function load(array $configs, ContainerBuilder $container) $serviceIds = []; $this->findServiceIds($config, $serviceIds); $container->setParameter('cache.provider_service_ids', $serviceIds); + + $container->register(CacheFlushCommand::class, CacheFlushCommand::class) + ->addTag('console.command', ['command' => 'cache:flush']); } /** @@ -98,10 +103,12 @@ protected function findServiceIds(array $config, array &$serviceIds) private function verifyDoctrineBridgeExists($name) { if (!class_exists('Cache\Bridge\Doctrine\DoctrineCacheBridge')) { - throw new \Exception(sprintf( - 'You need the DoctrineCacheBridge to be able to use "%s". Please run "composer require cache/psr-6-doctrine-bridge" to install the missing dependency.', - $name - )); + throw new \Exception( + sprintf( + 'You need the DoctrineCacheBridge to be able to use "%s". Please run "composer require cache/psr-6-doctrine-bridge" to install the missing dependency.', + $name + ) + ); } } @@ -117,7 +124,7 @@ public function getAlias() * Register services. All service ids will start witn "cache.service.". * * @param ContainerBuilder $container - * @param $config + * @param $config * * @throws \Exception */ From 3d53cb571e16a116ae1e9a0a43bd6b46884b1123 Mon Sep 17 00:00:00 2001 From: Aaron Scherer Date: Mon, 19 Mar 2018 16:34:01 -0700 Subject: [PATCH 4/4] style changes --- src/DependencyInjection/CacheExtension.php | 1 - src/DependencyInjection/Configuration.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DependencyInjection/CacheExtension.php b/src/DependencyInjection/CacheExtension.php index 3e4aab9..c2353de 100644 --- a/src/DependencyInjection/CacheExtension.php +++ b/src/DependencyInjection/CacheExtension.php @@ -22,7 +22,6 @@ use Cache\SessionHandler\Psr6SessionHandler; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 38b77dc..e073818 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -48,7 +48,7 @@ public function getConfigTreeBuilder() } /** - * Normalizes the enabled field to be truthy + * Normalizes the enabled field to be truthy. * * @param NodeDefinition $node *