79 lines
2.1 KiB
PHP
79 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace lib\Core;
|
|
|
|
class App
|
|
{
|
|
private ?Controller $_controller = null;
|
|
private $_method = null;
|
|
public function __construct(string $url)
|
|
{
|
|
// URL에서 호스트 뒤의 경로를 가져옴
|
|
$path = parse_url($url, PHP_URL_PATH);
|
|
// 슬래시(`/`)를 기준으로 분할
|
|
$arguments = explode('/', trim($path, '/'));
|
|
//Controller 추출
|
|
if (isset($arguments[0])) {
|
|
$route = "";
|
|
switch ($arguments[0]) {
|
|
case 'new':
|
|
$route = "SiteController";
|
|
break;
|
|
default:
|
|
$route = $arguments[0];
|
|
break;
|
|
}
|
|
$control = "lib\\Controllers\\" . $route;
|
|
$this->_controller = new $control();
|
|
}
|
|
//Method추출
|
|
if (isset($arguments[1])) {
|
|
$route = "";
|
|
switch ($arguments[1]) {
|
|
case 'service':
|
|
$route = "newservices";
|
|
break;
|
|
case 'history':
|
|
$route = "newhistorys";
|
|
break;
|
|
default:
|
|
$route = $arguments[1];
|
|
break;
|
|
}
|
|
$this->_method = $route;
|
|
}
|
|
//세그먼트 추출
|
|
$segments = [];
|
|
if (count($arguments) > 2) {
|
|
$isKey = true;
|
|
$key = "";
|
|
foreach (array_slice($arguments, 2) as $argument) {
|
|
if ($isKey) {
|
|
$segments[$argument] = null;
|
|
$key = $argument;
|
|
$isKey = false; //Value값을 넣어야하므로
|
|
} else {
|
|
$segments[$key] = $argument;
|
|
$isKey = true; //Key로 설정해야하므로
|
|
}
|
|
}
|
|
}
|
|
$this->_controller->setSegments($segments);
|
|
} //
|
|
|
|
final public function getController(): Controller
|
|
{
|
|
if ($this->_controller === null) {
|
|
throw new \Exception("Method 사용법 : 'http://test.com/Control/Method/arg1/arg2' 혹은 php index.php Control/Method/arg1/arg2");
|
|
}
|
|
return $this->_controller;
|
|
}
|
|
final public function getMethod(): string
|
|
{
|
|
if ($this->_method === null) {
|
|
throw new \Exception("Method 사용법 : 'http://test.com/Control/Method/arg1/arg2' 혹은 php index.php Control/Method/arg1/arg2");
|
|
}
|
|
return $this->_method;
|
|
}
|
|
} //Class
|