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