Completed
Push — next ( 8ef386...43bb99 )
by Thomas
11:01 queued 05:16
created

CreateWebCacheCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Exception\InvalidArgumentException;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Finder\SplFileInfo;
16
17
/**
18
 * Class CreateWebCacheCommand
19
 */
20
class CreateWebCacheCommand extends ContainerAwareCommand
21
{
22
    const COMMAND_NAME = 'cache:web:create';
23
24
    /**
25
     * @var OutputInterface
26
     */
27
    private $output;
28
29
    /**
30
     * Configures the command.
31
     *
32
     *
33
     * @throws InvalidArgumentException
34
     */
35
    protected function configure()
36
    {
37
        parent::configure();
38
39
        $this
40
            ->setName(self::COMMAND_NAME)
41
            ->setDescription('create legacy web caches');
42
    }
43
44
    /**
45
     * Executes the command.
46
     *
47
     * @param InputInterface $input
48
     * @param OutputInterface $output
49
     *
50
     * @return int|null
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $this->output = $output;
55
56
        $projectDir = $this->getContainer()->getParameter('kernel.project_dir');
57
58
        $output->writeln('Generating WebCache');
59
60
        $paths = [
61
            $projectDir . '/web/assets',
62
            $projectDir . '/web/assets/css',
63
            $projectDir . '/web/assets/js',
64
        ];
65
66
        foreach ($paths as $path) {
67
            if (!file_exists($path)) {
68
                mkdir($path, 0777, true);
69
            }
70
        }
71
72
        $this->compileCss($projectDir);
73
        $this->compileJs($projectDir);
74
75
        $output->writeln('WebCache generated');
76
    }
77
78
    /**
79
     * Compiles js to one file.
80
     *
81
     * @param string $projectDir
82
     */
83
    private function compileJs($projectDir)
84
    {
85
        $this->output->writeln('Generating javascript');
86
87
        $applicationJsPath = $projectDir . '/app/Resources/assets/js/';
88
89
        if (!file_exists($applicationJsPath)) {
90
            $this->output->writeln('- Javascript directory not found!');
91
92
            return;
93
        }
94
95
        $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($applicationJsPath));
96
97
        $js = '';
98
99
        /**
100
         * @var SplFileInfo
101
         */
102
        foreach ($rii as $file) {
103
            if ($file->isDir() || $file->getExtension() !== 'js') {
104
                continue;
105
            }
106
107
            $js .= file_get_contents($file->getRealPath()) . PHP_EOL;
108
        }
109
110
        file_put_contents($projectDir . '/web/assets/js/main.js', $js);
111
112
        $this->output->writeln('- Javascript generated');
113
    }
114
115
    /**
116
     * Compiles scss to one file.
117
     *
118
     * @param string $projectDir
119
     */
120
    private function compileCss($projectDir)
121
    {
122
        $this->output->writeln('Generating stylesheets');
123
124
        $applicationScssPath = $projectDir . '/app/Resources/assets/scss/';
125
126
        $scss = new Compiler();
127
        $scss->setIgnoreErrors(true);
128
        $scss->addImportPath($applicationScssPath);
129
        $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...
130
            //Check for tilde as this refers to the node_modules dir
131
            if (strpos($path, '~') === 0) {
132
                $path = str_replace(
133
                    ['~', 'bootstrap'],
134
                    [$projectDir . '/vendor/', 'twbs/bootstrap'],
135
                    $path
136
                );
137
138
                $path .= '.scss';
139
140
                //if file does not exist, try with underscore before filename
141
                if (!file_exists($path)) {
142
                    $chunks = explode('/', $path);
143
144
                    end($chunks);
145
                    $lastKey = key($chunks);
146
                    reset($chunks);
147
148
                    $chunks[$lastKey] = '_' . $chunks[$lastKey];
149
150
                    $path = implode('/', $chunks);
151
                }
152
153
                if (!file_exists($path)) {
154
                    return null;
155
                }
156
            }
157
158
            return $path;
159
        });
160
161
        file_put_contents(
162
            $projectDir . '/web/assets/css/style.min.css',
163
            $scss->compile(file_get_contents($applicationScssPath . '/all.scss'))
164
        );
165
166
        file_put_contents(
167
            $projectDir . '/web/assets/css/legacy.min.css',
168
            $scss->compile(file_get_contents($applicationScssPath . '/legacy.scss'))
169
        );
170
171
        $this->output->writeln('- Stylesheets generated');
172
    }
173
}
174