Skip to content

Commit

Permalink
Merge pull request #93 from rieschl/fix/invokable-form-with-options
Browse files Browse the repository at this point in the history
fix: revert bc-break introduced in version 2.16.0
  • Loading branch information
Slamdunk authored Mar 22, 2021
2 parents 43e0839 + 55a4656 commit 3ed0b37
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/FormElementManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@
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;

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;

/**
Expand Down Expand Up @@ -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<string, mixed> $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
*
Expand Down
13 changes: 13 additions & 0 deletions test/FormElementManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'));
}
}
11 changes: 11 additions & 0 deletions test/TestAsset/InvokableForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Form\TestAsset;

use Laminas\Form\Form;

final class InvokableForm extends Form
{
}

0 comments on commit 3ed0b37

Please sign in to comment.