Skip to content

Instantly share code, notes, and snippets.

@aertmann
Created January 5, 2012 13:05
Show Gist options
  • Save aertmann/1565182 to your computer and use it in GitHub Desktop.
Save aertmann/1565182 to your computer and use it in GitHub Desktop.
<?php
namespace TYPO3\TYPO3\Routing;
/* *
* This script belongs to the FLOW3 package "TYPO3". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License, either version 3 of the *
* License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\FLOW3\Annotations as FLOW3;
/**
* A route part handler for finding nodes specifically in the website's frontend.
*
* @FLOW3\Scope("singleton")
*/
class BackendModuleRoutePartHandler extends \TYPO3\FLOW3\MVC\Web\Routing\DynamicRoutePart {
const MATCHRESULT_FOUND = TRUE;
const MATCHRESULT_NOSUCHMODULE = -1;
/**
* @var \TYPO3\FLOW3\Configuration\ConfigurationManager
* @FLOW3\Inject
*/
protected $configurationManager;
/**
* @param string $value
* @return boolean|integer
*/
protected function matchValue($value) {
$settings = $this->configurationManager->getConfiguration(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS);
$config = $settings['TYPO3']['TYPO3']['modules'];
$level = 1;
$requestParts = explode('/', $value);
$pathParts = array();
foreach ($requestParts as $requestPart) {
array_push($pathParts, $requestPart);
if (isset($config[$requestPart])) {
$config = $config[$requestPart];
} elseif (isset($config['submodules']) && isset($config['submodules'][$requestPart])) {
$config = $config['submodules'][$requestPart];
} else {
if (count($requestParts) !== $level) {
return self::MATCHRESULT_NOSUCHMODULE;
}
}
$level++;
}
$this->value = implode('/', $pathParts);
return TRUE;
}
/**
* @param string $requestPath
* @return string
*/
protected function findValueToMatch($requestPath) {
return $requestPath;
}
/**
* @param array $routeValues
* @return array
*/
protected function findValueToResolve(array &$routeValues) {
$pathParts = explode('/', \TYPO3\FLOW3\Utility\Arrays::getValueByPath($routeValues, $this->name));
$subRequestRouteValues = \TYPO3\FLOW3\Utility\Arrays::getValueByPath($routeValues, 'mod');
if ($subRequestRouteValues) {
$routeValues = \TYPO3\FLOW3\Utility\Arrays::unsetValueByPath($routeValues, 'mod.@package');
$subController = \TYPO3\FLOW3\Utility\Arrays::getValueByPath($routeValues, 'mod.@controller');
if ($subController !== NULL) {
$subControllerParts = explode('\\', $subController);
if (end($subControllerParts) !== 'standard' && end($pathParts) !== end($subControllerParts)) {
array_pop($pathParts);
}
$routeValues = \TYPO3\FLOW3\Utility\Arrays::unsetValueByPath($routeValues, 'mod.@controller');
}
$subModule = \TYPO3\FLOW3\Utility\Arrays::getValueByPath($subRequestRouteValues, $this->name);
if ($subModule !== NULL) {
$routeValues = \TYPO3\FLOW3\Utility\Arrays::unsetValueByPath($routeValues, sprintf('mod.%s', $this->name));
$pathParts = explode('/', $subModule);
}
$subAction = \TYPO3\FLOW3\Utility\Arrays::getValueByPath($subRequestRouteValues, '@action');
if ($subAction !== NULL) {
$routeValues = \TYPO3\FLOW3\Utility\Arrays::unsetValueByPath($routeValues, 'mod.@action');
array_push($pathParts, $subAction);
}
}
return implode('/', $pathParts);
}
}
?>
<?php
namespace TYPO3\FLOW3\MVC\Web\Routing;
/* *
* This script belongs to the FLOW3 framework. *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\FLOW3\Annotations as FLOW3;
/**
* Dynamic Route Part
*
* @api
*/
class DynamicRoutePart extends \TYPO3\FLOW3\MVC\Web\Routing\AbstractRoutePart implements \TYPO3\FLOW3\MVC\Web\Routing\DynamicRoutePartInterface {
/**
* @var \TYPO3\FLOW3\Persistence\PersistenceManagerInterface
* @FLOW3\Inject
*/
protected $persistenceManager;
/**
* The split string represents the end of a Dynamic Route Part.
* If it is empty, Route Part will be equal to the remaining request path.
*
* @var string
*/
protected $splitString = '';
/**
* Sets split string of the Route Part.
*
* @param string $splitString
* @return void
* @api
*/
public function setSplitString($splitString) {
$this->splitString = $splitString;
}
/**
* Checks whether this Dynamic Route Part corresponds to the given $routePath.
*
* On successful match this method sets $this->value to the corresponding uriPart
* and shortens $routePath respectively.
*
* @param string $routePath The request path to be matched - without query parameters, host and fragment.
* @return boolean TRUE if Route Part matched $routePath, otherwise FALSE.
*/
final public function match(&$routePath) {
$this->value = NULL;
if ($this->name === NULL || $this->name === '') {
return FALSE;
}
$valueToMatch = $this->findValueToMatch($routePath);
$matchResult = $this->matchValue($valueToMatch);
if ($matchResult !== TRUE) {
return $matchResult;
}
$this->removeMatchingPortionFromRequestPath($routePath, $valueToMatch);
return TRUE;
}
/**
* Returns the first part of $routePath.
* If a split string is set, only the first part of the value until location of the splitString is returned.
* This method can be overridden by custom RoutePartHandlers to implement custom matching mechanisms.
*
* @param string $routePath The request path to be matched
* @return string value to match, or an empty string if $routePath is empty or split string was not found
* @api
*/
protected function findValueToMatch($routePath) {
if (!isset($routePath) || $routePath === '' || $routePath[0] === '/') {
return '';
}
$valueToMatch = $routePath;
if ($this->splitString !== '') {
$splitStringPosition = strpos($valueToMatch, $this->splitString);
if ($splitStringPosition !== FALSE) {
$valueToMatch = substr($valueToMatch, 0, $splitStringPosition);
}
}
if (strpos($valueToMatch, '/') !== FALSE) {
return '';
}
return $valueToMatch;
}
/**
* Checks, whether given value can be matched.
* In the case of default Dynamic Route Parts a value matches when it's not empty.
* This method can be overridden by custom RoutePartHandlers to implement custom matching mechanisms.
*
* @param string $value value to match
* @return boolean TRUE if value could be matched successfully, otherwise FALSE.
* @api
*/
protected function matchValue($value) {
if ($value === NULL || $value === '') {
return FALSE;
}
$this->value = $value;
return TRUE;
}
/**
* Removes matching part from $routePath.
* This method can be overridden by custom RoutePartHandlers to implement custom matching mechanisms.
*
* @param string $routePath The request path to be matched
* @param string $valueToMatch The matching value
* @return void
* @api
*/
protected function removeMatchingPortionFromRequestPath(&$routePath, $valueToMatch) {
if ($valueToMatch !== NULL && $valueToMatch !== '') {
$routePath = substr($routePath, strlen($valueToMatch));
}
}
/**
* Checks whether $routeValues contains elements which correspond to this Dynamic Route Part.
* If a corresponding element is found in $routeValues, this element is removed from the array.
*
* @param array $routeValues An array with key/value pairs to be resolved by Dynamic Route Parts.
* @return boolean TRUE if current Route Part could be resolved, otherwise FALSE
*/
final public function resolve(array &$routeValues) {
$this->value = NULL;
if ($this->name === NULL || $this->name === '') {
return FALSE;
}
$valueToResolve = $this->findValueToResolve($routeValues);
if (!$this->resolveValue($valueToResolve)) {
return FALSE;
}
if ($this->lowerCase) {
$this->value = strtolower($this->value);
}
$routeValues = \TYPO3\FLOW3\Utility\Arrays::unsetValueByPath($routeValues, $this->name);
return TRUE;
}
/**
* Returns the route value of the current route part.
* This method can be overridden by custom RoutePartHandlers to implement custom resolving mechanisms.
*
* @param array $routeValues An array with key/value pairs to be resolved by Dynamic Route Parts.
* @return string|array value to resolve.
* @api
*/
protected function findValueToResolve(array &$routeValues) {
return \TYPO3\FLOW3\Reflection\ObjectAccess::getPropertyPath($routeValues, $this->name);
}
/**
* Checks, whether given value can be resolved and if so, sets $this->value to the resolved value.
* If $value is empty, this method checks whether a default value exists.
* This method can be overridden by custom RoutePartHandlers to implement custom resolving mechanisms.
*
* @param string $value value to resolve
* @return boolean TRUE if value could be resolved successfully, otherwise FALSE.
* @api
*/
protected function resolveValue($value) {
if ($value === NULL) {
return FALSE;
}
if (is_object($value)) {
$value = $this->persistenceManager->getIdentifierByObject($value);
if ($value === NULL || !is_string($value)) {
return FALSE;
}
}
$this->value = (string)$value;
if ($this->lowerCase) {
$this->value = strtolower($this->value);
}
return TRUE;
}
}
?>
name: 'Backend - Modules'
uriPattern: 'typo3/{module}'
defaults:
'@package': 'TYPO3.TYPO3'
'@controller': 'Backend\Module'
'@action': 'index'
'@format': 'html'
appendExceedingArguments: true
routeParts:
module:
handler: TYPO3\TYPO3\Routing\BackendModuleRoutePartHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment