Passed
Push — master ( 8d0b59...1902d4 )
by Melech
09:45 queued 05:35
created

ServiceProvider::publishConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
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\Loader\FilesystemLoader;
19
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...
20
use Valkyrja\Container\Contract\Container;
21
use Valkyrja\Container\Support\Provider;
22
use Valkyrja\Http\Message\Factory\Contract\ResponseFactory as HttpMessageResponseFactory;
23
use Valkyrja\View\Config;
24
use Valkyrja\View\Config\OrkaConfiguration;
25
use Valkyrja\View\Config\PhpConfiguration;
26
use Valkyrja\View\Config\TwigConfiguration;
27
use Valkyrja\View\Contract\View;
28
use Valkyrja\View\Engine\Contract\Engine;
29
use Valkyrja\View\Engine\OrkaEngine;
30
use Valkyrja\View\Engine\PhpEngine;
31
use Valkyrja\View\Engine\TwigEngine;
32
use Valkyrja\View\Factory\ContainerFactory;
33
use Valkyrja\View\Factory\Contract\Factory;
34
use Valkyrja\View\Factory\Contract\ResponseFactory;
35
use Valkyrja\View\Template\Contract\Template;
36
37
/**
38
 * Class ServiceProvider.
39
 *
40
 * @author Melech Mizrachi
41
 */
42
final class ServiceProvider extends Provider
43
{
44
    /**
45
     * @inheritDoc
46
     */
47
    public static function publishers(): array
48
    {
49
        return [
50
            View::class            => [self::class, 'publishView'],
51
            Factory::class         => [self::class, 'publishFactory'],
52
            Template::class        => [self::class, 'publishTemplate'],
53
            PhpEngine::class       => [self::class, 'publishPhpEngine'],
54
            OrkaEngine::class      => [self::class, 'publishOrkaEngine'],
55
            TwigEngine::class      => [self::class, 'publishTwigEngine'],
56
            Environment::class     => [self::class, 'publishTwigEnvironment'],
57
            ResponseFactory::class => [self::class, 'publishResponseFactory'],
58
            Config::class          => [self::class, 'publishConfig'],
59
        ];
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public static function provides(): array
66
    {
67
        return [
68
            View::class,
69
            Factory::class,
70
            Template::class,
71
            PhpEngine::class,
72
            OrkaEngine::class,
73
            TwigEngine::class,
74
            Environment::class,
75
            ResponseFactory::class,
76
            Config::class,
77
        ];
78
    }
79
80
    /**
81
     * Publish the config service.
82
     */
83
    public static function publishConfig(Container $container): void
84
    {
85
        $env = $container->getSingleton(Env::class);
86
87
        $container->setSingleton(
88
            Config::class,
89
            Config::fromEnv($env::class)
90
        );
91
    }
92
93
    /**
94
     * Publish the view service.
95
     */
96
    public static function publishView(Container $container): void
97
    {
98
        $config = $container->getSingleton(Config::class);
99
100
        $container->setSingleton(
101
            View::class,
102
            new \Valkyrja\View\View(
103
                $container,
104
                $container->getSingleton(Factory::class),
105
                $config
106
            )
107
        );
108
    }
109
110
    /**
111
     * Publish the factory service.
112
     */
113
    public static function publishFactory(Container $container): void
114
    {
115
        $container->setSingleton(
116
            Factory::class,
117
            new ContainerFactory($container)
118
        );
119
    }
120
121
    /**
122
     * Publish the template service.
123
     */
124
    public static function publishTemplate(Container $container): void
125
    {
126
        $container->setCallable(
127
            Template::class,
128
            [self::class, 'createTemplate']
129
        );
130
    }
131
132
    /**
133
     * Create a template.
134
     */
135
    public static function createTemplate(Container $container, Engine $engine, string $name): Template
136
    {
137
        return new \Valkyrja\View\Template\Template($engine, $name);
138
    }
139
140
    /**
141
     * Publish the PHP engine service.
142
     */
143
    public static function publishPhpEngine(Container $container): void
144
    {
145
        $container->setCallable(
146
            PhpEngine::class,
147
            [self::class, 'createPhpEngine']
148
        );
149
    }
150
151
    /**
152
     * Create a Php engine.
153
     */
154
    public static function createPhpEngine(Container $container, PhpConfiguration $config): PhpEngine
155
    {
156
        return new PhpEngine($config);
157
    }
158
159
    /**
160
     * Publish the Orka engine service.
161
     */
162
    public static function publishOrkaEngine(Container $container): void
163
    {
164
        $container->setCallable(
165
            OrkaEngine::class,
166
            [self::class, 'createOrkaEngine']
167
        );
168
    }
169
170
    /**
171
     * Create an Orka engine.
172
     */
173
    public static function createOrkaEngine(Container $container, OrkaConfiguration $config): OrkaEngine
174
    {
175
        return new OrkaEngine($config);
176
    }
177
178
    /**
179
     * Publish the Twig engine service.
180
     */
181
    public static function publishTwigEngine(Container $container): void
182
    {
183
        $container->setCallable(
184
            TwigEngine::class,
185
            [self::class, 'createTwigEngine']
186
        );
187
    }
188
189
    /**
190
     * Create a Twig engine.
191
     */
192
    public static function createTwigEngine(Container $container, TwigConfiguration $config): TwigEngine
193
    {
194
        return new TwigEngine(
195
            $container->get(Environment::class, [$config])
196
        );
197
    }
198
199
    /**
200
     * Publish the response factory service.
201
     */
202
    public static function publishResponseFactory(Container $container): void
203
    {
204
        $container->setSingleton(
205
            ResponseFactory::class,
206
            new \Valkyrja\View\Factory\ResponseFactory(
207
                $container->getSingleton(HttpMessageResponseFactory::class),
208
                $container->getSingleton(View::class)
209
            )
210
        );
211
    }
212
213
    /**
214
     * Publish the Twig environment service.
215
     */
216
    public static function publishTwigEnvironment(Container $container): void
217
    {
218
        // Set the twig environment as a singleton in the container
219
        $container->setCallable(
220
            Environment::class,
221
            [self::class, 'createTwigEnvironment']
222
        );
223
    }
224
225
    /**
226
     * Create a Twig environment.
227
     *
228
     * @throws LoaderError
229
     */
230
    public static function createTwigEnvironment(Container $container, TwigConfiguration $config): Environment
231
    {
232
        $env         = $container->getSingleton(Env::class);
233
        $debug       = $env::APP_DEBUG_MODE;
234
        $paths       = $config->paths;
235
        $extensions  = $config->extensions;
236
        $compiledDir = $config->compiledDir;
237
238
        // Get the twig filesystem loader
239
        $loader = new FilesystemLoader();
240
241
        // Iterate through the dirs and add each as a path in the twig loader
242
        foreach ($paths as $namespace => $dir) {
243
            $loader->addPath($dir, $namespace);
244
        }
245
246
        // Create a new twig environment
247
        $twig = new Environment(
248
            $loader,
249
            [
250
                'cache'   => $compiledDir,
251
                'debug'   => $debug,
252
                'charset' => 'utf-8',
253
            ]
254
        );
255
256
        // Iterate through the extensions
257
        foreach ($extensions as $extension) {
258
            // And add each extension to the twig environment
259
            $twig->addExtension(new $extension());
260
        }
261
262
        return $twig;
263
    }
264
}
265