Passed
Pull Request — master (#21)
by Wanderson
03:27
created

Application::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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