You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* @var array $rules Rules which have been set using addRule()
11
14
*/
@@ -26,17 +29,30 @@ class Dice {
26
29
* @param string $name The name of the class to add the rule for
27
30
* @param array $rule The container can be fully configured using rules provided by associative arrays. See {@link https://r.je/dice.html#example3} for a description of the rules.
// Is there a shared instance set? Return it. Better here than a closure for this, calling a closure is slower.
63
79
if (!empty($this->instances[$name])) return$this->instances[$name];
64
80
@@ -75,7 +91,7 @@ public function create($name, array $args = [], array $share = []) {
75
91
* @param array $rule The container can be fully configured using rules provided by associative arrays. See {@link https://r.je/dice.html#example3} for a description of the rules.
if (isset($call[2]) && is_callable($call[2])) call_user_func($call[2], $return);
121
137
}
@@ -124,24 +140,29 @@ private function getClosure($name, array $rule) {
124
140
}
125
141
126
142
/**
127
-
* Looks for 'instance' array keys in $param and when found returns an object based on the value see {@link https:// r.je/dice.html#example3-1}
143
+
* Looks for Dice::INSTANCE, Dice::GLOBAL or Dice::CONSTANT array keys in $param and when found returns an object based on the value see {@link https:// r.je/dice.html#example3-1}
128
144
* @param mixed $param Either a string or an array,
129
-
* @param array $share Whether or not this class instance be shared, so that the same instance is passed around each time
145
+
* @param array $share Array of instances from 'shareInstances', required for calls to `create`
Copy file name to clipboardExpand all lines: README.md
+93-8Lines changed: 93 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ Simple example:
27
27
<?php
28
28
class A {
29
29
public $b;
30
-
30
+
31
31
public function __construct(B $b) {
32
32
$this->b = $b;
33
33
}
@@ -63,7 +63,7 @@ Dice is compatible with PHP5.4 and up, there are archived versions of Dice which
63
63
Performance
64
64
-----------
65
65
66
-
Dice uses reflection which is often wrongly labelled "slow". Reflection is considerably faster than loading and parsing a configuration file. There are a set of benchmarks [here](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test1-5_results.html) and [here](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test6_results.html) (To download the benchmark tool yourself see [this repository](https://github.com/TomBZombie/php-dependency-injection-benchmarks)) and Dice is faster than the others in most cases.
66
+
Dice uses reflection which is often wrongly labelled "slow". Reflection is considerably faster than loading and parsing a configuration file. There are a set of benchmarks [here](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test1-5_results.html) and [here](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test6_results.html) (To download the benchmark tool yourself see [this repository](https://github.com/TomBZombie/php-dependency-injection-benchmarks)) and Dice is faster than the others in most cases.
67
67
68
68
In the real world test ([test 6](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test6_results.html)) Dice is neck-and-neck with Pimple (which requires writing an awful lot of configuration code) and although Symfony\DependencyInjection is faster at creating objects, it has a larger overhead and you need to create over 500 objects on each page load until it becomes faster than Dice. The same is true of Phalcon, the overhead of loading the Phalcon extension means that unless you're creating well over a thousand objects per HTTP request, the overhead is not worthwhile.
69
69
@@ -77,6 +77,91 @@ Originally developed by Tom Butler (@TomBZombie), with many thanks to daniel-mei
77
77
Updates
78
78
------------
79
79
80
+
06/03/2018
81
+
82
+
### 3.0 Release - Backwards incompatible
83
+
84
+
**New Features**
85
+
86
+
#### 1. The JSON loader has been removed in favour of a new `addRules` method.
87
+
88
+
```php
89
+
$dice->addRules([
90
+
'\PDO' => [
91
+
'shared' => true
92
+
],
93
+
'Framework\Router' => [
94
+
'constructParams' => ['Foo', 'Bar']
95
+
]
96
+
]);
97
+
```
98
+
99
+
The puropse of this addition is to make the JSON loader redundant. Loading of rules from a JSON file can easily be achieved with the code:
#### 2. Better JSON file support: constants and superglobals
106
+
107
+
In order to improve support for rules being defined in external JSON files, constants and superglobals can now be passed into objects created by Dice.
108
+
109
+
For example, passing the `$_SERVER` superglobal into a router instance and calling PDO's `setAttribute` with `PDO::ATTR_ERRMODE` and `PDO::ERRMODE_EXCEPTION` can be achieved like this in a JSON file:
As noted in issue #125 this made it impossible to pass an array to a constructor if the array had a key `'instance'`. Instead, the new `\Dice\Dice::INSTANCE` constant should be used:
`$dice->create('$MyNamedInstance')` will now create a class following the rules applied to both `MyClass` and `$MyNamedInstance` so the instance will be shared.
187
+
`$dice->create('$MyNamedInstance')` will now create a class following the rules applied to both `MyClass` and `$MyNamedInstance` so the instance will be shared.
103
188
104
189
Previously only the rules applied to the named instance would be used.
* Based on [Issue #15](https://github.com/TomBZombie/Dice/issues/15), Dice will now only call closures if they are wrapped in \Dice\Instance. **PLEASE NOTE: THIS IS BACKWARDS INCOMPATIBLE **.
215
+
* Based on [Issue #15](https://github.com/TomBZombie/Dice/issues/15), Dice will now only call closures if they are wrapped in \Dice\Instance. **PLEASE NOTE: THIS IS BACKWARDS INCOMPATIBLE **.
131
216
132
217
Previously Dice ran closures that were passed as substitutions, constructParams and when calling methods:
//'abc' will be providedas the first constructor parameter
147
232
return 'abc';
148
233
};
149
-
```
234
+
```
150
235
151
-
This behaviour has changed as it makes it impossible to provide a closure as a construct parameter or when calling a method because the closure was always called and executed.
236
+
This behaviour has changed as it makes it impossible to provide a closure as a construct parameter or when calling a method because the closure was always called and executed.
152
237
153
238
To overcome this, Dice will now only call a closures if they're wrapped in \Dice\Instance:
Copy file name to clipboardExpand all lines: composer.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
{
2
2
"name": "level-2/dice",
3
-
"description": "A minimalist Dependency injection container (DIC) for PHP. Please note: This branch is only compatible with PHP5.6. 5.5, 5.4 and 5.3 compatible version is available as a separate branch on github. ",
3
+
"description": "A minimalist Dependency injection container (DIC) for PHP. Please note: 3.0+ is only compatible with PHP 7.0. The 2.0 branch is compatbile with PHP 5.6.",
0 commit comments