‪TYPO3CMS  11.5
BlogController.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 
21 use Psr\Http\Message\ResponseInterface;
29 
34 {
38  private ‪$blogRepository;
39 
43  protected ‪$defaultViewObjectName = JsonView::class;
44 
48  private ‪$dataMapFactory;
49 
50  public function ‪__construct(
51  \‪ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository ‪$blogRepository,
52  \‪TYPO3\CMS\‪Extbase\Persistence\Generic\Mapper\DataMapFactory ‪$dataMapFactory
53  ) {
54  $this->blogRepository = ‪$blogRepository;
55  $this->dataMapFactory = ‪$dataMapFactory;
56  }
57 
58  public function ‪listAction(): ResponseInterface
59  {
60  $blogs = $this->blogRepository->findAll();
61  $value = [];
62  $value[$this->‪getRuntimeIdentifier()] = $this->‪getStructure($blogs);
63 
64  $this->view->assign('value', $value);
65 
66  return $this->‪htmlResponse();
67  }
68 
69  public function ‪detailsAction(‪Blog $blog = null): ResponseInterface
70  {
71  return $this->‪htmlResponse($blog ? $blog->getTitle() : '');
72  }
73 
74  public function ‪testFormAction(): ResponseInterface
75  {
76  return $this->‪htmlResponse('testFormAction');
77  }
78 
84  public function ‪testForwardAction($blogPost): ForwardResponse
85  {
86  return (new ForwardResponse('testForwardTarget'))->withArguments(['blogPost' => $blogPost]);
87  }
88 
92  public function ‪testForwardTargetAction($blogPost): ResponseInterface
93  {
94  return $this->‪htmlResponse('testForwardTargetAction');
95  }
96 
101  public function ‪testRelatedObjectAction($blog, $blogPost = null): ResponseInterface
102  {
103  return $this->‪htmlResponse('testRelatedObject');
104  }
105 
109  public function ‪processRequest(RequestInterface ‪$request): ResponseInterface
110  {
111  try {
112  return parent::processRequest(‪$request);
113  } catch (Exception $exception) {
114  throw new \RuntimeException(
115  $this->‪getRuntimeIdentifier() . ': ' . $exception->getMessage() . ' (' . $exception->getCode() . ')',
116  1476122222
117  );
118  }
119  }
120 
127  protected function ‪getErrorFlashMessage(): bool
128  {
129  return false;
130  }
131 
136  protected function ‪getStructure($iterator): array
137  {
138  $structure = [];
139 
140  if (!$iterator instanceof \Iterator) {
141  $iterator = [$iterator];
142  }
143 
144  foreach ($iterator as $entity) {
145  $dataMap = $this->dataMapFactory->buildDataMap(get_class($entity));
146  $tableName = $dataMap->getTableName();
147  $identifier = $tableName . ':' . $entity->getUid();
148  $properties = ‪ObjectAccess::getGettableProperties($entity);
149 
150  $structureItem = [];
151  foreach ($properties as $propertyName => $propertyValue) {
152  $columnMap = $dataMap->getColumnMap($propertyName);
153  if ($columnMap !== null) {
154  $propertyName = $columnMap->getColumnName();
155  }
156  if ($propertyValue instanceof \Iterator) {
157  $structureItem[$propertyName] = $this->‪getStructure($propertyValue);
158  } else {
159  $structureItem[$propertyName] = $propertyValue;
160  }
161  }
162  $structure[$identifier] = $structureItem;
163  }
164 
165  return $structure;
166  }
167 
171  protected function ‪getRuntimeIdentifier(): string
172  {
173  ‪$arguments = [];
174  foreach ($this->request->getArguments() as $argumentName => $argumentValue) {
175  ‪$arguments[] = $argumentName . '=' . $argumentValue;
176  }
177  return $this->request->getControllerActionName() . '(' . implode(', ', ‪$arguments) . ')';
178  }
179 }
‪ExtbaseTeam\BlogExample\Controller\BlogController
Definition: BlogController.php:34
‪ExtbaseTeam\BlogExample\Controller\BlogController\testFormAction
‪testFormAction()
Definition: BlogController.php:71
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:18
‪TYPO3\CMS\Extbase\Annotation\IgnoreValidation
Definition: IgnoreValidation.php:25
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:18
‪ExtbaseTeam\BlogExample\Controller\BlogController\listAction
‪listAction()
Definition: BlogController.php:55
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$arguments
‪TYPO3 CMS Extbase Mvc Controller Arguments $arguments
Definition: ActionController.php:159
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\htmlResponse
‪ResponseInterface htmlResponse(string $html=null)
Definition: ActionController.php:1067
‪ExtbaseTeam\BlogExample\Controller
Definition: BlogController.php:18
‪ExtbaseTeam\BlogExample\Controller\BlogController\$defaultViewObjectName
‪string $defaultViewObjectName
Definition: BlogController.php:41
‪ExtbaseTeam\BlogExample\Controller\BlogController\testRelatedObjectAction
‪testRelatedObjectAction($blog, $blogPost=null)
Definition: BlogController.php:98
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$request
‪Request $request
Definition: ActionController.php:129
‪TYPO3
‪ExtbaseTeam\BlogExample\Controller\BlogController\__construct
‪__construct(\ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository $blogRepository, \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory $dataMapFactory)
Definition: BlogController.php:47
‪ExtbaseTeam\BlogExample\Controller\BlogController\$blogRepository
‪ExtbaseTeam BlogExample Domain Repository BlogRepository $blogRepository
Definition: BlogController.php:37
‪TYPO3\CMS\Extbase\Http\ForwardResponse
Definition: ForwardResponse.php:24
‪ExtbaseTeam\BlogExample\Controller\BlogController\getRuntimeIdentifier
‪string getRuntimeIdentifier()
Definition: BlogController.php:168
‪ExtbaseTeam\BlogExample\Controller\BlogController\processRequest
‪processRequest(RequestInterface $request)
Definition: BlogController.php:106
‪ExtbaseTeam\BlogExample\Controller\BlogController\getStructure
‪array getStructure($iterator)
Definition: BlogController.php:133
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:39
‪ExtbaseTeam\BlogExample\Controller\BlogController\testForwardAction
‪testForwardAction($blogPost)
Definition: BlogController.php:81
‪ExtbaseTeam\BlogExample\Controller\BlogController\testForwardTargetAction
‪testForwardTargetAction($blogPost)
Definition: BlogController.php:89
‪TYPO3\CMS\Extbase\Http\ForwardResponse\withArguments
‪withArguments(array $arguments)
Definition: ForwardResponse.php:66
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettableProperties
‪static array getGettableProperties(object $object)
Definition: ObjectAccess.php:371
‪ExtbaseTeam\BlogExample\Domain\Model\Blog
Definition: Blog.php:29
‪ExtbaseTeam\BlogExample\Controller\BlogController\$dataMapFactory
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMapFactory $dataMapFactory
Definition: BlogController.php:45
‪TYPO3\CMS\Extbase\Property\Exception
Definition: Exception.php:25
‪ExtbaseTeam\BlogExample\Controller\BlogController\detailsAction
‪detailsAction(Blog $blog=null)
Definition: BlogController.php:66
‪ExtbaseTeam\BlogExample\Controller\BlogController\getErrorFlashMessage
‪bool getErrorFlashMessage()
Definition: BlogController.php:124
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:27
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:65
‪ExtbaseTeam
‪TYPO3\CMS\Extbase\Mvc\View\JsonView
Definition: JsonView.php:31