-
Notifications
You must be signed in to change notification settings - Fork 0
/
InjectionException.php
101 lines (88 loc) · 2.98 KB
/
InjectionException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Qubus\Injector
*
* @link https://github.com/QubusPHP/injector
* @copyright 2020 Joshua Parker <joshua@joshuaparker.dev>
* @copyright 2013-2014 Daniel Lowrey, Levi Morrison, Dan Ackroyd
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Injector;
use ReflectionException;
use RuntimeException;
use function array_flip;
use function array_key_exists;
use function get_class;
use function is_array;
use function is_object;
use function is_string;
use function ksort;
use function sprintf;
use function substr;
class InjectionException extends RuntimeException implements InjectorException
{
/** @var array $dependencyChain */
public array $dependencyChain;
public function __construct(
array $inProgressMakes,
$message = "",
$code = 0,
?ReflectionException $previous = null
) {
$this->dependencyChain = array_flip($inProgressMakes);
ksort($this->dependencyChain);
parent::__construct($message, $code, $previous);
}
/**
* Add a human-readable version of the invalid callable to the standard 'invalid invokable' message.
*
* @param string|array|object $callableOrMethodStr
*/
public static function fromInvalidCallable(
array $inProgressMakes,
$callableOrMethodStr,
?ReflectionException $previous = null
) {
$callableString = null;
if (is_string($callableOrMethodStr)) {
$callableString .= $callableOrMethodStr;
} elseif (
is_array($callableOrMethodStr) &&
array_key_exists(0, $callableOrMethodStr) &&
array_key_exists(0, $callableOrMethodStr)
) {
if (is_string($callableOrMethodStr[0]) && is_string($callableOrMethodStr[1])) {
$callableString .= $callableOrMethodStr[0] . '::' . $callableOrMethodStr[1];
} elseif (is_object($callableOrMethodStr[0]) && is_string($callableOrMethodStr[1])) {
$callableString .= sprintf(
"[object(%s), '%s']",
get_class($callableOrMethodStr[0]),
$callableOrMethodStr[1]
);
}
}
if ($callableString) {
// Prevent accidental usage of long strings from filling logs.
$callableString = substr($callableString, 0, 250);
$message = sprintf(
"%s. Invalid callable was '%s'",
InjectorException::M_INVOKABLE,
$callableString
);
} else {
$message = InjectorException::M_INVOKABLE;
}
return new static($inProgressMakes, $message, InjectorException::E_INVOKABLE, $previous);
}
/**
* Returns the hierarchy of dependencies that were being created when
* the exception occurred.
*
* @return array
*/
public function getDependencyChain(): array
{
return $this->dependencyChain;
}
}