Passed
Push — master ( 6b1ebf...63457f )
by Melech
04:00
created

ServiceProvider::publishOrkaRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
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 Valkyrja\View\Provider;
15
16
use Twig\Environment;
17
use Twig\Error\LoaderError;
18
use Twig\Extension\ExtensionInterface as TwigExtensionInterface;
19
use Twig\Loader\FilesystemLoader;
20
use Valkyrja\Application\Env;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Application\Env 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...
21
use Valkyrja\Container\Contract\Container;
22
use Valkyrja\Container\Support\Provider;
23
use Valkyrja\Http\Message\Factory\Contract\ResponseFactory as HttpMessageResponseFactory;
24
use Valkyrja\View\Contract\Renderer;
25
use Valkyrja\View\Factory\Contract\ResponseFactory;
26
use Valkyrja\View\OrkaRenderer;
27
use Valkyrja\View\PhpRenderer;
28
use Valkyrja\View\TwigRenderer;
29
30
/**
31
 * Class ServiceProvider.
32
 *
33
 * @author Melech Mizrachi
34
 */
35
final class ServiceProvider extends Provider
36
{
37
    /**
38
     * @inheritDoc
39
     */
40
    public static function publishers(): array
41
    {
42
        return [
43
            Renderer::class        => [self::class, 'publishRenderer'],
44
            PhpRenderer::class     => [self::class, 'publishPhpRenderer'],
45
            OrkaRenderer::class    => [self::class, 'publishOrkaRenderer'],
46
            TwigRenderer::class    => [self::class, 'publishTwigRenderer'],
47
            Environment::class     => [self::class, 'publishTwigEnvironment'],
48
            ResponseFactory::class => [self::class, 'publishResponseFactory'],
49
        ];
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public static function provides(): array
56
    {
57
        return [
58
            Renderer::class,
59
            PhpRenderer::class,
60
            OrkaRenderer::class,
61
            TwigRenderer::class,
62
            Environment::class,
63
            ResponseFactory::class,
64
        ];
65
    }
66
67
    /**
68
     * Publish the renderer service.
69
     */
70
    public static function publishRenderer(Container $container): void
71
    {
72
        $container->setSingleton(
73
            Renderer::class,
74
            $container->getSingleton(PhpRenderer::class)
75
        );
76
    }
77
78
    /**
79
     * Publish the renderer service.
80
     */
81
    public static function publishPhpRenderer(Container $container): void
82
    {
83
        $env = $container->getSingleton(Env::class);
84
        /** @var non-empty-string $dir */
85
        $dir = $env::VIEW_PHP_DIR;
86
        /** @var non-empty-string $fileExtension */
87
        $fileExtension = $env::VIEW_PHP_FILE_EXTENSION;
88
        /** @var array<string, string> $paths */
89
        $paths = $env::VIEW_PHP_PATHS;
90
91
        $container->setSingleton(
92
            PhpRenderer::class,
93
            new PhpRenderer(
94
                dir: $dir,
95
                fileExtension: $fileExtension,
96
                paths: $paths
97
            ),
98
        );
99
    }
100
101
    /**
102
     * Publish the renderer service.
103
     */
104
    public static function publishOrkaRenderer(Container $container): void
105
    {
106
        $env = $container->getSingleton(Env::class);
107
        /** @var bool $debug */
108
        $debug = $env::APP_DEBUG_MODE;
109
        /** @var non-empty-string $dir */
110
        $dir = $env::VIEW_ORKA_DIR;
111
        /** @var non-empty-string $fileExtension */
112
        $fileExtension = $env::VIEW_ORKA_FILE_EXTENSION;
113
        /** @var array<string, string> $paths */
114
        $paths = $env::VIEW_ORKA_PATHS;
115
116
        $container->setSingleton(
117
            OrkaRenderer::class,
118
            new OrkaRenderer(
119
                dir: $dir,
120
                fileExtension: $fileExtension,
121
                paths: $paths,
122
                debug: $debug
123
            ),
124
        );
125
    }
126
127
    /**
128
     * Publish the renderer service.
129
     */
130
    public static function publishTwigRenderer(Container $container): void
131
    {
132
        $container->setSingleton(
133
            TwigRenderer::class,
134
            new TwigRenderer(
135
                $container->getSingleton(Environment::class),
136
            ),
137
        );
138
    }
139
140
    /**
141
     * Publish the renderer service.
142
     *
143
     * @throws LoaderError
144
     */
145
    public static function publishTwigEnvironment(Container $container): void
146
    {
147
        $env = $container->getSingleton(Env::class);
148
        /** @var bool $debug */
149
        $debug = $env::APP_DEBUG_MODE;
150
        /** @var array<string, string> $paths */
151
        $paths = $env::VIEW_TWIG_PATHS;
152
        /** @var class-string<TwigExtensionInterface>[] $extensions */
153
        $extensions = $env::VIEW_TWIG_EXTENSIONS;
154
        /** @var non-empty-string $compiledDir */
155
        $compiledDir = $env::VIEW_TWIG_COMPILED_DIR;
156
157
        // Get the twig filesystem loader
158
        $loader = new FilesystemLoader();
159
160
        // Iterate through the dirs and add each as a path in the twig loader
161
        foreach ($paths as $namespace => $dir) {
162
            $loader->addPath($dir, $namespace);
163
        }
164
165
        // Create a new twig environment
166
        $twig = new Environment(
167
            $loader,
168
            [
169
                'cache'   => $compiledDir,
170
                'debug'   => $debug,
171
                'charset' => 'utf-8',
172
            ]
173
        );
174
175
        // Iterate through the extensions
176
        foreach ($extensions as $extension) {
177
            // And add each extension to the twig environment
178
            $twig->addExtension(new $extension());
179
        }
180
181
        $container->setSingleton(
182
            Environment::class,
183
            $twig,
184
        );
185
    }
186
187
    /**
188
     * Publish the response factory service.
189
     */
190
    public static function publishResponseFactory(Container $container): void
191
    {
192
        $container->setSingleton(
193
            ResponseFactory::class,
194
            new \Valkyrja\View\Factory\ResponseFactory(
195
                $container->getSingleton(HttpMessageResponseFactory::class),
196
                $container->getSingleton(Renderer::class)
197
            )
198
        );
199
    }
200
}
201