Skip to content

Commit

Permalink
SearchExtension: also searches for accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 20, 2023
1 parent 6c422d0 commit c84fb97
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/DI/Extensions/SearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function findClasses(\stdClass $config): array
||
($rc->isInterface()
&& count($methods = $rc->getMethods()) === 1
&& $methods[0]->name === 'create')
&& in_array($methods[0]->name, ['get', 'create'], true))
)
&& (!$acceptRE || preg_match($acceptRE, $rc->name))
&& (!$rejectRE || !preg_match($rejectRE, $rc->name))
Expand All @@ -133,9 +133,13 @@ public function beforeCompile()
}

foreach ($this->classes as $class => $tags) {
$def = class_exists($class)
? $builder->addDefinition(null)->setType($class)
: $builder->addFactoryDefinition(null)->setImplement($class);
if (class_exists($class)) {
$def = $builder->addDefinition(null)->setType($class);
} elseif (method_exists($class, 'create')) {
$def = $builder->addFactoryDefinition(null)->setImplement($class);
} else {
$def = $builder->addAccessorDefinition(null)->setImplement($class);
}
$def->setTags(Arrays::normalize($tags, true));
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/SearchExtension/all.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Assert::same([
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));


Expand Down
1 change: 1 addition & 0 deletions tests/SearchExtension/batches.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ Assert::same([
'Foo\\Bar\\ClassBar' => ['foo' => true],
'Foo\\ClassBar' => ['foo' => true],
'InterfaceOk1' => ['ok' => true],
'InterfaceOk2' => ['ok' => true],
], $services);
3 changes: 3 additions & 0 deletions tests/SearchExtension/classes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Assert::same([
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));


Expand Down Expand Up @@ -51,6 +52,7 @@ Assert::same([
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));


Expand Down Expand Up @@ -99,4 +101,5 @@ Assert::same([
'CountableClass',
'ExtendsStdClass',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));
1 change: 1 addition & 0 deletions tests/SearchExtension/duplicates.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Assert::same([
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));
3 changes: 3 additions & 0 deletions tests/SearchExtension/files.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Assert::same([
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));


Expand All @@ -39,6 +40,7 @@ Assert::same([
'CountableClass',
'ExtendsStdClass',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));


Expand All @@ -60,4 +62,5 @@ Assert::same([
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));
7 changes: 7 additions & 0 deletions tests/SearchExtension/fixtures/OkI2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
declare(strict_types=1);

interface InterfaceOk2
{
function get(): InterfaceOk1;
}
2 changes: 2 additions & 0 deletions tests/SearchExtension/parents.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Assert::same([
'Foo\\Bar\\ClassBar',
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));


Expand Down Expand Up @@ -95,4 +96,5 @@ Assert::same([
'CountableClass',
'Foo\\ClassBar',
'InterfaceOk1',
'InterfaceOk2',
], array_keys($services));
2 changes: 2 additions & 0 deletions tests/SearchExtension/tags.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Assert::same([
'CountableClass' => ['a' => 1, 'b' => 2],
'ExtendsStdClass' => ['a' => 1, 'b' => 2],
'InterfaceOk1' => ['a' => 1, 'b' => 2],
'InterfaceOk2' => ['a' => 1, 'b' => 2],
], $services);


Expand All @@ -41,4 +42,5 @@ Assert::same([
'CountableClass' => [],
'ExtendsStdClass' => [],
'InterfaceOk1' => [],
'InterfaceOk2' => [],
], $services);

0 comments on commit c84fb97

Please sign in to comment.