refactored the the URI class into separate router and request classes

parent 1fd7cc2f
This diff is collapsed.
......@@ -46,31 +46,40 @@ class Breadcrumb
* @var Lang
*/
protected $lang;
/**
* @var URI
* @var Request
*/
protected $uri;
protected $request;
/**
* @var Router
*/
protected $router;
/**
* @var View
*/
protected $view;
public function __construct(\Doctrine\DBAL\Connection $db, Lang $lang, URI $uri, View $view)
public function __construct(
\Doctrine\DBAL\Connection $db,
Lang $lang,
Request $request,
Router $router,
View $view
)
{
$this->lang = $lang;
$this->uri = $uri;
$this->request = $request;
$this->router = $router;
$this->view = $view;
// Frontendbereich
if ($uri->area !== 'admin') {
if ($request->area !== 'admin') {
$in = array(
$uri->query,
$uri->getUriWithoutPages(),
$uri->mod . '/' . $uri->controller . '/' . $uri->file . '/',
$uri->mod . '/' . $uri->controller . '/',
$uri->mod
$request->query,
$request->getUriWithoutPages(),
$request->mod . '/' . $request->controller . '/' . $request->file . '/',
$request->mod . '/' . $request->controller . '/',
$request->mod
);
$items = $db->executeQuery('SELECT p.title, p.uri, p.left_id, p.right_id FROM ' . DB_PRE . 'menu_items AS c, ' . DB_PRE . 'menu_items AS p WHERE c.left_id BETWEEN p.left_id AND p.right_id AND c.uri IN(?) GROUP BY p.uri ORDER BY p.left_id ASC', array($in), array(\Doctrine\DBAL\Connection::PARAM_STR_ARRAY))->fetchAll();
$c_items = count($items);
......@@ -131,7 +140,7 @@ class Breadcrumb
{
$this->stepsFromDb[] = array(
'title' => $title,
'uri' => !empty($path) ? $this->uri->route($path) : ''
'uri' => !empty($path) ? $this->router->route($path) : ''
);
return $this;
......@@ -150,7 +159,7 @@ class Breadcrumb
{
$this->stepsFromModules[] = array(
'title' => $title,
'uri' => !empty($path) ? $this->uri->route($path) : ''
'uri' => !empty($path) ? $this->router->route($path) : ''
);
return $this;
......@@ -169,7 +178,7 @@ class Breadcrumb
{
$step = array(
'title' => $title,
'uri' => $this->uri->route($path)
'uri' => $this->router->route($path)
);
array_unshift($this->stepsFromModules, $step);
return $this;
......@@ -190,11 +199,11 @@ class Breadcrumb
if ($dbSteps === true) {
$index = count($this->stepsFromDb) - (!empty($this->stepsFromDb) ? 1 : 0);
$this->stepsFromDb[$index]['title'] = $title;
$this->stepsFromDb[$index]['uri'] = !empty($path) ? $this->uri->route($path) : '';
$this->stepsFromDb[$index]['uri'] = !empty($path) ? $this->router->route($path) : '';
} else {
$index = count($this->stepsFromModules) - (!empty($this->stepsFromModules) ? 1 : 0);
$this->stepsFromModules[$index]['title'] = $title;
$this->stepsFromModules[$index]['uri'] = !empty($path) ? $this->uri->route($path) : '';
$this->stepsFromModules[$index]['uri'] = !empty($path) ? $this->router->route($path) : '';
}
return $this;
......@@ -206,7 +215,7 @@ class Breadcrumb
private function _setBreadcrumbCache()
{
// Brotkrümelspur für das Admin-Panel
if ($this->uri->area === 'admin') {
if ($this->request->area === 'admin') {
$this->_setBreadcrumbCacheForAdmin();
} else { // Breadcrumb for frontend requests
$this->_setBreadcrumbCacheForFrontend();
......@@ -221,7 +230,7 @@ class Breadcrumb
*/
private function _setBreadcrumbCacheForAdmin()
{
$module = $this->uri->mod;
$module = $this->request->mod;
if ($module !== 'acp') {
$this->setTitlePostfix($this->lang->t('system', 'acp'));
......@@ -229,10 +238,10 @@ class Breadcrumb
// No breadcrumb is set yet
if (empty($this->stepsFromModules)) {
$controller = $this->uri->controller;
$file = $this->uri->file;
$languageKey = $this->uri->area . '_' . $controller . '_' . $file;
$languageKeyIndex = $this->uri->area . '_' . $controller . '_index';
$controller = $this->request->controller;
$file = $this->request->file;
$languageKey = $this->request->area . '_' . $controller . '_' . $file;
$languageKeyIndex = $this->request->area . '_' . $controller . '_index';
$this->append($this->lang->t('system', 'acp'), 'acp/acp');
......@@ -264,11 +273,11 @@ class Breadcrumb
{
// No breadcrumb has been set yet
if (empty($this->stepsFromModules)) {
$module = $this->uri->mod;
$controller = $this->uri->controller;
$file = $this->uri->file;
$languageKey = $this->uri->area . '_' . $controller . '_' . $file;
$languageKeyIndex = $this->uri->area . '_' . $controller . '_index';
$module = $this->request->mod;
$controller = $this->request->controller;
$file = $this->request->file;
$languageKey = $this->request->area . '_' . $controller . '_' . $file;
$languageKeyIndex = $this->request->area . '_' . $controller . '_index';
if ($module !== 'errors') {
$this->append($this->lang->t($module, $module), $module);
......
......@@ -20,9 +20,13 @@ class Context
*/
protected $modules;
/**
* @var URI
* @var Request
*/
protected $uri;
protected $request;
/**
* @var Router
*/
protected $router;
/**
* @var View
*/
......@@ -32,13 +36,15 @@ class Context
Auth $auth,
Lang $lang,
Modules $modules,
URI $uri,
Request $request,
Router $router,
View $view)
{
$this->auth = $auth;
$this->lang = $lang;
$this->modules = $modules;
$this->uri = $uri;
$this->request = $request;
$this->router = $router;
$this->view = $view;
}
......@@ -67,11 +73,19 @@ class Context
}
/**
* @return \ACP3\Core\URI
* @return \ACP3\Core\Request
*/
public function getRequest()
{
return $this->request;
}
/**
* @return \ACP3\Core\Router
*/
public function getUri()
public function getRouter()
{
return $this->uri;
return $this->router;
}
/**
......
......@@ -22,14 +22,15 @@ class Admin extends Frontend
Core\Auth $auth,
Core\Lang $lang,
Core\Modules $modules,
Core\URI $uri,
Core\Request $request,
Core\Router $router,
Core\View $view,
Core\Breadcrumb $breadcrumb,
Core\SEO $seo,
Core\Validate $validate,
Core\Session $session)
{
parent::__construct($auth, $lang, $modules, $uri, $view, $breadcrumb, $seo);
parent::__construct($auth, $lang, $modules, $request, $router, $view, $breadcrumb, $seo);
$this->validate = $validate;
$this->session = $session;
......
......@@ -22,12 +22,13 @@ class Frontend extends Core\Context
Core\Auth $auth,
Core\Lang $lang,
Core\Modules $modules,
Core\URI $uri,
Core\Request $request,
Core\Router $router,
Core\View $view,
Core\Breadcrumb $breadcrumb,
Core\SEO $seo)
{
parent::__construct($auth, $lang, $modules, $uri, $view);
parent::__construct($auth, $lang, $modules, $request, $router, $view);
$this->breadcrumb = $breadcrumb;
$this->seo = $seo;
......
......@@ -9,19 +9,24 @@ use ACP3\Core;
*/
class Alerts
{
/**
* @var \ACP3\Core\Functions
*/
private $functions;
/**
* @var Core\View
*/
private $view;
/**
* @var Core\URI
* @var Core\Request
*/
private $uri;
private $request;
public function __construct(Core\URI $uri, Core\View $view)
public function __construct(Core\Request $request, Core\View $view, Core\Functions $functions)
{
$this->uri = $uri;
$this->request = $request;
$this->view = $view;
$this->functions = $functions;
}
/**
......@@ -96,7 +101,7 @@ class Alerts
if (is_array($errors) === true) {
foreach (array_keys($errors) as $key) {
if (Core\Validate::isNumber($key) === false) {
if (is_numeric($key) === false) {
$hasNonIntegerKeys = true;
break;
}
......@@ -107,13 +112,13 @@ class Alerts
$this->view->assign('error_box', array('non_integer_keys' => $hasNonIntegerKeys, 'errors' => $errors));
$content = $this->view->fetchTemplate('system/error_box.tpl');
if ($this->uri->getIsAjax() === true) {
if ($this->request->getIsAjax() === true) {
$return = array(
'success' => false,
'content' => $content,
);
Core\Functions::outputJson($return);
$this->functions->outputJson($return);
}
return $content;
}
......
......@@ -10,17 +10,26 @@ use ACP3\Core;
class RedirectMessages
{
/**
* @var Core\URI
* @var Core\Request
*/
private $uri;
private $request;
/**
* @var Core\Redirect
*/
private $redirect;
/**
* @var Core\View
*/
private $view;
public function __construct(Core\URI $uri, Core\View $view)
public function __construct(
Core\Redirect $redirect,
Core\Request $request,
Core\View $view
)
{
$this->uri = $uri;
$this->redirect = $redirect;
$this->request = $request;
$this->view = $view;
}
......@@ -50,7 +59,12 @@ class RedirectMessages
'success' => is_int($success) ? true : (bool)$success,
'text' => $text
);
$this->uri->redirect($path);
if ($this->request->getIsAjax()) {
$this->redirect->ajax($path);
} else {
$this->redirect->temporary($path);
}
}
}
}
\ No newline at end of file
......@@ -16,18 +16,18 @@ class StringFormatter
protected $modules;
/**
* @var Core\URI
* @var Core\Router
*/
protected $uri;
protected $router;
/**
* @var Core\Validate
*/
protected $validate;
public function __construct(Core\Modules $modules, Core\URI $uri, Core\Validate $validate)
public function __construct(Core\Modules $modules, Core\Router $router, Core\Validate $validate)
{
$this->modules = $modules;
$this->uri = $uri;
$this->router = $router;
$this->validate = $validate;
}
......@@ -115,7 +115,7 @@ class StringFormatter
}
if ($this->modules->actionExists($path)) {
return '<a href="' . $this->uri->route($matches[6]) . '"';
return '<a href="' . $this->router->route($matches[6]) . '"';
} else {
return $matches[0];
}
......
......@@ -22,9 +22,17 @@ class TableOfContents
*/
protected $seo;
/**
* @var \ACP3\Core\URI
* @var \ACP3\Core\Request
*/
protected $uri;
protected $request;
/**
* @var \ACP3\Core\Router
*/
protected $router;
/**
* @var \ACP3\Core\Validate
*/
protected $validate;
/**
* @var \ACP3\Core\View
*/
......@@ -34,14 +42,18 @@ class TableOfContents
Core\Breadcrumb $breadcrumb,
Core\Lang $lang,
Core\SEO $seo,
Core\URI $uri,
Core\Request $request,
Core\Router $router,
Core\Validate $validate,
Core\View $view
)
{
$this->breadcrumb = $breadcrumb;
$this->lang = $lang;
$this->seo = $seo;
$this->uri = $uri;
$this->request = $request;
$this->router = $router;
$this->validate = $validate;
$this->view = $view;
}
......@@ -57,8 +69,8 @@ class TableOfContents
public function generateTOC(array $pages, $path = '', $titlesFromDb = false, $customUris = false)
{
if (!empty($pages)) {
$uri = $this->uri;
$path = empty($path) ? $uri->getUriWithoutPages() : $path;
$request = $this->request;
$path = empty($path) ? $request->getUriWithoutPages() : $path;
$toc = array();
$i = 0;
foreach ($pages as $page) {
......@@ -70,18 +82,18 @@ class TableOfContents
$toc[$i]['title'] = !empty($page['title']) ? $page['title'] : sprintf($this->lang->t('system', 'toc_page'), $pageNumber);
}
$toc[$i]['uri'] = $customUris === true ? $page['uri'] : $uri->route($path) . ($pageNumber > 1 ? 'page_' . $pageNumber . '/' : '');
$toc[$i]['uri'] = $customUris === true ? $page['uri'] : $this->router->route($path) . ($pageNumber > 1 ? 'page_' . $pageNumber . '/' : '');
$toc[$i]['selected'] = false;
if ($customUris === true) {
if ($page['uri'] === $uri->route($uri->query) ||
$uri->route($uri->query) === $uri->route($uri->mod . '/' . $uri->controller . '/' . $uri->file) && $i == 0
if ($page['uri'] === $this->router->route($request->query) ||
$this->router->route($request->query) === $this->router->route($request->mod . '/' . $request->controller . '/' . $request->file) && $i == 0
) {
$toc[$i]['selected'] = true;
$this->breadcrumb->setTitlePostfix($toc[$i]['title']);
}
} else {
if ((Core\Validate::isNumber($uri->page) === false && $i === 0) || $uri->page === $pageNumber) {
if (($this->validate->isNumber($request->page) === false && $i === 0) || $request->page === $pageNumber) {
$toc[$i]['selected'] = true;
$this->breadcrumb->setTitlePostfix($toc[$i]['title']);
}
......@@ -145,9 +157,9 @@ class TableOfContents
$matches = array();
preg_match_all($regex, $text, $matches);
$currentPage = Core\Validate::isNumber($this->uri->page) === true && $this->uri->page <= $c_pages ? $this->uri->page : 1;
$nextPage = !empty($pages[$currentPage]) ? $this->uri->route($path) . 'page_' . ($currentPage + 1) . '/' : '';
$previousPage = $currentPage > 1 ? $this->uri->route($path) . ($currentPage - 1 > 1 ? 'page_' . ($currentPage - 1) . '/' : '') : '';
$currentPage = $this->validate->isNumber($this->request->page) === true && $this->request->page <= $c_pages ? $this->request->page : 1;
$nextPage = !empty($pages[$currentPage]) ? $this->router->route($path) . 'page_' . ($currentPage + 1) . '/' : '';
$previousPage = $currentPage > 1 ? $this->router->route($path) . ($currentPage - 1 > 1 ? 'page_' . ($currentPage - 1) . '/' : '') : '';
if (!empty($nextPage)) {
$this->seo->setNextPage($nextPage);
......
......@@ -27,9 +27,13 @@ abstract class Controller
*/
protected $modules;
/**
* @var \ACP3\Core\URI
* @var \ACP3\Core\Request
*/
protected $uri;
protected $request;
/**
* @var \ACP3\Core\Router
*/
protected $router;
/**
* @var \ACP3\Core\View
*/
......@@ -71,7 +75,8 @@ abstract class Controller
{
$this->auth = $context->getAuth();
$this->lang = $context->getLang();
$this->uri = $context->getUri();
$this->request = $context->getRequest();
$this->router = $context->getRouter();
$this->view = $context->getView();
$this->modules = $context->getModules();
}
......
......@@ -34,8 +34,8 @@ abstract class Admin extends Core\Modules\Controller\Frontend
public function preDispatch()
{
if ($this->auth->isUser() === false) {
$redirectUri = base64_encode('acp/' . $this->uri->query);
$this->uri->redirect('users/index/login/redirect_' . $redirectUri);
$redirectUri = base64_encode('acp/' . $this->request->query);
$this->request->redirect('users/index/login/redirect_' . $redirectUri);
}
return parent::preDispatch();
......@@ -52,20 +52,20 @@ abstract class Admin extends Core\Modules\Controller\Frontend
{
if (isset($_POST['entries']) && is_array($_POST['entries']) === true) {
$entries = $_POST['entries'];
} elseif ($this->validate->deleteEntries($this->uri->entries) === true) {
$entries = $this->uri->entries;
} elseif ($this->validate->deleteEntries($this->request->entries) === true) {
$entries = $this->request->entries;
}
$alerts = $this->get('core.helpers.alerts');
if (!isset($entries)) {
$this->setContent($alerts->errorBox($this->lang->t('system', 'no_entries_selected')));
} elseif (is_array($entries) === true && $this->uri->action !== 'confirmed') {
} elseif (is_array($entries) === true && $this->request->action !== 'confirmed') {
$data = array(
'action' => 'confirmed',
'entries' => $entries
);
$confirmBox = $alerts->confirmBoxPost($this->lang->t('system', 'confirm_delete'), $data, $this->uri->route($moduleConfirmUrl), $this->uri->route($moduleIndexUrl));
$confirmBox = $alerts->confirmBoxPost($this->lang->t('system', 'confirm_delete'), $data, $this->router->route($moduleConfirmUrl), $this->router->route($moduleIndexUrl));
$this->setContent($confirmBox);
} else {
return is_array($entries) ? $entries : explode('|', $entries);
......@@ -76,7 +76,7 @@ abstract class Admin extends Core\Modules\Controller\Frontend
{
// Content-Template automatisch setzen
if ($this->getContentTemplate() === '') {
$this->setContentTemplate($this->uri->mod . '/Admin/' . $this->uri->controller . '.' . $this->uri->file . '.tpl');
$this->setContentTemplate($this->request->mod . '/Admin/' . $this->request->controller . '.' . $this->request->file . '.tpl');
}
parent::display();
......
......@@ -50,10 +50,10 @@ abstract class Frontend extends Core\Modules\Controller
{
// Aktuelle Datensatzposition bestimmen
if (!defined('POS')) {
define('POS', (int)$this->uri->page >= 1 ? (int)($this->uri->page - 1) * $this->auth->entries : 0);
define('POS', (int)$this->request->page >= 1 ? (int)($this->request->page - 1) * $this->auth->entries : 0);
}
$path = $this->uri->area . '/' . $this->uri->mod . '/' . $this->uri->controller . '/' . $this->uri->file;
$path = $this->request->area . '/' . $this->request->mod . '/' . $this->request->controller . '/' . $this->request->file;
if ($this->modules->hasPermission($path) === false) {
throw new Core\Exceptions\UnauthorizedAccess();
......@@ -67,7 +67,7 @@ abstract class Frontend extends Core\Modules\Controller
$this->view->assign('DESIGN_PATH', DESIGN_PATH);
$this->view->assign('DESIGN_PATH_ABSOLUTE', DESIGN_PATH_ABSOLUTE);
$this->view->assign('UA_IS_MOBILE', Core\Functions::isMobileBrowser());
$this->view->assign('IN_ADM', $this->uri->area === 'admin');
$this->view->assign('IN_ADM', $this->request->area === 'admin');
$this->view->assign('LANG_DIRECTION', $this->lang->getDirection());
$this->view->assign('LANG', $this->lang->getLanguage2Characters());
......@@ -106,7 +106,7 @@ abstract class Frontend extends Core\Modules\Controller
if ($this->getNoOutput() === false) {
// Content-Template automatisch setzen
if ($this->getContentTemplate() === '') {
$this->setContentTemplate($this->uri->mod . '/' . $this->uri->controller . '.' . $this->uri->file . '.tpl');
$this->setContentTemplate($this->request->mod . '/' . $this->request->controller . '.' . $this->request->file . '.tpl');
}
if ($this->getContent() === '') {
......@@ -124,7 +124,7 @@ abstract class Frontend extends Core\Modules\Controller
$this->view->assign('META', $this->seo->getMetaTags());
$this->view->assign('CONTENT', $this->getContent() . $this->getContentAppend());
if ($this->uri->getIsAjax() === true) {
if ($this->request->getIsAjax() === true) {
if ($this->layout !== 'layout.tpl') {
$file = $this->layout;
} else {
......@@ -154,4 +154,12 @@ abstract class Frontend extends Core\Modules\Controller
return $this->redirectMessages;
}
/**
* @return Core\Redirect
*/
public function redirect()
{
return $this->get('core.redirect');
}
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ class Pagination
*/
protected $seo;
/**
* @var URI
* @var Request
*/
protected $uri;
/**
......@@ -69,7 +69,7 @@ class Pagination
Breadcrumb $breadcrumb,
Lang $lang,
SEO $seo,
URI $uri,
Request $uri,
View $view,
$totalResults)
{
......
<?php
namespace ACP3\Core;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Class Redirect
* @package ACP3\Core
*/
class Redirect
{
/**
* @var string
*/
protected $protocol = '';
/**
* @var string
*/
protected $hostname = '';
/**
* @var Request
*/
protected $request;
/**
* @var Router
*/
protected $router;
public function __construct(Request $request, Router $router)
{
$this->request = $request;
$this->router = $router;
$this->protocol = empty($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) === 'off' ? 'http://' : 'https://';
$this->hostname = $_SERVER['HTTP_HOST'];
}
/**
* @param $url
*/
public function toNewPage($url)
{
$response = new RedirectResponse($url);
$response->send();
}
/**
* Outputs a JSON response with redirect url
* @param $path
*/
public function ajax($path)
{
$url = $this->protocol . $this->hostname . $this->router->route($path);
if ($this->request->getIsAjax() === true) {
$return = array(
'redirect_url' => $url
);
$response = new JsonResponse($return);
$response->send();
}
}
/**
* Executes a temporary redirect
* @param $path
*/
public function