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

Commit bacd780

Browse files
committed
first pass on working config generator which effectively does nothing
1 parent 94efd0f commit bacd780

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed

bin/dump-config.php

Lines changed: 23 additions & 0 deletions
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
require_once(__DIR__ . '/../vendor/autoload.php');
11+
chdir(__DIR__ . '/../');
12+
13+
$configPath = isset($argv[1]) ? $argv[1] : '';
14+
$className = isset($argv[2]) ? $argv[2] : '';
15+
16+
// Retrieve configuration
17+
if (!file_exists($configPath)) {
18+
throw new InvalidArgumentException('Cannot find any config at ' . $configPath);
19+
}
20+
21+
$appConfig = require $configPath;
22+
23+
\Zend\ServiceManager\Tool\CliTool::handle($appConfig, $className);

src/Tool/CliTool.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\Tool;
11+
12+
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
13+
use Zend\ServiceManager\Exception\InvalidArgumentException;
14+
15+
class CliTool
16+
{
17+
/**
18+
* @param array $config
19+
* @param $className
20+
* @return array
21+
* @throws InvalidArgumentException
22+
*/
23+
public static function handle(array $config, $className)
24+
{
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+
}
36+
37+
$reflectionClass = new \ReflectionClass($className);
38+
39+
if (!$reflectionClass->getConstructor()) {
40+
return $config;
41+
}
42+
43+
$constructorArguments = $reflectionClass->getConstructor()->getParameters();
44+
45+
$constructorArguments = array_filter(
46+
$constructorArguments,
47+
function (\ReflectionParameter $argument) {
48+
return !$argument->isOptional();
49+
}
50+
);
51+
52+
// has no required parameters, we can just add an empty array
53+
if (empty($constructorArguments)) {
54+
$config[ConfigAbstractFactory::class][$className] = [];
55+
return $config;
56+
}
57+
58+
foreach ($constructorArguments as $constructorArgument) {
59+
$argumentType = $constructorArgument->getClass();
60+
if (is_null($argumentType)) {
61+
throw new InvalidArgumentException('Cannot create config for ' . $className . ', it has no type hints in constructor');
62+
}
63+
$argumentName = $argumentType->getName();
64+
$config = self::handle($config, $argumentName);
65+
$config[ConfigAbstractFactory::class][$className][] = $argumentName;
66+
}
67+
68+
return $config;
69+
}
70+
}
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 ObjectWithScalarDependency
14+
{
15+
public function __construct($aName, $aValue)
16+
{
17+
}
18+
}

test/TestAsset/config/test.config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
return [];

test/Tool/CliToolTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: GeeH
5+
* Date: 06/09/2016
6+
* Time: 12:34
7+
*/
8+
9+
namespace ZendTest\ServiceManager\Tool;
10+
11+
12+
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
13+
use Zend\ServiceManager\Exception\InvalidArgumentException;
14+
use Zend\ServiceManager\Tool\CliTool;
15+
use ZendTest\ServiceManager\TestAsset\FailingFactory;
16+
use ZendTest\ServiceManager\TestAsset\InvokableObject;
17+
use ZendTest\ServiceManager\TestAsset\ObjectWithScalarDependency;
18+
use ZendTest\ServiceManager\TestAsset\SimpleDependencyObject;
19+
20+
class CliToolTest extends \PHPUnit_Framework_TestCase
21+
{
22+
public function testExceptsIfClassNameIsNotString()
23+
{
24+
self::expectException(InvalidArgumentException::class);
25+
self::expectExceptionMessage('Class name must be a string, integer given');
26+
CliTool::handle([], 42);
27+
}
28+
29+
public function testExceptsIfClassDoesNotExist()
30+
{
31+
$className = 'Dirk\Gentley\Holistic\Detective\Agency';
32+
self::expectException(InvalidArgumentException::class);
33+
self::expectExceptionMessage('Cannot find class with name ' . $className);
34+
CliTool::handle([], $className);
35+
}
36+
37+
public function testInvokableObjectReturnsEmptyArray()
38+
{
39+
$config = CliTool::handle([], InvokableObject::class);
40+
self::assertEquals(
41+
[
42+
ConfigAbstractFactory::class => [
43+
InvokableObject::class => []
44+
]
45+
],
46+
$config
47+
);
48+
}
49+
50+
public function testSimpleDependencyReturnsCorrectly()
51+
{
52+
$config = CliTool::handle([], SimpleDependencyObject::class);
53+
self::assertEquals(
54+
[
55+
ConfigAbstractFactory::class => [
56+
SimpleDependencyObject::class => [
57+
InvokableObject::class,
58+
],
59+
InvokableObject::class => [],
60+
]
61+
],
62+
$config
63+
);
64+
}
65+
66+
public function testClassWithoutConstructorChangesNothing()
67+
{
68+
$config = CliTool::handle([ConfigAbstractFactory::class => []], FailingFactory::class);
69+
self::assertEquals([ConfigAbstractFactory::class => []], $config);
70+
}
71+
72+
public function testWhatHappensWhenYouHaveNoTypeHint()
73+
{
74+
self::expectException(InvalidArgumentException::class);
75+
self::expectExceptionMessage(
76+
'Cannot create config for ' . ObjectWithScalarDependency::class . ', it has no type hints in constructor'
77+
);
78+
$config = CliTool::handle([ConfigAbstractFactory::class => []], ObjectWithScalarDependency::class);
79+
80+
}
81+
82+
}

0 commit comments

Comments
 (0)