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

Commit 60524e8

Browse files
committed
Allow the generate-deps-for-config-factory to create the specified config file
Modifies the tool slightly to allow it to create the specified config file if it does not currently exist.
1 parent 6ed63fd commit 60524e8

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-29
lines changed

doc/book/console-tools.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ Usage:
1414
Arguments:
1515

1616
-h|--help|help This usage message
17-
<configFile> Path to an existing config file for which to generate
18-
additional configuration. Must return an array.
17+
<configFile> Path to a config file for which to generate configuration.
18+
If the file does not exist, it will be created. If it does
19+
exist, it must return an array, and the file will be
20+
updated with new configuration.
1921
<className> Name of the class to reflect and for which to generate
2022
dependency configuration.
2123

2224

23-
Reads the provided configuration file, and injects it with
24-
ConfigAbstractFactory dependency configuration for the provided class
25-
name, writing the changes back to the file.
25+
Reads the provided configuration file (creating it if it does not exist),
26+
and injects it with ConfigAbstractFactory dependency configuration for
27+
the provided class name, writing the changes back to the file.
2628
```
2729
2830
This utility will generate dependency configuration for the named class for use
2931
with the [ConfigAbstractFactory](config-abstract-factory.md). When doing so, it
30-
will read the named configuration file, and merge any configuration it generates
31-
with the return values of that file, writing the changes back to the original
32-
file.
32+
will read the named configuration file (creating it if it does not exist), and
33+
merge any configuration it generates with the return values of that file,
34+
writing the changes back to the original file.
3335
3436
## generate-factory-for-class
3537

src/Tool/ConfigDumperCommand.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ class ConfigDumperCommand
2626
<info>Arguments:</info>
2727
2828
<info>-h|--help|help</info> This usage message
29-
<info><configFile></info> Path to an existing config file for which to generate
30-
additional configuration. Must return an array.
29+
<info><configFile></info> Path to a config file for which to generate configuration.
30+
If the file does not exist, it will be created. If it does
31+
exist, it must return an array, and the file will be
32+
updated with new configuration.
3133
<info><className></info> Name of the class to reflect and for which to generate
3234
dependency configuration.
3335
34-
Reads the provided configuration file, and injects it with
35-
ConfigAbstractFactory dependency configuration for the provided class
36-
name, writing the changes back to the file.
36+
Reads the provided configuration file (creating it if it does not exist),
37+
and injects it with ConfigAbstractFactory dependency configuration for
38+
the provided class name, writing the changes back to the file.
3739
EOH;
3840

3941
/**
@@ -119,21 +121,31 @@ private function parseArgs(array $args)
119121
return $this->createErrorArgument('Missing class name');
120122
}
121123

122-
if (! file_exists($arg1)) {
123-
return $this->createErrorArgument(sprintf(
124-
'Cannot find configuration file at path "%s"',
125-
$arg1
126-
));
127-
}
128-
129124
$configFile = $arg1;
130-
$config = require $configFile;
125+
switch (file_exists($configFile)) {
126+
case true:
127+
$config = require $configFile;
131128

132-
if (! is_array($config)) {
133-
return $this->createErrorArgument(sprintf(
134-
'Configuration at path "%s" does not return an array.',
135-
$configFile
136-
));
129+
if (! is_array($config)) {
130+
return $this->createErrorArgument(sprintf(
131+
'Configuration at path "%s" does not return an array.',
132+
$configFile
133+
));
134+
}
135+
136+
break;
137+
case false:
138+
// fall-through
139+
default:
140+
if (! is_writable(dirname($configFile))) {
141+
return $this->createErrorArgument(sprintf(
142+
'Cannot create configuration at path "%s"; not writable.',
143+
$configFile
144+
));
145+
}
146+
147+
$config = [];
148+
break;
137149
}
138150

139151
$class = array_shift($args);

test/Tool/ConfigDumperCommandTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,37 @@ public function testEmitsErrorWhenTooFewArgumentsPresent()
7777
$this->assertEquals(1, $command(['foo']));
7878
}
7979

80-
public function testEmitsErrorWhenConfigurationFileNotFound()
80+
public function testRaisesExceptionIfConfigFileNotFoundAndDirectoryNotWritable()
8181
{
8282
$command = $this->command;
83-
$this->assertErrorRaised('Cannot find configuration file at path "foo"');
83+
vfsStream::newDirectory('config', 0550)
84+
->at($this->configDir);
85+
$config = vfsStream::url('project/config/test.config.php');
86+
$this->assertErrorRaised(sprintf('Cannot create configuration at path "%s"; not writable.', $config));
8487
$this->assertHelp(STDERR);
85-
$this->assertEquals(1, $command(['foo', 'Not\A\Real\Class']));
88+
$this->assertEquals(1, $command([$config, 'Not\A\Real\Class']));
89+
}
90+
91+
public function testGeneratesConfigFileWhenProvidedConfigurationFileNotFound()
92+
{
93+
$command = $this->command;
94+
vfsStream::newDirectory('config', 0775)
95+
->at($this->configDir);
96+
$config = vfsStream::url('project/config/test.config.php');
97+
98+
$this->helper->writeLine('<info>[DONE]</info> Changes written to ' . $config)->shouldBeCalled();
99+
100+
$this->assertEquals(0, $command([$config, SimpleDependencyObject::class]));
101+
102+
$generated = include $config;
103+
$this->assertInternalType('array', $generated);
104+
$this->assertArrayHasKey(ConfigAbstractFactory::class, $generated);
105+
$factoryConfig = $generated[ConfigAbstractFactory::class];
106+
$this->assertInternalType('array', $factoryConfig);
107+
$this->assertArrayHasKey(SimpleDependencyObject::class, $factoryConfig);
108+
$this->assertArrayHasKey(InvokableObject::class, $factoryConfig);
109+
$this->assertContains(InvokableObject::class, $factoryConfig[SimpleDependencyObject::class]);
110+
$this->assertEquals([], $factoryConfig[InvokableObject::class]);
86111
}
87112

88113
public function testEmitsErrorWhenConfigurationFileDoesNotReturnArray()

0 commit comments

Comments
 (0)