1 | <?php |
||
24 | class Application |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * @var ContainerInterface |
||
29 | */ |
||
30 | private static $container; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $configPath; |
||
36 | |||
37 | /** |
||
38 | * @var Request |
||
39 | */ |
||
40 | private $request; |
||
41 | |||
42 | /** |
||
43 | * @var MiddlewareRunnerInterface |
||
44 | */ |
||
45 | private $runner; |
||
46 | |||
47 | /** |
||
48 | * @var Response |
||
49 | */ |
||
50 | private $response; |
||
51 | |||
52 | /** |
||
53 | * @var ContainerInterface |
||
54 | */ |
||
55 | private static $defaultContainer; |
||
56 | |||
57 | /** |
||
58 | * Creates an application with an HTTP request |
||
59 | * |
||
60 | * If no request is provided then a Slick\Http\PhpEnvironment\Request is |
||
61 | * created with values form current php environment settings. |
||
62 | * |
||
63 | * @param Request $request |
||
64 | */ |
||
65 | 8 | public function __construct(Request $request = null) |
|
66 | { |
||
67 | 8 | $this->request = $request; |
|
68 | 8 | Configuration::addPath(__DIR__.'/Configuration'); |
|
69 | 8 | $definitions = include __DIR__.'/Configuration/services.php'; |
|
70 | 4 | self::$defaultContainer = ( |
|
71 | 8 | new ContainerBuilder($definitions) |
|
72 | ) |
||
73 | 8 | ->getContainer(); |
|
74 | 8 | } |
|
75 | |||
76 | /** |
||
77 | * Sets a new request |
||
78 | * |
||
79 | * @param Request $request |
||
80 | * |
||
81 | * @return Application |
||
82 | */ |
||
83 | 2 | public function setRequest(Request $request) |
|
89 | |||
90 | /** |
||
91 | * Gets the application dependency injection container |
||
92 | * |
||
93 | * @return ContainerInterface|Container |
||
94 | */ |
||
95 | 6 | public function getContainer() |
|
96 | { |
||
97 | 6 | if (is_null(self::$container)) { |
|
98 | 2 | self::$container = $this->checkContainer(); |
|
99 | } |
||
100 | 6 | return self::$container; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Sets the dependency injection container |
||
105 | * |
||
106 | * @param ContainerInterface $container |
||
107 | */ |
||
108 | 22 | public static function setContainer(ContainerInterface $container) |
|
112 | |||
113 | /** |
||
114 | * Returns the application dependency container |
||
115 | * |
||
116 | * @return ContainerInterface|Container |
||
117 | */ |
||
118 | 20 | public static function container() |
|
119 | { |
||
120 | 20 | if (null == self::$container) { |
|
121 | 2 | $app = new static; |
|
122 | 2 | $app->getContainer(); |
|
123 | } |
||
124 | 20 | return self::$container; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Set middleware runner |
||
129 | * |
||
130 | * @param MiddlewareRunnerInterface $runner |
||
131 | * |
||
132 | * @return $this|self|Application |
||
133 | */ |
||
134 | 2 | public function setRunner(MiddlewareRunnerInterface $runner) |
|
139 | |||
140 | /** |
||
141 | * Returns the processed response |
||
142 | * |
||
143 | * @return Response |
||
144 | */ |
||
145 | 2 | public function getResponse() |
|
146 | { |
||
147 | 2 | if (is_null($this->response)) { |
|
148 | 2 | $this->response = $this->getRunner()->run(); |
|
149 | } |
||
150 | 2 | return $this->response; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Gets the HTTP middleware runner for this application |
||
155 | * |
||
156 | * @return MiddlewareRunnerInterface |
||
157 | */ |
||
158 | 2 | public function getRunner() |
|
159 | { |
||
160 | 2 | if (null === $this->runner) { |
|
161 | 2 | $runner = $this->getContainer() |
|
162 | 2 | ->get('middleware.runner') |
|
163 | 2 | ->setRequest($this->getRequest()) |
|
164 | ; |
||
165 | 2 | $this->setRunner($runner); |
|
166 | } |
||
167 | 2 | return $this->runner; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Set configuration path |
||
172 | * |
||
173 | * @param string $configPath |
||
174 | * |
||
175 | * @return Application|self |
||
176 | */ |
||
177 | public function setConfigPath($configPath) |
||
183 | |||
184 | /** |
||
185 | * Gets configuration path |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getConfigPath() |
||
197 | |||
198 | /** |
||
199 | * Gets HTTP request object |
||
200 | * |
||
201 | * @return Request |
||
202 | */ |
||
203 | 4 | public function getRequest() |
|
211 | |||
212 | /** |
||
213 | * Gets container with user overridden settings |
||
214 | * |
||
215 | * @return ContainerInterface|\Slick\Di\Container |
||
216 | */ |
||
217 | 2 | protected function checkContainer() |
|
234 | |||
235 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: