1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Framework; |
4
|
|
|
|
5
|
|
|
//use Psr\Container\ContainerInterface; |
6
|
|
|
//use Psr\Container\NotFoundExceptionInterface; |
7
|
|
|
|
8
|
|
|
use DrMVC\Router\RouterInterface; |
9
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
10
|
|
|
use Zend\Diactoros\ServerRequest; |
11
|
|
|
use Zend\Diactoros\Response; |
12
|
|
|
|
13
|
|
|
use DrMVC\Config\ConfigInterface; |
14
|
|
|
use DrMVC\Router; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class App |
18
|
|
|
* @package DrMVC\Framework |
19
|
|
|
* @method App options(string $pattern, callable $callable): App |
20
|
|
|
* @method App get(string $pattern, callable $callable): App |
21
|
|
|
* @method App head(string $pattern, callable $callable): App |
22
|
|
|
* @method App post(string $pattern, callable $callable): App |
23
|
|
|
* @method App put(string $pattern, callable $callable): App |
24
|
|
|
* @method App delete(string $pattern, callable $callable): App |
25
|
|
|
* @method App trace(string $pattern, callable $callable): App |
26
|
|
|
* @method App connect(string $pattern, callable $callable): App |
27
|
|
|
* @since 3.0 |
28
|
|
|
*/ |
29
|
|
|
class App implements AppInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ContainersInterface |
33
|
|
|
*/ |
34
|
|
|
private $_containers; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* App constructor. |
38
|
|
|
* @param ConfigInterface $config |
39
|
|
|
*/ |
40
|
|
|
public function __construct(ConfigInterface $config) |
41
|
|
|
{ |
42
|
|
|
$this |
43
|
|
|
->initContainers() |
44
|
|
|
->initConfig($config) |
45
|
|
|
->initRouter(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Initialize containers object |
50
|
|
|
* |
51
|
|
|
* @return App |
52
|
|
|
*/ |
53
|
|
|
private function initContainers(): App |
54
|
|
|
{ |
55
|
|
|
if (null === $this->_containers) { |
56
|
|
|
$this->_containers = new Containers(); |
57
|
|
|
} |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Put config object into the containers class |
63
|
|
|
* |
64
|
|
|
* @param ConfigInterface $config |
65
|
|
|
* @return App |
66
|
|
|
*/ |
67
|
|
|
private function initConfig(ConfigInterface $config): App |
68
|
|
|
{ |
69
|
|
|
$this->containers()->set('config', $config); |
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Put route into the container of classes |
75
|
|
|
* |
76
|
|
|
* @return App |
77
|
|
|
*/ |
78
|
|
|
private function initRouter(): App |
79
|
|
|
{ |
80
|
|
|
$req = ServerRequestFactory::fromGlobals(); |
81
|
|
|
$res = new Response(); |
82
|
|
|
$router = new Router($req, $res); |
83
|
|
|
|
84
|
|
|
$this->containers()->set('router', $router); |
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get containers object |
90
|
|
|
* |
91
|
|
|
* @param string|null $name |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function containers(string $name = null) |
95
|
|
|
{ |
96
|
|
|
return (null !== $name) |
97
|
|
|
? $this->_containers->get($name) |
98
|
|
|
: $this->_containers; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function __call(string $method, array $args): RouterInterface |
102
|
|
|
{ |
103
|
|
|
$router = $this->containers('router'); |
104
|
|
|
if (\in_array($method, Router::METHODS, false)) { |
105
|
|
|
$router->set([$method], $args); |
106
|
|
|
} |
107
|
|
|
return $router; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function any(string $pattern, $callable): RouterInterface |
111
|
|
|
{ |
112
|
|
|
$router = $this->containers('router'); |
113
|
|
|
$router->any($pattern, $callable); |
114
|
|
|
return $router; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function error($callable): RouterInterface |
118
|
|
|
{ |
119
|
|
|
$router = $this->containers('router'); |
120
|
|
|
$router->error($callable); |
121
|
|
|
return $router; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function map(array $methods, string $pattern, $callable): RouterInterface |
125
|
|
|
{ |
126
|
|
|
$router = $this->containers('router'); |
127
|
|
|
$router->map($methods, $pattern, $callable); |
128
|
|
|
return $router; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|