|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @license MIT |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Pivasic\Core; |
|
6
|
|
|
|
|
7
|
|
|
use Pivasic\Bundle\Debug\Wrap; |
|
8
|
|
|
use Pivasic\Bundle\Template\Native; |
|
9
|
|
|
use Pivasic\Core\Exception\RouteException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Initialize autoload. |
|
13
|
|
|
* Process request and get response. |
|
14
|
|
|
* |
|
15
|
|
|
* Class Application |
|
16
|
|
|
* @package Dspbee\Core |
|
17
|
|
|
*/ |
|
18
|
|
|
class Application |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Application constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $appRoot |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(string $appRoot) |
|
26
|
|
|
{ |
|
27
|
|
|
$appRoot = rtrim($appRoot, '/') . '/'; |
|
28
|
|
|
$this->packageRoot = $appRoot; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Register autoload to app/package/$package/src dir's. |
|
32
|
|
|
*/ |
|
33
|
|
|
spl_autoload_register(function ($path) use ($appRoot) { |
|
34
|
|
|
$path = explode('\\', $path); |
|
35
|
|
|
array_shift($path); // Vendor |
|
36
|
|
|
$package = $appRoot . 'package/' . array_shift($path); // Package |
|
37
|
|
|
$path = $package . '/src/' . implode('/', $path) . '.php'; |
|
38
|
|
|
if (file_exists($path)) { |
|
39
|
|
|
require_once $path; |
|
40
|
|
|
} |
|
41
|
|
|
}); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Process request. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $packageList |
|
48
|
|
|
* @param array $languageList |
|
49
|
|
|
* @param array $customRouteList |
|
50
|
|
|
* @param string $url |
|
51
|
|
|
* |
|
52
|
|
|
* @return Response |
|
53
|
|
|
* @throws RouteException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getResponse(array $packageList, array $languageList, array $customRouteList, string $url = ''): Response |
|
56
|
|
|
{ |
|
57
|
|
|
$request = new Request($languageList, $packageList, $url); |
|
58
|
|
|
|
|
59
|
|
|
$this->packageRoot .= 'package/' . $request->package() . '/'; |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
/** |
|
63
|
|
|
* Process request. |
|
64
|
|
|
*/ |
|
65
|
|
|
if (isset($customRouteList[$request->package()])) { |
|
66
|
|
|
/** |
|
67
|
|
|
* Path to custom router class. |
|
68
|
|
|
*/ |
|
69
|
|
|
$path = $this->packageRoot . 'CustomRoute.php'; |
|
70
|
|
|
if (file_exists($path)) { |
|
71
|
|
|
require $path; |
|
72
|
|
|
$route = $request->package() . '\\CustomRoute'; |
|
73
|
|
|
/** |
|
74
|
|
|
* @var IRoute $route |
|
75
|
|
|
*/ |
|
76
|
|
|
$route = new $route(); |
|
77
|
|
|
$this->response = $route->getResponse($this->packageRoot, $request); |
|
78
|
|
|
if ($this->response->is404()) { |
|
79
|
|
|
$this->set404(); |
|
80
|
|
|
} |
|
81
|
|
|
return $this->response; |
|
82
|
|
|
} else { |
|
83
|
|
|
throw new RouteException(sprintf('The file "%s" does not exist', $path)); |
|
84
|
|
|
} |
|
85
|
|
|
} else { |
|
86
|
|
|
$this->response = (new DefaultRoute())->getResponse($this->packageRoot, $request); |
|
87
|
|
|
if ($this->response->is404()) { |
|
88
|
|
|
$this->set404(); |
|
89
|
|
|
} |
|
90
|
|
|
return $this->response; |
|
91
|
|
|
} |
|
92
|
|
|
} catch (RouteException $e) { |
|
93
|
|
|
if (Wrap::isEnabled()) { |
|
94
|
|
|
throw $e; |
|
95
|
|
|
} else { |
|
96
|
|
|
$this->response = new Response(); |
|
97
|
|
|
$this->set404(); |
|
98
|
|
|
return $this->response; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Set 404 code and content. |
|
105
|
|
|
*/ |
|
106
|
|
|
private function set404() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->response->setStatusCode(404); |
|
109
|
|
|
$content = '404 Not Found'; |
|
110
|
|
|
if (file_exists($this->packageRoot . '/view/404.html.php')) { |
|
111
|
|
|
$content = (new Native($this->packageRoot))->getContent('404.html.php'); |
|
112
|
|
|
} |
|
113
|
|
|
$this->response->setContent($content); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private $packageRoot; |
|
117
|
|
|
/** |
|
118
|
|
|
* @var Response |
|
119
|
|
|
*/ |
|
120
|
|
|
private $response; |
|
121
|
|
|
} |