-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathFailedToMakeInstance.php
More file actions
149 lines (132 loc) · 4.27 KB
/
FailedToMakeInstance.php
File metadata and controls
149 lines (132 loc) · 4.27 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Exception FailedToMakeInstance.
*
* @package AmpProject\AmpWP
*/
namespace AmpProject\AmpWP\Exception;
use AmpProject\AmpWP\Infrastructure\Injector\InjectionChain;
use RuntimeException;
/**
* Exception thrown when the injector couldn't instantiate a given class or
* interface.
*
* @since 2.0
* @internal
*/
final class FailedToMakeInstance
extends RuntimeException
implements AmpWpException {
// These constants are public so you can use them to find out what exactly
// happened when you catch a "FailedToMakeInstance" exception.
const CIRCULAR_REFERENCE = 100;
const UNRESOLVED_INTERFACE = 200;
const UNREFLECTABLE_CLASS = 300;
const UNRESOLVED_ARGUMENT = 400;
const UNINSTANTIATED_SHARED_INSTANCE = 500;
const INVALID_DELEGATE = 600;
/**
* Create a new instance of the exception for an interface or class that
* created a circular reference.
*
* @param string $interface_or_class Interface or class name that
* generated the circular
* reference.
* @param InjectionChain $injection_chain Injection chain that led to the
* circular reference.
* @return self
*/
public static function for_circular_reference(
$interface_or_class,
InjectionChain $injection_chain
) {
$message = \sprintf(
'Circular reference detected while trying to resolve the interface or class "%s".',
$interface_or_class
);
$message .= "\nInjection chain:\n";
foreach ( $injection_chain->get_chain() as $link ) {
$message .= "{$link}\n";
}
return new self( $message, self::CIRCULAR_REFERENCE );
}
/**
* Create a new instance of the exception for an interface that could not
* be resolved to an instantiable class.
*
* @param string $interface Interface that was left unresolved.
*
* @return self
*/
public static function for_unresolved_interface( $interface ) {
$message = \sprintf(
'Could not resolve the interface "%s" to an instantiable class, probably forgot to bind an implementation.',
$interface
);
return new self( $message, self::UNRESOLVED_INTERFACE );
}
/**
* Create a new instance of the exception for an interface or class that
* could not be reflected upon.
*
* @param string $interface_or_class Interface or class that could not be
* reflected upon.
*
* @return self
*/
public static function for_unreflectable_class( $interface_or_class ) {
$message = \sprintf(
'Could not reflect on the interface or class "%s", probably not a valid FQCN.',
$interface_or_class
);
return new self( $message, self::UNREFLECTABLE_CLASS );
}
/**
* Create a new instance of the exception for an argument that could not be
* resolved.
*
* @param string $argument_name Name of the argument that could not be
* resolved.
* @param string $class Class that had the argument in its
* constructor.
* @return self
*/
public static function for_unresolved_argument( $argument_name, $class ) {
$message = \sprintf(
'Could not resolve the argument "%s" while trying to instantiate the class "%s".',
$argument_name,
$class
);
return new self( $message, self::UNRESOLVED_ARGUMENT );
}
/**
* Create a new instance of the exception for a class that was meant to be
* reused but was not yet instantiated.
*
* @param string $class Class that was not yet instantiated.
*
* @return self
*/
public static function for_uninstantiated_shared_instance( $class ) {
$message = \sprintf(
'Could not retrieve the shared instance for "%s" as it was not instantiated yet.',
$class
);
return new self( $message, self::UNINSTANTIATED_SHARED_INSTANCE );
}
/**
* Create a new instance of the exception for a delegate that was requested
* for a class that doesn't have one.
*
* @param string $class Class for which there is no delegate.
*
* @return self
*/
public static function for_invalid_delegate( $class ) {
$message = \sprintf(
'Could not retrieve a delegate for "%s", none was defined.',
$class
);
return new self( $message, self::INVALID_DELEGATE );
}
}