This repository was archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathApplication.php
More file actions
executable file
·151 lines (122 loc) · 3.6 KB
/
Copy pathApplication.php
File metadata and controls
executable file
·151 lines (122 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace b8;
use b8\Config;
use b8\Exception\Handler;
use b8\Exception\HttpException\NotFoundException;
use b8\Exception\HttpException\ServerErrorException;
use b8\Http;
use b8\View;
class Application
{
/**
* @var array
*/
protected $route;
/**
* @var \b8\Controller
*/
protected $controller;
/**
* @var b8\Http\Request
*/
protected $request;
/**
* @var b8\Http\Response
*/
protected $response;
/**
* @var b8\Config
*/
protected $config;
/**
* @var Exception\Handler
*/
protected $exceptionHandler;
/**
* @param Config $config
* @param Http\Request $request
*/
public function __construct(Config $config, Http\Request $request = null)
{
$this->config = $config;
$this->response = new Http\Response();
if (!is_null($request)) {
$this->request = $request;
} else {
$this->request = new Http\Request();
}
$this->exceptionHandler = new Handler($this->config, $this->request, $this->response);
$this->router = new Http\Router($this, $this->request, $this->config);
if (method_exists($this, 'init')) {
$this->init();
}
}
public function handleRequest()
{
$this->route = $this->router->dispatch();
if (!empty($this->route['callback'])) {
$callback = $this->route['callback'];
if (!$callback($this->route, $this->response)) {
return $this->response;
}
}
$action = lcfirst($this->toPhpName($this->route['action']));
if (!$this->getController()) {
throw new ServerErrorException('No controller found for this request.');
}
if (!$this->getController()->hasAction($action)) {
throw new NotFoundException('Controller ' . $this->toPhpName(
$this->route['controller']
) . ' does not have action ' . $action);
}
return $this->getController()->handleAction($action, $this->route['args']);
}
/**
* @return \b8\Controller
*/
public function getController()
{
if (empty($this->controller)) {
$controllerClass = implode('\\', array_filter(
array(
$this->config->get('b8.app.namespace'),
$this->toPhpName($this->route['namespace']),
$this->toPhpName($this->route['controller']) . 'Controller'
)
));
$this->controller = $this->loadController($controllerClass);
}
return $this->controller;
}
protected function loadController($class)
{
$controller = new $class($this->config, $this->request, $this->response);
$controller->init();
return $controller;
}
protected function controllerExists($route)
{
$controllerClass = implode('\\', array_filter(
array(
$this->config->get('b8.app.namespace'),
$this->toPhpName($this->route['namespace']),
$this->toPhpName($this->route['controller']) . 'Controller'
)
));
return class_exists($controllerClass);
}
public function isValidRoute($route)
{
if ($this->controllerExists($route)) {
return true;
}
return false;
}
protected function toPhpName($string)
{
$string = str_replace('-', ' ', $string);
$string = ucwords($string);
$string = str_replace(' ', '', $string);
return $string;
}
}