Skip to content

Commit 9d951f6

Browse files
committed
updated readme for 4.0
1 parent a280830 commit 9d951f6

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

README.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,78 @@ Originally developed by Tom Butler (@TomBZombie), with many thanks to daniel-mei
7777
Updates
7878
------------
7979

80-
06/03/2018
80+
### 15/11/2018 4.0 Release - Backwards incompatible
8181

82-
### 3.0 Release - Backwards incompatible
82+
Dice is now immutable and has better support for other immutable objects.
83+
84+
**New Features**
85+
86+
#### 1. Dice is Immutable
87+
88+
This avoids [issues surrounding mutability](https://www.yegor256.com/2014/06/09/objects-should-be-immutable.html) where a Dice instance is passed around the application and reconfigured. The only difference is that `addRules` and `addRule` return a new Dice instance with the updated rules rather than changing the state of the existing instance.
89+
90+
```php
91+
92+
// Pre-4.0 code:
93+
$dice->addRule('PDO', ['shared' => true]);
94+
95+
$db = $dice->create('PDO');
96+
97+
// 4.0 code:
98+
$dice = $dice->addRule('PDO', ['shared' => true]);
99+
100+
$db = $dice->create('PDO');
101+
```
102+
103+
From a practical perspective in most cases just put `$dice = ` in front of any `$dice->addRule()` call and it will work as before.
104+
105+
#### 2. Support for Object Method Chaining
106+
107+
One feature some immutable objects have is they offer object chaining.
108+
109+
Consider the following Object:
110+
111+
```php
112+
113+
$httpRequest = new HTTPRequest();
114+
$httpRequest = $httpRequest->url('http://example.org')->method('POST')->postdata('foo=bar');
115+
```
116+
117+
It was not possible for Dice to consturuct the configured object in previous versions. As of 4.0 Dice supports chaining method call using the `call` rule and the `Dice::CHAIN_CALL` constant:
118+
119+
```php
120+
$dice = $dice->addRule('HTTPRequest',
121+
['call' => [
122+
['url', ['http://example.org'], Dice::CHAIN_CALL],
123+
['method', ['POST'], Dice::CHAIN_CALL ],
124+
['postdata', ['foo=bar'], Dice::CHAIN_CALL]
125+
]
126+
]
127+
);
128+
```
129+
130+
Dice will replace the HTTPRequest object with the result of the chained call. This is also useful for factories:
131+
132+
133+
```php
134+
$dice = $dice->addRule('MyDatabase',
135+
[
136+
'instanceOf' => 'DatabaseFactory',
137+
'call' => [
138+
['get', ['Database'], Dice::CHAIN_CALL]
139+
]
140+
]
141+
);
142+
143+
$database = $dice->create('MyDatabase');
144+
//Equivalent of:
145+
146+
$factory = new DatabaseFactory();
147+
$database = $factory->get('Database');
148+
```
149+
150+
151+
### 06/03/2018 3.0 Release - Backwards incompatible
83152

84153
**New Features**
85154

@@ -135,7 +204,7 @@ _rules.json_
135204
]
136205
}
137206
}
138-
207+
```
139208

140209
```php
141210
$dice->addRules(json_decode(file_get_contents('rules.json')));

0 commit comments

Comments
 (0)