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