Skip to content

Instantly share code, notes, and snippets.

@radmiraal
Created March 28, 2013 15:59
Show Gist options
  • Save radmiraal/5264376 to your computer and use it in GitHub Desktop.
Save radmiraal/5264376 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\Company\Mvc\Routing;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Utility\Arrays;
/**
* @Flow\Scope("singleton")
*/
class UriBuilder extends \TYPO3\Flow\Mvc\Routing\UriBuilder {
/**
* @var array
*/
protected $settings;
/**
* @param array $settings
*/
public function injectSettings(array $settings) {
$this->settings = $settings;
}
/**
* @Flow\Inject
* @var \TYPO3\Flow\Mvc\Routing\RouterInterface
*/
protected $router;
/**
* @var \TYPO3\Flow\Configuration\ConfigurationManager
* @Flow\Inject
*/
protected $configurationManager;
/**
* Initialize the uriBuilder and set the routes configuration
* in the router if this is still needed.
* @return void
*/
public function initializeObject() {
if ($this->router->getRoutes() === array()) {
$this->router->setRoutesConfiguration(
$this->configurationManager->getConfiguration(\TYPO3\Flow\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES)
);
}
}
/**
* Creates an URI used for linking to an Controller action.
*
* @param string $actionName Name of the action to be called
* @param array $controllerArguments Additional query parameters. Will be merged with $this->arguments.
* @param string $controllerName Name of the target controller. If not set, current ControllerName is used.
* @param string $packageKey Name of the target package. If not set, current Package is used.
* @param string $subPackageKey Name of the target SubPackage. If not set, current SubPackage is used.
* @return string the rendered URI
* @api
* @see build()
* @throws \TYPO3\Flow\Mvc\Routing\Exception\MissingActionNameException if $actionName parameter is empty
*/
public function uriFor($actionName, array $controllerArguments, $controllerName, $packageKey, $subPackageKey = NULL) {
if ($actionName === NULL || $actionName === '') {
throw new \TYPO3\Flow\Mvc\Routing\Exception\MissingActionNameException('The URI Builder could not build a URI linking to an action controller because no action name was specified. Please check the stack trace to see which code or template was requesting the link and check the arguments passed to the URI Builder.', 1354629891);
}
$controllerArguments['@action'] = strtolower($actionName);
$controllerArguments['@controller'] = strtolower($controllerName);
$controllerArguments['@package'] = strtolower($packageKey);
if ($subPackageKey !== NULL) {
$controllerArguments['@subpackage'] = strtolower($subPackageKey);
}
if ($this->format !== NULL && $this->format !== '') {
$controllerArguments['@format'] = $this->format;
}
return $this->build($controllerArguments);
}
/**
* Builds the URI
*
* @param array $arguments optional URI arguments. Will be merged with $this->arguments with precedence to $arguments
* @return string The URI
* @api
*/
public function build(array $arguments = array()) {
$arguments = Arrays::arrayMergeRecursiveOverrule($this->arguments, $arguments);
$uri = $this->router->resolve($arguments);
$this->lastArguments = $arguments;
if ($this->createAbsoluteUri === TRUE && isset($this->settings['baseUrl'])) {
$uri = $this->settings['baseUrl'] . $uri;
}
if ($this->section !== '') {
$uri .= '#' . $this->section;
}
return $uri;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment