‪TYPO3CMS  ‪main
ContentController.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 
20 use Psr\Http\Message\ResponseInterface;
29 
31 {
33  protected string ‪$defaultViewObjectName = JsonView::class;
34 
36 
38  {
39  $this->contentRepository = ‪$contentRepository;
40  $this->dataMapFactory = ‪$dataMapFactory;
41  }
42 
43  public function ‪listAction(): ResponseInterface
44  {
45  $content = $this->contentRepository->findAll();
46  $value = [];
47  $value[$this->‪getRuntimeIdentifier()] = $this->‪getStructure($content);
48  // this is required so we don't try to json_encode content of the image
49  $this->view->setConfiguration([
50  'value' => [
51  '_descendAll' => [
52  '_descendAll' => [
53  '_descendAll' => [
54  '_descendAll' => [
55  '_descendAll' => [
56  '_exclude' => ['contents'],
57  ],
58  ],
59  ],
60  ],
61  ],
62  ],
63  ]);
64  $this->view->assign('value', $value);
65 
66  return $this->‪jsonResponse();
67  }
68 
72  public function ‪processRequest(‪RequestInterface ‪$request): ResponseInterface
73  {
74  try {
75  return parent::processRequest(‪$request);
76  } catch (‪Exception $exception) {
77  throw new \RuntimeException(
78  $this->‪getRuntimeIdentifier() . ': ' . $exception->getMessage() . ' (' . $exception->getCode() . ')',
79  1476122223
80  );
81  }
82  }
83 
87  protected function ‪getStructure(\Iterator|array $iterator): array
88  {
89  $structure = [];
90 
91  if (!$iterator instanceof \Iterator) {
92  $iterator = [$iterator];
93  }
94 
95  foreach ($iterator as $entity) {
96  $dataMap = $this->dataMapFactory->buildDataMap(get_class($entity));
97  $tableName = $dataMap->getTableName();
98  ‪$identifier = $tableName . ':' . $entity->getUid();
99  $properties = ObjectAccess::getGettableProperties($entity);
100 
101  $structureItem = [];
102  foreach ($properties as $propertyName => $propertyValue) {
103  $columnMap = $dataMap->getColumnMap($propertyName);
104  if ($columnMap !== null) {
105  $propertyName = $columnMap->getColumnName();
106  }
107  if ($propertyValue instanceof \Iterator) {
108  $structureItem[$propertyName] = $this->‪getStructure($propertyValue);
109  } else {
110  $structureItem[$propertyName] = $propertyValue;
111  }
112  }
113  //let's flatten the structure and put file reference properties level up, so we can use StructureHasRecordConstraint
114  if ($entity instanceof ‪FileReference
115  && isset($structureItem['originalResource'])
116  && $structureItem['originalResource'] instanceof \‪TYPO3\CMS\Core\Resource\‪FileReference
117  ) {
118  $structureItem = $structureItem['originalResource']->getProperties();
119  }
120  $structure[‪$identifier] = $structureItem;
121  }
122 
123  return $structure;
124  }
125 
126  protected function ‪getRuntimeIdentifier(): string
127  {
128  ‪$arguments = [];
129  foreach ($this->request->getArguments() as $argumentName => $argumentValue) {
130  ‪$arguments[] = $argumentName . '=' . $argumentValue;
131  }
132  return $this->request->getControllerActionName() . '(' . implode(', ', ‪$arguments) . ')';
133  }
134 }
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:18
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$request
‪RequestInterface $request
Definition: ActionController.php:110
‪TYPO3
‪TYPO3Tests\BlogExample\Controller\ContentController\listAction
‪listAction()
Definition: ContentController.php:43
‪TYPO3Tests\BlogExample\Controller\ContentController\$defaultViewObjectName
‪string $defaultViewObjectName
Definition: ContentController.php:33
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:39
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:34
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\jsonResponse
‪jsonResponse(string $json=null)
Definition: ActionController.php:817
‪TYPO3Tests\BlogExample\Controller
Definition: BlogController.php:18
‪TYPO3Tests\BlogExample\Controller\ContentController\processRequest
‪processRequest(RequestInterface $request)
Definition: ContentController.php:72
‪TYPO3Tests\BlogExample\Controller\ContentController\getRuntimeIdentifier
‪getRuntimeIdentifier()
Definition: ContentController.php:126
‪TYPO3\CMS\Extbase\Property\Exception
Definition: Exception.php:25
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$arguments
‪Arguments $arguments
Definition: ActionController.php:123
‪TYPO3Tests\BlogExample\Controller\ContentController\__construct
‪__construct(TtContentRepository $contentRepository, DataMapFactory $dataMapFactory)
Definition: ContentController.php:37
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:28
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3Tests\BlogExample\Domain\Repository\TtContentRepository
Definition: TtContentRepository.php:27
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:63
‪TYPO3Tests\BlogExample\Controller\ContentController\$contentRepository
‪TtContentRepository $contentRepository
Definition: ContentController.php:32
‪TYPO3Tests\BlogExample\Controller\ContentController\$dataMapFactory
‪DataMapFactory $dataMapFactory
Definition: ContentController.php:35
‪TYPO3Tests\BlogExample\Controller\ContentController\getStructure
‪getStructure(\Iterator|array $iterator)
Definition: ContentController.php:87
‪TYPO3Tests\BlogExample\Controller\ContentController
Definition: ContentController.php:31
‪TYPO3\CMS\Extbase\Mvc\View\JsonView
Definition: JsonView.php:29
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37