Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 8d6fa0e

Browse files
committed
Added new ConfigAbstractFactory per issue #128
1 parent bb04e03 commit 8d6fa0e

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\ServiceManager\AbstractFactory;
11+
12+
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
13+
14+
class ConfigAbstractFactory implements AbstractFactoryInterface
15+
{
16+
17+
/**
18+
* Can the factory create an instance for the service?
19+
*
20+
* @param \Interop\Container\ContainerInterface $container
21+
* @param string $requestedName
22+
* @return bool
23+
*/
24+
public function canCreate(\Interop\Container\ContainerInterface $container, $requestedName)
25+
{
26+
if (!$container->has('config') || !array_key_exists(self::class, $container->get('config'))) {
27+
return false;
28+
}
29+
$config = $container->get('config');
30+
$dependencies = $config[self::class];
31+
32+
return array_key_exists($requestedName, $dependencies);
33+
}
34+
35+
/**
36+
* Create an object
37+
*
38+
* @param \Interop\Container\ContainerInterface $container
39+
* @param string $requestedName
40+
* @param null|array $options
41+
* @return object
42+
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException if unable to resolve the service.
43+
* @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException if an exception is raised when
44+
* creating a service.
45+
* @throws \Interop\Container\Exception\ContainerException if any other error occurs
46+
*/
47+
public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, array $options = null)
48+
{
49+
$config = $container->get('config');
50+
$dependencies = $config[self::class][$requestedName];
51+
52+
// class has no dependencies, just create it and return
53+
if (empty($dependencies)) {
54+
return new $requestedName();
55+
}
56+
57+
$arguments = [];
58+
foreach ($dependencies as $dependency) {
59+
$arguments[] = $container->get($dependency);
60+
}
61+
62+
return new $requestedName(...$arguments);
63+
}
64+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\ServiceManager\AbstractFactory;
11+
12+
13+
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
14+
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
15+
use Zend\ServiceManager\Proxy\LazyServiceFactory;
16+
use Zend\ServiceManager\ServiceManager;
17+
use ZendTest\ServiceManager\TestAsset\ComplexDependencyObject;
18+
use ZendTest\ServiceManager\TestAsset\FailingFactory;
19+
use ZendTest\ServiceManager\TestAsset\InvokableObject;
20+
use ZendTest\ServiceManager\TestAsset\SimpleDependencyObject;
21+
22+
class ConfigAbstractFactoryTest extends \PHPUnit_Framework_TestCase
23+
{
24+
25+
function testCanCreateShortCircuits()
26+
{
27+
$abstractFactory = new ConfigAbstractFactory();
28+
$serviceManager = new ServiceManager();
29+
30+
self::assertFalse($abstractFactory->canCreate($serviceManager, 'MarcoSucks'));
31+
}
32+
33+
function testCanCreate()
34+
{
35+
$abstractFactory = new ConfigAbstractFactory();
36+
$serviceManager = new ServiceManager();
37+
$serviceManager->setService(
38+
'config',
39+
[
40+
ConfigAbstractFactory::class => [
41+
InvokableObject::class => [],
42+
]
43+
]
44+
);
45+
46+
self::assertTrue($abstractFactory->canCreate($serviceManager, InvokableObject::class));
47+
self::assertFalse($abstractFactory->canCreate($serviceManager, ServiceManager::class));
48+
}
49+
50+
function testInvokeWithInvokableClass()
51+
{
52+
$abstractFactory = new ConfigAbstractFactory();
53+
$serviceManager = new ServiceManager();
54+
$serviceManager->setService(
55+
'config',
56+
[
57+
ConfigAbstractFactory::class => [
58+
InvokableObject::class => [],
59+
]
60+
]
61+
);
62+
63+
self::assertInstanceOf(InvokableObject::class, $abstractFactory($serviceManager, InvokableObject::class));
64+
}
65+
66+
function testInvokeWithSimpleArguments()
67+
{
68+
$abstractFactory = new ConfigAbstractFactory();
69+
$serviceManager = new ServiceManager();
70+
$serviceManager->setService(
71+
'config',
72+
[
73+
ConfigAbstractFactory::class => [
74+
InvokableObject::class => [],
75+
FailingFactory::class => [],
76+
SimpleDependencyObject::class => [
77+
InvokableObject::class,
78+
FailingFactory::class,
79+
],
80+
]
81+
]
82+
);
83+
$serviceManager->addAbstractFactory($abstractFactory);
84+
85+
self::assertInstanceOf(
86+
SimpleDependencyObject::class,
87+
$abstractFactory($serviceManager, SimpleDependencyObject::class)
88+
);
89+
}
90+
91+
function testInvokeWithComplexArguments()
92+
{
93+
$abstractFactory = new ConfigAbstractFactory();
94+
$serviceManager = new ServiceManager();
95+
$serviceManager->setService(
96+
LazyLoadingValueHolderFactory::class . '.config',
97+
[
98+
'Proxies' => 'Suck',
99+
]
100+
);
101+
$serviceManager->setService(
102+
'config',
103+
[
104+
ConfigAbstractFactory::class => [
105+
InvokableObject::class => [],
106+
FailingFactory::class => [],
107+
SimpleDependencyObject::class => [
108+
InvokableObject::class,
109+
FailingFactory::class,
110+
],
111+
LazyLoadingValueHolderFactory::class => [],
112+
ComplexDependencyObject::class => [
113+
SimpleDependencyObject::class,
114+
LazyServiceFactory::class,
115+
],
116+
LazyServiceFactory::class => [
117+
LazyLoadingValueHolderFactory::class,
118+
LazyLoadingValueHolderFactory::class . '.config',
119+
],
120+
]
121+
]
122+
);
123+
$serviceManager->addAbstractFactory($abstractFactory);
124+
125+
self::assertInstanceOf(
126+
ComplexDependencyObject::class,
127+
$abstractFactory($serviceManager, ComplexDependencyObject::class)
128+
);
129+
}
130+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\ServiceManager\TestAsset;
11+
12+
13+
use Zend\ServiceManager\Proxy\LazyServiceFactory;
14+
15+
class ComplexDependencyObject
16+
{
17+
public function __construct(SimpleDependencyObject $simpleDependencyObject, LazyServiceFactory $lazyServiceFactory)
18+
{
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\ServiceManager\TestAsset;
11+
12+
13+
class SimpleDependencyObject
14+
{
15+
public function __construct(InvokableObject $invokableObject, FailingFactory $failingFactory)
16+
{
17+
}
18+
}

0 commit comments

Comments
 (0)