| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CapMousse\ReactRestify\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ReflectionClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ReflectionMethod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ReflectionFunction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ReflectionFunctionAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ReflectionParameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private static $instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * Resolved types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $definitions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Globally available container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public static function getInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if (is_null(self::$instance)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             self::$instance = new static; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return self::$instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Add item to the container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param mixed|null $concrete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function add($alias, $concrete = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (isset($this->definitions[$alias])) return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (is_null($concrete)) $concrete = $alias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->definitions[$alias] = $this->build($concrete); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Check if item is available in container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param  string  $alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return boolean | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     public function has($alias) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         if (array_key_exists($alias, $this->definitions)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Get an item of the container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param  string $alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function get($alias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $this->definitions[$alias]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Call method with given parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param  string|callable  $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param  array            $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function call ($action, array $args = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $method = $action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $class = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if (is_string($action)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             list($class, $method) = explode('@', $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $reflection = $this->getActionReflection($method, $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $args       = $this->getParametersDictionary($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $parameters = $this->getParameters($reflection, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if (is_callable($method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return $method(...$parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $class = $this->build($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $class->{$method}(...$parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Get reflection from an action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param string      $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param string|null $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function getActionReflection($method, $class = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if(!is_null($class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return new ReflectionMethod($class, $method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return new ReflectionFunction($method); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * Find object and set classname alias on argument list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param  array  $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function getParametersDictionary(array $args = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $dictionary = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         foreach ($args as $parameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             if (!is_object($parameter)) continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $dictionary[get_class($parameter)] = $parameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return array_merge($args, $dictionary); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * Get reflection parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param  ReflectionFunctionAbstract $reflection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param  array                      $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function getParameters(ReflectionFunctionAbstract $reflection, array $args = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $dependencies = $reflection->getParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         foreach ($dependencies as $parameter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $parameters[] = $this->getParameter($parameter, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         return $parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * Get paremeter value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param  ReflectionParameter $parameter  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @param  array               $args       | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     public function getParameter(ReflectionParameter $parameter, array $args = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $class = $parameter->getClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         if ($class && $this->has($class->name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             return $this->get($class->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         if ($class && array_key_exists($class->name, $args)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             return $args[$class->name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         if (array_key_exists($parameter->name, $args)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             return $args[$parameter->name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * Create a new container for asked class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @param  string $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @return Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     public function build ($class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         $reflection = new ReflectionClass($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         $parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         if (!is_null($reflection->getConstructor())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             $parameters = $this->getParameters($reflection->getConstructor()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         return new $class(...$parameters); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  | } |