TYPO3 CMS  TYPO3_8-7
FileLinkHandler.php
Go to the documentation of this file.
1 <?php
2 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  */
21 
28 {
29 
34  protected $baseUrn = 't3://file';
35 
40  protected $resourceFactory;
41 
48  public function asString(array $parameters): string
49  {
50  if ($parameters['file'] === null) {
51  return '';
52  }
53  $uid = $parameters['file']->getUid();
54  // I am not sure about this use case. Maybe if the file was not indexed and saved to DB (migration from old systems)
55  if ($uid > 0) {
56  $urn = '?uid=' . $uid;
57  } else {
58  $identifier = $parameters['file']->getIdentifier();
59  $urn = '?identifier=' . urlencode($identifier);
60  }
61 
62  return $this->baseUrn . $urn;
63  }
64 
72  public function resolveHandlerData(array $data): array
73  {
74  try {
75  $file = $this->resolveFile($data);
76  } catch (FileDoesNotExistException $e) {
77  $file = null;
78  }
79  return ['file' => $file];
80  }
81 
87  protected function resolveFile(array $data)
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 
103  protected function getResourceFactory(): ResourceFactory
104  {
105  if (!$this->resourceFactory) {
106  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
107  }
108  return $this->resourceFactory;
109  }
110 }
static makeInstance($className,... $constructorArguments)