AssetManager::shallIncludeAssets()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * AssetManager.php
5
 *
6
 * Generate static files for Jaxon CSS and Javascript codes.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Plugin\Code;
16
17
use Jaxon\App\Config\ConfigManager;
18
use Jaxon\App\Config\ConfigTrait;
19
use Jaxon\Plugin\AbstractPlugin;
20
21
use function file_put_contents;
22
use function is_dir;
23
use function is_file;
24
use function is_writable;
25
use function rtrim;
26
27
class AssetManager
28
{
29
    use ConfigTrait;
30
31
    /**
32
     * Default library URL
33
     *
34
     * @var string
35
     */
36
    const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist';
37
38
    /**
39
     * The constructor
40
     *
41
     * @param ConfigManager $xConfigManager
42
     * @param MinifierInterface $xMinifier
43
     */
44
    public function __construct(private ConfigManager $xConfigManager,
45
        private MinifierInterface $xMinifier)
46
    {}
47
48
    /**
49
     * @return ConfigManager
50
     */
51
    protected function config(): ConfigManager
52
    {
53
        return $this->xConfigManager;
54
    }
55
56
    /**
57
     * Get app js options
58
     *
59
     * @return string
60
     */
61
    public function getJsOptions(): string
62
    {
63
        return $this->getLibOption('js.app.options', '');
64
    }
65
66
    /**
67
     * Check if the assets of this plugin shall be included in Jaxon generated code.
68
     *
69
     * @param AbstractPlugin $xPlugin
70
     *
71
     * @return bool
72
     */
73
    public function shallIncludeAssets(AbstractPlugin $xPlugin): bool
74
    {
75
        $sPluginOptionName = 'assets.include.' . $xPlugin->getName();
76
        if($this->hasLibOption($sPluginOptionName))
77
        {
78
            return $this->getLibOption($sPluginOptionName);
79
        }
80
        return $this->getLibOption('assets.include.all', true);
81
    }
82
83
    /**
84
     * Get the HTML tags to include Jaxon javascript files into the page
85
     *
86
     * @return array
87
     */
88
    public function getJsLibFiles(): array
89
    {
90
        $sJsExtension = $this->getLibOption('js.app.minify') ? '.min.js' : '.js';
91
        // The URI for the javascript library files
92
        $sJsLibUri = $this->getLibOption('js.lib.uri', self::JS_LIB_URL);
93
        $sJsLibUri = rtrim($sJsLibUri, '/');
94
95
        // Add component files to the javascript file array;
96
        $aJsFiles = [
97
            $this->getLibOption('js.lib.jq', "$sJsLibUri/libs/chibi/chibi$sJsExtension"),
98
            "$sJsLibUri/jaxon.core$sJsExtension",
99
        ];
100
        if($this->getLibOption('core.debug.on'))
101
        {
102
            $sLanguage = $this->getLibOption('core.language');
103
            $aJsFiles[] = "$sJsLibUri/jaxon.debug$sJsExtension";
104
            $aJsFiles[] = "$sJsLibUri/lang/jaxon.$sLanguage$sJsExtension";
105
        }
106
107
        return $aJsFiles;
108
    }
109
110
    /**
111
     * Get the javascript file name
112
     *
113
     * @return bool
114
     */
115
    public function shallCreateJsFiles(): bool
116
    {
117
        // Check config options
118
        // - The js.app.export option must be set to true
119
        // - The js.app.uri and js.app.dir options must be set to non null values
120
        if(!$this->getLibOption('js.app.export', false) ||
121
            !$this->getLibOption('js.app.uri') || !$this->getLibOption('js.app.dir'))
122
        {
123
            return false;
124
        }
125
        return true;
126
    }
127
128
    /**
129
     * Write javascript files and return the corresponding URI
130
     *
131
     * @param CodeGenerator $xCodeGenerator
132
     *
133
     * @return string
134
     */
135
    public function createJsFiles(CodeGenerator $xCodeGenerator): string
136
    {
137
        // Check dir access
138
        $sJsFileName = $this->getLibOption('js.app.file') ?: $xCodeGenerator->getHash();
139
        $sJsDirectory = rtrim($this->getLibOption('js.app.dir'), '\/') . DIRECTORY_SEPARATOR;
140
        // - The js.app.dir must be writable
141
        if(!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory))
142
        {
143
            return '';
144
        }
145
146
        $sJsFilePath = $sJsDirectory . $sJsFileName . '.js';
147
        $sJsMinFilePath = $sJsDirectory . $sJsFileName . '.min.js';
148
        $sJsFileUri = rtrim($this->getLibOption('js.app.uri'), '/') . "/$sJsFileName";
149
150
        if(!is_file($sJsFilePath) &&
151
            !@file_put_contents($sJsFilePath, $xCodeGenerator->getJsScript()))
152
        {
153
            return '';
154
        }
155
        if(!$this->getLibOption('js.app.minify', false))
156
        {
157
            return $sJsFileUri . '.js';
158
        }
159
        if(!is_file($sJsMinFilePath) &&
160
            !$this->xMinifier->minify($sJsFilePath, $sJsMinFilePath))
161
        {
162
            // If the file cannot be minified, return the plain js file.
163
            return $sJsFileUri . '.js';
164
        }
165
        return $sJsFileUri . '.min.js';
166
    }
167
}
168