Skip to content

Commit

Permalink
Allow to define directories to be scanned for annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkael committed Oct 6, 2018
1 parent 4240a69 commit c443194
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('resources')
->scalarPrototype()->end()
->end()
->arrayNode('servers')
->performNoDeepMerging()
->defaultValue(array(
Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/GearmanExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
- "@annotation_reader"
- "@gearman.external.symfony_finder"
- "%gearman.bundles%"
- "%gearman.resources%"
- "%gearman.servers%"
- "%gearman.default.settings%"

Expand Down
36 changes: 34 additions & 2 deletions Service/GearmanParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class GearmanParser
*/
private $bundles;

/**
* @var array
*
* Namespaces paths to be searched
*/
private $resources;

/**
* @var array
*
Expand All @@ -71,13 +78,21 @@ class GearmanParser
*/
private $defaultSettings;

/**
* Root kernel directory
*
* @var string
*/
private $rootDir;

/**
* Construct method
*
* @param KernelInterface $kernel Kernel instance
* @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
*/
Expand All @@ -86,6 +101,7 @@ public function __construct(
Reader $reader,
Finder $finder,
array $bundles,
array $resources,
array $servers,
array $defaultSettings
)
Expand All @@ -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();
}

/**
Expand All @@ -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);
}
Expand All @@ -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();
Expand Down Expand Up @@ -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
*
Expand Down
27 changes: 23 additions & 4 deletions Tests/Service/GearmanParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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(

Expand Down

0 comments on commit c443194

Please sign in to comment.