1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Nano; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Framework |
8
|
|
|
* @package Nano |
9
|
|
|
*/ |
10
|
|
|
final class Framework |
11
|
|
|
{ |
12
|
|
|
private $projectNamespace = '\Project'; |
13
|
|
|
private $controllerPackage = '\Controller'; |
14
|
|
|
private $controllerActionSuffix = 'Action'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Dispatch the request |
18
|
|
|
* @throws \Exception |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public function dispatch() |
22
|
|
|
{ |
23
|
|
|
$requestUri = $_SERVER['REQUEST_URI']; |
24
|
|
|
$verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'get'; |
25
|
|
|
$appendUri = strpos($requestUri, '?'); |
26
|
|
|
$query = substr($requestUri, 0, $appendUri === false ? strlen($requestUri) : $appendUri); |
27
|
|
|
$parts = explode('/', preg_replace('~^' . Basepath::get() . '~', '', $query)); |
28
|
|
|
$action = count($parts) >= 2 ? array_pop($parts) : 'index'; |
29
|
|
|
$controllerName = isset($parts[0]) && $parts[0] ? implode($parts, '\\') : 'index'; |
30
|
|
|
$controller = $this->projectNamespace . $this->controllerPackage . '\\' . ucfirst($controllerName); |
31
|
|
|
if (!class_exists($controller)) { |
32
|
|
|
throw new \Exception('controller ' . $controllerName . ' not found'); |
33
|
|
|
}; |
34
|
|
|
$controller = new $controller; |
35
|
|
|
$finalAction = $verb . ($action ?: 'index') . $this->controllerActionSuffix; |
36
|
|
|
if (is_callable(array($controller, $finalAction))) { |
37
|
|
|
return $controller->$finalAction(); |
38
|
|
|
} |
39
|
|
|
$finalAction = ($action ?: 'index') . $this->controllerActionSuffix; |
40
|
|
|
if (!is_callable(array($controller, $finalAction))) { |
41
|
|
|
throw new \Exception('action ' . $action . ' not found in controller ' . $controllerName); |
42
|
|
|
} |
43
|
|
|
return $controller->$finalAction(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Redefine personal namespace |
48
|
|
|
* @param string $namespace |
49
|
|
|
* @return Framework |
50
|
|
|
*/ |
51
|
|
|
public function setNamespace(string $namespace = '\Project') : Framework |
52
|
|
|
{ |
53
|
|
|
$this->projectNamespace = strlen($namespace) && $namespace{0} != '\\' ? '\\' . $namespace : $namespace; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Redefine controller subpackage |
59
|
|
|
* @param string $controllerPackage |
60
|
|
|
* @return Framework |
61
|
|
|
*/ |
62
|
|
|
public function setControllerPackage(string $controllerPackage = '\Controller') : Framework |
63
|
|
|
{ |
64
|
|
|
$this->controllerPackage = strlen($controllerPackage) && $controllerPackage{0} != '\\' |
65
|
|
|
? '\\' . $controllerPackage |
66
|
|
|
: $controllerPackage; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Redefine controller action suffix |
72
|
|
|
* @param string $suffix |
73
|
|
|
* @return Framework |
74
|
|
|
*/ |
75
|
|
|
public function setControllerActionSuffix(string $suffix = 'Action') : Framework |
76
|
|
|
{ |
77
|
|
|
$this->controllerActionSuffix = (string)$suffix; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|