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

Commit 0a1cbad

Browse files
committed
Added feature for generating factory mappings
1 parent bacd780 commit 0a1cbad

File tree

5 files changed

+185
-35
lines changed

5 files changed

+185
-35
lines changed

bin/dump-config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020

2121
$appConfig = require $configPath;
2222

23-
\Zend\ServiceManager\Tool\CliTool::handle($appConfig, $className);
23+
\Zend\ServiceManager\Tool\CliTool::createDependencyConfig($appConfig, $className);

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
<!-- Paths to check -->
1919
<file>src</file>
2020
<file>test</file>
21+
<file>bin</file>
2122
<exclude-pattern>*/test/log/*</exclude-pattern>
2223
</ruleset>

src/Tool/CliTool.php

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,16 @@ class CliTool
2020
* @return array
2121
* @throws InvalidArgumentException
2222
*/
23-
public static function handle(array $config, $className)
23+
public static function createDependencyConfig(array $config, $className)
2424
{
25-
if (!is_string($className)) {
26-
throw new InvalidArgumentException('Class name must be a string, ' . gettype($className) . ' given');
27-
}
28-
29-
if (!class_exists($className)) {
30-
throw new InvalidArgumentException('Cannot find class with name ' . $className);
31-
}
32-
33-
if (!array_key_exists(ConfigAbstractFactory::class, $config)) {
34-
$config[ConfigAbstractFactory::class] = [];
35-
}
25+
self::validateClassName($className);
3626

3727
$reflectionClass = new \ReflectionClass($className);
38-
3928
if (!$reflectionClass->getConstructor()) {
4029
return $config;
4130
}
4231

4332
$constructorArguments = $reflectionClass->getConstructor()->getParameters();
44-
4533
$constructorArguments = array_filter(
4634
$constructorArguments,
4735
function (\ReflectionParameter $argument) {
@@ -58,13 +46,66 @@ function (\ReflectionParameter $argument) {
5846
foreach ($constructorArguments as $constructorArgument) {
5947
$argumentType = $constructorArgument->getClass();
6048
if (is_null($argumentType)) {
61-
throw new InvalidArgumentException('Cannot create config for ' . $className . ', it has no type hints in constructor');
49+
throw new InvalidArgumentException(
50+
'Cannot create config for ' . $className . ', it has no type hints in constructor'
51+
);
6252
}
6353
$argumentName = $argumentType->getName();
64-
$config = self::handle($config, $argumentName);
54+
$config = self::createDependencyConfig($config, $argumentName);
6555
$config[ConfigAbstractFactory::class][$className][] = $argumentName;
6656
}
6757

6858
return $config;
6959
}
60+
61+
/**
62+
* @param $className
63+
* @throws InvalidArgumentException
64+
*/
65+
private static function validateClassName($className)
66+
{
67+
if (!is_string($className)) {
68+
throw new InvalidArgumentException('Class name must be a string, ' . gettype($className) . ' given');
69+
}
70+
71+
if (!class_exists($className)) {
72+
throw new InvalidArgumentException('Cannot find class with name ' . $className);
73+
}
74+
}
75+
76+
public static function createFactoryMappingsFromConfig(array $config)
77+
{
78+
if (!array_key_exists(ConfigAbstractFactory::class, $config)) {
79+
return $config;
80+
}
81+
82+
if (!is_array($config[ConfigAbstractFactory::class])) {
83+
throw new InvalidArgumentException(
84+
'Config key for ' . ConfigAbstractFactory::class . ' should be an array, ' . gettype(
85+
$config[ConfigAbstractFactory::class]
86+
) . ' given'
87+
);
88+
}
89+
90+
foreach ($config[ConfigAbstractFactory::class] as $className => $dependency) {
91+
$config = self::createFactoryMappings($config, $className);
92+
}
93+
return $config;
94+
}
95+
96+
public static function createFactoryMappings(array $config, $className)
97+
{
98+
self::validateClassName($className);
99+
100+
if (
101+
array_key_exists('service_manager', $config)
102+
&& array_key_exists('factories', $config['service_manager'])
103+
&& array_key_exists($className, $config['service_manager']['factories'])
104+
) {
105+
return $config;
106+
}
107+
108+
$config['service_manager']['factories'][$className] = ConfigAbstractFactory::class;
109+
return $config;
110+
}
70111
}

test/TestAsset/ObjectWithScalarDependency.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace ZendTest\ServiceManager\TestAsset;
1111

12-
1312
class ObjectWithScalarDependency
1413
{
1514
public function __construct($aName, $aValue)

test/Tool/CliToolTest.php

Lines changed: 126 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
<?php
22
/**
3-
* Created by PhpStorm.
4-
* User: GeeH
5-
* Date: 06/09/2016
6-
* Time: 12:34
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
78
*/
89

9-
namespace ZendTest\ServiceManager\Tool;
1010

11+
namespace ZendTest\ServiceManager\Tool;
1112

1213
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
1314
use Zend\ServiceManager\Exception\InvalidArgumentException;
1415
use Zend\ServiceManager\Tool\CliTool;
1516
use ZendTest\ServiceManager\TestAsset\FailingFactory;
1617
use ZendTest\ServiceManager\TestAsset\InvokableObject;
1718
use ZendTest\ServiceManager\TestAsset\ObjectWithScalarDependency;
19+
use ZendTest\ServiceManager\TestAsset\SecondComplexDependencyObject;
1820
use ZendTest\ServiceManager\TestAsset\SimpleDependencyObject;
1921

2022
class CliToolTest extends \PHPUnit_Framework_TestCase
2123
{
22-
public function testExceptsIfClassNameIsNotString()
24+
public function testCreateDependencyConfigExceptsIfClassNameIsNotString()
2325
{
2426
self::expectException(InvalidArgumentException::class);
2527
self::expectExceptionMessage('Class name must be a string, integer given');
26-
CliTool::handle([], 42);
28+
CliTool::createDependencyConfig([], 42);
2729
}
2830

29-
public function testExceptsIfClassDoesNotExist()
31+
public function testCreateDependencyConfigExceptsIfClassDoesNotExist()
3032
{
3133
$className = 'Dirk\Gentley\Holistic\Detective\Agency';
3234
self::expectException(InvalidArgumentException::class);
3335
self::expectExceptionMessage('Cannot find class with name ' . $className);
34-
CliTool::handle([], $className);
36+
CliTool::createDependencyConfig([], $className);
3537
}
3638

37-
public function testInvokableObjectReturnsEmptyArray()
39+
public function testCreateDependencyConfigInvokableObjectReturnsEmptyArray()
3840
{
39-
$config = CliTool::handle([], InvokableObject::class);
41+
$config = CliTool::createDependencyConfig([], InvokableObject::class);
4042
self::assertEquals(
4143
[
4244
ConfigAbstractFactory::class => [
@@ -47,9 +49,9 @@ public function testInvokableObjectReturnsEmptyArray()
4749
);
4850
}
4951

50-
public function testSimpleDependencyReturnsCorrectly()
52+
public function testCreateDependencyConfigSimpleDependencyReturnsCorrectly()
5153
{
52-
$config = CliTool::handle([], SimpleDependencyObject::class);
54+
$config = CliTool::createDependencyConfig([], SimpleDependencyObject::class);
5355
self::assertEquals(
5456
[
5557
ConfigAbstractFactory::class => [
@@ -63,20 +65,127 @@ public function testSimpleDependencyReturnsCorrectly()
6365
);
6466
}
6567

66-
public function testClassWithoutConstructorChangesNothing()
68+
public function testCreateDependencyConfigClassWithoutConstructorChangesNothing()
6769
{
68-
$config = CliTool::handle([ConfigAbstractFactory::class => []], FailingFactory::class);
70+
$config = CliTool::createDependencyConfig([ConfigAbstractFactory::class => []], FailingFactory::class);
6971
self::assertEquals([ConfigAbstractFactory::class => []], $config);
7072
}
7173

72-
public function testWhatHappensWhenYouHaveNoTypeHint()
74+
public function testCreateDependencyConfigWithoutTypeHintedParameterExcepts()
7375
{
7476
self::expectException(InvalidArgumentException::class);
7577
self::expectExceptionMessage(
7678
'Cannot create config for ' . ObjectWithScalarDependency::class . ', it has no type hints in constructor'
7779
);
78-
$config = CliTool::handle([ConfigAbstractFactory::class => []], ObjectWithScalarDependency::class);
80+
$config = CliTool::createDependencyConfig(
81+
[ConfigAbstractFactory::class => []],
82+
ObjectWithScalarDependency::class
83+
);
84+
}
85+
86+
public function testCreateFactoryMappingsExceptsIfClassNameIsNotString()
87+
{
88+
self::expectException(InvalidArgumentException::class);
89+
self::expectExceptionMessage('Class name must be a string, integer given');
90+
CliTool::createFactoryMappings([], 42);
91+
}
92+
93+
public function testCreateFactoryMappingsExceptsIfClassDoesNotExist()
94+
{
95+
$className = 'Dirk\Gentley\Holistic\Detective\Agency';
96+
self::expectException(InvalidArgumentException::class);
97+
self::expectExceptionMessage('Cannot find class with name ' . $className);
98+
CliTool::createFactoryMappings([], $className);
99+
}
100+
101+
public function testCreateFactoryMappingsReturnsUnmodifiedArrayIfMappingExists()
102+
{
103+
$config = [
104+
'service_manager' => [
105+
'factories' => [
106+
InvokableObject::class => ConfigAbstractFactory::class,
107+
],
108+
],
109+
];
110+
self::assertEquals($config, CliTool::createFactoryMappings($config, InvokableObject::class));
111+
}
112+
113+
public function testCreateFactoryMappingsAddsClassIfNotExists()
114+
{
115+
$expectedConfig = [
116+
'service_manager' => [
117+
'factories' => [
118+
InvokableObject::class => ConfigAbstractFactory::class,
119+
],
120+
],
121+
];
122+
self::assertEquals($expectedConfig, CliTool::createFactoryMappings([], InvokableObject::class));
123+
}
124+
125+
public function testCreateFactoryMappingsIgnoresExistingsMappings()
126+
{
127+
$config = [
128+
'service_manager' => [
129+
'factories' => [
130+
InvokableObject::class => 'SomeOtherExistingFactory',
131+
],
132+
],
133+
];
134+
self::assertEquals($config, CliTool::createFactoryMappings($config, InvokableObject::class));
135+
}
136+
137+
public function testCreateFactoryMappingsFromConfigReturnsIfNoConfigKey()
138+
{
139+
self::assertEquals([], CliTool::createFactoryMappingsFromConfig([]));
140+
}
141+
142+
public function testCreateFactoryMappingsFromConfigExceptsWhenConfigNotArray()
143+
{
144+
self::expectException(InvalidArgumentException::class);
145+
self::expectExceptionMessage(
146+
'Config key for ' . ConfigAbstractFactory::class . ' should be an array, boolean given'
147+
);
79148

149+
CliTool::createFactoryMappingsFromConfig(
150+
[
151+
ConfigAbstractFactory::class => true,
152+
]
153+
);
80154
}
81155

156+
public function testCreateFactoryMappingsFromConfigWithWorkingConfig()
157+
{
158+
$config = [
159+
ConfigAbstractFactory::class => [
160+
InvokableObject::class => [],
161+
SimpleDependencyObject::class => [
162+
InvokableObject::class,
163+
],
164+
SecondComplexDependencyObject::class => [
165+
InvokableObject::class,
166+
],
167+
],
168+
];
169+
170+
$expectedConfig = [
171+
ConfigAbstractFactory::class => [
172+
InvokableObject::class => [],
173+
SimpleDependencyObject::class => [
174+
InvokableObject::class,
175+
],
176+
SecondComplexDependencyObject::class => [
177+
InvokableObject::class,
178+
],
179+
],
180+
'service_manager' => [
181+
'factories' => [
182+
InvokableObject::class => ConfigAbstractFactory::class,
183+
SimpleDependencyObject::class => ConfigAbstractFactory::class,
184+
SecondComplexDependencyObject::class => ConfigAbstractFactory::class,
185+
],
186+
],
187+
];
188+
189+
self::assertEquals($expectedConfig, CliTool::createFactoryMappingsFromConfig($config));
190+
}
82191
}

0 commit comments

Comments
 (0)