Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/invokables'
Browse files Browse the repository at this point in the history
Close #67
Fixes #66
Fixes #63
Fixes #62
  • Loading branch information
weierophinney committed May 1, 2016
2 parents 3e8b9a3 + 13b85d8 commit 978aa87
Show file tree
Hide file tree
Showing 15 changed files with 637 additions and 242 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ All notable changes to this project will be documented in this file, in reverse

- [#60](https://github.com/zendframework/zend-form/pull/60) adds an alias from
`Zend\Form\FormElementManager` to `FormElementManager` in the `ConfigProvider`.
- [#67](https://github.com/zendframework/zend-form/pull/67) adds polyfills for
the `FormElementManager` to vary its definitions based on the major version of
zend-servicemanager in use. `FormElementManagerFactory` was updated to return
the specific polyfill version, and an autoload rule was added to alias the
class to the correct polyfill version. The polyfills were necessary to ensure
that invokable classes are mapped to the new `ElementFactory` introduced in
the 2.7 series, thus ensuring instantiation is performed correctly.

### Deprecated

Expand All @@ -24,6 +31,12 @@ All notable changes to this project will be documented in this file, in reverse
`injectFactory()` and `callElementInit()` are registered as the first and last
initializers, respectively, during construction, restoring the pre-2.7
behavior.
- [#67](https://github.com/zendframework/zend-form/pull/67) fixes the behavior
of `Factory::create()` to the pre-2.7.1 behavior of *not* passing creation
options when retrieving an instance from the `FormElementManager`. This
ensures that options are not passed to Element/Fieldset/Form instances
until after they are fully initialized, ensuring that all dependencies are
present.

## 2.8.1 - 2016-04-18

Expand Down
17 changes: 17 additions & 0 deletions autoload/formElementManagerPolyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* @link http://github.com/zendframework/zend-form for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

use Zend\Form\FormElementManager;
use Zend\ServiceManager\ServiceManager;

call_user_func(function () {
$target = method_exists(ServiceManager::class, 'configure')
? FormElementManager\FormElementManagerV3Polyfill::class
: FormElementManager\FormElementManagerV2Polyfill::class;

class_alias($target, FormElementManager::class);
});
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"autoload": {
"psr-4": {
"Zend\\Form\\": "src/"
}
},
"files": [
"autoload/formElementManagerPolyfill.php"
]
},
"require": {
"php": "^5.5 || ^7.0",
Expand Down
22 changes: 20 additions & 2 deletions src/Annotation/AnnotationBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Interop\Container\ContainerInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Form\Factory;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand All @@ -30,8 +31,7 @@ public function __invoke(ContainerInterface $container, $name, array $options =
$eventManager = $container->get('EventManager');
$annotationBuilder->setEventManager($eventManager);

$formElementManager = $container->get('FormElementManager');
$formElementManager->injectFactory($container, $annotationBuilder);
$this->injectFactory($annotationBuilder->getFormFactory(), $container);

$config = $this->marshalConfig($container);
if (isset($config['preserve_defined_order'])) {
Expand Down Expand Up @@ -136,4 +136,22 @@ private function injectListeners(array $config, EventManagerInterface $events, C
$listener->attach($events);
}
}

/**
* Inject the annotation builder's factory instance with the FormElementManager.
*
* Also injects the factory with the InputFilterManager if present.
*
* @param Factory $factory
* @param ContainerInterface $container
*/
private function injectFactory(Factory $factory, ContainerInterface $container)
{
$factory->setFormElementManager($container->get('FormElementManager'));

if ($container->has('InputFilterManager')) {
$inputFilters = $container->get('InputFilterManager');
$factory->getInputFilterFactory()->setInputFilterManager($inputFilters);
}
}
}
7 changes: 1 addition & 6 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,7 @@ public function create($spec)
$spec = $this->validateSpecification($spec, __METHOD__);
$type = isset($spec['type']) ? $spec['type'] : Element::class;

$creationOptions = [];
if (isset($spec['options']) && is_array($spec['options'])) {
$creationOptions = $spec['options'];
}

$element = $this->getFormElementManager()->get($type, $creationOptions);
$element = $this->getFormElementManager()->get($type);

if ($element instanceof FormInterface) {
return $this->configureForm($element, $spec);
Expand Down
103 changes: 103 additions & 0 deletions src/FormElementManager/FormElementManagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* @link http://github.com/zendframework/zend-form for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Form\FormElementManager;

use Zend\Form\Exception;

/**
* Trait providing common logic between FormElementManager implementations.
*
* Trait does not define properties, as the properties common between the
* two versions are originally defined in their parent class, causing a
* resolution conflict.
*/
trait FormElementManagerTrait
{
/**
* 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
* @param bool $usePeeringServiceManagers
* @return object
*/
public function get($name, $options = [], $usePeeringServiceManagers = true)
{
if (is_string($options)) {
$options = ['name' => $options];
}
return parent::get($name, $options, $usePeeringServiceManagers);
}

/**
* Try to pull hydrator from the creation context, or instantiates it from its name
*
* @param string $hydratorName
* @return mixed
* @throws Exception\DomainException
*/
public function getHydratorFromName($hydratorName)
{
$services = isset($this->creationContext)
? $this->creationContext // v3
: $this->serviceLocator; // v2

if ($services && $services->has('HydratorManager')) {
$hydrators = $services->get('HydratorManager');
if ($hydrators->has($hydratorName)) {
return $hydrators->get($hydratorName);
}
}

if ($services && $services->has($hydratorName)) {
return $services->get($hydratorName);
}

if (! class_exists($hydratorName)) {
throw new Exception\DomainException(sprintf(
'Expects string hydrator name to be a valid class name; received "%s"',
$hydratorName
));
}

$hydrator = new $hydratorName;
return $hydrator;
}

/**
* Try to pull factory from the creation context, or instantiates it from its name
*
* @param string $factoryName
* @return mixed
* @throws Exception\DomainException
*/
public function getFactoryFromName($factoryName)
{
$services = isset($this->creationContext)
? $this->creationContext // v3
: $this->serviceLocator; // v2

if ($services && $services->has($factoryName)) {
return $services->get($factoryName);
}

if (! class_exists($factoryName)) {
throw new Exception\DomainException(sprintf(
'Expects string factory name to be a valid class name; received "%s"',
$factoryName
));
}

$factory = new $factoryName;
return $factory;
}
}
Loading

0 comments on commit 978aa87

Please sign in to comment.