|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Koded package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mihail Binev <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Please view the LICENSE distributed with this source code |
|
9
|
|
|
* for the full copyright and license information. |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Koded; |
|
14
|
|
|
|
|
15
|
|
|
use Psr\Container\{ContainerExceptionInterface, NotFoundExceptionInterface}; |
|
16
|
|
|
|
|
17
|
|
|
class DIException extends \LogicException implements ContainerExceptionInterface |
|
18
|
|
|
{ |
|
19
|
|
|
public const |
|
20
|
|
|
E_CIRCULAR_DEPENDENCY = 7001, |
|
21
|
|
|
E_NON_PUBLIC_METHOD = 7002, |
|
22
|
|
|
E_CANNOT_INSTANTIATE = 7003, |
|
23
|
|
|
E_INVALID_PARAMETER_NAME = 7004, |
|
24
|
|
|
E_INSTANCE_NOT_FOUND = 7005, |
|
25
|
|
|
E_MISSING_ARGUMENT = 7006, |
|
26
|
|
|
E_REFLECTION_ERROR = 7007; |
|
27
|
|
|
|
|
28
|
|
|
protected array $messages = [ |
|
29
|
|
|
DIException::E_CIRCULAR_DEPENDENCY => 'Circular dependency detected while creating an instance for :class', |
|
30
|
|
|
DIException::E_NON_PUBLIC_METHOD => 'Failed to create an instance, because the method ":class:::method" is not public', |
|
31
|
|
|
DIException::E_CANNOT_INSTANTIATE => 'Cannot instantiate :type :name', |
|
32
|
|
|
DIException::E_INVALID_PARAMETER_NAME => 'Provide a valid name for the global parameter: ":name"', |
|
33
|
|
|
DIException::E_INSTANCE_NOT_FOUND => 'The requested instance :id is not found in the container', |
|
34
|
|
|
DIException::E_MISSING_ARGUMENT => 'Required parameter ":name" is missing at position :position in :function()', |
|
35
|
|
|
DIException::E_REFLECTION_ERROR => ':message', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
15 |
|
public function __construct(int $code, array $arguments = [], \Throwable $previous = null) |
|
39
|
|
|
{ |
|
40
|
15 |
|
parent::__construct( |
|
41
|
15 |
|
\strtr($this->messages[$code] ?? ':message', $arguments + [':message' => $this->message]), |
|
42
|
15 |
|
$code, |
|
43
|
15 |
|
$previous |
|
44
|
15 |
|
); |
|
45
|
|
|
} |
|
46
|
1 |
|
|
|
47
|
|
|
public static function forCircularDependency(string $class): static |
|
48
|
1 |
|
{ |
|
49
|
|
|
return new static(static::E_CIRCULAR_DEPENDENCY, [':class' => $class]); |
|
50
|
|
|
} |
|
51
|
1 |
|
|
|
52
|
|
|
public static function forNonPublicMethod(string $class, string $method): static |
|
53
|
1 |
|
{ |
|
54
|
|
|
return new static(static::E_NON_PUBLIC_METHOD, [':class' => $class, ':method' => $method]); |
|
55
|
|
|
} |
|
56
|
4 |
|
|
|
57
|
|
|
public static function cannotInstantiate(\ReflectionClass $dependency): static |
|
58
|
4 |
|
{ |
|
59
|
|
|
$type = match (true) { |
|
60
|
|
|
$dependency->isInterface() => 'interface', |
|
61
|
7 |
|
$dependency->isAbstract() => 'abstract class', |
|
62
|
|
|
$dependency->isTrait() => 'trait', |
|
63
|
7 |
|
default => 'class', |
|
64
|
|
|
}; |
|
65
|
|
|
return new static(static::E_CANNOT_INSTANTIATE, [':name' => $dependency->name, ':type' => $type]); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public static function forInvalidParameterName(string $name): static |
|
69
|
1 |
|
{ |
|
70
|
1 |
|
return new static(static::E_INVALID_PARAMETER_NAME, [':name' => $name]); |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function forMissingArgument( |
|
74
|
|
|
string $name, |
|
75
|
|
|
\ReflectionParameter $parameter, |
|
76
|
|
|
\Throwable $e = null): static |
|
77
|
|
|
{ |
|
78
|
|
|
return new static(static::E_MISSING_ARGUMENT, [ |
|
79
|
1 |
|
':name' => $name, |
|
80
|
|
|
':position' => $parameter->getPosition(), |
|
81
|
1 |
|
':function' => \join('::', \array_filter([ |
|
82
|
|
|
$parameter->getDeclaringClass()?->name, |
|
83
|
|
|
$parameter->getDeclaringFunction()?->name |
|
84
|
|
|
])) |
|
85
|
|
|
], $e); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public static function forReflectionError(\ReflectionException $e): static |
|
89
|
|
|
{ |
|
90
|
|
|
return new static(static::E_REFLECTION_ERROR, [':message' => $e->getMessage()], $e); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
class DIInstanceNotFound extends DIException implements NotFoundExceptionInterface |
|
96
|
|
|
{ |
|
97
|
|
|
public static function for(string $id): NotFoundExceptionInterface |
|
98
|
|
|
{ |
|
99
|
|
|
return new static(static::E_INSTANCE_NOT_FOUND, [':id' => $id]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|