1 | <?php |
||
30 | abstract class Core extends AbstractCore implements DirectoriesInterface |
||
31 | { |
||
32 | use SharedTrait; |
||
33 | |||
34 | /** |
||
35 | * I need this constant for Symfony Console. :/ |
||
36 | */ |
||
37 | const VERSION = '0.9.0-rc'; |
||
38 | |||
39 | /** |
||
40 | * Memory section for bootloaders cache. |
||
41 | */ |
||
42 | const BOOT_MEMORY = 'app'; |
||
43 | |||
44 | /** |
||
45 | * Components to be autoloader while application initialization. This property can be redefined |
||
46 | * on application level. |
||
47 | */ |
||
48 | const LOAD = []; |
||
49 | |||
50 | /** |
||
51 | * Every application should have defined timezone. |
||
52 | * |
||
53 | * @see setTimezone() |
||
54 | * @see getTimezone() |
||
55 | * @var string |
||
56 | */ |
||
57 | private $timezone = 'UTC'; |
||
58 | |||
59 | /** |
||
60 | * Set of primary application directories. |
||
61 | * |
||
62 | * @see setDirectory() |
||
63 | * @see directory() |
||
64 | * @see getDirectories() |
||
65 | * @var array |
||
66 | */ |
||
67 | private $directories = [ |
||
68 | 'root' => null, |
||
69 | 'public' => null, |
||
70 | 'libraries' => null, |
||
71 | 'framework' => null, |
||
72 | 'application' => null, |
||
73 | 'locales' => null, |
||
74 | 'runtime' => null, |
||
75 | 'config' => null, |
||
76 | 'cache' => null |
||
77 | ]; |
||
78 | |||
79 | /** |
||
80 | * @var BootloadManager |
||
81 | */ |
||
82 | protected $bootloader; |
||
83 | |||
84 | /** |
||
85 | * @var EnvironmentInterface |
||
86 | */ |
||
87 | protected $environment; |
||
88 | |||
89 | /** |
||
90 | * Not set until start method. Can be set manually in bootload. |
||
91 | * |
||
92 | * @var DispatcherInterface |
||
93 | */ |
||
94 | protected $dispatcher; |
||
95 | |||
96 | /** |
||
97 | * Application memory. |
||
98 | * |
||
99 | * @invisible |
||
100 | * @var MemoryInterface |
||
101 | */ |
||
102 | protected $memory; |
||
103 | |||
104 | /** |
||
105 | * Components to be autoloader while application initialization. This property can be redefined |
||
106 | * on application level. |
||
107 | * |
||
108 | * @deprecated use LOAD constant instead |
||
109 | * @invisible |
||
110 | */ |
||
111 | protected $load = []; |
||
112 | |||
113 | /** |
||
114 | * Core class will extend default spiral container and initiate set of directories. You must |
||
115 | * provide application, libraries and root directories to constructor. |
||
116 | * |
||
117 | * @param array $directories Core directories list. Every directory must have / |
||
118 | * at the end. |
||
119 | * @param ContainerInterface $container |
||
120 | * @param MemoryInterface $memory |
||
121 | */ |
||
122 | public function __construct( |
||
154 | |||
155 | /** |
||
156 | * Change application timezone. |
||
157 | * |
||
158 | * @param string $timezone |
||
159 | * |
||
160 | * @return $this|self |
||
161 | * @throws CoreException |
||
162 | */ |
||
163 | public function setTimezone(string $timezone): Core |
||
175 | |||
176 | /** |
||
177 | * Get active application timezone. |
||
178 | * |
||
179 | * @return \DateTimeZone |
||
180 | */ |
||
181 | public function getTimezone(): \DateTimeZone |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function hasDirectory(string $alias): bool |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function setDirectory(string $alias, string $path): DirectoriesInterface |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function directory(string $alias): string |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function getDirectories(): array |
||
223 | |||
224 | /** |
||
225 | * Change application environment. Attention, already loaded configs would not be altered! |
||
226 | * |
||
227 | * @param EnvironmentInterface $environment |
||
228 | */ |
||
229 | public function setEnvironment(EnvironmentInterface $environment) |
||
236 | |||
237 | /** |
||
238 | * @return EnvironmentInterface |
||
239 | * |
||
240 | * @throws CoreException |
||
241 | */ |
||
242 | public function getEnvironment() |
||
250 | |||
251 | /** |
||
252 | * BootloadManager responsible for initiation of your application. |
||
253 | * |
||
254 | * @return BootloadManager |
||
255 | */ |
||
256 | public function getBootloader() |
||
260 | |||
261 | /** |
||
262 | * Handle php shutdown and search for fatal errors. |
||
263 | */ |
||
264 | public function handleShutdown() |
||
277 | |||
278 | /** |
||
279 | * Convert application error into exception. |
||
280 | * |
||
281 | * @param int $code |
||
282 | * @param string $message |
||
283 | * @param string $filename |
||
284 | * @param int $line |
||
285 | * |
||
286 | * @throws \ErrorException |
||
287 | */ |
||
288 | public function handleError($code, $message, $filename = '', $line = 0) |
||
292 | |||
293 | /** |
||
294 | * Handle exception using associated application dispatcher and snapshot class. |
||
295 | * |
||
296 | * @param \Throwable $exception |
||
297 | * |
||
298 | * @throws \Throwable |
||
299 | */ |
||
300 | public function handleException(\Throwable $exception) |
||
322 | |||
323 | /** |
||
324 | * Create appropriate snapshot for given exception. By default SnapshotInterface binding will be |
||
325 | * used. |
||
326 | * |
||
327 | * Method can return null, in this case exception will be ignored and handled default way. |
||
328 | * |
||
329 | * @param \Throwable $exception |
||
330 | * |
||
331 | * @return SnapshotInterface|null |
||
332 | */ |
||
333 | public function makeSnapshot(\Throwable $exception) |
||
341 | |||
342 | /** |
||
343 | * Start application using custom or default dispatcher. |
||
344 | * |
||
345 | * @param DispatcherInterface $dispatcher Custom dispatcher. |
||
346 | */ |
||
347 | public function start(DispatcherInterface $dispatcher = null) |
||
352 | |||
353 | /** |
||
354 | * Bootstrap application. Must be executed before start method. |
||
355 | */ |
||
356 | abstract protected function bootstrap(); |
||
357 | |||
358 | /** |
||
359 | * Create default application dispatcher based on environment value. |
||
360 | * |
||
361 | * @return DispatcherInterface|ConsoleDispatcher|HttpDispatcher |
||
362 | */ |
||
363 | protected function createDispatcher() |
||
371 | |||
372 | /** |
||
373 | * Bootload all registered classes using BootloadManager. |
||
374 | * |
||
375 | * @return $this |
||
376 | */ |
||
377 | private function bootload() |
||
386 | |||
387 | /** |
||
388 | * Shared container instance (needed for helpers and etc). Attention, method will fail if no |
||
389 | * global container is set. |
||
390 | * |
||
391 | * @return InteropContainer |
||
392 | * |
||
393 | * @throws ScopeException |
||
394 | */ |
||
395 | public static function sharedContainer() |
||
404 | |||
405 | /** |
||
406 | * Initiate application core. Method will set global container if none exists. |
||
407 | * |
||
408 | * @param array $directories Spiral directories should include root, libraries |
||
409 | * and application directories. |
||
410 | * @param EnvironmentInterface $environment Application specific environment if any. |
||
411 | * @param ContainerInterface $container Initial container instance. |
||
412 | * @param bool $handleErrors |
||
413 | * |
||
414 | * @return self |
||
415 | */ |
||
416 | public static function init( |
||
483 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.