aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Fix/IconDimensions.php
blob: 13703e9ac91af94a9238e457770f4a54b0908fcd (plain)
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
<?php
namespace Studip\Cli\Commands\Fix;

use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class IconDimensions extends Command
{
    protected static $defaultName = 'fix:icon-dimensions';

    protected function configure(): void
    {
        $this->setDescription('Fix icon dimensions in their svg files');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $folder = $GLOBALS['STUDIP_BASE_PATH'] . '/public/assets/images/icons';
        $iterator = new RecursiveDirectoryIterator(
            $folder,
            FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::UNIX_PATHS
        );
        $iterator = new RecursiveIteratorIterator($iterator);
        $regexp_iterator = new RegexIterator($iterator, '/\.svg$/', RecursiveRegexIterator::MATCH);

        foreach ($regexp_iterator as $file) {
            $contents = file_get_contents($file);

            $xml = simplexml_load_string($contents);
            $attr = $xml->attributes();
            if ($attr->width && $attr->height) {
                continue;
            }

            $contents = str_replace('<svg ', '<svg width="16" height="16" ', $contents);
            file_put_contents($file, $contents);

            $output->writeln("Adjusted {$file}");
        }

        return Command::SUCCESS;
    }
}