Passed
Pull Request — master (#1)
by Peter
07:07
created

AssetManagerBootstrapper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 109
rs 10
c 2
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCachePaths() 0 10 1
A registerViewFunction() 0 50 5
A registerAssets() 0 15 1
A getBindings() 0 4 1
A registerBindings() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Bootstrappers\Assets;
6
7
use AbterPhp\Framework\Assets\AssetManager;
8
use AbterPhp\Framework\Assets\CacheManager\ICacheManager;
9
use AbterPhp\Framework\Assets\Factory\Minifier as MinifierFactory;
10
use AbterPhp\Framework\Assets\UrlFixer;
11
use AbterPhp\Framework\Config\Routes;
12
use AbterPhp\Framework\Constant\Env;
13
use AbterPhp\Framework\Filesystem\FileFinder;
14
use League\Flysystem\Adapter\Local;
15
use League\Flysystem\Filesystem;
16
use Opulence\Environments\Environment;
17
use Opulence\Ioc\Bootstrappers\Bootstrapper;
18
use Opulence\Ioc\Bootstrappers\ILazyBootstrapper;
19
use Opulence\Ioc\IContainer;
20
use Opulence\Views\Compilers\Fortune\ITranspiler;
21
22
class AssetManagerBootstrapper extends Bootstrapper implements ILazyBootstrapper
23
{
24
    /**
25
     * @return array
26
     */
27
    public function getBindings(): array
28
    {
29
        return [
30
            AssetManager::class,
31
        ];
32
    }
33
34
    /**
35
     * @param IContainer $container
36
     */
37
    public function registerBindings(IContainer $container)
38
    {
39
        $this->registerAssets($container);
40
        $this->registerViewFunction($container);
41
    }
42
43
    /**
44
     * @param IContainer $container
45
     */
46
    private function registerAssets(IContainer $container)
47
    {
48
        /** @var FileFinder $fileFinder */
49
        $fileFinder = $container->resolve(FileFinder::class);
50
51
        /** @var ICacheManager $cacheManager */
52
        $cacheManager    = $container->resolve(ICacheManager::class);
53
        $minifierFactory = new MinifierFactory();
54
        $urlFixer        = new UrlFixer(Routes::getCacheUrl());
55
56
        $this->registerCachePaths($cacheManager);
57
58
        $assetManager = new AssetManager($minifierFactory, $fileFinder, $cacheManager, $urlFixer);
59
60
        $container->bindInstance(AssetManager::class, $assetManager);
61
    }
62
63
    /**
64
     * @param ICacheManager $cacheManager
65
     */
66
    private function registerCachePaths(ICacheManager $cacheManager)
67
    {
68
        $cacheDir = sprintf(
69
            '%s%s%s',
70
            rtrim(Environment::getVar(Env::DIR_MEDIA), DIRECTORY_SEPARATOR),
0 ignored issues
show
Bug introduced by
It seems like Opulence\Environments\En...onstant\Env::DIR_MEDIA) can also be of type null; however, parameter $string of rtrim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            rtrim(/** @scrutinizer ignore-type */ Environment::getVar(Env::DIR_MEDIA), DIRECTORY_SEPARATOR),
Loading history...
71
            DIRECTORY_SEPARATOR,
72
            rtrim(Environment::getVar(Env::CACHE_BASE_PATH), DIRECTORY_SEPARATOR)
73
        );
74
75
        $cacheManager->registerFilesystem(new Filesystem(new Local($cacheDir)));
76
    }
77
78
    /**
79
     * @param IContainer $container
80
     */
81
    private function registerViewFunction(IContainer $container)
82
    {
83
        /** @var AssetManager $assets */
84
        $assets = $container->resolve(AssetManager::class);
85
86
        /** @var ITranspiler $transpiler */
87
        $transpiler = $container->resolve(ITranspiler::class);
88
        $transpiler->registerViewFunction(
89
            'assetJs',
90
            function ($keys, $type = '') use ($assets) {
91
                $callback = function ($key) use ($assets, $type) {
92
                    $path = $assets->ensureJsWebPath($key);
93
                    if (empty($path)) {
94
                        return '';
95
                    }
96
97
                    if ($type) {
98
                        return sprintf('<script type="%s" src="%s"></script>', $type, $path) . PHP_EOL;
99
                    }
100
101
                    return sprintf('<script src="%s"></script>', $path) . PHP_EOL;
102
                };
103
104
                return implode(PHP_EOL, array_map($callback, (array)$keys));
105
            }
106
        );
107
        $transpiler->registerViewFunction(
108
            'assetCss',
109
            function ($keys) use ($assets) {
110
                $callback = function ($key) use ($assets) {
111
                    $path = $assets->ensureCssWebPath($key);
112
                    if (empty($path)) {
113
                        return '';
114
                    }
115
116
                    return sprintf('<link href="%s" rel="stylesheet">', $path) . PHP_EOL;
117
                };
118
119
                return implode("\n", array_map($callback, (array)$keys));
120
            }
121
        );
122
        $transpiler->registerViewFunction(
123
            'assetImg',
124
            function ($key, $alt = '') use ($assets) {
125
                $path = $assets->ensureImgWebPath($key);
126
                if (empty($path)) {
127
                    return '';
128
                }
129
130
                return sprintf('<img src="%s" alt="%s">', $path, $alt) . PHP_EOL;
131
            }
132
        );
133
    }
134
}
135