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

set correct Factory when autoAddInvokableClass=true #134

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}