Skip to content

Commit ae4cc95

Browse files
committed
Updated readme
1 parent 2c72e4b commit ae4cc95

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

README.md

+18-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Just use Composer:
1515
```
1616
composer require semperton/container
1717
```
18-
Container requires PHP 7.1+
18+
Container requires PHP 7.2+
1919

2020
## Interface
2121

@@ -25,6 +25,7 @@ The container ships with four public methods:
2525
with(string $id, $value): Container // add a container entry
2626
get(string $id) // get entry (PSR-11)
2727
has(string $id): bool // has entry (PSR-11)
28+
create(string $id, array $args = []); // create a class with optional constructor substitution args
2829
entries(): array // list all container entries
2930
```
3031

@@ -63,46 +64,44 @@ $hello instanceof Hello::class // true
6364
$hello->print(); // 'Hello World'
6465
```
6566

66-
Note that the container only creates instances once. It does not work as a factory. You should consider the Factory Pattern instead:
67+
Note that the container only creates instances once. It does not work as a factory.
68+
You should consider the [Factory Pattern](https://designpatternsphp.readthedocs.io/en/latest/Creational/SimpleFactory/README.html) or use the ```create()``` method instead:
6769

6870
```php
6971
use Semperton\Container\Container;
7072

7173
class Mail
7274
{
75+
public function __construct(Config $c, string $to)
76+
{
77+
}
7378
}
7479

7580
class MailFactory
7681
{
77-
public function createMail()
82+
public function createMail(string $to)
7883
{
79-
return new Mail();
84+
return new Mail(new Config(), $to);
8085
}
8186
}
8287

83-
$container = new Container();
84-
$factory1 = $container->get(MailFactory::class);
85-
$factory2 = $container->get(MailFactory::class);
86-
87-
$factory1 === $factory2 // true
88-
89-
$mail1 = $factory1->createMail();
90-
$mail2 = $factory1->createMail();
88+
$mail1 = $container->get(MailFactory::class)->createMail('[email protected]');
89+
$mail2 = $container->create(Mail::class, [1 =>'[email protected]']);
9190

92-
$mail1 === $mail2 // false
9391
```
92+
The ```create()``` method will automatically resolve the ```Config``` dependency for ```Mail```.
9493

9594
## Configuration
9695

97-
You can configure the container with definitions. Closures are always treated as factories and can (!should) be used to bootstrap class instances:
96+
You can configure the container with definitions. ```callables``` (except invokable objects) are always treated as factories and can (!should) be used to bootstrap class instances:
9897

9998
```php
10099
use Semperton\Container\Container;
101100

102101
$container = new Container([
103102

104103
'mail' => '[email protected]',
105-
'closure' => function () {
104+
'closure' => function () { // closures must be wrapped in another closure
106105
return function () {
107106
return 42;
108107
};
@@ -112,7 +111,10 @@ $container = new Container([
112111

113112
$sender = $c->get('mail');
114113
return new MailFactory($sender);
115-
}
114+
}, // or
115+
// factory params are automatically resolved from the container
116+
MailFactory::class => fn (string $mail) => new MailFactory($mail),
117+
Service::class => fn(Dependency $dep) => new Service($dep)
116118
]);
117119

118120
$container->get('mail'); // '[email protected]'

0 commit comments

Comments
 (0)