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/134'
Browse files Browse the repository at this point in the history
Close #134
Fixes #114
  • Loading branch information
weierophinney committed Apr 26, 2017
2 parents b203033 + 38d282c commit 5f35978
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#134](https://github.com/zendframework/zend-form/pull/134) fixes how the
`FormElementManager` handles invokable classes when the `autoAddInvokableClass`
flag is enabled. Previously, it used the built-in utilities from
zend-servicemanager, but now correctly uses its own `setInvokableClass()`
method, which forces usage of the `ElementFactory` for such classes, and thus
ensures the name and options are passed to the element constructor.

## 2.10.0 - 2017-02-23

Expand Down
12 changes: 12 additions & 0 deletions src/FormElementManager/FormElementManagerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public function get($name, $options = [], $usePeeringServiceManagers = true)
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, $usePeeringServiceManagers);
}

Expand Down
12 changes: 12 additions & 0 deletions test/FormElementManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ public function testAddingInvokableCreatesAliasAndMapsClassToElementFactory()
}
}

public function testAutoAddInvokableClass()
{
$instance = $this->manager->get(
TestAsset\ConstructedElement::class,
['constructedKey' => 'constructedKey']
);
$this->assertEquals('constructedelement', $instance->getName());
$this->assertEquals([
'constructedKey' => 'constructedKey'
], $instance->getOptions());
}

public function testAllAliasesShouldBeCanonicalized()
{
if (method_exists($this->manager, 'configure')) {
Expand Down
27 changes: 27 additions & 0 deletions test/TestAsset/ConstructedElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* @see https://github.com/zendframework/zend-navigation for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-form/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Form\TestAsset;

use Zend\Form\Element;

class ConstructedElement extends Element
{
public $constructedKey;

/**
* @param null|int|string $name
* @param array $options
*/
public function __construct($name = null, $options = [])
{
if (isset($options['constructedKey'])) {
$this->constructedKey = $options['constructedKey'];
}
parent::__construct($name, $options);
}
}

0 comments on commit 5f35978

Please sign in to comment.