Skip to content

Commit 05f35fd

Browse files
committed
Merge branch 'hotfix/2750'
Close zendframework#2750
2 parents b91ed24 + 852c192 commit 05f35fd

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

library/Zend/ServiceManager/AbstractPluginManager.php

+38
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,42 @@ protected function createFromInvokable($canonicalName, $requestedName)
181181

182182
return $instance;
183183
}
184+
185+
/**
186+
* Attempt to create an instance via a factory class
187+
*
188+
* Overrides parent implementation by passing $creationOptions to the
189+
* constructor, if non-null.
190+
*
191+
* @param string $canonicalName
192+
* @param string $requestedName
193+
* @return mixed
194+
* @throws Exception\ServiceNotCreatedException If factory is not callable
195+
*/
196+
protected function createFromFactory($canonicalName, $requestedName)
197+
{
198+
$factory = $this->factories[$canonicalName];
199+
if (is_string($factory) && class_exists($factory, true)) {
200+
if (null === $this->creationOptions || (is_array($this->creationOptions) && empty($this->creationOptions))) {
201+
$factory = new $factory();
202+
} else {
203+
$factory = new $factory($this->creationOptions);
204+
}
205+
206+
$this->factories[$canonicalName] = $factory;
207+
}
208+
209+
if ($factory instanceof FactoryInterface) {
210+
$instance = $this->createServiceViaCallback(array($factory, 'createService'), $canonicalName, $requestedName);
211+
} elseif (is_callable($factory)) {
212+
$instance = $this->createServiceViaCallback($factory, $canonicalName, $requestedName);
213+
} else {
214+
throw new Exception\ServiceNotCreatedException(sprintf(
215+
'While attempting to create %s%s an invalid factory was registered for this instance type.', $canonicalName, ($requestedName ? '(alias: ' . $requestedName . ')' : '')
216+
));
217+
}
218+
219+
return $instance;
220+
}
221+
184222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Mvc
9+
*/
10+
11+
namespace ZendTest\Mvc\Controller\Plugin\TestAsset;
12+
13+
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
14+
use Zend\ServiceManager\FactoryInterface;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
16+
17+
class SamplePluginFactory implements FactoryInterface
18+
{
19+
public function createService(ServiceLocatorInterface $serviceLocator)
20+
{
21+
return new SamplePlugin();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Mvc
9+
*/
10+
11+
namespace ZendTest\Mvc\Controller\Plugin\TestAsset;
12+
13+
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
14+
use Zend\ServiceManager\FactoryInterface;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
16+
17+
class SamplePluginWithConstructorFactory implements FactoryInterface
18+
{
19+
protected $options;
20+
21+
public function __construct($options)
22+
{
23+
$this->options = $options;
24+
}
25+
26+
public function createService(ServiceLocatorInterface $serviceLocator)
27+
{
28+
return new SamplePluginWithConstructor($this->options);
29+
}
30+
}

tests/ZendTest/Mvc/Controller/PluginManagerTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,22 @@ public function testGetWithConstrutorAndOptions()
7171
$plugin = $pluginManager->get('samplePlugin', 'foo');
7272
$this->assertEquals($plugin->getBar(), 'foo');
7373
}
74+
75+
public function testCanCreateByFactory()
76+
{
77+
$pluginManager = new PluginManager;
78+
$pluginManager->setFactory('samplePlugin', 'ZendTest\Mvc\Controller\Plugin\TestAsset\SamplePluginFactory');
79+
$plugin = $pluginManager->get('samplePlugin');
80+
$this->assertInstanceOf('\ZendTest\Mvc\Controller\Plugin\TestAsset\SamplePlugin', $plugin);
81+
}
82+
83+
public function testCanCreateByFactoryWithConstrutor()
84+
{
85+
$pluginManager = new PluginManager;
86+
$pluginManager->setFactory('samplePlugin', 'ZendTest\Mvc\Controller\Plugin\TestAsset\SamplePluginWithConstructorFactory');
87+
$plugin = $pluginManager->get('samplePlugin', 'foo');
88+
$this->assertInstanceOf('\ZendTest\Mvc\Controller\Plugin\TestAsset\SamplePluginWithConstructor', $plugin);
89+
$this->assertEquals($plugin->getBar(), 'foo');
90+
}
91+
7492
}

0 commit comments

Comments
 (0)