Refactored Http.
This commit is contained in:
parent
aadfabd714
commit
1fdf9a7ab1
19 changed files with 48 additions and 42 deletions
|
|
@ -3,12 +3,12 @@
|
|||
namespace PHPCensor;
|
||||
|
||||
use PHPCensor\Exception\HttpException;
|
||||
use b8\Http\Response;
|
||||
use b8\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use b8\Http\Request;
|
||||
use b8\Http\Router;
|
||||
use PHPCensor\Http\Request;
|
||||
use PHPCensor\Http\Router;
|
||||
|
||||
/**
|
||||
* @author Dan Cryer <dan@block8.co.uk>
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
namespace PHPCensor;
|
||||
|
||||
use PHPCensor\Exception\HttpException\ForbiddenException;
|
||||
use b8\Http\Request;
|
||||
use b8\Http\Response;
|
||||
use PHPCensor\Http\Request;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\Model\User;
|
||||
use PHPCensor\Store\UserStore;
|
||||
|
|
|
|||
|
|
@ -3,11 +3,12 @@
|
|||
namespace PHPCensor\Controller;
|
||||
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use b8\Http\Response\JsonResponse;
|
||||
use PHPCensor\Http\Response\JsonResponse;
|
||||
use JasonGrimes\Paginator;
|
||||
use PHPCensor\BuildFactory;
|
||||
use PHPCensor\Helper\AnsiConverter;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Model\Build;
|
||||
use PHPCensor\Model\User;
|
||||
use PHPCensor\Service\BuildService;
|
||||
|
|
@ -251,7 +252,7 @@ class BuildController extends Controller
|
|||
$_SESSION['global_error'] = Lang::get('add_to_queue_failed');
|
||||
}
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'build/view/' . $build->getId());
|
||||
|
||||
return $response;
|
||||
|
|
@ -272,7 +273,7 @@ class BuildController extends Controller
|
|||
|
||||
$this->buildService->deleteBuild($build);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'project/view/' . $build->getProjectId());
|
||||
|
||||
return $response;
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
namespace PHPCensor\Controller;
|
||||
|
||||
use b8\Http\Response;
|
||||
use b8\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\BuildFactory;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace PHPCensor\Controller;
|
|||
use b8;
|
||||
use b8\Form;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Model\ProjectGroup;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Model\User;
|
||||
|
|
@ -58,8 +59,10 @@ class GroupController extends Controller
|
|||
|
||||
/**
|
||||
* Add or edit a project group.
|
||||
*
|
||||
* @param null $groupId
|
||||
* @return void|b8\Http\Response\RedirectResponse
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function edit($groupId = null)
|
||||
{
|
||||
|
|
@ -83,7 +86,7 @@ class GroupController extends Controller
|
|||
|
||||
$this->groupStore->save($group);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'group');
|
||||
|
||||
return $response;
|
||||
|
|
@ -112,7 +115,7 @@ class GroupController extends Controller
|
|||
/**
|
||||
* Delete a project group.
|
||||
* @param $groupId
|
||||
* @return b8\Http\Response\RedirectResponse
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function delete($groupId)
|
||||
{
|
||||
|
|
@ -120,7 +123,7 @@ class GroupController extends Controller
|
|||
$group = $this->groupStore->getById($groupId);
|
||||
|
||||
$this->groupStore->delete($group);
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL.'group');
|
||||
return $response;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use PHPCensor\Helper\SshKey;
|
|||
use PHPCensor\Service\BuildService;
|
||||
use PHPCensor\Service\ProjectService;
|
||||
use PHPCensor\Model\Build;
|
||||
use b8\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Store\Factory;
|
||||
|
||||
|
|
@ -58,7 +58,7 @@ class ProjectController extends PHPCensor\Controller
|
|||
/**
|
||||
* @param int $projectId
|
||||
*
|
||||
* @return b8\Http\Response
|
||||
* @return PHPCensor\Http\Response
|
||||
*/
|
||||
public function ajaxBuilds($projectId)
|
||||
{
|
||||
|
|
@ -581,7 +581,7 @@ class ProjectController extends PHPCensor\Controller
|
|||
{
|
||||
$github = new Github();
|
||||
|
||||
$response = new b8\Http\Response\JsonResponse();
|
||||
$response = new PHPCensor\Http\Response\JsonResponse();
|
||||
$response->setContent($github->getRepositories());
|
||||
|
||||
return $response;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use b8;
|
|||
use PHPCensor\Helper\Email;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Security\Authentication\Service;
|
||||
use PHPCensor\Store\UserStore;
|
||||
use PHPCensor\Store\Factory;
|
||||
|
|
@ -48,7 +49,7 @@ class SessionController extends Controller
|
|||
if ($user) {
|
||||
$_SESSION['php-censor-user-id'] = $user->getId();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', $this->getLoginRedirect());
|
||||
|
||||
return $response;
|
||||
|
|
@ -108,7 +109,7 @@ class SessionController extends Controller
|
|||
);
|
||||
}
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', $this->getLoginRedirect());
|
||||
|
||||
return $response;
|
||||
|
|
@ -176,7 +177,7 @@ class SessionController extends Controller
|
|||
true
|
||||
);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL);
|
||||
return $response;
|
||||
}
|
||||
|
|
@ -237,7 +238,7 @@ class SessionController extends Controller
|
|||
|
||||
$_SESSION['php-censor-user-id'] = $user->getId();
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL);
|
||||
return $response;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use PHPCensor\Exception\HttpException\NotFoundException;
|
|||
use b8\Form;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Helper\Lang;
|
||||
use PHPCensor\Http\Response\RedirectResponse;
|
||||
use PHPCensor\Model\User;
|
||||
use PHPCensor\Service\UserService;
|
||||
use PHPCensor\View;
|
||||
|
|
@ -183,7 +184,7 @@ class UserController extends Controller
|
|||
|
||||
$this->userService->createUser($name, $email, 'internal', json_encode(['type' => 'internal']), $password, $isAdmin);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL . 'user');
|
||||
return $response;
|
||||
}
|
||||
|
|
@ -224,7 +225,7 @@ class UserController extends Controller
|
|||
|
||||
$this->userService->updateUser($user, $name, $email, $password, $isAdmin);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL . 'user');
|
||||
return $response;
|
||||
}
|
||||
|
|
@ -298,7 +299,7 @@ class UserController extends Controller
|
|||
|
||||
$this->userService->deleteUser($user);
|
||||
|
||||
$response = new b8\Http\Response\RedirectResponse();
|
||||
$response = new RedirectResponse();
|
||||
$response->setHeader('Location', APP_URL . 'user');
|
||||
return $response;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ use PHPCensor\Controller;
|
|||
use PHPCensor\Config;
|
||||
use PHPCensor\Exception\HttpException\NotFoundException;
|
||||
use PHPCensor\Store\Factory;
|
||||
use b8\Http\Request;
|
||||
use b8\Http\Response;
|
||||
use PHPCensor\Http\Request;
|
||||
use PHPCensor\Http\Response;
|
||||
|
||||
/**
|
||||
* Webhook Controller - Processes webhook pings from BitBucket, Github, Gitlab, Gogs, etc.
|
||||
|
|
@ -70,7 +70,7 @@ class WebhookController extends Controller
|
|||
* @param string $action
|
||||
* @param mixed $actionParams
|
||||
*
|
||||
* @return \b8\Http\Response
|
||||
* @return Response
|
||||
*/
|
||||
public function handleAction($action, $actionParams)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use PHPCensor\Controller;
|
|||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\View;
|
||||
use PHPCensor\Model\Project;
|
||||
use b8\Http\Response;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\Store\BuildStore;
|
||||
use PHPCensor\Store\ProjectStore;
|
||||
use PHPCensor\Store\ProjectGroupStore;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace PHPCensor\Controller;
|
|||
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\View;
|
||||
use b8\Http\Response;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Store\BuildStore;
|
||||
use PHPCensor\Store\ProjectStore;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace PHPCensor\Controller;
|
|||
|
||||
use PHPCensor\Store\Factory;
|
||||
use PHPCensor\View;
|
||||
use b8\Http\Response;
|
||||
use PHPCensor\Http\Response;
|
||||
use PHPCensor\BuildFactory;
|
||||
use PHPCensor\Controller;
|
||||
use PHPCensor\Store\BuildStore;
|
||||
|
|
|
|||
147
src/PHPCensor/Http/Request.php
Normal file
147
src/PHPCensor/Http/Request.php
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Http;
|
||||
|
||||
class Request
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $params = [];
|
||||
|
||||
/**
|
||||
* Request data.
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* Set up the request.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->parseInput();
|
||||
|
||||
$this->data['path'] = $this->getRequestPath();
|
||||
$this->data['parts'] = array_values(array_filter(explode('/', $this->data['path'])));
|
||||
}
|
||||
|
||||
protected function getRequestPath()
|
||||
{
|
||||
$path = '';
|
||||
|
||||
// Start out with the REQUEST_URI:
|
||||
if (!empty($_SERVER['REQUEST_URI'])) {
|
||||
$path = $_SERVER['REQUEST_URI'];
|
||||
}
|
||||
|
||||
if ($_SERVER['SCRIPT_NAME'] != $_SERVER['REQUEST_URI']) {
|
||||
$scriptPath = str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']);
|
||||
$path = str_replace($scriptPath, '', $path);
|
||||
}
|
||||
|
||||
// Remove index.php from the URL if it is present:
|
||||
$path = str_replace(['/index.php', 'index.php'], '', $path);
|
||||
|
||||
// Also cut out the query string:
|
||||
$path = explode('?', $path);
|
||||
$path = array_shift($path);
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse incoming variables, incl. $_GET, $_POST and also reads php://input for PUT/DELETE.
|
||||
*/
|
||||
protected function parseInput()
|
||||
{
|
||||
$params = $_REQUEST;
|
||||
|
||||
if (!isset($_SERVER['REQUEST_METHOD']) || in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'DELETE'])) {
|
||||
$vars = file_get_contents('php://input');
|
||||
|
||||
if (!is_string($vars) || strlen(trim($vars)) === 0) {
|
||||
$vars = '';
|
||||
}
|
||||
|
||||
$inputData = [];
|
||||
parse_str($vars, $inputData);
|
||||
|
||||
$params = array_merge($params, $inputData);
|
||||
}
|
||||
|
||||
$this->setParams($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all request parameters.
|
||||
* @return array
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a specific request parameter, or a default value if not set.
|
||||
*/
|
||||
public function getParam($key, $default = null)
|
||||
{
|
||||
if (isset($this->params[$key])) {
|
||||
return $this->params[$key];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or override a request parameter.
|
||||
*/
|
||||
public function setParam($key, $value = null)
|
||||
{
|
||||
$this->params[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an array of request parameters.
|
||||
*/
|
||||
public function setParams(array $params)
|
||||
{
|
||||
$this->params = array_merge($this->params, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Un-set a specific parameter.
|
||||
*/
|
||||
public function unsetParam($key)
|
||||
{
|
||||
unset($this->params[$key]);
|
||||
}
|
||||
|
||||
public function getMethod()
|
||||
{
|
||||
return strtoupper($_SERVER['REQUEST_METHOD']);
|
||||
}
|
||||
|
||||
public function getPath()
|
||||
{
|
||||
return $this->data['path'];
|
||||
}
|
||||
|
||||
public function getPathParts()
|
||||
{
|
||||
return $this->data['parts'];
|
||||
}
|
||||
|
||||
public function isAjax()
|
||||
{
|
||||
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
131
src/PHPCensor/Http/Response.php
Normal file
131
src/PHPCensor/Http/Response.php
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Http;
|
||||
|
||||
class Response
|
||||
{
|
||||
protected $data = [];
|
||||
|
||||
public function __construct(Response $createFrom = null)
|
||||
{
|
||||
if (!is_null($createFrom)) {
|
||||
$this->data = $createFrom->getData();
|
||||
}
|
||||
}
|
||||
|
||||
public function hasLayout()
|
||||
{
|
||||
return !isset($this->data['layout']) ? true : $this->data['layout'];
|
||||
}
|
||||
|
||||
public function disableLayout()
|
||||
{
|
||||
$this->data['layout'] = false;
|
||||
}
|
||||
|
||||
public function enableLayout()
|
||||
{
|
||||
$this->data['layout'] = true;
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function setResponseCode($code)
|
||||
{
|
||||
$this->data['code'] = (int)$code;
|
||||
}
|
||||
|
||||
public function setHeader($key, $val)
|
||||
{
|
||||
$this->data['headers'][$key] = $val;
|
||||
}
|
||||
|
||||
public function clearHeaders()
|
||||
{
|
||||
$this->data['headers'] = [];
|
||||
}
|
||||
|
||||
public function setContent($content)
|
||||
{
|
||||
$this->data['body'] = $content;
|
||||
}
|
||||
|
||||
public function getContent()
|
||||
{
|
||||
return $this->data['body'];
|
||||
}
|
||||
|
||||
public function flush()
|
||||
{
|
||||
$this->sendResponseCode();
|
||||
|
||||
if (isset($this->data['headers'])) {
|
||||
foreach ($this->data['headers'] as $header => $val) {
|
||||
header($header . ': ' . $val, true);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->flushBody();
|
||||
}
|
||||
|
||||
protected function sendResponseCode()
|
||||
{
|
||||
if (!isset($this->data['code'])) {
|
||||
$this->data['code'] = 200;
|
||||
}
|
||||
|
||||
switch ($this->data['code']) {
|
||||
// 300 class
|
||||
case 301:
|
||||
$text = 'Moved Permanently';
|
||||
break;
|
||||
case 302:
|
||||
$text = 'Moved Temporarily';
|
||||
break;
|
||||
|
||||
// 400 class errors
|
||||
case 400:
|
||||
$text = 'Bad Request';
|
||||
break;
|
||||
case 401:
|
||||
$text = 'Not Authorized';
|
||||
break;
|
||||
case 403:
|
||||
$text = 'Forbidden';
|
||||
break;
|
||||
case 404:
|
||||
$text = 'Not Found';
|
||||
break;
|
||||
|
||||
// 500 class errors
|
||||
case 500:
|
||||
$text = 'Internal Server Error';
|
||||
break;
|
||||
|
||||
// OK
|
||||
case 200:
|
||||
default:
|
||||
$text = 'OK';
|
||||
break;
|
||||
}
|
||||
|
||||
header('HTTP/1.1 ' . $this->data['code'] . ' ' . $text, true, $this->data['code']);
|
||||
}
|
||||
|
||||
protected function flushBody()
|
||||
{
|
||||
if (isset($this->data['body'])) {
|
||||
return $this->data['body'];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->flush();
|
||||
}
|
||||
}
|
||||
30
src/PHPCensor/Http/Response/JsonResponse.php
Normal file
30
src/PHPCensor/Http/Response/JsonResponse.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Http\Response;
|
||||
|
||||
use PHPCensor\Http\Response;
|
||||
|
||||
class JsonResponse extends Response
|
||||
{
|
||||
public function __construct(Response $createFrom = null)
|
||||
{
|
||||
parent::__construct($createFrom);
|
||||
|
||||
$this->setContent([]);
|
||||
$this->setHeader('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
public function hasLayout()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function flushBody()
|
||||
{
|
||||
if (isset($this->data['body'])) {
|
||||
return json_encode($this->data['body']);
|
||||
}
|
||||
|
||||
return json_encode(null);
|
||||
}
|
||||
}
|
||||
27
src/PHPCensor/Http/Response/RedirectResponse.php
Normal file
27
src/PHPCensor/Http/Response/RedirectResponse.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Http\Response;
|
||||
|
||||
use PHPCensor\Http\Response;
|
||||
|
||||
class RedirectResponse extends Response
|
||||
{
|
||||
public function __construct(Response $createFrom = null)
|
||||
{
|
||||
parent::__construct($createFrom);
|
||||
|
||||
$this->setContent(null);
|
||||
$this->setResponseCode(302);
|
||||
}
|
||||
|
||||
public function hasLayout()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function flush()
|
||||
{
|
||||
parent::flush();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
128
src/PHPCensor/Http/Router.php
Normal file
128
src/PHPCensor/Http/Router.php
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCensor\Http;
|
||||
|
||||
use PHPCensor\Application;
|
||||
use PHPCensor\Config;
|
||||
|
||||
class Router
|
||||
{
|
||||
/**
|
||||
* @var Request;
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* @var Config;
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
protected $application;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $routes = [['route' => '/:controller/:action', 'callback' => null, 'defaults' => []]];
|
||||
|
||||
public function __construct(Application $application, Request $request, Config $config)
|
||||
{
|
||||
$this->application = $application;
|
||||
$this->request = $request;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function clearRoutes()
|
||||
{
|
||||
$this->routes = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $route Route definition
|
||||
* @param array $options
|
||||
* @param callable $callback
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function register($route, $options = [], $callback = null)
|
||||
{
|
||||
if (!is_callable($callback)) {
|
||||
throw new \InvalidArgumentException('$callback must be callable.');
|
||||
}
|
||||
|
||||
array_unshift($this->routes, ['route' => $route, 'callback' => $callback, 'defaults' => $options]);
|
||||
}
|
||||
|
||||
public function dispatch()
|
||||
{
|
||||
foreach ($this->routes as $route) {
|
||||
$pathParts = $this->request->getPathParts();
|
||||
|
||||
//-------
|
||||
// Set up default values for everything:
|
||||
//-------
|
||||
$thisNamespace = 'Controller';
|
||||
$thisController = null;
|
||||
$thisAction = null;
|
||||
|
||||
if (array_key_exists('namespace', $route['defaults'])) {
|
||||
$thisNamespace = $route['defaults']['namespace'];
|
||||
}
|
||||
|
||||
if (array_key_exists('controller', $route['defaults'])) {
|
||||
$thisController = $route['defaults']['controller'];
|
||||
}
|
||||
|
||||
if (array_key_exists('action', $route['defaults'])) {
|
||||
$thisAction = $route['defaults']['action'];
|
||||
}
|
||||
|
||||
$routeParts = array_filter(explode('/', $route['route']));
|
||||
$routeMatches = true;
|
||||
|
||||
while (count($routeParts)) {
|
||||
$routePart = array_shift($routeParts);
|
||||
$pathPart = array_shift($pathParts);
|
||||
|
||||
switch ($routePart) {
|
||||
case ':namespace':
|
||||
$thisNamespace = !is_null($pathPart) ? $pathPart : $thisNamespace;
|
||||
break;
|
||||
case ':controller':
|
||||
$thisController = !is_null($pathPart) ? $pathPart : $thisController;
|
||||
break;
|
||||
case ':action':
|
||||
$thisAction = !is_null($pathPart) ? $pathPart : $thisAction;
|
||||
break;
|
||||
default:
|
||||
if ($routePart != $pathPart) {
|
||||
$routeMatches = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$routeMatches || !count($pathParts)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$thisArgs = $pathParts;
|
||||
|
||||
if ($routeMatches) {
|
||||
$route = [
|
||||
'namespace' => $thisNamespace,
|
||||
'controller' => $thisController,
|
||||
'action' => $thisAction,
|
||||
'args' => $thisArgs,
|
||||
'callback' => $route['callback']
|
||||
];
|
||||
|
||||
if ($this->application->isValidRoute($route)) {
|
||||
return $route;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue