Passed
Push — develop ( 406eb0...79ade9 )
by Nikolay
04:37
created

AssetManager::outputCombinedHeaderJs()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 28
rs 9.3554
cc 5
nc 4
nop 2
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2021 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\AdminCabinet\Plugins;
21
22
23
use Phalcon\Assets\Manager;
24
use function MikoPBX\Common\Config\appPath;
25
26
class AssetManager extends Manager
27
{
28
    private string $version;
29
30
    /**
31
     * Sets asset versions
32
     *
33
     * @param string $version
34
     */
35
    public function setVersion(string $version)
36
    {
37
        $this->version = $version;
38
    }
39
40
    /**
41
     * Prints the HTML for CSS assets
42
     *
43
     * @param string|null $collectionName
44
     *
45
     * @return string
46
     */
47
    public function outputCss(string $collectionName = null): string
48
    {
49
        if ($collectionName !== null) {
50
            foreach ($this->collection($collectionName) as $resource) {
51
                $resource->setVersion($this->version);
52
            }
53
        }
54
55
        return parent::outputCss($collectionName);
56
    }
57
58
    /**
59
     * Prints the HTML for JS assets
60
     *
61
     * @param string|null $collectionName
62
     *
63
     * @return string
64
     */
65
    public function outputJs(string $collectionName = null): string
66
    {
67
        if ($collectionName !== null) {
68
            foreach ($this->collection($collectionName) as $resource) {
69
                $resource->setVersion($this->version);
70
            }
71
        }
72
73
        return parent::outputJs($collectionName);
74
    }
75
76
    public function outputCombinedHeaderJs(string $controller, string $action){
77
78
        $jsCachePath = appPath('sites/admin-cabinet/assets/js/cache').'/'.$this->version;
79
        if (!is_dir($jsCachePath)){
80
            mkdir($jsCachePath, 0755, true);
81
        }
82
83
        $headerJSCollections = [
84
            'headerSentryJS',
85
            'headerPBXJS',
86
            'headerJS'
87
        ];
88
89
        $combinedJsFilePath = "{$jsCachePath}/{$controller}-{$action}-header.js";
90
        if (!file_exists($combinedJsFilePath)){
91
            file_put_contents($combinedJsFilePath, '', LOCK_EX);
92
            foreach ($headerJSCollections as $collectionName){
93
                file_put_contents($combinedJsFilePath, '// Data collection '.$collectionName.PHP_EOL, FILE_APPEND | LOCK_EX);
94
                foreach ($this->collection($collectionName) as $resource) {
95
                    file_put_contents($combinedJsFilePath, '// JS file '.$resource->getPath().PHP_EOL, FILE_APPEND | LOCK_EX);
96
                    $sourceJsPath = appPath('sites/admin-cabinet/assets/').$resource->getPath();
97
                    file_put_contents($combinedJsFilePath, file_get_contents($sourceJsPath),FILE_APPEND | LOCK_EX);
98
                    file_put_contents($combinedJsFilePath, PHP_EOL, FILE_APPEND | LOCK_EX);
99
                }
100
            }
101
        }
102
103
        return "<script src='/admin-cabinet/assets/js/cache/{$this->version}/{$controller}-{$action}-header.js?ver=".$this->version."'></script>";
104
105
    }
106
107
    public function outputCombinedFooterJs(string $controller, string $action){
108
109
        $jsCachePath = appPath('sites/admin-cabinet/assets/js/cache').'/'.$this->version;
110
        if (!is_dir($jsCachePath)){
111
            mkdir($jsCachePath, 0755, true);
112
        }
113
114
        $headerJSCollections = [
115
            'SemanticUIJS',
116
            'footerACE',
117
            'footerLoc',
118
            'footerPBXJS'
119
        ];
120
121
        $combinedJsFilePath = "{$jsCachePath}/{$controller}-{$action}-footer.js";
122
        if (!file_exists($combinedJsFilePath)){
123
            file_put_contents($combinedJsFilePath, '', LOCK_EX);
124
            foreach ($headerJSCollections as $collectionName){
125
                file_put_contents($combinedJsFilePath, '// Data collection '.$collectionName.PHP_EOL, FILE_APPEND | LOCK_EX);
126
                foreach ($this->collection($collectionName) as $resource) {
127
                    file_put_contents($combinedJsFilePath, '// JS file '.$resource->getPath().PHP_EOL, FILE_APPEND | LOCK_EX);
128
                    $sourceJsPath = appPath('sites/admin-cabinet/assets/').$resource->getPath();
129
                    file_put_contents($combinedJsFilePath, file_get_contents($sourceJsPath),FILE_APPEND | LOCK_EX);
130
                    file_put_contents($combinedJsFilePath, PHP_EOL, FILE_APPEND | LOCK_EX);
131
                }
132
            }
133
        }
134
135
        return "<script src='/admin-cabinet/assets/js/cache/{$this->version}/{$controller}-{$action}-footer.js?ver=".$this->version."'></script>";
136
137
    }
138
139
    public function outputCombinedHeaderCSS(string $controller, string $action){
140
141
        $CSSCachePath = appPath('sites/admin-cabinet/assets/css/cache').'/'.$this->version;
142
        if (!is_dir($CSSCachePath)){
143
            mkdir($CSSCachePath, 0755, true);
144
        }
145
146
        $headerJSCollections = [
147
            'SemanticUICSS',
148
            'headerCSS'
149
        ];
150
151
        $combinedCSSFilePath = "{$CSSCachePath}/{$controller}-{$action}.css";
152
        if (!file_exists($combinedCSSFilePath)){
153
            file_put_contents($combinedCSSFilePath, '', LOCK_EX);
154
            foreach ($headerJSCollections as $collectionName){
155
                file_put_contents($combinedCSSFilePath, '/* Data collection '.$collectionName.'*/'.PHP_EOL.PHP_EOL, FILE_APPEND | LOCK_EX);
156
                foreach ($this->collection($collectionName) as $resource) {
157
                    file_put_contents($combinedCSSFilePath, '/* CSS file '.$resource->getPath().'*/'.PHP_EOL.PHP_EOL, FILE_APPEND | LOCK_EX);
158
                    $sourceCSSPath = appPath('sites/admin-cabinet/assets/').$resource->getPath();
159
                    $sourceCSSContent = file_get_contents($sourceCSSPath);
160
                    $sourceCSSContent = str_replace(
161
                        [   './themes/default/assets/fonts/',
162
                            'url(icons.woff)',
163
                            'url(icons.woff2)',
164
                            'url(outline-icons.woff2)',
165
                            'url(outline-icons.woff)',
166
                            'url(brand-icons.woff2)',
167
                            'url(brand-icons.woff)',
168
                            'url(../themes/default/assets/images/flags.png)',
169
                            'font/lato-v15-latin',
170
                            'font/lato-v14-latin',
171
                        ],
172
                        [
173
                            './../vendor/themes/default/assets/fonts/',
174
                            'url("./../vendor/themes/default/assets/fonts/icons.woff")',
175
                            'url("./../vendor/themes/default/assets/fonts/icons.woff2")',
176
                            'url("./../vendor/themes/default/assets/fonts/outline-icons.woff2")',
177
                            'url("./../vendor/themes/default/assets/fonts/outline-icons.woff")',
178
                            'url("./../vendor/themes/default/assets/fonts/brand-icons.woff2")',
179
                            'url("./../vendor/themes/default/assets/fonts/brand-icons.woff")',
180
                            'url(./../../vendor/themes/default/assets/images/flags.png)',
181
                            './../../vendor/semantic/font/lato-v15-latin',
182
                            './../../vendor/semantic/font/lato-v14-latin'
183
                        ],
184
                        $sourceCSSContent
185
                    );
186
                    file_put_contents($combinedCSSFilePath, $sourceCSSContent,FILE_APPEND | LOCK_EX);
187
                    file_put_contents($combinedCSSFilePath, PHP_EOL.PHP_EOL, FILE_APPEND | LOCK_EX);
188
                }
189
            }
190
        }
191
192
        return "<link rel='stylesheet' type='text/css' href='/admin-cabinet/assets/css/cache/{$this->version}/{$controller}-{$action}.css?ver=".$this->version."'>";
193
194
    }
195
}