Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
07:03
created

Version20231110194300::updateWebpackConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 21
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
use Symfony\Component\Finder\Finder;
12
13
final class Version20231110194300 extends AbstractMigrationChamilo
14
{
15
    public function getDescription(): string
16
    {
17
        return 'Copy custom theme folder to assets and update webpack.config';
18
    }
19
20
    private function getDefaultThemeNames(): array
21
    {
22
        return [
23
            'academica',
24
            'chamilo',
25
            'chamilo_red',
26
            'cosmic_campus',
27
            'holi',
28
            'readable',
29
            'sober_brown',
30
            'baby_orange',
31
            'chamilo_electric_blue',
32
            'chamilo_sport_red',
33
            'delicious_bordeaux',
34
            'journal',
35
            'royal_purple',
36
            'spacelab',
37
            'beach',
38
            'chamilo_green',
39
            'cool_blue',
40
            'empire_green',
41
            'kiddy',
42
            'silver_line',
43
            'steel_grey',
44
            'blue_lagoon',
45
            'chamilo_orange',
46
            'corporate',
47
            'fruity_orange',
48
            'medical',
49
            'simplex',
50
            'tasty_olive',
51
        ];
52
    }
53
54
    public function up(Schema $schema): void
55
    {
56
        $kernel = $this->container->get('kernel');
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        /** @scrutinizer ignore-call */ 
57
        $kernel = $this->container->get('kernel');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        $rootPath = $kernel->getProjectDir();
58
59
        $defaulThemesFolders = $this->getDefaultThemeNames();
60
61
        $sourceDir = $rootPath.'/app/Resources/public/css/themes';
62
63
        if (!is_dir($sourceDir)) {
64
            return;
65
        }
66
67
        $filesystem = $this->container->get('oneup_flysystem.themes_filesystem');
68
69
        $finder = new Finder();
70
        $finder->directories()->in($sourceDir)->depth('== 0');
71
        $newThemes = [];
72
        foreach ($finder as $folder) {
73
            $themeFolderName = $folder->getRelativePathname();
74
75
            if (\in_array($themeFolderName, $defaulThemesFolders, true)) {
76
                continue;
77
            }
78
79
            if ($filesystem->directoryExists($themeFolderName)) {
80
                continue;
81
            }
82
83
            $newThemes[] = $themeFolderName;
84
85
            $filesystem->createDirectory($themeFolderName);
86
87
            $directory = (new Finder())->in($folder->getRealPath());
88
89
            foreach ($directory as $file) {
90
                if (!$file->isFile()) {
91
                    continue;
92
                }
93
94
                $newFileRelativePathname = $themeFolderName.DIRECTORY_SEPARATOR.$file->getRelativePathname();
95
                $fileContents = $file->getContents();
96
                $filesystem->write($newFileRelativePathname, $fileContents);
97
            }
98
        }
99
100
        $this->updateWebpackConfig($rootPath, $newThemes);
101
    }
102
103
    private function updateWebpackConfig(string $rootPath, array $newThemes): void
104
    {
105
        $webpackConfigPath = $rootPath.'/webpack.config.js';
106
107
        if (!file_exists($webpackConfigPath)) {
108
            return;
109
        }
110
111
        $content = file_get_contents($webpackConfigPath);
112
        $pattern = '/(const themes = \\[\\s*")([^"\\]]+)("\\s*\\])/';
113
        $replacement = function ($matches) use ($newThemes) {
114
            $existingThemes = explode('", "', trim($matches[2], '"'));
115
            $allThemes = array_unique(array_merge($existingThemes, $newThemes));
116
            $newThemesString = implode('", "', $allThemes);
117
118
            return $matches[1].$newThemesString.$matches[3];
119
        };
120
121
        $newContent = preg_replace_callback($pattern, $replacement, $content);
122
123
        file_put_contents($webpackConfigPath, $newContent);
124
    }
125
}
126