Issues (61)

src/FrontControllerModule.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack;
13
14
use JsonException;
15
use Psr\Container\ContainerExceptionInterface;
16
use Psr\Container\NotFoundExceptionInterface;
17
use Slick\Di\ContainerInterface;
18
use Slick\Di\Definition\ObjectDefinition;
19
use Slick\Http\Message\Response;
20
use Slick\Http\Session\Driver\ServerDriver;
21
use Slick\Http\Session\SessionDriverInterface;
22
use Slick\ModuleApi\Infrastructure\AbstractModule;
23
use Slick\ModuleApi\Infrastructure\Console\ConsoleModuleInterface;
24
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandler;
25
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandlerInterface;
26
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewarePosition;
0 ignored issues
show
The type Slick\ModuleApi\Infrastr...ller\MiddlewarePosition 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...
27
use Slick\ModuleApi\Infrastructure\FrontController\Position;
28
use Slick\ModuleApi\Infrastructure\FrontController\WebModuleInterface;
29
use Slick\ModuleApi\Infrastructure\SlickModuleInterface;
30
use Slick\WebStack\Infrastructure\ComposerParser;
31
use Slick\WebStack\Infrastructure\DependencyContainerFactory;
32
use Slick\WebStack\Infrastructure\Http\FlashMessageStorage;
33
use Slick\WebStack\UserInterface\Console\StackDisplayCommand;
34
use Symfony\Component\Console\Application;
35
use function Slick\ModuleApi\importSettingsFile;
36
37
/**
38
 * FrontControllerModule
39
 *
40
 * @package Slick\WebStack
41
 * @SuppressWarnings(PHPMD)
42
 */
43
final class FrontControllerModule extends AbstractModule implements
44
    SlickModuleInterface,
45
    WebModuleInterface,
46
    ConsoleModuleInterface
47
{
48
49
    private ComposerParser $composerParser;
50
51
    /**
52
     * Creates a FrontControllerSlickModule
53
     *
54
     * @throws JsonException
55
     */
56
    public function __construct()
57
    {
58
        $this->composerParser = new ComposerParser(APP_ROOT.'/composer.json');
59
    }
60
61
    public function description(): string
62
    {
63
        return "Core module that initializes a web application using the front controller pattern.";
64
    }
65
66
    /**
67
     * @inheritDoc
68
     * @return array<string, mixed>
69
     */
70
    public function services(): array
71
    {
72
        $default = [
73
            FlashMessageStorage::class => ObjectDefinition::
74
                create(FlashMessageStorage::class)
75
                ->with('@session'),
76
            SessionDriverInterface::class => '@session',
77
            'session' => ObjectDefinition::create(ServerDriver::class),
78
            'default.middleware' => function () {
79
                return fn() => new Response(
80
                    200,
81
                    $this->createDefaultContent(),
82
                    ['Content-Type' => 'text/html']
83
                );
84
            }
85
        ];
86
        return importSettingsFile(dirname(__DIR__).'/config/logging.php', $default);
87
    }
88
89
    /**
90
     * Returns an array of middleware handlers.
91
     *
92
     * @return array<MiddlewareHandlerInterface> The middleware handlers.
93
     * @throws ContainerExceptionInterface
94
     * @throws NotFoundExceptionInterface
95
     */
96
    public function middlewareHandlers(): array
97
    {
98
        return [
99
            new MiddlewareHandler(
100
                'default-response',
101
                new MiddlewarePosition(Position::Bottom),
102
                DependencyContainerFactory::instance()->container()->get('default.middleware')
103
            )
104
        ];
105
    }
106
107
    public function configureConsole(Application $cli, ContainerInterface $container): void
108
    {
109
        $args = [null];
110
        $cli->add($container->make(StackDisplayCommand::class, ...$args));
111
    }
112
113
114
    /**
115
     * Creates the default content for the HTML body.
116
     *
117
     * @return string The default content for the HTML body.
118
     */
119
    private function createDefaultContent(): string
120
    {
121
        $head = "<html lang=\"en\"><title>%s</title><body>%s</body></html>";
122
        $body = sprintf(
123
            "<h1>%s <span class=\"small\">%s</span></h1><p>%s</p>",
124
            $this->composerParser->appName(),
125
            $this->composerParser->version(),
126
            $this->composerParser->description()
127
        );
128
        return sprintf($head, $this->composerParser->appName(), $body);
129
    }
130
}
131