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