1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win; |
4
|
|
|
|
5
|
|
|
use App\Controllers\IndexController; |
6
|
|
|
use Win\Common\DependenceInjector as DI; |
7
|
|
|
use Win\Common\Utils\Str; |
8
|
|
|
use Win\Controllers\Controller; |
9
|
|
|
use Win\Repositories\Database\Connection; |
10
|
|
|
use Win\Request\Url; |
11
|
|
|
use Win\HttpException; |
12
|
|
|
use Win\Repositories\Session; |
13
|
|
|
use Win\Views\View; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Application (WinPHP Framework) |
17
|
|
|
* |
18
|
|
|
* Framework em PHP baseado em MVC |
19
|
|
|
* Responsável por incluir as páginas de acordo com a URL e criar a estrutura MVC |
20
|
|
|
* @author winPHP Framework <http://github.com/winframework/winphp/> |
21
|
|
|
* @version 1.6.0 |
22
|
|
|
*/ |
23
|
|
|
class Application |
24
|
|
|
{ |
25
|
|
|
protected static Application $instance; |
26
|
|
|
|
27
|
|
|
public Controller $controller; |
28
|
|
|
public View $view; |
29
|
|
|
public Session $session; |
30
|
|
|
public ?Connection $conn = null; |
31
|
|
|
public string $action = ''; |
32
|
|
|
public string $page = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Cria a aplicação principal |
36
|
|
|
*/ |
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
static::$instance = $this; |
40
|
|
|
Url::init(); |
41
|
|
|
$this->session = new Session(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retorna o ponteiro para a aplicação principal |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
|
|
public static function app() |
49
|
|
|
{ |
50
|
|
|
return static::$instance; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Executa o Controller@action e envia o retorno como resposta |
55
|
|
|
* @param string $class Controller |
56
|
|
|
* @param string $method Action |
57
|
|
|
* @param array $args |
58
|
|
|
*/ |
59
|
|
|
public function run($class, $method, $args = []) |
60
|
|
|
{ |
61
|
|
|
if (!class_exists($class)) { |
62
|
|
|
throw new HttpException("Controller '{$class}' not found", 404); |
63
|
|
|
} |
64
|
|
|
/** @var Controller $controller */ |
65
|
|
|
$controller = DI::make($class); |
66
|
|
|
$controller->app = $this; |
67
|
|
|
$this->controller = $controller; |
68
|
|
|
$this->action = $method; |
69
|
|
|
$this->setPage($class); |
70
|
|
|
|
71
|
|
|
if (!method_exists($controller, $method)) { |
72
|
|
|
throw new HttpException("Action '{$method}' not found in '{$class}'", 404); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$controller->init(); |
76
|
|
|
$response = $controller->$method(...$args); |
77
|
|
|
echo $this->send($response); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Envia a resposta baseado no tipo |
82
|
|
|
* @param mixed $response |
83
|
|
|
* @return mixed |
84
|
|
|
* @codeCoverageIgnore |
85
|
|
|
*/ |
86
|
|
|
private function send($response) |
87
|
|
|
{ |
88
|
|
|
if (is_array($response)) { |
89
|
|
|
header('Content-Type: application/json'); |
90
|
|
|
return json_encode($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Retorna TRUE se está na página inicial |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function isHomePage() |
101
|
|
|
{ |
102
|
|
|
return Url::$segments == Url::HOME; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Define a página como 404 |
107
|
|
|
* @param string $message |
108
|
|
|
* @throws HttpException |
109
|
|
|
*/ |
110
|
|
|
public function page404($message = '') |
111
|
|
|
{ |
112
|
|
|
throw new HttpException($message, 404); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Define a página atual como algum erro |
117
|
|
|
* @param int $code |
118
|
|
|
* @param string $message |
119
|
|
|
* @throws HttpException |
120
|
|
|
*/ |
121
|
|
|
public function errorPage($code, $message = '') |
122
|
|
|
{ |
123
|
|
|
throw new HttpException($message, $code); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Retorna a página atual |
128
|
|
|
* @param string |
129
|
|
|
*/ |
130
|
|
|
private function setPage($class) |
131
|
|
|
{ |
132
|
|
|
$replaces = ['Controllers\\', 'Controller', 'App\\', '\\']; |
133
|
|
|
$this->page = Str::toUrl(str_replace($replaces, ' ', $class)); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|