‪TYPO3CMS  ‪main
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 
20 use Psr\Http\Message\ResponseInterface;
32 
34 {
36  protected string ‪$defaultViewObjectName = JsonView::class;
37 
39 
41  {
42  $this->blogRepository = ‪$blogRepository;
43  $this->dataMapFactory = ‪$dataMapFactory;
44  }
45 
46  public function ‪listAction(): ResponseInterface
47  {
48  $blogs = $this->blogRepository->findAll();
49  $value = [];
50  $value[$this->‪getRuntimeIdentifier()] = $this->‪getStructure($blogs);
51 
52  $this->view->assign('value', $value);
53 
54  return $this->‪htmlResponse();
55  }
56 
57  public function ‪detailsAction(‪Blog $blog = null): ResponseInterface
58  {
59  return $this->‪htmlResponse($blog ? $blog->getTitle() : '');
60  }
61 
62  public function ‪testFormAction(): ResponseInterface
63  {
64  return $this->‪htmlResponse('testFormAction');
65  }
66 
71  public function ‪testForwardAction(‪Post $blogPost): ‪ForwardResponse
72  {
73  return (new ‪ForwardResponse('testForwardTarget'))->‪withArguments(['blogPost' => $blogPost]);
74  }
75 
76  public function ‪testForwardTargetAction(‪Post $blogPost): ResponseInterface
77  {
78  return $this->‪htmlResponse('testForwardTargetAction');
79  }
80 
81  public function ‪testRelatedObjectAction(‪Blog $blog, ?‪Post $blogPost = null): ResponseInterface
82  {
83  return $this->‪htmlResponse('testRelatedObject');
84  }
85 
89  public function ‪processRequest(‪RequestInterface ‪$request): ResponseInterface
90  {
91  try {
92  return parent::processRequest(‪$request);
93  } catch (‪Exception $exception) {
94  throw new \RuntimeException(
95  $this->‪getRuntimeIdentifier() . ': ' . $exception->getMessage() . ' (' . $exception->getCode() . ')',
96  1476122222
97  );
98  }
99  }
100 
105  protected function ‪getErrorFlashMessage(): bool
106  {
107  return false;
108  }
109 
113  protected function ‪getStructure(\Iterator|array $iterator): array
114  {
115  $structure = [];
116 
117  if (!$iterator instanceof \Iterator) {
118  $iterator = [$iterator];
119  }
120 
121  foreach ($iterator as $entity) {
122  $dataMap = $this->dataMapFactory->buildDataMap(get_class($entity));
123  $tableName = $dataMap->getTableName();
124  ‪$identifier = $tableName . ':' . $entity->getUid();
125  $properties = ObjectAccess::getGettableProperties($entity);
126 
127  $structureItem = [];
128  foreach ($properties as $propertyName => $propertyValue) {
129  $columnMap = $dataMap->getColumnMap($propertyName);
130  if ($columnMap !== null) {
131  $propertyName = $columnMap->getColumnName();
132  }
133  if ($propertyValue instanceof \Iterator) {
134  $structureItem[$propertyName] = $this->‪getStructure($propertyValue);
135  } else {
136  $structureItem[$propertyName] = $propertyValue;
137  }
138  }
139  $structure[‪$identifier] = $structureItem;
140  }
141 
142  return $structure;
143  }
144 
145  protected function ‪getRuntimeIdentifier(): string
146  {
147  ‪$arguments = [];
148  foreach ($this->request->getArguments() as $argumentName => $argumentValue) {
149  ‪$arguments[] = $argumentName . '=' . $argumentValue;
150  }
151  return $this->request->getControllerActionName() . '(' . implode(', ', ‪$arguments) . ')';
152  }
153 }
‪TYPO3Tests\BlogExample\Controller\BlogController\testRelatedObjectAction
‪testRelatedObjectAction(Blog $blog, ?Post $blogPost=null)
Definition: BlogController.php:81
‪TYPO3Tests\BlogExample\Controller\BlogController
Definition: BlogController.php:34
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:18
‪TYPO3\CMS\Extbase\Annotation\IgnoreValidation
Definition: IgnoreValidation.php:26
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$request
‪RequestInterface $request
Definition: ActionController.php:106
‪TYPO3Tests\BlogExample\Controller\BlogController\testForwardAction
‪testForwardAction(Post $blogPost)
Definition: BlogController.php:71
‪TYPO3Tests\BlogExample\Controller\BlogController\getErrorFlashMessage
‪getErrorFlashMessage()
Definition: BlogController.php:105
‪TYPO3Tests\BlogExample\Controller\BlogController\getRuntimeIdentifier
‪getRuntimeIdentifier()
Definition: BlogController.php:145
‪TYPO3Tests\BlogExample\Controller\BlogController\processRequest
‪processRequest(RequestInterface $request)
Definition: BlogController.php:89
‪TYPO3\CMS\Extbase\Http\ForwardResponse
Definition: ForwardResponse.php:24
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:39
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:34
‪TYPO3Tests\BlogExample\Controller\BlogController\__construct
‪__construct(BlogRepository $blogRepository, DataMapFactory $dataMapFactory)
Definition: BlogController.php:40
‪TYPO3Tests\BlogExample\Controller\BlogController\$blogRepository
‪BlogRepository $blogRepository
Definition: BlogController.php:35
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\htmlResponse
‪htmlResponse(string $html=null)
Definition: ActionController.php:802
‪TYPO3Tests\BlogExample\Controller
Definition: BlogController.php:18
‪TYPO3\CMS\Extbase\Http\ForwardResponse\withArguments
‪withArguments(array $arguments)
Definition: ForwardResponse.php:64
‪TYPO3Tests\BlogExample\Controller\BlogController\$dataMapFactory
‪DataMapFactory $dataMapFactory
Definition: BlogController.php:38
‪TYPO3Tests\BlogExample\Controller\BlogController\testFormAction
‪testFormAction()
Definition: BlogController.php:62
‪TYPO3Tests\BlogExample\Domain\Model\Post
Definition: Post.php:28
‪TYPO3Tests\BlogExample\Controller\BlogController\detailsAction
‪detailsAction(Blog $blog=null)
Definition: BlogController.php:57
‪TYPO3\CMS\Extbase\Property\Exception
Definition: Exception.php:25
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$arguments
‪Arguments $arguments
Definition: ActionController.php:119
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3Tests\BlogExample\Domain\Model\Blog
Definition: Blog.php:30
‪TYPO3Tests\BlogExample\Controller\BlogController\listAction
‪listAction()
Definition: BlogController.php:46
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:63
‪TYPO3Tests\BlogExample\Domain\Repository\BlogRepository
Definition: BlogRepository.php:29
‪TYPO3Tests\BlogExample\Controller\BlogController\getStructure
‪getStructure(\Iterator|array $iterator)
Definition: BlogController.php:113
‪TYPO3\CMS\Extbase\Mvc\View\JsonView
Definition: JsonView.php:29
‪TYPO3Tests\BlogExample\Controller\BlogController\$defaultViewObjectName
‪string $defaultViewObjectName
Definition: BlogController.php:36
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3Tests\BlogExample\Controller\BlogController\testForwardTargetAction
‪testForwardTargetAction(Post $blogPost)
Definition: BlogController.php:76