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