Completed
Push — master ( 371f09...65d63b )
by Revin
03:01
created

components/JS.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * JS.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.com
6
 */
7
8
namespace rmrevin\yii\minify\components;
9
10
use yii\helpers\Html;
11
12
/**
13
 * Class JS
14
 * @package rmrevin\yii\minify\components
15
 */
16
class JS extends MinifyComponent
17
{
18
19 4
    public function export()
20
    {
21 4
        $jsFiles = $this->view->jsFiles;
22
23 4
        if (!empty($jsFiles)) {
24 4
            foreach ($jsFiles as $position => $files) {
25 4
                if (false === in_array($position, $this->view->js_position, true)) {
26 4
                    $this->view->jsFiles[$position] = [];
27 4
                    foreach ($files as $file => $html) {
28 4
                        $this->view->jsFiles[$position][$file] = $html;
29 4
                    }
30 4
                } else {
31
                    $this->view->jsFiles[$position] = [];
32 4
33
                    $toMinify = [];
34 4
35 4
                    foreach ($files as $file => $html) {
36 4 View Code Duplication
                        if ($this->thisFileNeedMinify($file, $html)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37 4
                            if ($this->view->concatJs) {
38 4
                                $toMinify[$file] = $html;
39
                            } else {
40
                                $this->process($position, [$file => $html]);
41
                            }
42
                        } else {
43
                            if (!empty($toMinify)) {
44 4
                                $this->process($position, $toMinify);
45
46 4
                                $toMinify = [];
47
                            }
48 4
49 4
                            $this->view->jsFiles[$position][$file] = $html;
50 4
                        }
51
                    }
52 4
53
                    if (!empty($toMinify)) {
54 4
                        $this->process($position, $toMinify);
55 4
                    }
56
57
                    unset($toMinify);
58
                }
59
            }
60
        }
61 4
    }
62
63 4
    /**
64 4
     * @param integer $position
65 4
     * @param array $files
66 4
     */
67 4
    protected function process($position, $files)
68 4
    {
69 4
        $resultFile = sprintf('%s/%s.js', $this->view->minify_path, $this->_getSummaryFilesHash($files));
70
        if (!file_exists($resultFile)) {
71 4
            $js = '';
72
            foreach ($files as $file => $html) {
73 4
                $file = $this->getAbsoluteFilePath($file);
74 4
                $js .= file_get_contents($file) . ';' . PHP_EOL;
75
            }
76 4
77
            $this->removeJsComments($js);
78 4
79 4
            if ($this->view->minifyJs) {
80 4
                $js = (new \JSMin($js))
81 4
                    ->min();
82
            }
83 4
84
            file_put_contents($resultFile, $js);
85 4
86 4
            if (false !== $this->view->file_mode) {
87
                @chmod($resultFile, $this->view->file_mode);
88
            }
89
        }
90
91 4
        $file = sprintf('%s%s', \Yii::getAlias($this->view->web_path), str_replace(\Yii::getAlias($this->view->base_path), '', $resultFile));
92
93
        $this->view->jsFiles[$position][$file] = Html::jsFile($file);
94 4
    }
95
96 4
    /**
97 4
     * @todo
98
     * @param string $code
99
     */
100
    protected function removeJsComments(&$code)
101
    {
102
        if (true === $this->view->removeComments) {
103
            //$code = preg_replace('', '', $code);
104
        }
105
    }
106
}