Issues (20)

DisplayPrettyExceptionsAction.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Action\DisplayPrettyExceptions;
15
16
use Ekino\Drupal\Debug\Action\ActionWithOptionsInterface;
17
use Ekino\Drupal\Debug\Action\CompilerPassActionInterface;
18
use Ekino\Drupal\Debug\Action\EventSubscriberActionInterface;
19
use Ekino\Drupal\Debug\Action\ValidateContainerDefinitionTrait;
20
use Ekino\Drupal\Debug\Kernel\Event\AfterAttachSyntheticEvent;
21
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents;
22
use Psr\Log\LoggerInterface;
23
use Symfony\Component\Debug\ExceptionHandler;
0 ignored issues
show
The type Symfony\Component\Debug\ExceptionHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
use Symfony\Component\DependencyInjection\Definition;
26
use Symfony\Component\DependencyInjection\Reference;
27
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
29
30
class DisplayPrettyExceptionsAction implements CompilerPassActionInterface, EventSubscriberActionInterface, ActionWithOptionsInterface
31
{
32
    use ValidateContainerDefinitionTrait;
33
34
    /**
35
     * @var string
36
     */
37
    const LOGGER_SERVICE_ID = 'ekino.drupal.debug.action.display_pretty_exceptions.logger';
38
39
    /**
40
     * @var DisplayPrettyExceptionsOptions
41
     */
42
    private $options;
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public static function getSubscribedEvents(): array
48
    {
49
        return array(
50 1
            DebugKernelEvents::AFTER_ATTACH_SYNTHETIC => 'setLogger',
51
        );
52
    }
53
54
    /**
55
     * @param DisplayPrettyExceptionsOptions $options
56
     */
57 19
    public function __construct(DisplayPrettyExceptionsOptions $options)
58
    {
59 19
        $this->options = $options;
60 19
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 11
    public function process(ContainerBuilder $container): void
66
    {
67 11
        $eventDispatcherDefinition = $this->validateContainerDefinitionClassImplements($container, 'event_dispatcher', EventDispatcherInterface::class);
68
69 8
        $loggerReference = null;
70 8
        if ($this->options->getLogger() instanceof LoggerInterface) {
71 4
            $loggerDefinition = new Definition();
72 4
            $loggerDefinition->setSynthetic(true);
73
74 4
            $container->setDefinition(self::LOGGER_SERVICE_ID, $loggerDefinition);
75
76 4
            $loggerReference = new Reference(self::LOGGER_SERVICE_ID);
77
        }
78
79 8
        $eventDispatcherDefinition->addMethodCall('addSubscriber', array(
80 8
            new Definition(ExceptionListener::class, array(
81 8
                new Definition(ExceptionController::class, array(
82 8
                    new Definition(ExceptionHandler::class, array(
83 8
                        true,
84 8
                        $this->options->getCharset(),
85 8
                        $this->options->getFileLinkFormat(),
86
                    )),
87
                )),
88 8
                $loggerReference,
89
                true,
90
            )),
91
        ));
92 8
    }
93
94
    /**
95
     * @param AfterAttachSyntheticEvent $event
96
     */
97 4
    public function setLogger(AfterAttachSyntheticEvent $event): void
98
    {
99 4
        $container = $event->getContainer();
100
101 4
        $logger = $this->options->getLogger();
102 4
        if ($logger instanceof LoggerInterface) {
103 2
            if (!$container->has(self::LOGGER_SERVICE_ID)) {
104 1
                throw new \LogicException(\sprintf('The container should have a synthetic service with the id "%s".', self::LOGGER_SERVICE_ID));
105
            }
106
107 1
            $container->set(self::LOGGER_SERVICE_ID, $logger);
108 2
        } elseif ($container->has(self::LOGGER_SERVICE_ID)) {
109 1
            throw new \LogicException('The options should return a concrete logger.');
110
        }
111 2
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 1
    public static function getOptionsClass(): string
117
    {
118 1
        return DisplayPrettyExceptionsOptions::class;
119
    }
120
}
121