Skip to content

Commit 42fd712

Browse files
committed
Altered namespace and some of the structure, added some more documentation to the README.
1 parent 5223202 commit 42fd712

File tree

12 files changed

+273
-199
lines changed

12 files changed

+273
-199
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,59 @@ $ composer require strident/container ~1.0
1818
The container is incredibly simple to instantiate. Simply do the following:
1919

2020
```php
21-
use Container\Container;
21+
use Strident\Container\Container;
2222

2323
$container = new Container();
2424
```
2525

26+
###Defining Services
27+
28+
From there, you can define services like so:
29+
30+
```php
31+
// 'Service' class defined somewhere, and 'dependency_name' service defined
32+
33+
$container->set("service_name", function($container) {
34+
return new Service($container->get("dependency_name"));
35+
});
36+
```
37+
38+
The services are lazy loaded (i.e. nothing is instantiated until it is called).
39+
40+
###Extending a Service After Definition
41+
42+
You may also extend services once they have been defined (for example, if you wish to augment a service, alter settings, swap out dependencies etc). But note that once a service is used, it becomes locked and cannot be modified further.
43+
44+
```php
45+
$container->extend("service_name", function($service, $container) {
46+
$service->doSomething();
47+
$service->addSomething($container->get("dependency_name"));
48+
49+
return $service;
50+
});
51+
```
52+
53+
###Defining Factory Services
54+
55+
If you wish to return a new instance of your service every time instead of the same instance, you may define a factory service like so:
56+
57+
```php
58+
$container->set("service_name", $container->factory(function($container) {
59+
return new Service($container->get("dependency_name"));
60+
}));
61+
```
62+
63+
###Parameters
64+
65+
Parameters are get / set by accessing the container like an array:
66+
67+
```php
68+
// Set
69+
$container["foo"] = "A parameter can be anything";
70+
71+
// Get
72+
$foo = $container["foo"];
73+
```
74+
2675
[1]: https://github.com/silexphp/Pimple
2776

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "strident/container",
33
"type": "library",
4-
"description": "Simple dependency injection container, inspired by Pimple.",
5-
"keywords": [ "dic" ],
4+
"description": "Dependency injection container component designed for ease of use and speed. Built for Trident. Heavily inspired by Pimple.",
5+
"keywords": [ "dic", "component", "strident", "trident" ],
66
"license": "MIT",
77
"homepage": "https://github.com/Strident/Container",
88
"authors": [
@@ -19,10 +19,10 @@
1919
"codeclimate/php-test-reporter": "~0.1"
2020
},
2121
"autoload": {
22-
"psr-0": { "Container\\": "src/" }
22+
"psr-4": { "Strident\\Container\\": "src/" }
2323
},
2424
"autoload-dev": {
25-
"psr-4": { "Container\\Tests\\": "tests/" }
25+
"psr-4": { "Strident\\Container\\Tests\\": "tests/" }
2626
},
2727
"minimum-stability": "dev"
2828
}
Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Container;
12+
namespace Strident\Container;
1313

1414
use ArrayAccess;
1515
use Closure;
@@ -96,30 +96,20 @@ public function offsetUnset($name)
9696
return $this->removeParameter($name);
9797
}
9898

99-
/**
100-
* {@inheritDoc}
101-
*/
102-
public function set($name, $value)
103-
{
104-
$this->services->set($name, $value);
105-
106-
return $this;
107-
}
108-
10999
/**
110100
* {@inheritDoc}
111101
*/
112102
public function get($name)
113103
{
114104
if (!$this->services->has($name)) {
115-
throw new InvalidArgumentException(sprintf('Service "%s" is not defined.', $name));
105+
throw new InvalidArgumentException(sprintf("Service \"%s\" is not defined.", $name));
116106
}
117107

118108
/** @var Closure $service */
119109
$service = $this->services->get($name);
120110

121111
if ($this->services->isLocked($name)
122-
|| !method_exists($this->services->get($name), '__invoke')) {
112+
|| !method_exists($this->services->get($name), "__invoke")) {
123113
return $service;
124114
}
125115

@@ -160,9 +150,9 @@ public function remove($name)
160150
/**
161151
* {@inheritDoc}
162152
*/
163-
public function setParameter($name, $value)
153+
public function set($name, $value)
164154
{
165-
$this->parameters->set($name, $value);
155+
$this->services->set($name, $value);
166156

167157
return $this;
168158
}
@@ -173,7 +163,7 @@ public function setParameter($name, $value)
173163
public function getParameter($name)
174164
{
175165
if (!$this->parameters->has($name)) {
176-
throw new InvalidArgumentException(sprintf('Parameter "%s" is not defined.', $name));
166+
throw new InvalidArgumentException(sprintf("Parameter \"%s\" is not defined.", $name));
177167
}
178168

179169
return $this->parameters->get($name);
@@ -195,6 +185,16 @@ public function removeParameter($name)
195185
return $this->parameters->remove($name);
196186
}
197187

188+
/**
189+
* {@inheritDoc}
190+
*/
191+
public function setParameter($name, $value)
192+
{
193+
$this->parameters->set($name, $value);
194+
195+
return $this;
196+
}
197+
198198
/**
199199
* All array access keys
200200
*
@@ -216,8 +216,10 @@ public function keys()
216216
*/
217217
public function factory($callable)
218218
{
219-
if (!is_object($callable) || !method_exists($callable, '__invoke')) {
220-
throw new InvalidArgumentException('Service definition is not a Closure or invokable object.');
219+
if (!is_object($callable) || !method_exists($callable, "__invoke")) {
220+
throw new InvalidArgumentException(
221+
"Service definition is not a Closure or invokable object."
222+
);
221223
}
222224

223225
$this->factories->attach($callable);
@@ -240,20 +242,28 @@ public function extend($name, $callable, $strict = true)
240242
{
241243
if (!$this->services->has($name)) {
242244
if ($strict) {
243-
throw new InvalidArgumentException(sprintf('Service "%s" is not defined.', $name));
245+
throw new InvalidArgumentException(sprintf(
246+
"Service \"%s\" is not defined.",
247+
$name
248+
));
244249
} else {
245250
return false;
246251
}
247252
}
248253

249254
$factory = $this->services->get($name);
250255

251-
if (!is_object($factory) || !method_exists($factory, '__invoke')) {
252-
throw new InvalidArgumentException(sprintf('Service "%s" does not contain an object definition.', $name));
256+
if (!is_object($factory) || !method_exists($factory, "__invoke")) {
257+
throw new InvalidArgumentException(sprintf(
258+
"Service \"%s\" does not contain an object definition.",
259+
$name
260+
));
253261
}
254262

255-
if (!is_object($callable) || !method_exists($callable, '__invoke')) {
256-
throw new InvalidArgumentException('Extension service definition is not a Closure or invokable object.');
263+
if (!is_object($callable) || !method_exists($callable, "__invoke")) {
264+
throw new InvalidArgumentException(
265+
"Extension service definition is not a Closure or invokable object."
266+
);
257267
}
258268

259269
$extended = function($c) use($callable, $factory) {
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Container;
12+
namespace Strident\Container;
1313

1414
/**
1515
* Container Interface
@@ -18,16 +18,6 @@
1818
*/
1919
interface ContainerInterface
2020
{
21-
/**
22-
* Set item in container
23-
*
24-
* @param string $name
25-
* @param mixed $value
26-
*
27-
* @return mixed
28-
*/
29-
public function set($name, $value);
30-
3121
/**
3222
* Get item in container
3323
*
@@ -56,14 +46,14 @@ public function has($name);
5646
public function remove($name);
5747

5848
/**
59-
* Set a parameter
49+
* Set item in container
6050
*
6151
* @param string $name
6252
* @param mixed $value
6353
*
6454
* @return mixed
6555
*/
66-
public function setParameter($name, $value);
56+
public function set($name, $value);
6757

6858
/**
6959
* Get a parameter
@@ -91,4 +81,14 @@ public function hasParameter($name);
9181
* @return mixed
9282
*/
9383
public function removeParameter($name);
84+
85+
/**
86+
* Set a parameter
87+
*
88+
* @param string $name
89+
* @param mixed $value
90+
*
91+
* @return mixed
92+
*/
93+
public function setParameter($name, $value);
9494
}

src/Container/Exception/ItemNotFoundException.php renamed to src/Exception/ItemNotFoundException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Container\Exception;
12+
namespace Strident\Container\Exception;
1313

1414
/**
1515
* Item Not Found Exception
@@ -26,6 +26,6 @@ class ItemNotFoundException extends \InvalidArgumentException
2626
*/
2727
public function __construct($name, \Exception $previous = null)
2828
{
29-
parent::__construct(sprintf('Item "%s" could not be found.', $name), 0, $previous);
29+
parent::__construct(sprintf("Item \"%s\" could not be found.", $name), 0, $previous);
3030
}
3131
}

src/Container/Exception/LockedItemException.php renamed to src/Exception/LockedItemException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Container\Exception;
12+
namespace Strident\Container\Exception;
1313

1414
/**
1515
* Locked Item Exception
@@ -26,6 +26,6 @@ class LockedItemException extends \RuntimeException
2626
*/
2727
public function __construct($name, \Exception $previous = null)
2828
{
29-
parent::__construct(sprintf('Cannot override locked item "%s".', $name), 0, $previous);
29+
parent::__construct(sprintf("Cannot override locked item \"%s\".", $name), 0, $previous);
3030
}
3131
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Container;
12+
namespace Strident\Container;
1313

14-
use Container\Exception\ItemNotFoundException;
15-
use Container\Exception\LockedItemException;
14+
use Strident\Container\Exception\ItemNotFoundException;
15+
use Strident\Container\Exception\LockedItemException;
1616

1717
/**
1818
* LockBox stores items, and can lock them on request.

0 commit comments

Comments
 (0)