TYPO3 CMS  TYPO3_6-2
UriBuilder.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is part of the TYPO3 project - inspiring people to share! *
6  * *
7  * TYPO3 is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU General Public License version 2 as published by *
9  * the Free Software Foundation. *
10  * *
11  * This script is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14  * Public License for more details. *
15  * */
17 
23 class UriBuilder {
24 
30 
35  protected $extensionService;
36 
42  protected $contentObject;
43 
47  protected $request;
48 
52  protected $arguments = array();
53 
59  protected $lastArguments = array();
60 
64  protected $section = '';
65 
69  protected $createAbsoluteUri = FALSE;
70 
74  protected $absoluteUriScheme = NULL;
75 
79  protected $addQueryString = FALSE;
80 
84  protected $addQueryStringMethod = NULL;
85 
90 
94  protected $linkAccessRestrictedPages = FALSE;
95 
99  protected $targetPageUid = NULL;
100 
104  protected $targetPageType = 0;
105 
109  protected $noCache = FALSE;
110 
114  protected $useCacheHash = TRUE;
115 
119  protected $format = '';
120 
124  protected $argumentPrefix = NULL;
125 
131 
137  public function initializeObject() {
138  $this->contentObject = $this->configurationManager->getContentObject();
139  }
140 
147  public function setRequest(\TYPO3\CMS\Extbase\Mvc\Request $request) {
148  $this->request = $request;
149  return $this;
150  }
151 
155  public function getRequest() {
156  return $this->request;
157  }
158 
168  public function setArguments(array $arguments) {
169  $this->arguments = $arguments;
170  return $this;
171  }
172 
177  public function getArguments() {
178  return $this->arguments;
179  }
180 
188  public function setSection($section) {
189  $this->section = $section;
190  return $this;
191  }
192 
197  public function getSection() {
198  return $this->section;
199  }
200 
208  public function setFormat($format) {
209  $this->format = $format;
210  return $this;
211  }
212 
217  public function getFormat() {
218  return $this->format;
219  }
220 
229  $this->createAbsoluteUri = $createAbsoluteUri;
230  return $this;
231  }
232 
237  public function getCreateAbsoluteUri() {
239  }
240 
244  public function getAbsoluteUriScheme() {
246  }
247 
255  $this->absoluteUriScheme = $absoluteUriScheme;
256  return $this;
257  }
258 
268  $this->addQueryString = (boolean) $addQueryString;
269  return $this;
270  }
271 
276  public function getAddQueryString() {
277  return $this->addQueryString;
278  }
279 
290  $this->addQueryStringMethod = $addQueryStringMethod;
291  return $this;
292  }
293 
298  public function getAddQueryStringMethod() {
299  return (string)$this->addQueryStringMethod;
300  }
301 
313  $this->argumentsToBeExcludedFromQueryString = $argumentsToBeExcludedFromQueryString;
314  return $this;
315  }
316 
323  }
324 
332  $this->argumentPrefix = (string) $argumentPrefix;
333  return $this;
334  }
335 
339  public function getArgumentPrefix() {
340  return $this->argumentPrefix;
341  }
342 
351  $this->linkAccessRestrictedPages = (boolean) $linkAccessRestrictedPages;
352  return $this;
353  }
354 
359  public function getLinkAccessRestrictedPages() {
361  }
362 
370  public function setTargetPageUid($targetPageUid) {
371  $this->targetPageUid = $targetPageUid;
372  return $this;
373  }
374 
381  public function getTargetPageUid() {
382  return $this->targetPageUid;
383  }
384 
393  $this->targetPageType = (int)$targetPageType;
394  return $this;
395  }
396 
400  public function getTargetPageType() {
401  return $this->targetPageType;
402  }
403 
412  public function setNoCache($noCache) {
413  $this->noCache = (boolean) $noCache;
414  return $this;
415  }
416 
421  public function getNoCache() {
422  return $this->noCache;
423  }
424 
433  public function setUseCacheHash($useCacheHash) {
434  $this->useCacheHash = (boolean) $useCacheHash;
435  return $this;
436  }
437 
442  public function getUseCacheHash() {
443  return $this->useCacheHash;
444  }
445 
452  public function getLastArguments() {
453  return $this->lastArguments;
454  }
455 
462  public function reset() {
463  $this->arguments = array();
464  $this->section = '';
465  $this->format = '';
466  $this->createAbsoluteUri = FALSE;
467  $this->addQueryString = FALSE;
468  $this->addQueryStringMethod = NULL;
469  $this->argumentsToBeExcludedFromQueryString = array();
470  $this->linkAccessRestrictedPages = FALSE;
471  $this->targetPageUid = NULL;
472  $this->targetPageType = 0;
473  $this->noCache = FALSE;
474  $this->useCacheHash = TRUE;
475  $this->argumentPrefix = NULL;
476  return $this;
477  }
478 
492  public function uriFor($actionName = NULL, $controllerArguments = array(), $controllerName = NULL, $extensionName = NULL, $pluginName = NULL) {
493  if ($actionName !== NULL) {
494  $controllerArguments['action'] = $actionName;
495  }
496  if ($controllerName !== NULL) {
497  $controllerArguments['controller'] = $controllerName;
498  } else {
499  $controllerArguments['controller'] = $this->request->getControllerName();
500  }
501  if ($extensionName === NULL) {
502  $extensionName = $this->request->getControllerExtensionName();
503  }
504  if ($pluginName === NULL && $this->environmentService->isEnvironmentInFrontendMode()) {
505  $pluginName = $this->extensionService->getPluginNameByAction($extensionName, $controllerArguments['controller'], $controllerArguments['action']);
506  }
507  if ($pluginName === NULL) {
508  $pluginName = $this->request->getPluginName();
509  }
510  if ($this->environmentService->isEnvironmentInFrontendMode() && $this->configurationManager->isFeatureEnabled('skipDefaultArguments')) {
511  $controllerArguments = $this->removeDefaultControllerAndAction($controllerArguments, $extensionName, $pluginName);
512  }
513  if ($this->targetPageUid === NULL && $this->environmentService->isEnvironmentInFrontendMode()) {
514  $this->targetPageUid = $this->extensionService->getTargetPidByPlugin($extensionName, $pluginName);
515  }
516  if ($this->format !== '') {
517  $controllerArguments['format'] = $this->format;
518  }
519  if ($this->argumentPrefix !== NULL) {
520  $prefixedControllerArguments = array($this->argumentPrefix => $controllerArguments);
521  } else {
522  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
523  $prefixedControllerArguments = array($pluginNamespace => $controllerArguments);
524  }
525  \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->arguments, $prefixedControllerArguments);
526  return $this->build();
527  }
528 
540  protected function removeDefaultControllerAndAction(array $controllerArguments, $extensionName, $pluginName) {
541  $defaultControllerName = $this->extensionService->getDefaultControllerNameByPlugin($extensionName, $pluginName);
542  if (isset($controllerArguments['action'])) {
543  $defaultActionName = $this->extensionService->getDefaultActionNameByPluginAndController($extensionName, $pluginName, $controllerArguments['controller']);
544  if ($controllerArguments['action'] === $defaultActionName) {
545  unset($controllerArguments['action']);
546  }
547  }
548  if ($controllerArguments['controller'] === $defaultControllerName) {
549  unset($controllerArguments['controller']);
550  }
551  return $controllerArguments;
552  }
553 
563  public function build() {
564  if ($this->environmentService->isEnvironmentInBackendMode()) {
565  return $this->buildBackendUri();
566  } else {
567  return $this->buildFrontendUri();
568  }
569  }
570 
579  public function buildBackendUri() {
580  if ($this->addQueryString === TRUE) {
581  if ($this->addQueryStringMethod) {
582  switch ($this->addQueryStringMethod) {
583  case 'GET':
585  break;
586  case 'POST':
588  break;
589  case 'GET,POST':
590  $arguments = array_replace_recursive(\TYPO3\CMS\Core\Utility\GeneralUtility::_GET(), \TYPO3\CMS\Core\Utility\GeneralUtility::_POST());
591  break;
592  case 'POST,GET':
593  $arguments = array_replace_recursive(\TYPO3\CMS\Core\Utility\GeneralUtility::_POST(), \TYPO3\CMS\Core\Utility\GeneralUtility::_GET());
594  break;
595  default:
596  $arguments = \TYPO3\CMS\Core\Utility\GeneralUtility::explodeUrl2Array(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('QUERY_STRING'), TRUE);
597  }
598  } else {
600  }
601  foreach ($this->argumentsToBeExcludedFromQueryString as $argumentToBeExcluded) {
602  $argumentToBeExcluded = \TYPO3\CMS\Core\Utility\GeneralUtility::explodeUrl2Array($argumentToBeExcluded, TRUE);
604  }
605  } else {
606  $arguments = array(
607  'M' => \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('M'),
608  'id' => \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id')
609  );
610  }
613  $this->lastArguments = $arguments;
614  $moduleName = $arguments['M'];
615  unset($arguments['M'], $arguments['moduleToken']);
617  if ($this->section !== '') {
618  $uri .= '#' . $this->section;
619  }
620  if ($this->createAbsoluteUri === TRUE) {
621  $uri = $this->request->getBaseUri() . $uri;
622  }
623  return $uri;
624  }
625 
632  public function buildFrontendUri() {
633  $typolinkConfiguration = $this->buildTypolinkConfiguration();
634  if ($this->createAbsoluteUri === TRUE) {
635  $typolinkConfiguration['forceAbsoluteUrl'] = TRUE;
636  if ($this->absoluteUriScheme !== NULL) {
637  $typolinkConfiguration['forceAbsoluteUrl.']['scheme'] = $this->absoluteUriScheme;
638  }
639  }
640  $uri = $this->contentObject->typoLink_URL($typolinkConfiguration);
641  return $uri;
642  }
643 
650  protected function buildTypolinkConfiguration() {
651  $typolinkConfiguration = array();
652  $typolinkConfiguration['parameter'] = $this->targetPageUid !== NULL ? $this->targetPageUid : $GLOBALS['TSFE']->id;
653  if ($this->targetPageType !== 0) {
654  $typolinkConfiguration['parameter'] .= ',' . $this->targetPageType;
655  } elseif ($this->format !== '') {
656  $targetPageType = $this->extensionService->getTargetPageTypeByFormat($this->request->getControllerExtensionName(), $this->format);
657  $typolinkConfiguration['parameter'] .= ',' . $targetPageType;
658  }
659  if (count($this->arguments) > 0) {
660  $arguments = $this->convertDomainObjectsToIdentityArrays($this->arguments);
661  $this->lastArguments = $arguments;
662  $typolinkConfiguration['additionalParams'] = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl(NULL, $arguments);
663  }
664  if ($this->addQueryString === TRUE) {
665  $typolinkConfiguration['addQueryString'] = 1;
666  if (count($this->argumentsToBeExcludedFromQueryString) > 0) {
667  $typolinkConfiguration['addQueryString.'] = array(
668  'exclude' => implode(',', $this->argumentsToBeExcludedFromQueryString)
669  );
670  }
671  if ($this->addQueryStringMethod) {
672  $typolinkConfiguration['addQueryString.']['method'] = $this->addQueryStringMethod;
673  }
674  }
675  if ($this->noCache === TRUE) {
676  $typolinkConfiguration['no_cache'] = 1;
677  } elseif ($this->useCacheHash) {
678  $typolinkConfiguration['useCacheHash'] = 1;
679  }
680  if ($this->section !== '') {
681  $typolinkConfiguration['section'] = $this->section;
682  }
683  if ($this->linkAccessRestrictedPages === TRUE) {
684  $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
685  }
686  return $typolinkConfiguration;
687  }
688 
698  foreach ($arguments as $argumentKey => $argumentValue) {
699  // if we have a LazyLoadingProxy here, make sure to get the real instance for further processing
700  if ($argumentValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
701  $argumentValue = $argumentValue->_loadRealInstance();
702  // also update the value in the arguments array, because the lazyLoaded object could be
703  // hidden and thus the $argumentValue would be NULL.
704  $arguments[$argumentKey] = $argumentValue;
705  }
706  if ($argumentValue instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject) {
707  if ($argumentValue->getUid() !== NULL) {
708  $arguments[$argumentKey] = $argumentValue->getUid();
709  } elseif ($argumentValue instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject) {
710  $arguments[$argumentKey] = $this->convertTransientObjectToArray($argumentValue);
711  } else {
712  throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException('Could not serialize Domain Object ' . get_class($argumentValue) . '. It is neither an Entity with identity properties set, nor a Value Object.', 1260881688);
713  }
714  } elseif (is_array($argumentValue)) {
715  $arguments[$argumentKey] = $this->convertDomainObjectsToIdentityArrays($argumentValue);
716  }
717  }
718  return $arguments;
719  }
720 
728  public function convertTransientObjectToArray(\TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject $object) {
729  $result = array();
730  foreach ($object->_getProperties() as $propertyName => $propertyValue) {
731  if ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject) {
732  if ($propertyValue->getUid() !== NULL) {
733  $result[$propertyName] = $propertyValue->getUid();
734  } else {
735  $result[$propertyName] = $this->convertTransientObjectToArray($propertyValue);
736  }
737  } elseif (is_array($propertyValue)) {
738  $result[$propertyName] = $this->convertDomainObjectsToIdentityArrays($propertyValue);
739  } else {
740  $result[$propertyName] = $propertyValue;
741  }
742  }
743  return $result;
744  }
745 
746 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)
static explodeUrl2Array($string, $multidim=FALSE)
removeDefaultControllerAndAction(array $controllerArguments, $extensionName, $pluginName)
Definition: UriBuilder.php:540
convertDomainObjectsToIdentityArrays(array $arguments)
Definition: UriBuilder.php:697
$moduleName
Definition: mod.php:22
setArgumentsToBeExcludedFromQueryString(array $argumentsToBeExcludedFromQueryString)
Definition: UriBuilder.php:312
setLinkAccessRestrictedPages($linkAccessRestrictedPages)
Definition: UriBuilder.php:350
uriFor($actionName=NULL, $controllerArguments=array(), $controllerName=NULL, $extensionName=NULL, $pluginName=NULL)
Definition: UriBuilder.php:492
convertTransientObjectToArray(\TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject $object)
Definition: UriBuilder.php:728
static getModuleUrl($moduleName, $urlParameters=array(), $backPathOverride=FALSE, $returnAbsoluteUrl=FALSE)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
setRequest(\TYPO3\CMS\Extbase\Mvc\Request $request)
Definition: UriBuilder.php:147
static implodeArrayForUrl($name, array $theArray, $str='', $skipBlank=FALSE, $rawurlencodeParamName=FALSE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static arrayDiffAssocRecursive(array $array1, array $array2)
setAddQueryStringMethod($addQueryStringMethod)
Definition: UriBuilder.php:289