[TASK] Use CLI and Composer Checks via Environment class
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Messaging / FlashMessageRendererResolver.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Messaging;
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
18 use TYPO3\CMS\Core\Core\Environment;
19 use TYPO3\CMS\Core\Messaging\Renderer\FlashMessageRendererInterface;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * A class for rendering flash messages.
24 */
25 class FlashMessageRendererResolver
26 {
27 /**
28 * @var array
29 */
30 protected $renderer = [
31 'BE' => Renderer\BootstrapRenderer::class,
32 'FE' => Renderer\ListRenderer::class,
33 'CLI' => Renderer\PlaintextRenderer::class,
34 '_default' => Renderer\PlaintextRenderer::class,
35 ];
36
37 /**
38 * This method resolves a FlashMessageRendererInterface for the given $context.
39 *
40 * In case $context is null, the context will be detected automatic.
41 *
42 * @return FlashMessageRendererInterface
43 */
44 public function resolve(): FlashMessageRendererInterface
45 {
46 $rendererClass = $this->resolveFlashMessageRenderClass();
47 $renderer = GeneralUtility::makeInstance($rendererClass);
48 if (!$renderer instanceof FlashMessageRendererInterface) {
49 throw new \RuntimeException('Renderer ' . get_class($renderer)
50 . ' does not implement FlashMessageRendererInterface', 1476958086);
51 }
52 return $renderer;
53 }
54
55 /**
56 * This method resolves the renderer class by given context.
57 *
58 * @return string
59 */
60 protected function resolveFlashMessageRenderClass(): string
61 {
62 $context = $this->resolveContext();
63 $renderClass = $this->renderer['_default'];
64
65 if (!empty($this->renderer[$context])) {
66 $renderClass = $this->renderer[$context];
67 }
68
69 return $renderClass;
70 }
71
72 /**
73 * This method detect the current context and return one of the
74 * following strings:
75 * - FE
76 * - BE
77 * - CLI
78 *
79 * @return string
80 */
81 protected function resolveContext(): string
82 {
83 $context = '';
84 if (Environment::isCli()) {
85 $context = 'CLI';
86 } elseif (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_BE) {
87 $context = 'BE';
88 } elseif (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_FE) {
89 $context = 'FE';
90 }
91 return $context;
92 }
93 }