From c4431947043dc5a5c398072ed490fc59f7a4e273 Mon Sep 17 00:00:00 2001 From: Mickael Perraud Date: Sat, 6 Oct 2018 18:11:07 +0200 Subject: [PATCH] Allow to define directories to be scanned for annotations --- DependencyInjection/Configuration.php | 3 ++ DependencyInjection/GearmanExtension.php | 5 ++++ Resources/config/services.yml | 1 + Service/GearmanParser.php | 36 ++++++++++++++++++++++-- Tests/Service/GearmanParserTest.php | 27 +++++++++++++++--- 5 files changed, 66 insertions(+), 6 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 595a2f0..6ef70d8 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -53,6 +53,9 @@ public function getConfigTreeBuilder() ->end() ->end() ->end() + ->arrayNode('resources') + ->scalarPrototype()->end() + ->end() ->arrayNode('servers') ->performNoDeepMerging() ->defaultValue(array( diff --git a/DependencyInjection/GearmanExtension.php b/DependencyInjection/GearmanExtension.php index 5ea532d..5fd71ae 100644 --- a/DependencyInjection/GearmanExtension.php +++ b/DependencyInjection/GearmanExtension.php @@ -46,6 +46,11 @@ public function load(array $config, ContainerBuilder $container) $config['bundles'] ); + $container->setParameter( + 'gearman.resources', + $config['resources'] + ); + $container->setParameter( 'gearman.servers', $config['servers'] diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 01b1ce5..0363c25 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -11,6 +11,7 @@ services: - "@annotation_reader" - "@gearman.external.symfony_finder" - "%gearman.bundles%" + - "%gearman.resources%" - "%gearman.servers%" - "%gearman.default.settings%" diff --git a/Service/GearmanParser.php b/Service/GearmanParser.php index 581c5b7..eb09860 100644 --- a/Service/GearmanParser.php +++ b/Service/GearmanParser.php @@ -57,6 +57,13 @@ class GearmanParser */ private $bundles; + /** + * @var array + * + * Namespaces paths to be searched + */ + private $resources; + /** * @var array * @@ -71,6 +78,13 @@ class GearmanParser */ private $defaultSettings; + /** + * Root kernel directory + * + * @var string + */ + private $rootDir; + /** * Construct method * @@ -78,6 +92,7 @@ class GearmanParser * @param Reader $reader Reader * @param Finder $finder Finder * @param array $bundles Bundle array where to parse workers, defined on condiguration + * @param array $resources Array of namespace paths to be searched for worker annotations * @param array $servers Server list defined on configuration * @param array $defaultSettings Default settings defined on configuration */ @@ -86,6 +101,7 @@ public function __construct( Reader $reader, Finder $finder, array $bundles, + array $resources, array $servers, array $defaultSettings ) @@ -94,8 +110,10 @@ public function __construct( $this->reader = $reader; $this->finder = $finder; $this->bundles = $bundles; + $this->resources = $resources; $this->servers = $servers; $this->defaultSettings = $defaultSettings; + $this->rootDir = $kernel->getRootDir(); } /** @@ -105,7 +123,8 @@ public function __construct( */ public function load() { - list($paths, $excludedPaths) = $this->loadNamespaceMap($this->kernelBundles, $this->bundles); + list($paths, $excludedPaths) = $this->loadBundleNamespaceMap($this->kernelBundles, $this->bundles); + $paths = array_merge($paths, $this->loadResourceNamespaceMap($this->rootDir, $this->resources)); return $this->parseNamespaceMap($this->finder, $this->reader, $paths, $excludedPaths); } @@ -120,7 +139,7 @@ public function load() * * @return array Return an array containing paths and ignore paths */ - public function loadNamespaceMap(array $kernelBundles, array $bundles) + public function loadBundleNamespaceMap(array $kernelBundles, array $bundles) { $paths = array(); $excludedPaths = array(); @@ -165,6 +184,19 @@ public function loadNamespaceMap(array $kernelBundles, array $bundles) ); } + /** + * Get resource paths + * @param string $rootDir + * @param array $resources + * @return array + */ + public function loadResourceNamespaceMap($rootDir, array $resources) + { + return array_map(function($resource) use ($rootDir) { + return $rootDir . '/' . trim($resource, '/') . '/'; + }, $resources); + } + /** * Perform a parsing inside all namespace map * diff --git a/Tests/Service/GearmanParserTest.php b/Tests/Service/GearmanParserTest.php index 3c7e1c5..48a4e3c 100644 --- a/Tests/Service/GearmanParserTest.php +++ b/Tests/Service/GearmanParserTest.php @@ -216,9 +216,9 @@ public function testParseNamespaceMapSomePaths() /** * Testing parseNamespaceMap with some paths * - * @dataProvider loadNamespaceMapDataProvider + * @dataProvider loadBundleNamespaceMapDataProvider */ - public function testLoadNamespaceMap($active, $include, $ignore, $expectedPaths, $expectedExcludedPaths) + public function testLoadBundleNamespaceMap($active, $include, $ignore, $expectedPaths, $expectedExcludedPaths) { $this ->bundleMock @@ -231,7 +231,7 @@ public function testLoadNamespaceMap($active, $include, $ignore, $expectedPaths, "FirstBundleName" => $this->bundleMock, ); - list($paths, $excludedPaths) = $this->gearmanParser->loadNamespaceMap($this->kernelBundles, array( + list($paths, $excludedPaths) = $this->gearmanParser->loadBundleNamespaceMap($this->kernelBundles, array( "FirstBundle" => array( "name" => "FirstBundleName", "active" => $active, @@ -244,10 +244,29 @@ public function testLoadNamespaceMap($active, $include, $ignore, $expectedPaths, $this->assertEquals($excludedPaths, $expectedExcludedPaths); } + /** + * Testing loadResourceNamespaceMap + */ + public function testLoadResourceNamespaceMap() + { + $rootDir = '/app/kernel/root/directory'; + $data = array( + '/Worker/' => $rootDir . '/Worker/', + 'Infrastructure/Gearman/Workers' => $rootDir . '/Infrastructure/Gearman/Workers/', + ); + $this->gearmanParser = $this + ->getMockBuilder('\Mkk\GearmanBundle\Service\GearmanParser') + ->disableOriginalConstructor() + ->setMethods(null) + ->getMock(); + $paths = $this->gearmanParser->loadResourceNamespaceMap($rootDir, array_keys($data)); + $this->assertEquals($paths, array_values($data)); + } + /** * Load namespace map Data Provider */ - public function loadNamespaceMapDataProvider() + public function loadBundleNamespaceMapDataProvider() { return array(