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
·130 lines (102 loc) · 3 KB
/
Application.php
File metadata and controls
executable file
·130 lines (102 loc) · 3 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
<?php
namespace b8;
use b8\Config;
use b8\Exception\HttpException\NotFoundException;
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;
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->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->controllerExists($this->route)) {
throw new NotFoundException('Route not found');
}
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 = $this->getControllerClass($this->route);
$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)
{
return class_exists($this->getControllerClass($route));
}
protected function getControllerClass($route)
{
$namespace = $this->toPhpName($route['namespace']);
$controller = $this->toPhpName($route['controller']);
return $this->config->get('b8.app.namespace') . '\\' . $namespace . '\\' . $controller . 'Controller';
}
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;
}
}