Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils/svg action + namespace system SVG files
Resolves #7247
- Loading branch information
1 parent
af35fd9
commit e178e4d
Showing
44 changed files
with
210 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.