Passed
Pull Request — master (#99)
by Wilmer
12:59
created

WebViewProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 11
c 1
b 0
f 0
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 24 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Provider;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Psr\Log\LoggerInterface;
10
use Yiisoft\Aliases\Aliases;
11
use Yiisoft\Assets\AssetManager;
12
use Yiisoft\Di\Container;
13
use Yiisoft\Di\Support\ServiceProvider;
14
use Yiisoft\Router\UrlGeneratorInterface;
15
use Yiisoft\View\Theme;
16
use Yiisoft\View\WebView;
17
18
final class WebViewProvider extends ServiceProvider
19
{
20
    public function register(Container $container): void
21
    {
22
        $container->set(WebView::class, static function (ContainerInterface $container) {
23
            /** WebView config */
24
            $webView = new WebView(
25
                $container->get(Aliases::class)->get('@views'),
26
                $container->get(Theme::class),
27
                $container->get(EventDispatcherInterface::class),
28
                $container->get(LoggerInterface::class)
29
            );
30
31
            /**
32
             * Passes {@see UrlGeneratorInterface} {@see AssetManager} {@see params } to view files.
33
             *
34
             * It will be available as $urlGenerator, $assetManager in view or layout.
35
             */
36
            $webView->setDefaultParameters(
37
                [
38
                    'assetManager' => $container->get(AssetManager::class),
39
                    'urlGenerator' => $container->get(UrlGeneratorInterface::class)
40
                ]
41
            );
42
43
            return $webView;
44
        });
45
    }
46
}
47