Skip to content
Permalink
Browse files

SearchExtension: ability to add factory definitions from interfaces w…

…ith method create() (#200)
  • Loading branch information...
peldax authored and dg committed Jul 24, 2019
1 parent 01dce95 commit e55f84735d3536a8e61a3e3f5fae47df24d16c69
@@ -80,7 +80,6 @@ public function findClasses(\stdClass $config): array
$robot->reportParseErrors(false);
$robot->refresh();
$classes = array_unique(array_keys($robot->getIndexedClasses()));
$classes = array_filter($classes, 'class_exists');
$exclude = $config->exclude;
$acceptRE = self::buildNameRegexp($config->classes);
@@ -92,7 +91,12 @@ public function findClasses(\stdClass $config): array
foreach ($classes as $class) {
$rc = new \ReflectionClass($class);
if (
$rc->isInstantiable()
($rc->isInstantiable()
||
($rc->isInterface()
&& count($methods = $rc->getMethods()) === 1
&& $methods[0]->getName() === 'create')
)
&& (!$acceptRE || preg_match($acceptRE, $rc->getName()))
&& (!$rejectRE || !preg_match($rejectRE, $rc->getName()))
&& (!$acceptParent || Arrays::some($acceptParent, function ($nm) use ($rc) { return $rc->isSubclassOf($nm); }))
@@ -110,11 +114,16 @@ public function beforeCompile()
$builder = $this->getContainerBuilder();
foreach ($this->classes as $class => $tags) {
if (!$builder->findByType($class)) {
$builder->addDefinition(null)
->setType($class)
->setTags(Arrays::normalize($tags, true));
if ($builder->findByType($class)) {
continue;
} elseif (class_exists($class)) {
$def = $builder->addDefinition(null)
->setType($class);
} else {
$def = $builder->addFactoryDefinition(null)
->setImplement($class);
}
$def->setTags(Arrays::normalize($tags, true));
}
}
@@ -20,6 +20,7 @@ Assert::same([
'ExtendsStdClass',
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
], array_keys($services));
@@ -35,4 +35,5 @@ Assert::same([
'ExtendsStdClass' => ['ok' => true],
'Foo\\Bar\\ClassBar' => ['foo' => true],
'Foo\\ClassBar' => ['foo' => true],
'InterfaceOk1' => ['ok' => true],
], $services);
@@ -21,6 +21,7 @@ Assert::same([
'ExtendsStdClass',
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
], array_keys($services));
@@ -49,6 +50,7 @@ Assert::same([
'ClassOk3',
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
], array_keys($services));
@@ -96,4 +98,5 @@ Assert::same([
'ClassOk3',
'CountableClass',
'ExtendsStdClass',
'InterfaceOk1',
], array_keys($services));
@@ -26,4 +26,5 @@ Assert::same([
'ExtendsStdClass',
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
], array_keys($services));
@@ -21,6 +21,7 @@ Assert::same([
'ExtendsStdClass',
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
], array_keys($services));
@@ -37,6 +38,7 @@ Assert::same([
'ClassOk3',
'CountableClass',
'ExtendsStdClass',
'InterfaceOk1',
], array_keys($services));
@@ -57,4 +59,5 @@ Assert::same([
'ExtendsStdClass',
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
], array_keys($services));
@@ -0,0 +1,7 @@
<?php
declare(strict_types=1);
interface InterfaceOk1
{
function create(): stdClass;
}
@@ -21,6 +21,7 @@ Assert::same([
'ExtendsStdClass',
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
], array_keys($services));
@@ -87,7 +88,11 @@ search:
extends: stdClass
');
Assert::same(
['ClassOk1', 'ClassOk2', 'ClassOk3', 'CountableClass', 'Foo\\ClassBar'],
array_keys($services)
);
Assert::same([
'ClassOk1',
'ClassOk2',
'ClassOk3',
'CountableClass',
'Foo\\ClassBar',
'InterfaceOk1',
], array_keys($services));
@@ -22,6 +22,7 @@ Assert::same([
'ClassOk3' => ['a' => 1, 'b' => 2],
'CountableClass' => ['a' => 1, 'b' => 2],
'ExtendsStdClass' => ['a' => 1, 'b' => 2],
'InterfaceOk1' => ['a' => 1, 'b' => 2],
], $services);
@@ -39,4 +40,5 @@ Assert::same([
'ClassOk3' => [],
'CountableClass' => [],
'ExtendsStdClass' => [],
'InterfaceOk1' => [],
], $services);

0 comments on commit e55f847

Please sign in to comment.
You can’t perform that action at this time.