Skip to content

Commit 34b5031

Browse files
committed
Updated README
1 parent 48081ea commit 34b5031

File tree

4 files changed

+78
-88
lines changed

4 files changed

+78
-88
lines changed

README.md

+13-17
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ PHPBench Service Container
44
[![Build Status](https://travis-ci.org/phpbench/container.svg?branch=master)](https://travis-ci.org/phpbench/container)
55
[![StyleCI](https://styleci.io/repos/55606670/shield)](https://styleci.io/repos/55606670)
66

7-
This is a simple but powerful (of course its powerful) dependency injection container:
7+
This is a simple but powerful dependency injection container:
88

99
- Extendable (a.k.a service providers);
1010
- Configurable;
11+
- Taggable services;
1112
- Extensions provide default configuration;
12-
- Service tagging;
1313

1414
This container was developed incidentally for PHPBench, and is not a very
1515
polished library, but I like it.
@@ -18,7 +18,7 @@ Simple usage
1818
------------
1919

2020
```php
21-
$container = new Container();
21+
$container = Container::create();
2222
$container->register('foobar', function (Container $container) {
2323
return new \stdClass();
2424
});
@@ -39,7 +39,6 @@ $container = new Container(
3939
'foo.bar' => 'my_new_value',
4040
]
4141
);
42-
$container->init(); // will trigger loading of the extensions.
4342
```
4443

4544
```php
@@ -48,18 +47,25 @@ class MyExtension implements ExtensionInterface
4847
public function load(Container $container)
4948
{
5049
$container->register('my_service', function (Container $container) {
51-
return new MyService(
50+
$service = new MyService(
5251
$container->getParameter('foo_bar'),
5352
$container->get('some_other_service')
5453
);
54+
55+
// get all tagged services and add them to the instantiated object
56+
foreach ($container->getServiceIdsForTag('my_tag') as $serviceId) {
57+
$service->add($container->get($serviceId));
58+
}
5559
});
5660

5761
$container->register('tagged_service', function (Container $container) {
58-
return new MyService(
62+
$service = new MyService(
5963
$container->getParameter('foo_bar'),
6064
$container->get('some_other_service')
6165
);
62-
}, [ 'tag' => []);
66+
67+
return $service;
68+
}, [ 'my_tag' => [ 'attribute_1' => 'foo' ]); // optional tag attributes
6369
}
6470

6571
/**
@@ -73,16 +79,6 @@ class MyExtension implements ExtensionInterface
7379
'foo_bar' => 'this is foo'
7480
];
7581
}
76-
77-
/**
78-
* Build the container.
79-
*/
80-
public function build(Container $container)
81-
{
82-
foreach ($container->getServiceIdsForTag() as $serviceId) {
83-
$container->get('my_service')->add($container->get($serviceId));
84-
}
85-
}
8682
}
8783
```
8884

lib/Container.php

+60-58
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,66 @@ public function __construct(array $extensionClasses = [], array $userConfig = []
3232
{
3333
$this->extensionClasses = $extensionClasses;
3434
$this->userConfig = $userConfig;
35-
$this->init();
35+
}
36+
37+
/**
38+
* Configure the container. This method will call the `configure()` method
39+
* on each extension. Extensions must use this opportunity to register their
40+
* services and define any default config.
41+
*
42+
* This method must be called before `build()`.
43+
*/
44+
public function init()
45+
{
46+
$extensions = [];
47+
48+
if (empty($this->extensionClasses) && empty($this->userConfig)) {
49+
return;
50+
}
51+
52+
foreach ($this->extensionClasses as $extensionClass) {
53+
if (!class_exists($extensionClass)) {
54+
throw new \InvalidArgumentException(sprintf(
55+
'Extension class "%s" does not exist',
56+
$extensionClass
57+
));
58+
}
59+
60+
$extension = new $extensionClass();
61+
62+
if (!$extension instanceof ExtensionInterface) {
63+
throw new \InvalidArgumentException(sprintf(
64+
// add any manually specified extensions
65+
'Extension "%s" must implement the PhpBench\\Extension interface',
66+
get_class($extension)
67+
));
68+
}
69+
70+
$extensions[] = $extension;
71+
72+
$this->config = array_merge(
73+
$this->config,
74+
$extension->getDefaultConfig()
75+
);
76+
}
77+
78+
$diff = array_diff(array_keys($this->userConfig), array_keys($this->config));
79+
80+
if ($diff) {
81+
throw new \InvalidArgumentException(sprintf(
82+
'Unknown configuration keys: "%s". Permitted keys: "%s"',
83+
implode('", "', $diff), implode('", "', array_keys($this->config))
84+
));
85+
}
86+
87+
$this->config = array_merge(
88+
$this->config,
89+
$this->userConfig
90+
);
91+
92+
foreach ($extensions as $extension) {
93+
$extension->load($this);
94+
}
3695
}
3796

3897
/**
@@ -177,61 +236,4 @@ public function hasParameter($name)
177236
{
178237
return array_key_exists($name, $this->config);
179238
}
180-
181-
private function init()
182-
{
183-
$extensions = [];
184-
185-
if (empty($this->extensionClasses) && empty($this->userConfig)) {
186-
return;
187-
}
188-
189-
foreach ($this->extensionClasses as $extensionClass) {
190-
if (!class_exists($extensionClass)) {
191-
throw new \InvalidArgumentException(sprintf(
192-
'Extension class "%s" does not exist',
193-
$extensionClass
194-
));
195-
}
196-
197-
$extension = new $extensionClass();
198-
199-
if (!$extension instanceof ExtensionInterface) {
200-
throw new \InvalidArgumentException(sprintf(
201-
// add any manually specified extensions
202-
'Extension "%s" must implement the PhpBench\\Extension interface',
203-
get_class($extension)
204-
));
205-
}
206-
207-
$extensions[] = $extension;
208-
209-
$this->config = array_merge(
210-
$this->config,
211-
$extension->getDefaultConfig()
212-
);
213-
}
214-
215-
$diff = array_diff(array_keys($this->userConfig), array_keys($this->config));
216-
217-
if ($diff) {
218-
throw new \InvalidArgumentException(sprintf(
219-
'Unknown configuration keys: "%s". Permitted keys: "%s"',
220-
implode('", "', $diff), implode('", "', array_keys($this->config))
221-
));
222-
}
223-
224-
$this->config = array_merge(
225-
$this->config,
226-
$this->userConfig
227-
);
228-
229-
foreach ($extensions as $extension) {
230-
$extension->load($this);
231-
}
232-
233-
foreach ($extensions as $extension) {
234-
$extension->build($this);
235-
}
236-
}
237239
}

lib/ExtensionInterface.php

-7
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,4 @@ public function load(Container $container);
2626
* @return array
2727
*/
2828
public function getDefaultConfig();
29-
30-
/**
31-
* Called after all services in all extensions have been registered.
32-
*
33-
* @param Container $container
34-
*/
35-
public function build(Container $container);
3629
}

tests/Unit/ContainerTest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public function testRegisterExtension()
103103
__NAMESPACE__ . '\\TestExtension',
104104
]);
105105

106+
$container->init();
106107
$object = $container->get('foobar');
107108
$this->assertInstanceOf('stdClass', $object);
108109
$this->assertEquals('bar', $object->foobar);
109-
$this->assertEquals('barfoo', $object->setOnBuild);
110110
}
111111

112112
/**
@@ -123,6 +123,7 @@ public function testRegisterExtensionWithUserConfig()
123123
]
124124
);
125125

126+
$container->init();
126127
$object = $container->get('foobar');
127128
$this->assertInstanceOf('stdClass', $object);
128129
$this->assertEquals('bazz', $object->foobar);
@@ -163,6 +164,7 @@ public function testMergeParameterNonArray()
163164
public function testRegisterNotExistingExtension()
164165
{
165166
$container = new Container(['NotExistingExtension']);
167+
$container->init();
166168
}
167169

168170
/**
@@ -175,6 +177,7 @@ public function testRegisterNotExistingExtension()
175177
public function testRegisterNotImplementingExtension()
176178
{
177179
$container = new Container(['stdClass']);
180+
$container->init();
178181
}
179182

180183
/**
@@ -188,6 +191,7 @@ public function testUnknownUserConfig()
188191
$container = new Container([], [
189192
'not' => 'existing',
190193
]);
194+
$container->init();
191195
}
192196

193197
/**
@@ -221,9 +225,4 @@ public function load(Container $container)
221225
return $stdClass;
222226
});
223227
}
224-
225-
public function build(Container $container)
226-
{
227-
$container->get('foobar')->setOnBuild = 'barfoo';
228-
}
229228
}

0 commit comments

Comments
 (0)