Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Definition/Source/DefinitionArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getDefinition(string $name) : Definition|null
foreach ($this->wildcardDefinitions as $key => $definition) {
// Turn the pattern into a regex
$key = preg_quote($key, '#');
$key = '#' . str_replace('\\' . self::WILDCARD, self::WILDCARD_PATTERN, $key) . '#';
$key = '#^' . str_replace('\\' . self::WILDCARD, self::WILDCARD_PATTERN, $key) . '#';
if (preg_match($key, $name, $matches) === 1) {
array_shift($matches);

Expand Down
17 changes: 17 additions & 0 deletions tests/UnitTest/Definition/Source/DefinitionArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function testWildcards()
'Namespaced\*Interface' => \DI\create('Namespaced\*'),
'Namespaced2\*Interface' => \DI\create('Namespaced2\Foo'),
'Multiple\*\*\Matches' => \DI\create('Multiple\*\*\Implementation'),
'*Interface' => \DI\create('GlobalImplementation'),
]);

$definition = $source->getDefinition('foo1');
Expand All @@ -201,6 +202,11 @@ public function testWildcards()
$this->assertInstanceOf(ObjectDefinition::class, $definition);
$this->assertEquals('Multiple\Foo\Bar\Matches', $definition->getName());
$this->assertEquals('Multiple\Foo\Bar\Implementation', $definition->getClassName());

$definition = $source->getDefinition('GlobalInterface');
$this->assertInstanceOf(ObjectDefinition::class, $definition);
$this->assertEquals('GlobalInterface', $definition->getName());
$this->assertEquals('GlobalImplementation', $definition->getClassName());
}

/**
Expand Down Expand Up @@ -240,6 +246,17 @@ public function testWildcardShouldNotMatchAcrossNamespaces()
$this->assertNull($source->getDefinition('My\Foo\BarInterface'));
}

/**
* The wildcard for global namespace should not match across namespaces.
*/
public function testGlobalNamespaceWildcardShouldNotMatchAcrossNamespace()
{
$source = new DefinitionArray([
'*Interface' => \DI\create(),
]);
$this->assertNull($source->getDefinition('My\FooInterface'));
}

/**
* @see https://github.com/PHP-DI/PHP-DI/issues/379
*/
Expand Down