1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use Win\DI; |
7
|
|
|
use Win\Controllers\Controller; |
8
|
|
|
use Win\HttpException; |
9
|
|
|
use Win\Services\Router; |
10
|
|
|
use Win\Templates\Template; |
11
|
|
|
use Win\Templates\View; |
12
|
|
|
use Win\Utils\Str; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Application (WinPHP Framework) |
16
|
|
|
* |
17
|
|
|
* Framework em PHP baseado em MVC |
18
|
|
|
* Responsável por incluir as páginas de acordo com a URL e criar a estrutura MVC |
19
|
|
|
* @author winPHP Framework http://github.com/winframework/winphp/ |
20
|
|
|
* @version 1.9.0 |
21
|
|
|
*/ |
22
|
|
|
class Application |
23
|
|
|
{ |
24
|
|
|
protected static Application $instance; |
25
|
|
|
|
26
|
|
|
public Controller $controller; |
27
|
|
|
public Router $router; |
28
|
|
|
public View $view; |
29
|
|
|
public ?PDO $pdo = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Cria a aplicação principal |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
static::$instance = $this; |
37
|
|
|
$this->router = Router::instance(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retorna o ponteiro para a aplicação principal |
42
|
|
|
* @return static |
43
|
|
|
*/ |
44
|
|
|
public static function app() |
45
|
|
|
{ |
46
|
|
|
return static::$instance; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Executa o [Controller, action] e envia o retorno como resposta |
51
|
|
|
* @param string $class Controller |
52
|
|
|
* @param string $method Action |
53
|
|
|
* @param array $args |
54
|
|
|
*/ |
55
|
|
|
public function run($class, $method = 'index', ...$args) |
56
|
|
|
{ |
57
|
|
|
if (!class_exists($class)) { |
58
|
|
|
throw new HttpException("Controller '{$class}' não encontrado", 404); |
59
|
|
|
} |
60
|
|
|
/** @var Controller $controller */ |
61
|
|
|
$controller = DI::instance($class); |
62
|
|
|
$controller->app = $this; |
63
|
|
|
$this->controller = $controller; |
64
|
|
|
$this->router->page = $this->getPage(); |
65
|
|
|
$this->router->action = $method; |
66
|
|
|
|
67
|
|
|
if (!method_exists($controller, $method)) { |
68
|
|
|
throw new HttpException("Action '{$method}' não encontrado em '{$class}'", 404); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$controller->init(); |
72
|
|
|
$response = $controller->$method(...$args); |
73
|
|
|
echo $this->send($response); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Envia a resposta baseado no tipo |
78
|
|
|
* @param mixed $response |
79
|
|
|
* @return mixed |
80
|
|
|
* @codeCoverageIgnore |
81
|
|
|
*/ |
82
|
|
|
private function send($response) |
83
|
|
|
{ |
84
|
|
|
if (is_array($response)) { |
85
|
|
|
header('Content-Type: application/json'); |
86
|
|
|
return json_encode($response); |
87
|
|
|
} |
88
|
|
|
if ($response instanceof View && $this->controller->layout) { |
89
|
|
|
$response = new Template($this->controller->layout, ['content' => $response]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Retorna a página atual |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function getPage() |
100
|
|
|
{ |
101
|
|
|
$replaces = ['Controllers\\', 'Controller', 'App\\', '\\']; |
102
|
|
|
return Str::toUrl(str_replace($replaces, ' ', get_class($this->controller))); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|