[TASK] Tests for rendering tt_content with FAL in Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Fixtures / Extensions / blog_example / Classes / Controller / ContentController.php
1 <?php
2 namespace ExtbaseTeam\BlogExample\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * ContentController
19 */
20 class ContentController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
21 {
22 /**
23 * @inject
24 * @var \ExtbaseTeam\BlogExample\Domain\Repository\TtContentRepository
25 */
26 protected $contentRepository;
27
28 /**
29 * @var string
30 */
31 protected $defaultViewObjectName = \TYPO3\CMS\Extbase\Mvc\View\JsonView::class;
32
33 /**
34 * @inject
35 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
36 */
37 protected $dataMapFactory;
38
39 /**
40 * @return array
41 */
42 public function listAction()
43 {
44 $content = $this->contentRepository->findAll();
45 $value[$this->getRuntimeIdentifier()] = $this->getStructure($content);
46 // this is required so we don't try to json_encode content of the image
47 $this->view->setConfiguration(['value' => [
48 '_descendAll' => [
49 '_descendAll' => [
50 '_descendAll' => [
51 '_descendAll' => [
52 '_descendAll' => [
53 '_exclude' => ['contents']
54 ]
55 ]
56 ]
57 ]
58 ]
59 ]]);
60 $this->view->assign('value', $value);
61 }
62
63 /**
64 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request
65 * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response
66 * @throws \RuntimeException
67 */
68 public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
69 {
70 try {
71 parent::processRequest($request, $response);
72 } catch (\TYPO3\CMS\Extbase\Property\Exception $exception) {
73 throw new \RuntimeException(
74 $this->getRuntimeIdentifier() . ': ' . $exception->getMessage() . ' (' . $exception->getCode() . ')',
75 1476122223
76 );
77 }
78 }
79
80 /**
81 * @param \Iterator|\TYPO3\CMS\Extbase\DomainObject\AbstractEntity[] $iterator
82 * @return array
83 */
84 protected function getStructure($iterator)
85 {
86 $structure = [];
87
88 if (!$iterator instanceof \Iterator) {
89 $iterator = [$iterator];
90 }
91
92 foreach ($iterator as $entity) {
93 $dataMap = $this->dataMapFactory->buildDataMap(get_class($entity));
94 $tableName = $dataMap->getTableName();
95 $identifier = $tableName . ':' . $entity->getUid();
96 $properties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($entity);
97
98 $structureItem = [];
99 foreach ($properties as $propertyName => $propertyValue) {
100 $columnMap = $dataMap->getColumnMap($propertyName);
101 if ($columnMap !== null) {
102 $propertyName = $columnMap->getColumnName();
103 }
104 if ($propertyValue instanceof \Iterator) {
105 $structureItem[$propertyName] = $this->getStructure($propertyValue);
106 } else {
107 $structureItem[$propertyName] = $propertyValue;
108 }
109 }
110 //let's flatten the structure and put file reference properties level up, so we can use StructureHasRecordConstraint
111 if ($entity instanceof \TYPO3\CMS\Extbase\Domain\Model\FileReference
112 && isset($structureItem['originalResource'])
113 && $structureItem['originalResource'] instanceof \TYPO3\CMS\Core\Resource\FileReference
114 ) {
115 $structureItem = $structureItem['originalResource']->getProperties();
116 }
117 $structure[$identifier] = $structureItem;
118 }
119
120 return $structure;
121 }
122
123 /**
124 * @return string
125 */
126 protected function getRuntimeIdentifier()
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 }