[TASK] Make code PHP 5.3 compatible again for now
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / Exception / ProductionExceptionHandler.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject\Exception;
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 use Psr\Log\LoggerInterface;
18 use TYPO3\CMS\Core\Log\LogManager;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject;
21
22 /**
23 * Exception handler class for content object rendering
24 */
25 class ProductionExceptionHandler implements ExceptionHandlerInterface {
26
27 /**
28 * @var array
29 */
30 protected $configuration = array();
31
32 /**
33 * @param array $configuration
34 */
35 public function __construct(array $configuration = array()) {
36 $this->configuration = $configuration;
37 }
38
39 /**
40 * Handles exceptions thrown during rendering of content objects
41 * The handler can decide whether to re-throw the exception or
42 * return a nice error message for production context.
43 *
44 * @param \Exception $exception
45 * @param AbstractContentObject $contentObject
46 * @param array $contentObjectConfiguration
47 * @return string
48 * @throws \Exception
49 */
50 public function handle(\Exception $exception, AbstractContentObject $contentObject = NULL, $contentObjectConfiguration = array()) {
51 if (!empty($this->configuration['ignoreCodes.'])) {
52 if (in_array($exception->getCode(), array_map('intval', $this->configuration['ignoreCodes.']), TRUE)) {
53 throw $exception;
54 }
55 }
56 $errorMessage = isset($this->configuration['errorMessage']) ? $this->configuration['errorMessage'] : 'Oops, an error occurred! Code: %s';
57 $code = date('YmdHis', $_SERVER['REQUEST_TIME']) . GeneralUtility::getRandomHexString(8);
58
59 $this->logException($exception, $errorMessage, $code);
60
61 return sprintf($errorMessage, $code);
62 }
63
64 /**
65 * @param \Exception $exception
66 * @param string $errorMessage
67 * @param string $code
68 */
69 protected function logException(\Exception $exception, $errorMessage, $code) {
70 $this->getLogger()->alert(sprintf($errorMessage, $code), array('exception' => $exception));
71 }
72
73 /**
74 * @return LoggerInterface
75 */
76 protected function getLogger() {
77 return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger(__CLASS__);
78 }
79 }