1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\ExtensionsModule\Composer; |
15
|
|
|
|
16
|
|
|
use ComponentInstaller\Process\BuildJsProcess; |
17
|
|
|
use ComponentInstaller\Process\Process; |
18
|
|
|
use Composer\Script\Event; |
19
|
|
|
use RuntimeException; |
20
|
|
|
use Zikula\ExtensionsModule\Composer\Process\RequireJsProcess; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A class to rewrite RequireJS configuration |
24
|
|
|
*/ |
25
|
|
|
class RequireJsConfigGenerator |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Generates a RequireJS configuration file. |
29
|
|
|
*/ |
30
|
|
|
public static function regenerateRequireJs(Event $event): void |
31
|
|
|
{ |
32
|
|
|
// Retrieve basic information about the environment and present a |
33
|
|
|
// message to the user. |
34
|
|
|
$composer = $event->getComposer(); |
35
|
|
|
$io = $event->getIO(); |
36
|
|
|
$io->write('<info>Compiling component files</info>'); |
37
|
|
|
|
38
|
|
|
// Set up all the processes. |
39
|
|
|
$processes = [ |
40
|
|
|
// Build the require.js file. |
41
|
|
|
RequireJsProcess::class, |
42
|
|
|
// Compile the require-built.js file. |
43
|
|
|
BuildJsProcess::class, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
// Initialize and execute each process in sequence. |
47
|
|
|
foreach ($processes as $class) { |
48
|
|
|
if (!class_exists($class)) { |
49
|
|
|
$io->write("<warning>Process class '${class}' not found, skipping this process</warning>"); |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
$io->write("<info>Running '${class}' </info>"); |
53
|
|
|
/** @var Process $process */ |
54
|
|
|
$process = new $class($composer, $io); |
55
|
|
|
// When an error occurs during initialization, end the process. |
56
|
|
|
if (!$process->init()) { |
57
|
|
|
$io->write("<warning>An error occurred while initializing the '${class}' process.</warning>"); |
58
|
|
|
break; |
59
|
|
|
} |
60
|
|
|
$process->process(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// move files into subfolder |
64
|
|
|
$publicDir = $composer->getConfig()->get('public-dir') . '/'; |
65
|
|
|
$requireDir = $publicDir . 'require/'; |
66
|
|
|
if (!file_exists($requireDir) && !mkdir($requireDir, 0755) && !is_dir($requireDir)) { |
67
|
|
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $requireDir)); |
68
|
|
|
} |
69
|
|
|
$requireJsFiles = [ |
70
|
|
|
'require.config.js', |
71
|
|
|
'require.css', |
72
|
|
|
'require.js', |
73
|
|
|
'require-built.js' |
74
|
|
|
]; |
75
|
|
|
foreach ($requireJsFiles as $fileName) { |
76
|
|
|
if (!file_exists($publicDir . $fileName)) { |
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
rename($publicDir . $fileName, $requireDir . $fileName); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|