-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.php
54 lines (37 loc) · 1.35 KB
/
script.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* This script is used to convert all SVG files in a folder to
* Blade components. Read the README.md file for more information.
* @author AJ Meireles
*/
require __DIR__ . '/vendor/autoload.php';
function output(string $message, bool $die = true)
{
echo "\n" . $message . "\n" . PHP_EOL;
if ($die) {
exit;
}
}
if (($folder = $_SERVER['argv'][1]) === '') {
output("You must specific the folder name.");
}
$main = __DIR__.'/'.$folder;
if (!is_dir($main)) {
output("The folder [$folder] does not exist!");
}
$folders = array_filter(scandir($main), fn ($item) => is_dir($main . '/' . $item) && !in_array($item, ['.', '..']));
foreach ($folders as $sub) {
$folder = $main . '/' . $sub;
foreach (scandir($folder) as $file) {
if ($file === '.' || $file === '..') continue;
if (pathinfo($file, PATHINFO_EXTENSION) !== 'svg') continue;
$filepath = $folder . '/' . $file;
$content = file_get_contents($filepath);
$modifiedContents = preg_replace('/<svg(.*?)>/', '<svg {{ $attributes }}$1>', $content);
$name = pathinfo($file, PATHINFO_FILENAME) . '.blade.php';
$path = $folder . '/' . $name;
file_put_contents($path, $modifiedContents);
if (pathinfo($file, PATHINFO_EXTENSION) === 'svg') unlink($filepath);
}
}
output("Done!");