Completed
Push — development ( 8ef386...754cf1 )
by Thomas
19s
created

CreateWebCacheCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 25
rs 8.8571
1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 ***************************************************************************/
5
6
namespace Oc\Command;
7
8
use Leafo\ScssPhp\Compiler;
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use RegexIterator;
12
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
13
use Symfony\Component\Console\Exception\InvalidArgumentException;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Finder\SplFileInfo;
17
18
/**
19
 * Class CreateWebCacheCommand
20
 *
21
 * @package Oc\Command
22
 */
23
class CreateWebCacheCommand extends ContainerAwareCommand
24
{
25
    const COMMAND_NAME = 'cache:web:create';
26
27
    /**
28
     * @var OutputInterface
29
     */
30
    private $output;
31
32
    /**
33
     * Configures the command.
34
     *
35
     * @return void
36
     *
37
     * @throws InvalidArgumentException
38
     */
39
    protected function configure()
40
    {
41
        parent::configure();
42
43
        $this
44
            ->setName(self::COMMAND_NAME)
45
            ->setDescription('create legacy web caches');
46
    }
47
48
    /**
49
     * Executes the command.
50
     *
51
     * @param InputInterface $input
52
     * @param OutputInterface $output
53
     *
54
     * @return int|null
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $this->output = $output;
59
60
        $projectDir = $this->getContainer()->getParameter('kernel.project_dir');
61
62
        $output->writeln('Generating WebCache');
63
64
        $paths = [
65
            $projectDir . '/web/assets',
66
            $projectDir . '/web/assets/css',
67
            $projectDir . '/web/assets/js'
68
        ];
69
70
        foreach ($paths as $path) {
71
            if (!file_exists($path)) {
72
                mkdir($path, 0777, true);
73
            }
74
        }
75
76
        $this->compileCss($projectDir);
77
        $this->compileJs($projectDir);
78
79
        $output->writeln('WebCache generated');
80
    }
81
82
    /**
83
     * Compiles js to one file.
84
     *
85
     * @param string $projectDir
86
     *
87
     * @return void
88
     */
89
    private function compileJs($projectDir)
90
    {
91
        $this->output->writeln('Generating javascript');
92
93
        $applicationJsPath = $projectDir . '/app/Resources/assets/js/';
94
95
        if (!file_exists($applicationJsPath)) {
96
            $this->output->writeln('- Javascript directory not found!');
97
            return;
98
        }
99
100
        $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($applicationJsPath));
101
102
        $js = '';
103
104
        /**
105
         * @var SplFileInfo $file
106
         */
107
        foreach ($rii as $file) {
108
109
            if ($file->isDir() || $file->getExtension() !== 'js'){
110
                continue;
111
            }
112
113
            $js .= file_get_contents($file->getRealPath()) . PHP_EOL;
114
        }
115
116
        file_put_contents($projectDir . '/web/assets/js/main.js', $js);
117
118
        $this->output->writeln('- Javascript generated');
119
    }
120
121
    /**
122
     * Compiles scss to one file.
123
     *
124
     * @param string $projectDir
125
     *
126
     * @return void
127
     */
128
    private function compileCss($projectDir)
129
    {
130
        $this->output->writeln('Generating stylesheets');
131
132
        $applicationScssPath = $projectDir . '/app/Resources/assets/scss/';
133
134
        $scss = new Compiler();
135
        $scss->setIgnoreErrors(true);
136
        $scss->addImportPath($applicationScssPath);
137
        $scss->addImportPath(function ($path) use ($projectDir) {
0 ignored issues
show
Documentation introduced by
function ($path) use($pr... } return $path; } is of type object<Closure>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
            //Check for tilde as this refers to the node_modules dir
139
            if (strpos($path, '~') === 0) {
140
                $path = str_replace(
141
                    ['~', 'bootstrap'],
142
                    [$projectDir . '/vendor/', 'twbs/bootstrap'],
143
                    $path
144
                );
145
146
                $path .= '.scss';
147
148
                //if file does not exist, try with underscore before filename
149
                if (!file_exists($path)) {
150
                    $chunks = explode('/', $path);
151
152
                    end($chunks);
153
                    $lastKey = key($chunks);
154
                    reset($chunks);
155
156
                    $chunks[$lastKey] = '_' . $chunks[$lastKey];
157
158
                    $path = implode('/', $chunks);
159
                }
160
161
                if (!file_exists($path)) {
162
                    return null;
163
                }
164
            }
165
166
            return $path;
167
        });
168
169
        file_put_contents(
170
            $projectDir . '/web/assets/css/style.min.css',
171
            $scss->compile(file_get_contents($applicationScssPath . '/all.scss'))
172
        );
173
174
        file_put_contents(
175
            $projectDir . '/web/assets/css/legacy.min.css',
176
            $scss->compile(file_get_contents($applicationScssPath . '/legacy.scss'))
177
        );
178
179
        $this->output->writeln('- Stylesheets generated');
180
    }
181
}
182