-
Notifications
You must be signed in to change notification settings - Fork 614
/
SvgController.php
96 lines (83 loc) · 2.37 KB
/
SvgController.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\console\controllers\utils;
use Craft;
use craft\console\Controller;
use craft\db\Table;
use craft\helpers\Console;
use craft\helpers\FileHelper;
use craft\helpers\Html;
use craft\helpers\StringHelper;
use yii\console\ExitCode;
use yii\db\Expression;
/**
* Processes SVG files.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.5.17
*/
class SvgController extends Controller
{
/**
* @var bool Whether the SVGs should be sanitized
*/
public $sanitize = false;
/**
* @var bool Whether `id` and other attributes should be namespaced
*/
public $namespace = false;
/**
* @inheritdoc
*/
public function options($actionID)
{
$actions = parent::options($actionID);
if ($actionID === 'index') {
$actions[] = 'sanitize';
$actions[] = 'namespace';
}
return $actions;
}
/**
* Processes SVG files.
*
* @param string $pattern The glob pattern to match SVG files.
* @return int
*/
public function actionIndex(string $pattern): int
{
$paths = glob($pattern);
foreach ($paths as $path) {
if (is_dir($path)) {
$files = FileHelper::findFiles($path, [
'filter' => function(string $path): bool {
return is_dir($path) || FileHelper::isSvg($path);
},
]);
} else {
$files = [$path];
}
foreach ($files as $file) {
$this->stdout('- Processing ');
$this->stdout($file, Console::FG_CYAN);
$this->stdout(' ... ');
$svg = file_get_contents($file);
if ($this->sanitize) {
$svg = Html::sanitizeSvg($svg);
}
// Namespace class names and IDs
if ($this->namespace) {
$ns = StringHelper::randomString(10);
$svg = Html::namespaceAttributes($svg, $ns, true);
}
FileHelper::writeToFile($file, $svg);
$this->stdout("done\n", Console::FG_GREEN);
}
}
return ExitCode::OK;
}
}