TYPO3 CMS  TYPO3_8-7
UriBuilder.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
28 {
33 
37  protected $extensionService;
38 
44  protected $contentObject;
45 
49  protected $request;
50 
54  protected $arguments = [];
55 
61  protected $lastArguments = [];
62 
66  protected $section = '';
67 
71  protected $createAbsoluteUri = false;
72 
76  protected $absoluteUriScheme = null;
77 
81  protected $addQueryString = false;
82 
86  protected $addQueryStringMethod = null;
87 
92 
96  protected $linkAccessRestrictedPages = false;
97 
101  protected $targetPageUid = null;
102 
106  protected $targetPageType = 0;
107 
111  protected $noCache = false;
112 
116  protected $useCacheHash = true;
117 
121  protected $format = '';
122 
126  protected $argumentPrefix = null;
127 
132 
137  {
138  $this->configurationManager = $configurationManager;
139  }
140 
144  public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
145  {
146  $this->extensionService = $extensionService;
147  }
148 
152  public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
153  {
154  $this->environmentService = $environmentService;
155  }
156 
160  public function initializeObject()
161  {
162  $this->contentObject = $this->configurationManager->getContentObject();
163  }
164 
171  public function setRequest(Request $request)
172  {
173  $this->request = $request;
174  return $this;
175  }
176 
180  public function getRequest()
181  {
182  return $this->request;
183  }
184 
194  public function setArguments(array $arguments)
195  {
196  $this->arguments = $arguments;
197  return $this;
198  }
199 
204  public function getArguments()
205  {
206  return $this->arguments;
207  }
208 
216  public function setSection($section)
217  {
218  $this->section = $section;
219  return $this;
220  }
221 
226  public function getSection()
227  {
228  return $this->section;
229  }
230 
238  public function setFormat($format)
239  {
240  $this->format = $format;
241  return $this;
242  }
243 
248  public function getFormat()
249  {
250  return $this->format;
251  }
252 
261  {
262  $this->createAbsoluteUri = $createAbsoluteUri;
263  return $this;
264  }
265 
270  public function getCreateAbsoluteUri()
271  {
273  }
274 
278  public function getAbsoluteUriScheme()
279  {
281  }
282 
290  {
291  $this->absoluteUriScheme = $absoluteUriScheme;
292  return $this;
293  }
294 
304  {
305  $this->addQueryString = (bool)$addQueryString;
306  return $this;
307  }
308 
313  public function getAddQueryString()
314  {
315  return $this->addQueryString;
316  }
317 
328  {
329  $this->addQueryStringMethod = $addQueryStringMethod;
330  return $this;
331  }
332 
337  public function getAddQueryStringMethod()
338  {
339  return (string)$this->addQueryStringMethod;
340  }
341 
353  {
354  $this->argumentsToBeExcludedFromQueryString = $argumentsToBeExcludedFromQueryString;
355  return $this;
356  }
357 
363  {
365  }
366 
374  {
375  $this->argumentPrefix = (string)$argumentPrefix;
376  return $this;
377  }
378 
382  public function getArgumentPrefix()
383  {
384  return $this->argumentPrefix;
385  }
386 
395  {
396  $this->linkAccessRestrictedPages = (bool)$linkAccessRestrictedPages;
397  return $this;
398  }
399 
405  {
407  }
408 
417  {
418  $this->targetPageUid = $targetPageUid;
419  return $this;
420  }
421 
428  public function getTargetPageUid()
429  {
430  return $this->targetPageUid;
431  }
432 
441  {
442  $this->targetPageType = (int)$targetPageType;
443  return $this;
444  }
445 
449  public function getTargetPageType()
450  {
451  return $this->targetPageType;
452  }
453 
462  public function setNoCache($noCache)
463  {
464  $this->noCache = (bool)$noCache;
465  return $this;
466  }
467 
472  public function getNoCache()
473  {
474  return $this->noCache;
475  }
476 
486  {
487  $this->useCacheHash = (bool)$useCacheHash;
488  return $this;
489  }
490 
495  public function getUseCacheHash()
496  {
497  return $this->useCacheHash;
498  }
499 
506  public function getLastArguments()
507  {
508  return $this->lastArguments;
509  }
510 
517  public function reset()
518  {
519  $this->arguments = [];
520  $this->section = '';
521  $this->format = '';
522  $this->createAbsoluteUri = false;
523  $this->addQueryString = false;
524  $this->addQueryStringMethod = null;
525  $this->argumentsToBeExcludedFromQueryString = [];
526  $this->linkAccessRestrictedPages = false;
527  $this->targetPageUid = null;
528  $this->targetPageType = 0;
529  $this->noCache = false;
530  $this->useCacheHash = true;
531  $this->argumentPrefix = null;
532  return $this;
533  }
534 
548  public function uriFor($actionName = null, $controllerArguments = [], $controllerName = null, $extensionName = null, $pluginName = null)
549  {
550  if ($actionName !== null) {
551  $controllerArguments['action'] = $actionName;
552  }
553  if ($controllerName !== null) {
554  $controllerArguments['controller'] = $controllerName;
555  } else {
556  $controllerArguments['controller'] = $this->request->getControllerName();
557  }
558  if ($extensionName === null) {
559  $extensionName = $this->request->getControllerExtensionName();
560  }
561  if ($pluginName === null && $this->environmentService->isEnvironmentInFrontendMode()) {
562  $pluginName = $this->extensionService->getPluginNameByAction($extensionName, $controllerArguments['controller'], $controllerArguments['action']);
563  }
564  if ($pluginName === null) {
565  $pluginName = $this->request->getPluginName();
566  }
567  if ($this->environmentService->isEnvironmentInFrontendMode() && $this->configurationManager->isFeatureEnabled('skipDefaultArguments')) {
568  $controllerArguments = $this->removeDefaultControllerAndAction($controllerArguments, $extensionName, $pluginName);
569  }
570  if ($this->targetPageUid === null && $this->environmentService->isEnvironmentInFrontendMode()) {
571  $this->targetPageUid = $this->extensionService->getTargetPidByPlugin($extensionName, $pluginName);
572  }
573  if ($this->format !== '') {
574  $controllerArguments['format'] = $this->format;
575  }
576  if ($this->argumentPrefix !== null) {
577  $prefixedControllerArguments = [$this->argumentPrefix => $controllerArguments];
578  } else {
579  $pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
580  $prefixedControllerArguments = [$pluginNamespace => $controllerArguments];
581  }
582  ArrayUtility::mergeRecursiveWithOverrule($this->arguments, $prefixedControllerArguments);
583  return $this->build();
584  }
585 
597  protected function removeDefaultControllerAndAction(array $controllerArguments, $extensionName, $pluginName)
598  {
599  $defaultControllerName = $this->extensionService->getDefaultControllerNameByPlugin($extensionName, $pluginName);
600  if (isset($controllerArguments['action'])) {
601  $defaultActionName = $this->extensionService->getDefaultActionNameByPluginAndController($extensionName, $pluginName, $controllerArguments['controller']);
602  if ($controllerArguments['action'] === $defaultActionName) {
603  unset($controllerArguments['action']);
604  }
605  }
606  if ($controllerArguments['controller'] === $defaultControllerName) {
607  unset($controllerArguments['controller']);
608  }
609  return $controllerArguments;
610  }
611 
621  public function build()
622  {
623  if ($this->environmentService->isEnvironmentInBackendMode()) {
624  return $this->buildBackendUri();
625  }
626  return $this->buildFrontendUri();
627  }
628 
637  public function buildBackendUri()
638  {
639  $arguments = [];
640  if ($this->addQueryString === true) {
641  if ($this->addQueryStringMethod) {
642  switch ($this->addQueryStringMethod) {
643  case 'GET':
645  break;
646  case 'POST':
648  break;
649  case 'GET,POST':
650  $arguments = array_replace_recursive(GeneralUtility::_GET(), GeneralUtility::_POST());
651  break;
652  case 'POST,GET':
653  $arguments = array_replace_recursive(GeneralUtility::_POST(), GeneralUtility::_GET());
654  break;
655  default:
657  }
658  } else {
660  }
661  foreach ($this->argumentsToBeExcludedFromQueryString as $argumentToBeExcluded) {
662  $argumentToBeExcluded = GeneralUtility::explodeUrl2Array($argumentToBeExcluded, true);
664  }
665  } else {
666  $id = GeneralUtility::_GP('id');
667  $module = GeneralUtility::_GP('M');
668  if ($id !== null) {
669  $arguments['id'] = $id;
670  }
671  if ($module !== null) {
672  $arguments['M'] = $module;
673  }
674  }
677  $this->lastArguments = $arguments;
678  $moduleName = $arguments['M'];
679  unset($arguments['M'], $arguments['moduleToken']);
680  $backendUriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
681  if ($this->request instanceof WebRequest && $this->createAbsoluteUri) {
682  $uri = (string)$backendUriBuilder->buildUriFromModule($moduleName, $arguments, \TYPO3\CMS\Backend\Routing\UriBuilder::ABSOLUTE_URL);
683  } else {
684  $uri = (string)$backendUriBuilder->buildUriFromModule($moduleName, $arguments);
685  }
686  if ($this->section !== '') {
687  $uri .= '#' . $this->section;
688  }
689  return $uri;
690  }
691 
698  public function buildFrontendUri()
699  {
700  $typolinkConfiguration = $this->buildTypolinkConfiguration();
701  if ($this->createAbsoluteUri === true) {
702  $typolinkConfiguration['forceAbsoluteUrl'] = true;
703  if ($this->absoluteUriScheme !== null) {
704  $typolinkConfiguration['forceAbsoluteUrl.']['scheme'] = $this->absoluteUriScheme;
705  }
706  }
707  $uri = $this->contentObject->typoLink_URL($typolinkConfiguration);
708  return $uri;
709  }
710 
717  protected function buildTypolinkConfiguration()
718  {
719  $typolinkConfiguration = [];
720  $typolinkConfiguration['parameter'] = $this->targetPageUid !== null ? $this->targetPageUid : $GLOBALS['TSFE']->id;
721  if ($this->targetPageType !== 0) {
722  $typolinkConfiguration['parameter'] .= ',' . $this->targetPageType;
723  } elseif ($this->format !== '') {
724  $targetPageType = $this->extensionService->getTargetPageTypeByFormat($this->request->getControllerExtensionName(), $this->format);
725  $typolinkConfiguration['parameter'] .= ',' . $targetPageType;
726  }
727  if (!empty($this->arguments)) {
728  $arguments = $this->convertDomainObjectsToIdentityArrays($this->arguments);
729  $this->lastArguments = $arguments;
730  $typolinkConfiguration['additionalParams'] = GeneralUtility::implodeArrayForUrl(null, $arguments);
731  }
732  if ($this->addQueryString === true) {
733  $typolinkConfiguration['addQueryString'] = 1;
734  if (!empty($this->argumentsToBeExcludedFromQueryString)) {
735  $typolinkConfiguration['addQueryString.'] = [
736  'exclude' => implode(',', $this->argumentsToBeExcludedFromQueryString)
737  ];
738  }
739  if ($this->addQueryStringMethod) {
740  $typolinkConfiguration['addQueryString.']['method'] = $this->addQueryStringMethod;
741  }
742  }
743  if ($this->noCache === true) {
744  $typolinkConfiguration['no_cache'] = 1;
745  } elseif ($this->useCacheHash) {
746  $typolinkConfiguration['useCacheHash'] = 1;
747  }
748  if ($this->section !== '') {
749  $typolinkConfiguration['section'] = $this->section;
750  }
751  if ($this->linkAccessRestrictedPages === true) {
752  $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
753  }
754  return $typolinkConfiguration;
755  }
756 
766  {
767  foreach ($arguments as $argumentKey => $argumentValue) {
768  // if we have a LazyLoadingProxy here, make sure to get the real instance for further processing
769  if ($argumentValue instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
770  $argumentValue = $argumentValue->_loadRealInstance();
771  // also update the value in the arguments array, because the lazyLoaded object could be
772  // hidden and thus the $argumentValue would be NULL.
773  $arguments[$argumentKey] = $argumentValue;
774  }
775  if ($argumentValue instanceof \Iterator) {
776  $argumentValue = $this->convertIteratorToArray($argumentValue);
777  }
778  if ($argumentValue instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject) {
779  if ($argumentValue->getUid() !== null) {
780  $arguments[$argumentKey] = $argumentValue->getUid();
781  } elseif ($argumentValue instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject) {
782  $arguments[$argumentKey] = $this->convertTransientObjectToArray($argumentValue);
783  } else {
784  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);
785  }
786  } elseif (is_array($argumentValue)) {
787  $arguments[$argumentKey] = $this->convertDomainObjectsToIdentityArrays($argumentValue);
788  }
789  }
790  return $arguments;
791  }
792 
797  protected function convertIteratorToArray(\Iterator $iterator)
798  {
799  if (method_exists($iterator, 'toArray')) {
800  $array = $iterator->toArray();
801  } else {
802  $array = iterator_to_array($iterator);
803  }
804  return $array;
805  }
806 
814  public function convertTransientObjectToArray(\TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject $object)
815  {
816  $result = [];
817  foreach ($object->_getProperties() as $propertyName => $propertyValue) {
818  if ($propertyValue instanceof \Iterator) {
819  $propertyValue = $this->convertIteratorToArray($propertyValue);
820  }
821  if ($propertyValue instanceof \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject) {
822  if ($propertyValue->getUid() !== null) {
823  $result[$propertyName] = $propertyValue->getUid();
824  } else {
825  $result[$propertyName] = $this->convertTransientObjectToArray($propertyValue);
826  }
827  } elseif (is_array($propertyValue)) {
828  $result[$propertyName] = $this->convertDomainObjectsToIdentityArrays($propertyValue);
829  } else {
830  $result[$propertyName] = $propertyValue;
831  }
832  }
833  return $result;
834  }
835 }
uriFor($actionName=null, $controllerArguments=[], $controllerName=null, $extensionName=null, $pluginName=null)
Definition: UriBuilder.php:548
removeDefaultControllerAndAction(array $controllerArguments, $extensionName, $pluginName)
Definition: UriBuilder.php:597
convertDomainObjectsToIdentityArrays(array $arguments)
Definition: UriBuilder.php:765
setArgumentsToBeExcludedFromQueryString(array $argumentsToBeExcludedFromQueryString)
Definition: UriBuilder.php:352
injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService)
Definition: UriBuilder.php:144
injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
Definition: UriBuilder.php:152
setLinkAccessRestrictedPages($linkAccessRestrictedPages)
Definition: UriBuilder.php:394
convertTransientObjectToArray(\TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject $object)
Definition: UriBuilder.php:814
static makeInstance($className,... $constructorArguments)
static implodeArrayForUrl($name, array $theArray, $str='', $skipBlank=false, $rawurlencodeParamName=false)
static explodeUrl2Array($string, $multidim=false)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
setAddQueryStringMethod($addQueryStringMethod)
Definition: UriBuilder.php:327
static arrayDiffAssocRecursive(array $array1, array $array2)
injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
Definition: UriBuilder.php:136