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

Commit 5238c02

Browse files
committed
Merge branch 'feature/154' into develop
Close #154
2 parents d42f638 + e546170 commit 5238c02

27 files changed

+1574
-13
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ All notable changes to this project will be documented in this file, in reverse
1111
configuration-based approach to providing class dependencies when all
1212
dependencies are services known to the `ServiceManager`. Please see
1313
[the documentation](doc/book/config-abstract-factory.md) for details.
14+
- [#154](https://github.com/zendframework/zend-servicemanager/pull/154) adds
15+
`Zend\ServiceManager\Tool\ConfigDumper`, which will introspect a given class
16+
to determine dependencies, and then create configuration for
17+
`Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory`, merging it with
18+
the provided configuration file. It also adds a vendor binary,
19+
`generate-deps-for-config-factory`, for generating these from the command
20+
line.
21+
- [#154](https://github.com/zendframework/zend-servicemanager/pull/154) adds
22+
`Zend\ServiceManager\Tool\FactoryCreator`, which will introspect a given class
23+
and generate a factory for it. It also adds a vendor binary,
24+
`generate-factory-for-class`, for generating these from the command line.
1425

1526
### Deprecated
1627

bin/generate-deps-for-config-factory

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
5+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
6+
* @license http://framework.zend.com/license/new-bsd New BSD License
7+
*/
8+
9+
namespace Zend\ServiceManager;
10+
11+
// Setup/verify autoloading
12+
if (file_exists($a = getcwd() . '/vendor/autoload.php')) {
13+
require $a;
14+
} elseif (file_exists($a = __DIR__ . '/../../../autoload.php')) {
15+
require $a;
16+
} elseif (file_exists($a = __DIR__ . '/../vendor/autoload.php')) {
17+
require $a;
18+
} else {
19+
fwrite(STDERR, 'Cannot locate autoloader; please run "composer install"' . PHP_EOL);
20+
exit(1);
21+
}
22+
23+
$command = new Tool\ConfigDumperCommand($argv[0]);
24+
$status = $command(array_slice($argv, 1));
25+
exit($status);

bin/generate-factory-for-class

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
5+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
6+
* @license http://framework.zend.com/license/new-bsd New BSD License
7+
*/
8+
9+
namespace Zend\ServiceManager;
10+
11+
// Setup/verify autoloading
12+
if (file_exists($a = getcwd() . '/vendor/autoload.php')) {
13+
require $a;
14+
} elseif (file_exists($a = __DIR__ . '/../../../autoload.php')) {
15+
require $a;
16+
} elseif (file_exists($a = __DIR__ . '/../vendor/autoload.php')) {
17+
require $a;
18+
} else {
19+
fwrite(STDERR, 'Cannot locate autoloader; please run "composer install"' . PHP_EOL);
20+
exit(1);
21+
}
22+
23+
$command = new Tool\FactoryCreatorCommand($argv[0]);
24+
$status = $command(array_slice($argv, 1));
25+
exit($status);

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"require": {
1717
"php": "^5.6 || ^7.0",
18-
"container-interop/container-interop": "~1.0"
18+
"container-interop/container-interop": "~1.0",
19+
"zendframework/zend-stdlib": "^3.1"
1920
},
2021
"require-dev": {
2122
"phpunit/phpunit": "^4.6 || ^5.2.10",
@@ -43,5 +44,9 @@
4344
},
4445
"provide": {
4546
"container-interop/container-interop-implementation": "^1.1"
46-
}
47+
},
48+
"bin": [
49+
"bin/generate-deps-for-config-factory",
50+
"bin/generate-factory-for-class"
51+
]
4752
}

doc/book/console-tools.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Console Tools
2+
3+
Starting in 3.2.0, zend-servicemanager began shipping with console tools. This
4+
document details each.
5+
6+
## generate-deps-for-config-factory
7+
8+
```bash
9+
$ ./vendor/bin/generate-deps-for-config-factory
10+
Usage:
11+
12+
generate-deps-for-config-factory [-h|--help|help] <configFile> <className>
13+
14+
Arguments:
15+
16+
-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.
19+
<className> Name of the class to reflect and for which to generate
20+
dependency configuration.
21+
22+
Generates to STDOUT a replacement configuration file containing dependency
23+
configuration for the named class with which to configure the
24+
ConfigAbstractFactory.
25+
```
26+
27+
This utility will generate dependency configuration for the named class for use
28+
with the [ConfigAbstractFactory](config-abstract-factory.md). When doing so, it
29+
will read the named configuration file, and merge any configuration it generates
30+
with the return values of that file, emitting the updated version to STDOUT.
31+
This allows you to pipe it back to the original:
32+
33+
```bash
34+
$ ./vendor/bin/generate-deps-for-config-factory \
35+
> ./config/autoload/dependencies.local.php \
36+
> "Application\\Model\\AlbumModel" > ./config/autoload/dependencies.local.php
37+
```
38+
39+
Alternately, you can pipe them to a new file, so that you can diff the original
40+
to the generated file.
41+
42+
## generate-factory-for-class
43+
44+
```bash
45+
$ ./vendor/bin/generate-factory-for-class
46+
47+
Usage:
48+
49+
./bin/generate-factory-for-class [-h|--help|help] <className>
50+
51+
Arguments:
52+
53+
-h|--help|help This usage message
54+
<className> Name of the class to reflect and for which to generate
55+
a factory.
56+
57+
Generates to STDOUT a factory for creating the specified class; this may then
58+
be added to your application, and configured as a factory for the class.
59+
```
60+
61+
This utility generates a factory class for the given class, based on the
62+
typehints in its constructor. The factory is emitted to STDOUT, and may be piped
63+
to a file if desired:
64+
65+
```bash
66+
$ ./vendor/bin/generate-factory-for-class \
67+
> "Application\\Model\\AlbumModel" > ./module/Application/src/Model/AlbumModelFactory.php
68+
```
69+
70+
The class generated implements `Zend\ServiceManager\Factory\FactoryInterface`,
71+
and is generated within the same namespace as the originating class.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pages:
99
- 'Lazy services': lazy-services.md
1010
- 'Plugin managers': plugin-managers.md
1111
- 'Configuration-based Abstract Factory': config-abstract-factory.md
12+
- 'Console Tools': console-tools.md
1213
- 'Migration Guide': migration.md
1314
site_name: zend-servicemanager
1415
site_description: 'zend-servicemanager: factory-driven dependency injection container'

phpcs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99
<!-- inherit rules from: -->
1010
<rule ref="PSR2"/>
1111
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
12+
<rule ref="Generic.Formatting.SpaceAfterNot"/>
13+
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
14+
<properties>
15+
<property name="ignoreNewlines" value="true"/>
16+
</properties>
17+
</rule>
1218
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
1319
<properties>
1420
<property name="ignoreBlankLines" value="false"/>
1521
</properties>
1622
</rule>
1723

1824
<!-- Paths to check -->
25+
<file>bin</file>
1926
<file>src</file>
2027
<file>test</file>
2128
<exclude-pattern>*/test/log/*</exclude-pattern>

src/AbstractFactory/ConfigAbstractFactory.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class ConfigAbstractFactory implements AbstractFactoryInterface
2222
*/
2323
public function canCreate(\Interop\Container\ContainerInterface $container, $requestedName)
2424
{
25-
if (!$container->has('config') || !array_key_exists(self::class, $container->get('config'))) {
25+
if (! $container->has('config') || ! array_key_exists(self::class, $container->get('config'))) {
2626
return false;
2727
}
2828
$config = $container->get('config');
@@ -36,25 +36,25 @@ public function canCreate(\Interop\Container\ContainerInterface $container, $req
3636
*/
3737
public function __invoke(\Interop\Container\ContainerInterface $container, $requestedName, array $options = null)
3838
{
39-
if (!$container->has('config')) {
39+
if (! $container->has('config')) {
4040
throw new ServiceNotCreatedException('Cannot find a config array in the container');
4141
}
4242

4343
$config = $container->get('config');
4444

45-
if (!is_array($config)) {
45+
if (! is_array($config)) {
4646
throw new ServiceNotCreatedException('Config must be an array');
4747
}
4848

49-
if (!array_key_exists(self::class, $config)) {
49+
if (! array_key_exists(self::class, $config)) {
5050
throw new ServiceNotCreatedException('Cannot find a `' . self::class . '` key in the config array');
5151
}
5252

5353
$dependencies = $config[self::class];
5454

55-
if (!is_array($dependencies)
56-
|| !array_key_exists($requestedName, $dependencies)
57-
|| !is_array($dependencies[$requestedName])
55+
if (! is_array($dependencies)
56+
|| ! array_key_exists($requestedName, $dependencies)
57+
|| ! is_array($dependencies[$requestedName])
5858
) {
5959
throw new ServiceNotCreatedException('Dependencies config must exist and be an array');
6060
}

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private function merge(array $a, array $b)
111111
$a[$key] = $value;
112112
}
113113
} else {
114-
if (!$value instanceof MergeRemoveKey) {
114+
if (! $value instanceof MergeRemoveKey) {
115115
$a[$key] = $value;
116116
}
117117
}

src/ServiceManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ private function resolveAbstractFactories(array $abstractFactories)
520520
foreach ($abstractFactories as $abstractFactory) {
521521
if (is_string($abstractFactory) && class_exists($abstractFactory)) {
522522
//Cached string
523-
if (!isset($this->cachedAbstractFactories[$abstractFactory])) {
523+
if (! isset($this->cachedAbstractFactories[$abstractFactory])) {
524524
$this->cachedAbstractFactories[$abstractFactory] = new $abstractFactory();
525525
}
526526

@@ -754,7 +754,7 @@ private function createDelegatorFromName($name, array $options = null)
754754
private function doCreate($resolvedName, array $options = null)
755755
{
756756
try {
757-
if (!isset($this->delegators[$resolvedName])) {
757+
if (! isset($this->delegators[$resolvedName])) {
758758
// Let's create the service by fetching the factory
759759
$factory = $this->getFactory($resolvedName);
760760
$object = $factory($this->creationContext, $resolvedName, $options);
@@ -889,7 +889,7 @@ private function createFactoriesForInvokables(array $invokables)
889889
*/
890890
private function validateOverrides(array $config)
891891
{
892-
if ($this->allowOverride || !$this->configured) {
892+
if ($this->allowOverride || ! $this->configured) {
893893
return;
894894
}
895895

0 commit comments

Comments
 (0)