Skip to content

Commit

Permalink
Merge pull request #193 from gsteel/use-fqcn-for-container-retrieval
Browse files Browse the repository at this point in the history
Retrieve dependencies by FQCN where appropriate
  • Loading branch information
Ocramius authored Nov 20, 2022
2 parents 636953e + 1d21cdb commit e6d71e8
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 41 deletions.
5 changes: 3 additions & 2 deletions docs/book/v2/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions docs/book/v3/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Annotation/BuilderAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/FormElementManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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<HydratorInterface> $hydratorName
* @return mixed
* @psalm-return ($hydratorName is class-string<HydratorInterface> ? 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);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Laminas\Form;

use Laminas\ModuleManager\Feature\FormElementProviderInterface;
use Laminas\ModuleManager\Listener\ServiceListener;
use Laminas\ModuleManager\ModuleManager;

final class Module
Expand All @@ -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'
Expand Down
21 changes: 11 additions & 10 deletions test/Annotation/BuilderAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -99,15 +100,15 @@ public function testFactoryCanSetPreserveDefinedOrderFlagFromConfiguration(): vo
->method('get')
->willReturnMap([
['EventManager', $events],
['FormElementManager', new FormElementManager(new ServiceManager())],
[FormElementManager::class, new FormElementManager(new ServiceManager())],
['config', $config],
]);

$container->expects(self::atLeast(2))
->method('has')
->willReturnMap([
['config', true],
['InputFilterManager', false],
[InputFilterPluginManager::class, false],
]);

$factory = new BuilderAbstractFactory();
Expand All @@ -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],
]);
Expand All @@ -142,7 +143,7 @@ public function testFactoryAllowsAttachingListenersFromConfiguration(): void
->method('has')
->willReturnMap([
['config', true],
['InputFilterManager', false],
[InputFilterPluginManager::class, false],
]);

$listener->expects(self::once())
Expand All @@ -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',
Expand All @@ -182,7 +183,7 @@ public function testFactoryThrowsExceptionWhenAttachingInvalidListeners(): void
->method('has')
->willReturnMap([
['config', true],
['InputFilterManager', false],
[InputFilterPluginManager::class, false],
]);

$factory = new BuilderAbstractFactory();
Expand Down
2 changes: 1 addition & 1 deletion test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions test/FieldsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions test/FormElementManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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'));
Expand All @@ -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);
}
Expand All @@ -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'));
Expand Down
5 changes: 3 additions & 2 deletions test/FormElementManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions test/Integration/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e6d71e8

Please sign in to comment.