| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace Imagify\Dependencies\League\Container\Definition; |
| 4 |
|
| 5 |
use Imagify\Dependencies\League\Container\Argument\{ |
| 6 |
ArgumentResolverInterface, ArgumentResolverTrait, ClassNameInterface, RawArgumentInterface |
| 7 |
}; |
| 8 |
use Imagify\Dependencies\League\Container\ContainerAwareTrait; |
| 9 |
use ReflectionClass; |
| 10 |
use ReflectionException; |
| 11 |
|
| 12 |
class Definition implements ArgumentResolverInterface, DefinitionInterface |
| 13 |
{ |
| 14 |
use ArgumentResolverTrait; |
| 15 |
use ContainerAwareTrait; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $alias; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var mixed |
| 24 |
*/ |
| 25 |
protected $concrete; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var boolean |
| 29 |
*/ |
| 30 |
protected $shared = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
protected $tags = []; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
protected $arguments = []; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var array |
| 44 |
*/ |
| 45 |
protected $methods = []; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var mixed |
| 49 |
*/ |
| 50 |
protected $resolved; |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor. |
| 54 |
* |
| 55 |
* @param string $id |
| 56 |
* @param mixed $concrete |
| 57 |
*/ |
| 58 |
public function __construct(string $id, $concrete = null) |
| 59 |
{ |
| 60 |
$concrete = $concrete ?? $id; |
| 61 |
|
| 62 |
$this->alias = $id; |
| 63 |
$this->concrete = $concrete; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* {@inheritdoc} |
| 68 |
*/ |
| 69 |
public function addTag(string $tag) : DefinitionInterface |
| 70 |
{ |
| 71 |
$this->tags[$tag] = true; |
| 72 |
|
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* {@inheritdoc} |
| 78 |
*/ |
| 79 |
public function hasTag(string $tag) : bool |
| 80 |
{ |
| 81 |
return isset($this->tags[$tag]); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* {@inheritdoc} |
| 86 |
*/ |
| 87 |
public function setAlias(string $id) : DefinitionInterface |
| 88 |
{ |
| 89 |
$this->alias = $id; |
| 90 |
|
| 91 |
return $this; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* {@inheritdoc} |
| 96 |
*/ |
| 97 |
public function getAlias() : string |
| 98 |
{ |
| 99 |
return $this->alias; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* {@inheritdoc} |
| 104 |
*/ |
| 105 |
public function setShared(bool $shared = true) : DefinitionInterface |
| 106 |
{ |
| 107 |
$this->shared = $shared; |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* {@inheritdoc} |
| 114 |
*/ |
| 115 |
public function isShared() : bool |
| 116 |
{ |
| 117 |
return $this->shared; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* {@inheritdoc} |
| 122 |
*/ |
| 123 |
public function getConcrete() |
| 124 |
{ |
| 125 |
return $this->concrete; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* {@inheritdoc} |
| 130 |
*/ |
| 131 |
public function setConcrete($concrete) : DefinitionInterface |
| 132 |
{ |
| 133 |
$this->concrete = $concrete; |
| 134 |
$this->resolved = null; |
| 135 |
|
| 136 |
return $this; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* {@inheritdoc} |
| 141 |
*/ |
| 142 |
public function addArgument($arg) : DefinitionInterface |
| 143 |
{ |
| 144 |
$this->arguments[] = $arg; |
| 145 |
|
| 146 |
return $this; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* {@inheritdoc} |
| 151 |
*/ |
| 152 |
public function addArguments(array $args) : DefinitionInterface |
| 153 |
{ |
| 154 |
foreach ($args as $arg) { |
| 155 |
$this->addArgument($arg); |
| 156 |
} |
| 157 |
|
| 158 |
return $this; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* {@inheritdoc} |
| 163 |
*/ |
| 164 |
public function addMethodCall(string $method, array $args = []) : DefinitionInterface |
| 165 |
{ |
| 166 |
$this->methods[] = [ |
| 167 |
'method' => $method, |
| 168 |
'arguments' => $args |
| 169 |
]; |
| 170 |
|
| 171 |
return $this; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* {@inheritdoc} |
| 176 |
*/ |
| 177 |
public function addMethodCalls(array $methods = []) : DefinitionInterface |
| 178 |
{ |
| 179 |
foreach ($methods as $method => $args) { |
| 180 |
$this->addMethodCall($method, $args); |
| 181 |
} |
| 182 |
|
| 183 |
return $this; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* {@inheritdoc} |
| 188 |
*/ |
| 189 |
public function resolve(bool $new = false) |
| 190 |
{ |
| 191 |
$concrete = $this->concrete; |
| 192 |
|
| 193 |
if ($this->isShared() && $this->resolved !== null && $new === false) { |
| 194 |
return $this->resolved; |
| 195 |
} |
| 196 |
|
| 197 |
if (is_callable($concrete)) { |
| 198 |
$concrete = $this->resolveCallable($concrete); |
| 199 |
} |
| 200 |
|
| 201 |
if ($concrete instanceof RawArgumentInterface) { |
| 202 |
$this->resolved = $concrete->getValue(); |
| 203 |
|
| 204 |
return $concrete->getValue(); |
| 205 |
} |
| 206 |
|
| 207 |
if ($concrete instanceof ClassNameInterface) { |
| 208 |
$concrete = $concrete->getClassName(); |
| 209 |
} |
| 210 |
|
| 211 |
if (is_string($concrete) && class_exists($concrete)) { |
| 212 |
$concrete = $this->resolveClass($concrete); |
| 213 |
} |
| 214 |
|
| 215 |
if (is_object($concrete)) { |
| 216 |
$concrete = $this->invokeMethods($concrete); |
| 217 |
} |
| 218 |
|
| 219 |
if (is_string($concrete) && $this->getContainer()->has($concrete)) { |
| 220 |
$concrete = $this->getContainer()->get($concrete); |
| 221 |
} |
| 222 |
|
| 223 |
$this->resolved = $concrete; |
| 224 |
|
| 225 |
return $concrete; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Resolve a callable. |
| 230 |
* |
| 231 |
* @param callable $concrete |
| 232 |
* |
| 233 |
* @return mixed |
| 234 |
*/ |
| 235 |
protected function resolveCallable(callable $concrete) |
| 236 |
{ |
| 237 |
$resolved = $this->resolveArguments($this->arguments); |
| 238 |
|
| 239 |
return call_user_func_array($concrete, $resolved); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Resolve a class. |
| 244 |
* |
| 245 |
* @param string $concrete |
| 246 |
* |
| 247 |
* @return object |
| 248 |
* |
| 249 |
* @throws ReflectionException |
| 250 |
*/ |
| 251 |
protected function resolveClass(string $concrete) |
| 252 |
{ |
| 253 |
$resolved = $this->resolveArguments($this->arguments); |
| 254 |
$reflection = new ReflectionClass($concrete); |
| 255 |
|
| 256 |
return $reflection->newInstanceArgs($resolved); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Invoke methods on resolved instance. |
| 261 |
* |
| 262 |
* @param object $instance |
| 263 |
* |
| 264 |
* @return object |
| 265 |
*/ |
| 266 |
protected function invokeMethods($instance) |
| 267 |
{ |
| 268 |
foreach ($this->methods as $method) { |
| 269 |
$args = $this->resolveArguments($method['arguments']); |
| 270 |
|
| 271 |
/** @var callable $callable */ |
| 272 |
$callable = [$instance, $method['method']]; |
| 273 |
call_user_func_array($callable, $args); |
| 274 |
} |
| 275 |
|
| 276 |
return $instance; |
| 277 |
} |
| 278 |
} |
| 279 |
|