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 1 commit
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\ServiceNotFoundException(sprintf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ServiceNotFoundException is not imported.

'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
7 changes: 7 additions & 0 deletions test/FormElementManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ public function testAddingInvokableCreatesAliasAndMapsClassToElementFactory()
}
}

public function testAutoAddInvokableClass()
{
$instance = $this->manager->get(TestAsset\ConstructedElement::class, ['constructedKey' => 'constructedKey']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reduce the line length to 80.

$this->assertEquals('constructedelement', $instance->getName());
$this->assertEquals(['constructedKey' => 'constructedKey'], $instance->getOptions());
}

public function testAllAliasesShouldBeCanonicalized()
{
if (method_exists($this->manager, 'configure')) {
Expand Down
25 changes: 25 additions & 0 deletions test/TestAsset/ConstructedElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the license header and use:

/**
 * @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
 */

*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Form\TestAsset;

use Zend\Form\Element;

class ConstructedElement extends Element
{
public $constructedKey = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove null.


public function __construct($name = null, $options = array())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a type hint for $options.

{
if (isset($options['constructedKey'])) {
$this->constructedKey = $options['constructedKey'];
}
parent::__construct($name, $options);
}
}