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\Views\View; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Application (WinPHP Framework) |
14
|
|
|
* |
15
|
|
|
* Framework em PHP baseado em MVC |
16
|
|
|
* Responsável por incluir as páginas de acordo com a URL e criar a estrutura MVC |
17
|
|
|
* @author winPHP Framework <http://github.com/winframework/winphp/> |
18
|
|
|
* @version 1.6.0 |
19
|
|
|
*/ |
20
|
|
|
class Application |
21
|
|
|
{ |
22
|
|
|
public Controller $controller; |
23
|
|
|
public View $view; |
24
|
|
|
public Connection $conn; |
25
|
|
|
protected static Application $instance; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Cria a aplicação principal |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
static::$instance = $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Retorna o ponteiro para a aplicação principal |
37
|
|
|
* @return static |
38
|
|
|
*/ |
39
|
|
|
public static function app() |
40
|
|
|
{ |
41
|
|
|
return static::$instance; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Executa o Controller@action e envia o retorno como resposta |
46
|
|
|
* @param string $class Controller |
47
|
|
|
* @param string $method Action |
48
|
|
|
* @param array $args |
49
|
|
|
*/ |
50
|
|
|
public function run($class, $method, $args = []) |
51
|
|
|
{ |
52
|
|
|
if (!class_exists($class)) { |
53
|
|
|
throw new HttpException("Controller '{$class}' not found", 404); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$controller = new $class($this); |
57
|
|
|
$controller->app = $this; |
58
|
|
|
$this->controller = $controller; |
59
|
|
|
|
60
|
|
|
if (!method_exists($controller, $method)) { |
61
|
|
|
throw new HttpException("Action '{$method}' not found in '{$class}'", 404); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$controller->init(); |
65
|
|
|
$response = $controller->$method(...$args); |
66
|
|
|
echo $this->send($response); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Envia a resposta baseado no tipo |
71
|
|
|
* @param mixed $response |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
private function send($response) |
75
|
|
|
{ |
76
|
|
|
if (is_array($response)) { |
77
|
|
|
@header('Content-Type: application/json'); |
|
|
|
|
78
|
|
|
return json_encode($response); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $response; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** @return string */ |
85
|
|
|
public function getFullUrl() |
86
|
|
|
{ |
87
|
|
|
return Url::instance()->getBaseUrl() . Url::instance()->getUrl(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @return string */ |
91
|
|
|
public function getBaseUrl() |
92
|
|
|
{ |
93
|
|
|
return Url::instance()->getBaseUrl(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Retorna a URL Atual |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getUrl() |
101
|
|
|
{ |
102
|
|
|
return Url::instance()->getUrl(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retorna a página atual |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getPage() |
110
|
|
|
{ |
111
|
|
|
return Url::instance()->getSegments()[0]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Retorna TRUE se está na página inicial |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function isHomePage() |
119
|
|
|
{ |
120
|
|
|
return $this->controller instanceof IndexController; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Define a página como 404 |
125
|
|
|
* @param string $message |
126
|
|
|
* @throws HttpException |
127
|
|
|
*/ |
128
|
|
|
public function page404($message = '') |
129
|
|
|
{ |
130
|
|
|
throw new HttpException($message, 404); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Define a página atual como algum erro |
135
|
|
|
* @param int $code |
136
|
|
|
* @param string $message |
137
|
|
|
* @throws HttpException |
138
|
|
|
*/ |
139
|
|
|
public function errorPage($code, $message = '') |
140
|
|
|
{ |
141
|
|
|
throw new HttpException($message, $code); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.