瀏覽代碼

Update Core\Container for calculated variables

Visman 3 年之前
父節點
當前提交
1f64397404
共有 1 個文件被更改,包括 46 次插入22 次删除
  1. 46 22
      app/Core/Container.php

+ 46 - 22
app/Core/Container.php

@@ -19,21 +19,25 @@ use InvalidArgumentException;
 class Container
 class Container
 {
 {
     protected $instances = [];
     protected $instances = [];
-    protected $shared = [];
-    protected $multiple = [];
+    protected $shared    = [];
+    protected $multiple  = [];
 
 
     public function __construct(array $config = null)
     public function __construct(array $config = null)
     {
     {
         if (empty($config)) {
         if (empty($config)) {
             return;
             return;
         }
         }
+
         if (isset($config['shared'])) {
         if (isset($config['shared'])) {
             $this->shared = $config['shared'];
             $this->shared = $config['shared'];
         }
         }
+
         if (isset($config['multiple'])) {
         if (isset($config['multiple'])) {
             $this->multiple = $config['multiple'];
             $this->multiple = $config['multiple'];
         }
         }
+
         unset($config['shared'], $config['multiple']);
         unset($config['shared'], $config['multiple']);
+
         $this->instances = $config;
         $this->instances = $config;
     }
     }
 
 
@@ -45,10 +49,13 @@ class Container
         if (isset($config['shared'])) {
         if (isset($config['shared'])) {
             $this->shared = \array_replace($this->shared, $config['shared']);
             $this->shared = \array_replace($this->shared, $config['shared']);
         }
         }
+
         if (isset($config['multiple'])) {
         if (isset($config['multiple'])) {
             $this->multiple = \array_replace($this->multiple, $config['multiple']);
             $this->multiple = \array_replace($this->multiple, $config['multiple']);
         }
         }
+
         unset($config['shared'], $config['multiple']);
         unset($config['shared'], $config['multiple']);
+
         if (! empty($config)) {
         if (! empty($config)) {
             $this->instances = \array_replace($this->instances, $config);
             $this->instances = \array_replace($this->instances, $config);
         }
         }
@@ -57,13 +64,14 @@ class Container
     /**
     /**
      * Gets a service or parameter.
      * Gets a service or parameter.
      */
      */
-    public function __get(string $id) /* : mixed */
+    public function __get(string $key) /* : mixed */
     {
     {
-        if (\array_key_exists($id, $this->instances)) {
-            return $this->instances[$id];
-        } elseif (false !== \strpos($id, '.')) {
-            $tree    = \explode('.', $id);
+        if (\array_key_exists($key, $this->instances)) {
+            return $this->instances[$key];
+        } elseif (false !== \strpos($key, '.')) {
+            $tree    = \explode('.', $key);
             $service = $this->__get(\array_shift($tree));
             $service = $this->__get(\array_shift($tree));
+
             if (\is_array($service)) {
             if (\is_array($service)) {
                 return $this->fromArray($service, $tree);
                 return $this->fromArray($service, $tree);
             } elseif (\is_object($service)) {
             } elseif (\is_object($service)) {
@@ -72,28 +80,39 @@ class Container
                 return null;
                 return null;
             }
             }
         }
         }
-        if (isset($this->shared[$id])) {
+
+        if (isset($this->shared[$key])) {
             $toShare = true;
             $toShare = true;
-            $config  = (array) $this->shared[$id];
-        } elseif (isset($this->multiple[$id])) {
+            $config  = $this->shared[$key];
+        } elseif (isset($this->multiple[$key])) {
             $toShare = false;
             $toShare = false;
-            $config  = (array) $this->multiple[$id];
+            $config  = $this->multiple[$key];
+        } elseif (isset($this->shared["%{$key}%"])) {
+            return $this->instances[$key] = $this->resolve($this->shared["%{$key}%"]);
         } else {
         } else {
-            throw new InvalidArgumentException("Wrong property name: {$id}");
+            throw new InvalidArgumentException("Wrong property name: {$key}");
         }
         }
-        // N.B. "class" is just the first element, regardless of its key
-        $class = \array_shift($config);
+
         $args  = [];
         $args  = [];
-        // If you want to susbtitute some values in arguments, use non-numeric keys for them
-        foreach ($config as $k => $v) {
-            $args[] = \is_numeric($k) ? $v : $this->resolve($v);
+
+        if (\is_array($config)) {
+            // N.B. "class" is just the first element, regardless of its key
+            $class = \array_shift($config);
+            // If you want to susbtitute some values in arguments, use non-numeric keys for them
+            foreach ($config as $k => $v) {
+                $args[] = \is_numeric($k) ? $v : $this->resolve($v);
+            }
+        } else {
+            $class = $config;
         }
         }
+
         // Special case: reference to factory method
         // Special case: reference to factory method
         if (
         if (
             '@' === $class[0]
             '@' === $class[0]
             && false !== \strpos($class, ':')
             && false !== \strpos($class, ':')
         ) {
         ) {
             list($name, $method) = \explode(':', \substr($class, 1), 2);
             list($name, $method) = \explode(':', \substr($class, 1), 2);
+
             $factory = $this->__get($name);
             $factory = $this->__get($name);
             $service = $factory->$method(...$args);
             $service = $factory->$method(...$args);
         } else {
         } else {
@@ -101,8 +120,9 @@ class Container
             $args[]  = $this;
             $args[]  = $this;
             $service = new $class(...$args);
             $service = new $class(...$args);
         }
         }
+
         if ($toShare) {
         if ($toShare) {
-            $this->instances[$id] = $service;
+            $this->instances[$key] = $service;
         }
         }
 
 
         return $service;
         return $service;
@@ -112,12 +132,12 @@ class Container
      * Sets a service or parameter.
      * Sets a service or parameter.
      * Provides a fluent interface.
      * Provides a fluent interface.
      */
      */
-    public function __set(string $id, /* mixed */ $service): void
+    public function __set(string $key, /* mixed */ $service): void
     {
     {
-        if (false !== \strpos($id, '.')) {
-            //????
+        if (false !== \strpos($key, '.')) {
+            throw new InvalidArgumentException("Wrong property name: {$key}");
         } else {
         } else {
-            $this->instances[$id] = $service;
+            $this->instances[$key] = $service;
         }
         }
     }
     }
 
 
@@ -127,6 +147,7 @@ class Container
     public function fromArray(array $array, array $tree) /* : mixed */
     public function fromArray(array $array, array $tree) /* : mixed */
     {
     {
         $ptr = &$array;
         $ptr = &$array;
+
         foreach ($tree as $s) {
         foreach ($tree as $s) {
             if (isset($ptr[$s])) {
             if (isset($ptr[$s])) {
                 $ptr = &$ptr[$s];
                 $ptr = &$ptr[$s];
@@ -147,6 +168,7 @@ class Container
         $segments = \explode('.', $name);
         $segments = \explode('.', $name);
         $n        = \count($segments);
         $n        = \count($segments);
         $ptr      = &$this->config;
         $ptr      = &$this->config;
+
         foreach ($segments as $s) {
         foreach ($segments as $s) {
             if (--$n) {
             if (--$n) {
                 if (! \array_key_exists($s, $ptr)) {
                 if (! \array_key_exists($s, $ptr)) {
@@ -154,6 +176,7 @@ class Container
                 } elseif (! \is_array($ptr[$s])) {
                 } elseif (! \is_array($ptr[$s])) {
                     throw new InvalidArgumentException("Scalar '{$s}' in the path '{$name}'");
                     throw new InvalidArgumentException("Scalar '{$s}' in the path '{$name}'");
                 }
                 }
+
                 $ptr = &$ptr[$s];
                 $ptr = &$ptr[$s];
             } else {
             } else {
                 $ptr[$s] = $value;
                 $ptr[$s] = $value;
@@ -190,6 +213,7 @@ class Container
             foreach ($value as &$v) {
             foreach ($value as &$v) {
                 $v = $this->resolve($v);
                 $v = $this->resolve($v);
             }
             }
+
             unset($v);
             unset($v);
         }
         }