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

Commit 7dd9ef6

Browse files
committed
Create logic to generate valid config file output
- Added `dumpConfigFile` static method, which will dump a config file written in PHP that returns an array. Values use array notation and `::class` notation. - Updated bin script to echo results of `dumpConfigFile()`, and to use try/catch block around `createDependencyConfig()`, reporting errors if necessary.
1 parent b8a7b6b commit 7dd9ef6

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

bin/dump-config.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@
3333
exit(1);
3434
}
3535

36-
if (! class_exists($className)) {
37-
fwrite(STDERR, sprintf('Class "%s" does not exist%s', $className, PHP_EOL));
38-
exit(1);
36+
try {
37+
$config = Tool\CliTool::createDependencyConfig($appConfig, $className);
38+
} catch (Exception\InvalidArgumentException $e) {
39+
fwrite(STDERR, sprintf(
40+
'Unable to create config for "%s": %s%s',
41+
$className,
42+
$e->getMessage(),
43+
PHP_EOL
44+
));
3945
}
40-
41-
Tool\CliTool::createDependencyConfig($appConfig, $className);
46+
echo Tool\CliTool::dumpConfigFile($config);
47+
exit(0);

src/Tool/CliTool.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@
99

1010
namespace Zend\ServiceManager\Tool;
1111

12+
use Traversable;
1213
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
1314
use Zend\ServiceManager\Exception\InvalidArgumentException;
1415

1516
class CliTool
1617
{
18+
const CONFIG_TEMPLATE = <<<EOC
19+
<?php
20+
/**
21+
* This file generated by Zend\ServiceManager\Tool\CliTool.
22+
* Generated %s
23+
*/
24+
25+
return %s;
26+
EOC;
27+
1728
/**
1829
* @param array $config
1930
* @param $className
@@ -107,4 +118,38 @@ public static function createFactoryMappings(array $config, $className)
107118
$config['service_manager']['factories'][$className] = ConfigAbstractFactory::class;
108119
return $config;
109120
}
121+
122+
public static function dumpConfigFile(array $config)
123+
{
124+
$prepared = self::prepareConfig($config);
125+
return sprintf(self::CONFIG_TEMPLATE, date('Y-m-d H:i:s'), $prepared);
126+
}
127+
128+
private static function prepareConfig($config, $indentLevel = 1)
129+
{
130+
$indent = str_repeat(' ', $indentLevel * 4);
131+
$entries = [];
132+
foreach ($config as $key => $value) {
133+
$key = class_exists($key)
134+
? sprintf('\\%s::class', $key)
135+
: sprintf("'%s'", $key);
136+
$value = is_array($value) || $value instanceof Traversable
137+
? self::prepareConfig($value, $indentLevel + 1)
138+
: var_export($value, true);
139+
$entries[] = sprintf(
140+
'%s%s => %s,',
141+
$indent,
142+
$key,
143+
$value
144+
);
145+
}
146+
147+
$outerIndent = str_repeat(' ', ($indentLevel - 1) * 4);
148+
149+
return sprintf(
150+
"[\n%s\n%s]",
151+
implode("\n", $entries),
152+
$outerIndent
153+
);
154+
}
110155
}

test/Tool/CliToolTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace ZendTest\ServiceManager\Tool;
1212

13+
use PHPUnit_Framework_TestCase as TestCase;
1314
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
1415
use Zend\ServiceManager\Exception\InvalidArgumentException;
1516
use Zend\ServiceManager\Tool\CliTool;
@@ -19,7 +20,7 @@
1920
use ZendTest\ServiceManager\TestAsset\SecondComplexDependencyObject;
2021
use ZendTest\ServiceManager\TestAsset\SimpleDependencyObject;
2122

22-
class CliToolTest extends \PHPUnit_Framework_TestCase
23+
class CliToolTest extends TestCase
2324
{
2425
public function testCreateDependencyConfigExceptsIfClassNameIsNotString()
2526
{
@@ -63,6 +64,7 @@ public function testCreateDependencyConfigSimpleDependencyReturnsCorrectly()
6364
],
6465
$config
6566
);
67+
return $config;
6668
}
6769

6870
public function testCreateDependencyConfigClassWithoutConstructorChangesNothing()
@@ -188,4 +190,26 @@ public function testCreateFactoryMappingsFromConfigWithWorkingConfig()
188190

189191
self::assertEquals($expectedConfig, CliTool::createFactoryMappingsFromConfig($config));
190192
}
193+
194+
/**
195+
* @depends testCreateDependencyConfigSimpleDependencyReturnsCorrectly
196+
*/
197+
public function testDumpConfigFileReturnsContentsForConfigFileUsingUsingClassNotationAndShortArrays(array $config)
198+
{
199+
$formatted = CliTool::dumpConfigFile($config);
200+
$this->assertContains(
201+
'<' . "?php\n/**\n * This file generated by Zend\ServiceManager\Tool\CliTool.\n",
202+
$formatted
203+
);
204+
205+
$this->assertNotContains('array(', $formatted);
206+
$this->assertContains('::class', $formatted);
207+
208+
$file = tempnam(sys_get_temp_dir(), 'ZSCLI');
209+
file_put_contents($file, $formatted);
210+
$test = include($file);
211+
unlink($file);
212+
213+
$this->assertEquals($test, $config);
214+
}
191215
}

0 commit comments

Comments
 (0)