This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'weierophinney-hotfix/forwards-compat'
Close #60
- Loading branch information
Showing
6 changed files
with
234 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\ServiceManager\Factory; | ||
|
||
use Interop\Container\ContainerInterface; | ||
use Zend\ServiceManager\Exception\InvalidServiceNameException; | ||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
/** | ||
* Factory for instantiating classes with no dependencies or which accept a single array. | ||
* | ||
* The InvokableFactory can be used for any class that: | ||
* | ||
* - has no constructor arguments; | ||
* - accepts a single array of arguments via the constructor. | ||
* | ||
* It replaces the "invokables" and "invokable class" functionality of the v2 | ||
* service manager, and can also be used in v2 code for forwards compatibility | ||
* with v3. | ||
*/ | ||
final class InvokableFactory implements FactoryInterface | ||
{ | ||
/** | ||
* Create an instance of the requested class name. | ||
* | ||
* @param ContainerInterface $container | ||
* @param string $requestedName | ||
* @param null|array $options | ||
* @return object | ||
*/ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
return (null === $options) ? new $requestedName : new $requestedName($options); | ||
} | ||
|
||
/** | ||
* Create an instance of the named service. | ||
* | ||
* If `$requestedName` is not provided, raises an exception; otherwise, | ||
* proxies to the `__invoke()` method to create an instance of the | ||
* requested class. | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @param null|string $canonicalName Ignored | ||
* @param null|string $requestedName | ||
* @return object | ||
* @throws InvalidServiceNameException | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator, $canonicalName = null, $requestedName = null) | ||
{ | ||
if (! $requestedName) { | ||
throw new InvalidServiceNameException(sprintf( | ||
'%s requires that the requested name is provided on invocation; please update your tests or consuming container', | ||
__CLASS__ | ||
)); | ||
} | ||
return $this($serviceLocator, $requestedName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\ServiceManager\Factory; | ||
|
||
use Interop\Container\ContainerInterface; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\ServiceManager\Factory\InvokableFactory; | ||
use ZendTest\ServiceManager\TestAsset\InvokableObject; | ||
|
||
/** | ||
* @covers \Zend\ServiceManager\Factory\InvokableFactory | ||
*/ | ||
class InvokableFactoryTest extends TestCase | ||
{ | ||
public function testCanCreateObject() | ||
{ | ||
$container = $this->getMock(ContainerInterface::class); | ||
$factory = new InvokableFactory(); | ||
|
||
$object = $factory($container, InvokableObject::class, ['foo' => 'bar']); | ||
|
||
$this->assertInstanceOf(InvokableObject::class, $object); | ||
$this->assertEquals(['foo' => 'bar'], $object->options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\ServiceManager\TestAsset; | ||
|
||
class InvokableObject | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
public $options; | ||
|
||
/** | ||
* @param array $options | ||
*/ | ||
public function __construct(array $options = []) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getOptions() | ||
{ | ||
return $this->options; | ||
} | ||
} |