‪TYPO3CMS  ‪main
FileLinkHandler.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 
24 
31 {
35  protected string ‪$baseUrn = 't3://file';
36 
41 
45  public function ‪asString(array $parameters): string
46  {
47  if ($parameters['file'] === null) {
48  return '';
49  }
50  ‪$uid = $parameters['file']->getUid();
51  // I am not sure about this use case. Maybe if the file was not indexed and saved to DB (migration from old systems)
52  if (‪$uid > 0) {
53  $urn = '?uid=' . ‪$uid;
54  } else {
55  ‪$identifier = $parameters['file']->getIdentifier();
56  $urn = '?identifier=' . urlencode(‪$identifier);
57  }
58  if (!empty($parameters['fragment'])) {
59  $urn .= '#' . $parameters['fragment'];
60  }
61  return $this->baseUrn . $urn;
62  }
63 
70  public function ‪resolveHandlerData(array $data): array
71  {
72  try {
73  $file = $this->‪resolveFile($data);
74  } catch (‪FileDoesNotExistException $e) {
75  $file = null;
76  }
77  $result = ['file' => $file];
78  if (!empty($data['fragment'])) {
79  $result['fragment'] = $data['fragment'];
80  }
81  return $result;
82  }
83 
87  protected function ‪resolveFile(array $data): ?‪FileInterface
88  {
89  if (isset($data['uid'])) {
90  return $this->‪getResourceFactory()->getFileObject($data['uid']);
91  }
92  if (isset($data['identifier'])) {
93  return $this->‪getResourceFactory()->getFileObjectFromCombinedIdentifier($data['identifier']);
94  }
95  return null;
96  }
97 
102  {
103  return $this->resourceFactory ??= GeneralUtility::makeInstance(ResourceFactory::class);
104  }
105 }
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
Definition: FileDoesNotExistException.php:21
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37