From 2be5e34ca143c874334c50807d40290c001b1058 Mon Sep 17 00:00:00 2001 From: George Steel Date: Fri, 14 Oct 2022 11:30:33 +0100 Subject: [PATCH 1/2] Retrieve dependencies by FQCN where appropriate This patch updates all `$container->get()` call sites to use a FQCN instead of a string alias. DI configuration is updated so that factories are registered against FQCN but maintains existing service aliases. Signed-off-by: George Steel --- docs/book/v2/advanced.md | 5 +++-- .../usage-in-a-laminas-mvc-application.md | 6 +++--- docs/book/v3/advanced.md | 5 +++-- src/Annotation/BuilderAbstractFactory.php | 10 +++++---- src/ConfigProvider.php | 12 +++++------ src/FormElementManager.php | 13 ++++++++---- src/Module.php | 8 ++++--- .../Annotation/BuilderAbstractFactoryTest.php | 21 ++++++++++--------- test/FactoryTest.php | 2 +- test/FieldsetTest.php | 4 ++-- test/FormElementManagerFactoryTest.php | 8 +++---- test/FormElementManagerTest.php | 5 +++-- test/Integration/ServiceManagerTest.php | 8 +++---- 13 files changed, 60 insertions(+), 47 deletions(-) diff --git a/docs/book/v2/advanced.md b/docs/book/v2/advanced.md index b96ec8ee4..e64902e24 100644 --- a/docs/book/v2/advanced.md +++ b/docs/book/v2/advanced.md @@ -265,14 +265,15 @@ This now requires a factory to inject the form instance: ```php namespace Application\Controller; -use Interop\Container\ContainerInterface; use Application\Form\MyForm; +use Laminas\Form\FormElementManager; +use Psr\Container\ContainerInterface; class IndexControllerFactory { public function __invoke(ContainerInterface $container) { - $formManager = $container->get('FormElementManager'); + $formManager = $container->get(FormElementManager::class); return new IndexController($formManager->get(MyForm::class)); } } diff --git a/docs/book/v2/application-integration/usage-in-a-laminas-mvc-application.md b/docs/book/v2/application-integration/usage-in-a-laminas-mvc-application.md index 55813b59d..c8f426ff6 100644 --- a/docs/book/v2/application-integration/usage-in-a-laminas-mvc-application.md +++ b/docs/book/v2/application-integration/usage-in-a-laminas-mvc-application.md @@ -128,15 +128,15 @@ e.g. `src/Album/Controller/AlbumControllerFactory.php`: namespace Album\Controller; use Album\Form\AlbumForm; -use Laminas\ServiceManager\PluginManagerInterface; +use Laminas\Form\FormElementManager; use Psr\Container\ContainerInterface; class AlbumControllerFactory { public function __invoke(ContainerInterface $container) : AlbumController { - /** @var PluginManagerInterface $formElementManager */ - $formElementManager = $container->get('FormElementManager'); + /** @var FormElementManager $formElementManager */ + $formElementManager = $container->get(FormElementManager::class); /** @var AlbumForm */ $form = $formElementManager->get(AlbumForm::class); diff --git a/docs/book/v3/advanced.md b/docs/book/v3/advanced.md index 1e263e171..c7d64cab3 100644 --- a/docs/book/v3/advanced.md +++ b/docs/book/v3/advanced.md @@ -265,14 +265,15 @@ This now requires a factory to inject the form instance: ```php namespace Application\Controller; -use Interop\Container\ContainerInterface; use Application\Form\MyForm; +use Laminas\Form\FormElementManager; +use Psr\Container\ContainerInterface; class IndexControllerFactory { public function __invoke(ContainerInterface $container) { - $formManager = $container->get('FormElementManager'); + $formManager = $container->get(FormElementManager::class); return new IndexController($formManager->get(MyForm::class)); } } diff --git a/src/Annotation/BuilderAbstractFactory.php b/src/Annotation/BuilderAbstractFactory.php index b7bc4c429..0dd4bdb70 100644 --- a/src/Annotation/BuilderAbstractFactory.php +++ b/src/Annotation/BuilderAbstractFactory.php @@ -4,12 +4,14 @@ namespace Laminas\Form\Annotation; -use Interop\Container\ContainerInterface; // phpcs:disable WebimpressCodingStandard.PHP.CorrectClassNameCase use Laminas\EventManager\EventManagerInterface; use Laminas\EventManager\ListenerAggregateInterface; use Laminas\Form\Factory; +use Laminas\Form\FormElementManager; +use Laminas\InputFilter\InputFilterPluginManager; use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Laminas\ServiceManager\Factory\AbstractFactoryInterface; +use Psr\Container\ContainerInterface; use function is_array; use function is_subclass_of; @@ -121,10 +123,10 @@ private function injectListeners(array $config, EventManagerInterface $events, C */ private function injectFactory(Factory $factory, ContainerInterface $container): void { - $factory->setFormElementManager($container->get('FormElementManager')); + $factory->setFormElementManager($container->get(FormElementManager::class)); - if ($container->has('InputFilterManager')) { - $inputFilters = $container->get('InputFilterManager'); + if ($container->has(InputFilterPluginManager::class)) { + $inputFilters = $container->get(InputFilterPluginManager::class); $factory->getInputFilterFactory()->setInputFilterManager($inputFilters); } } diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 352e25536..0a9fbad34 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -34,14 +34,14 @@ public function getDependencyConfig(): array FormAbstractServiceFactory::class, ], 'aliases' => [ - Annotation\AnnotationBuilder::class => 'FormAnnotationBuilder', - Annotation\AttributeBuilder::class => 'FormAttributeBuilder', - FormElementManager::class => 'FormElementManager', + 'FormAnnotationBuilder' => Annotation\AnnotationBuilder::class, + 'FormAttributeBuilder' => Annotation\AttributeBuilder::class, + 'FormElementManager' => FormElementManager::class, ], 'factories' => [ - 'FormAnnotationBuilder' => Annotation\BuilderAbstractFactory::class, - 'FormAttributeBuilder' => Annotation\BuilderAbstractFactory::class, - 'FormElementManager' => FormElementManagerFactory::class, + Annotation\AnnotationBuilder::class => Annotation\BuilderAbstractFactory::class, + Annotation\AttributeBuilder::class => Annotation\BuilderAbstractFactory::class, + FormElementManager::class => FormElementManagerFactory::class, ], ]; } diff --git a/src/FormElementManager.php b/src/FormElementManager.php index 185362fe3..ae5893d15 100644 --- a/src/FormElementManager.php +++ b/src/FormElementManager.php @@ -6,6 +6,9 @@ use Interop\Container\ContainerInterface; // phpcs:disable WebimpressCodingStandard.PHP.CorrectClassNameCase use Laminas\Form\Exception; +use Laminas\Hydrator\HydratorInterface; +use Laminas\Hydrator\HydratorPluginManager; +use Laminas\InputFilter\InputFilterPluginManager; use Laminas\ServiceManager\AbstractPluginManager; use Laminas\ServiceManager\Exception\InvalidServiceException; use Laminas\Stdlib\InitializableInterface; @@ -217,8 +220,8 @@ public function injectFactory(ContainerInterface $container, mixed $instance): v $factory = $instance->getFormFactory(); $factory->setFormElementManager($this); - if ($container->has('InputFilterManager')) { - $inputFilters = $container->get('InputFilterManager'); + if ($container->has(InputFilterPluginManager::class)) { + $inputFilters = $container->get(InputFilterPluginManager::class); $factory->getInputFilterFactory()->setInputFilterManager($inputFilters); } } @@ -343,15 +346,17 @@ public function get($name, ?array $options = null) /** * Try to pull hydrator from the creation context, or instantiates it from its name * + * @param string|class-string $hydratorName * @return mixed + * @psalm-return ($hydratorName is class-string ? HydratorInterface : mixed) * @throws Exception\DomainException */ public function getHydratorFromName(string $hydratorName) { $services = $this->creationContext; - if ($services && $services->has('HydratorManager')) { - $hydrators = $services->get('HydratorManager'); + if ($services && $services->has(HydratorPluginManager::class)) { + $hydrators = $services->get(HydratorPluginManager::class); if ($hydrators->has($hydratorName)) { return $hydrators->get($hydratorName); } diff --git a/src/Module.php b/src/Module.php index a1e917f1e..3d2368eb4 100644 --- a/src/Module.php +++ b/src/Module.php @@ -5,6 +5,7 @@ namespace Laminas\Form; use Laminas\ModuleManager\Feature\FormElementProviderInterface; +use Laminas\ModuleManager\Listener\ServiceListener; use Laminas\ModuleManager\ModuleManager; final class Module @@ -28,12 +29,13 @@ public function getConfig(): array */ public function init(ModuleManager $moduleManager): void { - $event = $moduleManager->getEvent(); - $container = $event->getParam('ServiceManager'); + $event = $moduleManager->getEvent(); + $container = $event->getParam('ServiceManager'); + /** @var ServiceListener $serviceListener */ $serviceListener = $container->get('ServiceListener'); $serviceListener->addServiceManager( - 'FormElementManager', + FormElementManager::class, 'form_elements', FormElementProviderInterface::class, 'getFormElementConfig' diff --git a/test/Annotation/BuilderAbstractFactoryTest.php b/test/Annotation/BuilderAbstractFactoryTest.php index 6a9c0eada..ebd1fab83 100644 --- a/test/Annotation/BuilderAbstractFactoryTest.php +++ b/test/Annotation/BuilderAbstractFactoryTest.php @@ -10,6 +10,7 @@ use Laminas\Form\Annotation\AttributeBuilder; use Laminas\Form\Annotation\BuilderAbstractFactory; use Laminas\Form\FormElementManager; +use Laminas\InputFilter\InputFilterPluginManager; use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Laminas\ServiceManager\ServiceManager; use PHPUnit\Framework\TestCase; @@ -26,14 +27,14 @@ public function testFactoryReturnsAnnotationBuilder(): void ->method('get') ->willReturnMap([ ['EventManager', $events], - ['FormElementManager', new FormElementManager(new ServiceManager())], + [FormElementManager::class, new FormElementManager(new ServiceManager())], ]); $container->expects(self::atLeast(2)) ->method('has') ->willReturnMap([ ['config', false], - ['InputFilterManager', false], + [InputFilterPluginManager::class, false], ]); $factory = new BuilderAbstractFactory(); @@ -60,14 +61,14 @@ public function testFactoryReturnsAttributeBuilderForPhp8(): void ->method('get') ->willReturnMap([ ['EventManager', $events], - ['FormElementManager', new FormElementManager(new ServiceManager())], + [FormElementManager::class, new FormElementManager(new ServiceManager())], ]); $container->expects(self::atLeast(2)) ->method('has') ->willReturnMap([ ['config', false], - ['InputFilterManager', false], + [InputFilterPluginManager::class, false], ]); $factory = new BuilderAbstractFactory(); @@ -99,7 +100,7 @@ public function testFactoryCanSetPreserveDefinedOrderFlagFromConfiguration(): vo ->method('get') ->willReturnMap([ ['EventManager', $events], - ['FormElementManager', new FormElementManager(new ServiceManager())], + [FormElementManager::class, new FormElementManager(new ServiceManager())], ['config', $config], ]); @@ -107,7 +108,7 @@ public function testFactoryCanSetPreserveDefinedOrderFlagFromConfiguration(): vo ->method('has') ->willReturnMap([ ['config', true], - ['InputFilterManager', false], + [InputFilterPluginManager::class, false], ]); $factory = new BuilderAbstractFactory(); @@ -133,7 +134,7 @@ public function testFactoryAllowsAttachingListenersFromConfiguration(): void ->method('get') ->willReturnMap([ ['EventManager', $events], - ['FormElementManager', new FormElementManager(new ServiceManager())], + [FormElementManager::class, new FormElementManager(new ServiceManager())], ['config', $config], ['test-listener', $listener], ]); @@ -142,7 +143,7 @@ public function testFactoryAllowsAttachingListenersFromConfiguration(): void ->method('has') ->willReturnMap([ ['config', true], - ['InputFilterManager', false], + [InputFilterPluginManager::class, false], ]); $listener->expects(self::once()) @@ -169,7 +170,7 @@ public function testFactoryThrowsExceptionWhenAttachingInvalidListeners(): void ->method('get') ->willReturnMap([ ['EventManager', $events], - ['FormElementManager', new FormElementManager(new ServiceManager())], + [FormElementManager::class, new FormElementManager(new ServiceManager())], ['config', $config], [ 'test-listener', @@ -182,7 +183,7 @@ public function testFactoryThrowsExceptionWhenAttachingInvalidListeners(): void ->method('has') ->willReturnMap([ ['config', true], - ['InputFilterManager', false], + [InputFilterPluginManager::class, false], ]); $factory = new BuilderAbstractFactory(); diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 471838c52..d64621e9d 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -343,7 +343,7 @@ public function testCanCreateFormsAndSpecifyHydratorManagedByHydratorManager(): $hydratorShortName = 'ObjectPropertyHydrator'; $hydratorType = ObjectPropertyHydrator::class; - $this->services->setService('HydratorManager', new HydratorPluginManager($this->services)); + $this->services->setService(HydratorPluginManager::class, new HydratorPluginManager($this->services)); $form = $this->factory->createForm([ 'name' => 'foo', diff --git a/test/FieldsetTest.php b/test/FieldsetTest.php index 4ff6af038..2f9ad2fb8 100644 --- a/test/FieldsetTest.php +++ b/test/FieldsetTest.php @@ -626,10 +626,10 @@ public function testSetHydratorByNameMethodShouldSetValidHydratorForForm(): void // Service container $container = $this->createMock(ContainerInterface::class); $container->method('has') - ->with('HydratorManager') + ->with(Hydrator\HydratorPluginManager::class) ->willReturn(true); $container->method('get') - ->with('HydratorManager') + ->with(Hydrator\HydratorPluginManager::class) ->willReturn($hydratorManager); $this->fieldset->getFormFactory()->setFormElementManager( diff --git a/test/FormElementManagerFactoryTest.php b/test/FormElementManagerFactoryTest.php index 3c9d6ea55..cbdcf8d9c 100644 --- a/test/FormElementManagerFactoryTest.php +++ b/test/FormElementManagerFactoryTest.php @@ -65,7 +65,7 @@ public function testConfiguresFormElementsServicesWhenFound(): void ->willReturn($config); $factory = new FormElementManagerFactory(); - $elements = $factory($container, 'FormElementManager'); + $elements = $factory($container, FormElementManager::class); self::assertInstanceOf(FormElementManager::class, $elements); self::assertTrue($elements->has('test')); @@ -85,7 +85,7 @@ public function testDoesNotConfigureFormElementsServicesWhenServiceListenerPrese ->method('get'); $factory = new FormElementManagerFactory(); - $elements = $factory($container, 'FormElementManager'); + $elements = $factory($container, FormElementManager::class); self::assertInstanceOf(FormElementManager::class, $elements); self::assertFalse($elements->has('test')); @@ -105,7 +105,7 @@ public function testDoesNotConfigureFormElementsServicesWhenConfigServiceNotPres ->method('get'); $factory = new FormElementManagerFactory(); - $elements = $factory($container, 'FormElementManager'); + $elements = $factory($container, FormElementManager::class); self::assertInstanceOf(FormElementManager::class, $elements); } @@ -125,7 +125,7 @@ public function testDoesNotConfigureFormElementServicesWhenConfigServiceDoesNotC ->willReturn(['foo' => 'bar']); $factory = new FormElementManagerFactory(); - $elements = $factory($container, 'FormElementManager'); + $elements = $factory($container, FormElementManager::class); self::assertInstanceOf(FormElementManager::class, $elements); self::assertFalse($elements->has('foo')); diff --git a/test/FormElementManagerTest.php b/test/FormElementManagerTest.php index 8b525341a..c59bc0da6 100644 --- a/test/FormElementManagerTest.php +++ b/test/FormElementManagerTest.php @@ -12,6 +12,7 @@ use Laminas\Form\Form; use Laminas\Form\FormElementManager; use Laminas\Hydrator\HydratorInterface; +use Laminas\Hydrator\HydratorPluginManager; use Laminas\ServiceManager\Exception\InvalidServiceException; use Laminas\ServiceManager\PluginManagerInterface; use Laminas\ServiceManager\ServiceManager; @@ -270,10 +271,10 @@ public function testGetHydratorByNameMethodShouldUseHydratorManagerIfExists(): v // Service container $container = $this->createMock(ContainerInterface::class); $container->method('has') - ->with('HydratorManager') + ->with(HydratorPluginManager::class) ->willReturn(true); $container->method('get') - ->with('HydratorManager') + ->with(HydratorPluginManager::class) ->willReturn($hydratorManager); $formElementManager = new FormElementManager($container); diff --git a/test/Integration/ServiceManagerTest.php b/test/Integration/ServiceManagerTest.php index 48b241feb..9ef95ee43 100644 --- a/test/Integration/ServiceManagerTest.php +++ b/test/Integration/ServiceManagerTest.php @@ -21,14 +21,14 @@ public function testInitInitializerShouldBeCalledAfterAllOtherInitializers(): vo // Reproducing the behaviour of a full stack MVC + ModuleManager $serviceManagerConfig = new Config([ 'factories' => [ - 'FormElementManager' => FormElementManagerFactory::class, + FormElementManager::class => FormElementManagerFactory::class, ], ]); $serviceManager = new ServiceManager(); $serviceManagerConfig->configureServiceManager($serviceManager); - $formElementManager = $serviceManager->get('FormElementManager'); + $formElementManager = $serviceManager->get(FormElementManager::class); self::assertInstanceOf(FormElementManager::class, $formElementManager); $element = new class extends Element { @@ -62,13 +62,13 @@ public function testInjectFactoryInitializerShouldTriggerBeforeInitInitializer() // Reproducing the behaviour of a full stack MVC + ModuleManager $serviceManagerConfig = new Config([ 'factories' => [ - 'FormElementManager' => FormElementManagerFactory::class, + FormElementManager::class => FormElementManagerFactory::class, ], ]); $serviceManager = new ServiceManager(); $serviceManagerConfig->configureServiceManager($serviceManager); - $formElementManager = $serviceManager->get('FormElementManager'); + $formElementManager = $serviceManager->get(FormElementManager::class); self::assertInstanceOf(FormElementManager::class, $formElementManager); $initializer = new class ($formElementManager) implements InitializerInterface From 1d21cdba1cf2d59416a91ca052809566913e75a9 Mon Sep 17 00:00:00 2001 From: George Steel Date: Fri, 14 Oct 2022 12:44:30 +0100 Subject: [PATCH 2/2] Revert swapping aliases and factories - aliases are not delegated, therefore it's a BC break Signed-off-by: George Steel --- src/ConfigProvider.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 0a9fbad34..352e25536 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -34,14 +34,14 @@ public function getDependencyConfig(): array FormAbstractServiceFactory::class, ], 'aliases' => [ - 'FormAnnotationBuilder' => Annotation\AnnotationBuilder::class, - 'FormAttributeBuilder' => Annotation\AttributeBuilder::class, - 'FormElementManager' => FormElementManager::class, + Annotation\AnnotationBuilder::class => 'FormAnnotationBuilder', + Annotation\AttributeBuilder::class => 'FormAttributeBuilder', + FormElementManager::class => 'FormElementManager', ], 'factories' => [ - Annotation\AnnotationBuilder::class => Annotation\BuilderAbstractFactory::class, - Annotation\AttributeBuilder::class => Annotation\BuilderAbstractFactory::class, - FormElementManager::class => FormElementManagerFactory::class, + 'FormAnnotationBuilder' => Annotation\BuilderAbstractFactory::class, + 'FormAttributeBuilder' => Annotation\BuilderAbstractFactory::class, + 'FormElementManager' => FormElementManagerFactory::class, ], ]; }