Skip to content

Commit

Permalink
register new interface for translator
Browse files Browse the repository at this point in the history
  • Loading branch information
reinfi committed Aug 4, 2024
1 parent ca5905d commit a68a271
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"laminas/laminas-inputfilter": "^2.12",
"bnf/phpstan-psr-container": "^1.0",
"symplify/easy-coding-standard": "^12.0",
"doctrine/annotations": "^2.0"
"doctrine/annotations": "^2.0",
"laminas/laminas-translator": "^1.0"
},
"extra": {
"zf": {
Expand Down
2 changes: 0 additions & 2 deletions src/AbstractFactory/FallbackAutoWiringFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Reinfi\DependencyInjection\Factory\AutoWiringFactory;

/**
* Class FallbackAutoWiringFactory
*
* Note: Please DO NOT use this factory for everything.
* If you're implementing new classes, please write a concrete factory.
*
Expand Down
20 changes: 16 additions & 4 deletions src/Service/AutoWiring/Resolver/TranslatorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@
class TranslatorResolver implements ResolverInterface
{
/**
* used to avoid requirement of laminas/laminas-i18n module
* used to avoid requirement of laminas/laminas-i18n module, deprecated interface name.
*/
private const TRANSLATOR_INTERFACE = 'Laminas\I18n\Translator\TranslatorInterface';
private const TRANSLATOR_INTERFACE_OLD = 'Laminas\I18n\Translator\TranslatorInterface';

/**
* used to avoid requirement of laminas/laminas-translator module.
*/
private const TRANSLATOR_INTERFACE = 'Laminas\Translator\TranslatorInterface';

/**
* possible names for translator service within container
*/
private const TRANSLATOR_CONTAINER_SERVICE_NAME = ['MvcTranslator', self::TRANSLATOR_INTERFACE, 'Translator'];
private const TRANSLATOR_CONTAINER_SERVICE_NAME = [
self::TRANSLATOR_INTERFACE,
'MvcTranslator',
self::TRANSLATOR_INTERFACE_OLD,
'Translator',
];

private ContainerInterface $container;

Expand Down Expand Up @@ -65,7 +75,9 @@ private function isValid(ReflectionParameter $parameter): bool
$reflectionClass = new ReflectionClass($type->getName());
$interfaceNames = $reflectionClass->getInterfaceNames();

return $reflectionClass->getName() === self::TRANSLATOR_INTERFACE
return $reflectionClass->getName() === self::TRANSLATOR_INTERFACE_OLD
|| in_array(self::TRANSLATOR_INTERFACE_OLD, $interfaceNames, true)
|| $reflectionClass->getName() === self::TRANSLATOR_INTERFACE
|| in_array(self::TRANSLATOR_INTERFACE, $interfaceNames, true)
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function testItReturnsNullIfNoTranslatorRegistered(): void

$container->has('MvcTranslator')->willReturn(false);
$container->has(TranslatorInterface::class)->willReturn(false);
$container->has(\Laminas\Translator\TranslatorInterface::class)->willReturn(false);
$container->has('Translator')->willReturn(false);

$resolver = new TranslatorResolver($container->reveal());
Expand Down Expand Up @@ -141,17 +142,25 @@ public static function containerHasCallsProvider(): array
return [
[
[
'Laminas\Translator\TranslatorInterface' => true,
],
],
[
[
'Laminas\Translator\TranslatorInterface' => false,
'MvcTranslator' => true,
],
],
[
[
'Laminas\Translator\TranslatorInterface' => false,
'MvcTranslator' => false,
'Laminas\I18n\Translator\TranslatorInterface' => true,
],
],
[
[
'Laminas\Translator\TranslatorInterface' => false,
'MvcTranslator' => false,
'Laminas\I18n\Translator\TranslatorInterface' => false,
'Translator' => true,
Expand Down

0 comments on commit a68a271

Please sign in to comment.