‪TYPO3CMS  10.4
LinkService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
33 {
34  const ‪TYPE_PAGE = 'page';
35  const ‪TYPE_URL = 'url';
36  const ‪TYPE_EMAIL = 'email';
37  const ‪TYPE_TELEPHONE = 'telephone';
38  const ‪TYPE_FILE = 'file';
39  const ‪TYPE_FOLDER = 'folder';
40  const ‪TYPE_RECORD = 'record';
41  const ‪TYPE_UNKNOWN = 'unknown';
42 
48  protected ‪$handlers;
49 
53  public function ‪__construct()
54  {
55  if (!empty(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['linkHandler'])) {
56  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['linkHandler'] as $type => $handler) {
57  if (!isset($this->handlers[$type]) || !is_object($this->handlers[$type])) {
58  $this->handlers[$type] = GeneralUtility::makeInstance($handler);
59  }
60  }
61  }
62  }
63 
80  public function ‪resolve(string $linkParameter): array
81  {
82  try {
83  // Check if the new syntax with "t3://" is used
84  return $this->‪resolveByStringRepresentation($linkParameter);
85  } catch (UnknownUrnException $e) {
86  $legacyLinkNotationConverter = GeneralUtility::makeInstance(LegacyLinkNotationConverter::class);
87  return $legacyLinkNotationConverter->resolve($linkParameter);
88  }
89  }
90 
99  public function ‪resolveByStringRepresentation(string $urn): array
100  {
101  // linking to any t3:// syntax
102  if (stripos($urn, 't3://') === 0) {
103  // lets parse the urn
104  $urnParsed = parse_url($urn);
105  $type = $urnParsed['host'];
106  if (isset($urnParsed['query'])) {
107  parse_str(htmlspecialchars_decode($urnParsed['query']), $data);
108  } else {
109  $data = [];
110  }
111  $fragment = $urnParsed['fragment'] ?? null;
112 
113  if (is_object($this->handlers[$type])) {
114  $result = $this->handlers[$type]->resolveHandlerData($data);
115  $result['type'] = $type;
116  } else {
117  throw new UnknownLinkHandlerException('LinkHandler for ' . $type . ' was not registered', 1460581769);
118  }
119  // this was historically named "section"
120  if ($fragment) {
121  $result['fragment'] = $fragment;
122  }
123  } elseif ((strpos($urn, '://') || ‪StringUtility::beginsWith($urn, '//')) && $this->handlers[self::TYPE_URL]) {
124  $result = $this->handlers[‪self::TYPE_URL]->resolveHandlerData(['url' => $urn]);
125  $result['type'] = ‪self::TYPE_URL;
126  } elseif (stripos($urn, 'mailto:') === 0 && $this->handlers[self::TYPE_EMAIL]) {
127  $result = $this->handlers[‪self::TYPE_EMAIL]->resolveHandlerData(['email' => $urn]);
128  $result['type'] = ‪self::TYPE_EMAIL;
129  } elseif (stripos($urn, 'tel:') === 0 && $this->handlers[self::TYPE_TELEPHONE]) {
130  $result = $this->handlers[‪self::TYPE_TELEPHONE]->resolveHandlerData(['telephone' => $urn]);
131  $result['type'] = ‪self::TYPE_TELEPHONE;
132  } else {
133  $result = [];
134  if (is_array(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Link']['resolveByStringRepresentation'] ?? null)) {
135  $params = ['urn' => $urn, 'result' => &$result];
136  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Link']['resolveByStringRepresentation'] as $hookMethod) {
137  $fakeThis = null;
138  GeneralUtility::callUserFunction($hookMethod, $params, $fakeThis);
139  }
140  }
141  if (empty($result) || empty($result['type'])) {
142  throw new UnknownUrnException('No valid URN to resolve found', 1457177667);
143  }
144  }
145 
146  return $result;
147  }
148 
162  public function ‪asString(array $parameters): string
163  {
164  $linkHandler = $this->handlers[$parameters['type']] ?? null;
165  if ($linkHandler !== null) {
166  return $this->handlers[$parameters['type']]->asString($parameters);
167  }
168  if (isset($parameters['url']) && !empty($parameters['url'])) {
169  // This usually happens for tel: or other types where a URL is available and the
170  // legacy link service could resolve at least something
171  return $parameters['url'];
172  }
173  throw new UnknownLinkHandlerException('No valid handlers found for type: ' . $parameters['type'], 1460629247);
174  }
175 }
‪TYPO3\CMS\Core\Utility\StringUtility\beginsWith
‪static bool beginsWith($haystack, $needle)
Definition: StringUtility.php:32
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22