diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2022-01-11 12:35:47 +0100 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2022-01-11 12:35:47 +0100 |
commit | 19985dbb8c0aa66dc4bf7905abc1148de909097d (patch) | |
tree | 2cd5a5d20d7e80fc2a51adf60d838d8a2c40999e /admin/survey/minify/vendor/mrclay/props-dic/scripts/pizza.php | |
download | 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.gz 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.bz2 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.lz 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.xz 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.tar.zst 1ka-19985dbb8c0aa66dc4bf7905abc1148de909097d.zip |
Diffstat (limited to 'admin/survey/minify/vendor/mrclay/props-dic/scripts/pizza.php')
-rw-r--r-- | admin/survey/minify/vendor/mrclay/props-dic/scripts/pizza.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/admin/survey/minify/vendor/mrclay/props-dic/scripts/pizza.php b/admin/survey/minify/vendor/mrclay/props-dic/scripts/pizza.php new file mode 100644 index 0000000..fcb30b0 --- /dev/null +++ b/admin/survey/minify/vendor/mrclay/props-dic/scripts/pizza.php @@ -0,0 +1,55 @@ +<?php
+
+require __DIR__ . '/../vendor/autoload.php';
+
+class Dough {}
+class Cheese {}
+class Slice {}
+class Pizza {
+ function __construct($style, Cheese $cheese) {}
+ function setDough(Dough $dough) {}
+ function getSlice() { return new Slice(); }
+}
+class CheeseFactory {
+ static function getCheese() { return new Cheese(); }
+}
+
+/**
+ * @property-read string $style
+ * @property-read Dough $dough
+ * @property-read Cheese $cheese
+ * @property-read Pizza $pizza
+ * @method Slice new_slice()
+ */
+class MyDI extends \Props\Container {
+ public function __construct() {
+ $this->style = 'deluxe';
+
+ $this->dough = function (MyContainer $c) {
+ return new Dough();
+ };
+
+ $this->setFactory('cheese', 'CheeseFactory::getCheese');
+
+ $this->pizza = function (MyContainer $c) {
+ $pizza = new Pizza($c->style, $c->cheese);
+ $pizza->setDough($c->dough);
+ return $pizza;
+ };
+
+ // note 3rd argument $shared is false
+ $this->slice = function (MyContainer $c) {
+ return $c->pizza->getSlice();
+ };
+ }
+}
+
+$c = new MyContainer;
+
+// You can request dependencies in any order. They're resolved as needed.
+
+$slice1 = $c->new_slice(); // This first resolves and caches the cheese, dough, and pizza.
+$slice2 = $c->new_slice(); // This just gets a new slice from the existing pizza.
+
+assert($slice1 !== $slice2);
+assert($c->pizza === $c->pizza);
|