From 55a46564ac06f9be19e2f19ad83fd99b20b19caa Mon Sep 17 00:00:00 2001 From: Thomas Rieschl Date: Sun, 21 Mar 2021 20:49:12 +0100 Subject: [PATCH] fix: revert bc-break introduced in version 2.16.0 Signed-off-by: Thomas Rieschl --- src/FormElementManager.php | 42 +++++++++++++++++++++++++++----- test/FormElementManagerTest.php | 13 ++++++++++ test/TestAsset/InvokableForm.php | 11 +++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 test/TestAsset/InvokableForm.php diff --git a/src/FormElementManager.php b/src/FormElementManager.php index e9af6a3a4..7e8e6d54d 100644 --- a/src/FormElementManager.php +++ b/src/FormElementManager.php @@ -9,13 +9,7 @@ namespace Laminas\Form; use Interop\Container\ContainerInterface; -use Laminas\Form\Element; use Laminas\Form\Exception; -use Laminas\Form\ElementFactory; -use Laminas\Form\ElementInterface; -use Laminas\Form\Fieldset; -use Laminas\Form\Form; -use Laminas\Form\FormFactoryAwareInterface; use Laminas\ServiceManager\AbstractPluginManager; use Laminas\ServiceManager\Exception\InvalidServiceException; use Laminas\Stdlib\InitializableInterface; @@ -23,9 +17,11 @@ use function array_push; use function array_search; use function array_unshift; +use function class_exists; use function get_class; use function gettype; use function is_object; +use function is_string; use function sprintf; /** @@ -391,6 +387,40 @@ public function configure(array $config) return $this; } + + /** + * Retrieve a service from the manager by name + * + * Allows passing an array of options to use when creating the instance. + * createFromInvokable() will use these and pass them to the instance + * constructor if not null and a non-empty array. + * + * @param string $name + * @param string|array $options + * @return mixed + */ + public function get($name, $options = []) + { + if (is_string($options)) { + $options = ['name' => $options]; + } + + if (! $this->has($name)) { + if (! $this->autoAddInvokableClass || ! class_exists($name)) { + throw new Exception\InvalidElementException( + sprintf( + 'A plugin by the name "%s" was not found in the plugin manager %s', + $name, + get_class($this) + ) + ); + } + + $this->setInvokableClass($name, $name); + } + return parent::get($name, $options); + } + /** * Try to pull hydrator from the creation context, or instantiates it from its name * diff --git a/test/FormElementManagerTest.php b/test/FormElementManagerTest.php index e82612a2d..42fa8d808 100644 --- a/test/FormElementManagerTest.php +++ b/test/FormElementManagerTest.php @@ -15,6 +15,7 @@ use Laminas\Form\FormElementManager; use Laminas\ServiceManager\Exception\InvalidServiceException; use Laminas\ServiceManager\ServiceManager; +use LaminasTest\Form\TestAsset\InvokableForm; use PHPUnit\Framework\TestCase; use ReflectionProperty; @@ -242,4 +243,16 @@ public function testAllAliasesShouldBeCanonicalized() $this->addToAssertionCount(1); } + + public function testOptionsAreSetInInvokableForm(): void + { + $options = ['foo' => 'bar']; + + /** @var InvokableForm $form */ + $form = $this->manager->get(InvokableForm::class, $options); + + self::assertInstanceOf(InvokableForm::class, $form); + self::assertSame('invokableform', $form->getName()); + self::assertSame('bar', $form->getOption('foo')); + } } diff --git a/test/TestAsset/InvokableForm.php b/test/TestAsset/InvokableForm.php new file mode 100644 index 000000000..a51066505 --- /dev/null +++ b/test/TestAsset/InvokableForm.php @@ -0,0 +1,11 @@ +