1 | <?php |
||
26 | class Phile implements MiddlewareInterface |
||
27 | { |
||
28 | /** @var Config Phile configuration */ |
||
29 | protected $config; |
||
30 | |||
31 | /** @var Event event-bus */ |
||
32 | protected $eventBus; |
||
33 | |||
34 | /** @var array callbacks run at bootstrap */ |
||
35 | protected $bootstrapConfigs = []; |
||
36 | |||
37 | /** @var array callbacks run on middleware-setup */ |
||
38 | protected $middlewareConfigs = []; |
||
39 | |||
40 | /** |
||
41 | * Constructor sets-up base Phile environment |
||
42 | * |
||
43 | * @param Event $eventBus |
||
44 | * @param Config $config |
||
45 | */ |
||
46 | 28 | public function __construct(Event $eventBus, Config $config) |
|
51 | |||
52 | /** |
||
53 | * Adds bootstrap-setup |
||
54 | */ |
||
55 | 28 | public function addBootstrap(callable $bootstrap): self |
|
60 | |||
61 | /** |
||
62 | * Adds middleware-setup |
||
63 | */ |
||
64 | 28 | public function addMiddleware(callable $middleware): self |
|
69 | |||
70 | /** |
||
71 | * Performs bootstrap |
||
72 | */ |
||
73 | 27 | public function bootstrap(): self |
|
80 | |||
81 | /** |
||
82 | * Populates Phile controlled middle-ware-queue |
||
83 | */ |
||
84 | 4 | public function middleware(MiddlewareQueue $queue): MiddlewareQueue |
|
85 | { |
||
86 | 4 | foreach ($this->middlewareConfigs as $config) { |
|
87 | 4 | call_user_func_array($config, [$queue, $this->eventBus, $this->config]); |
|
88 | } |
||
89 | 4 | return $queue; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Run a request through Phile and create a response |
||
94 | * |
||
95 | * @param ServerRequestInterface $request |
||
96 | * @return ResponseInterface |
||
97 | */ |
||
98 | 4 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
99 | { |
||
100 | 4 | $this->config->lock(); |
|
101 | |||
102 | 4 | $router = new Router($request->getServerParams()); |
|
103 | 4 | Container::getInstance()->set('Phile_Router', $router); |
|
104 | |||
105 | // BC: send response in after_init_core event |
||
106 | 4 | $response = new Response; |
|
107 | 4 | $response->setCharset($this->config->get('charset')); |
|
108 | 4 | $this->eventBus->trigger('after_init_core', ['response' => &$response]); |
|
109 | 4 | if ($response instanceof ResponseInterface) { |
|
110 | 1 | return $response; |
|
111 | } |
||
112 | |||
113 | 4 | $page = $this->resolveCurrentPage($router); |
|
114 | 4 | if ($page instanceof ResponseInterface) { |
|
115 | 2 | return $page; |
|
116 | } |
||
117 | |||
118 | 3 | $html = $this->renderHtml($page); |
|
119 | 3 | if ($html instanceof ResponseInterface) { |
|
120 | 1 | return $html; |
|
121 | } |
||
122 | |||
123 | 2 | $charset = $this->config->get('charset'); |
|
124 | 2 | $response = (new Response)->createHtmlResponse($html) |
|
125 | 2 | ->withHeader('Content-Type', 'text/html; charset=' . $charset); |
|
126 | |||
127 | 2 | if ($page->getPageId() == $this->config->get('not_found_page')) { |
|
128 | 1 | $response = $response->withStatus(404); |
|
129 | } |
||
130 | |||
131 | 2 | return $response; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Resolves request into the current page |
||
136 | */ |
||
137 | 4 | protected function resolveCurrentPage(Router $router) |
|
173 | |||
174 | /** |
||
175 | * Renders page into output format (HTML) |
||
176 | */ |
||
177 | 3 | protected function renderHtml(Page $page) |
|
205 | } |
||
206 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: