Completed
Push — master ( 98c123...5771fa )
by Craig
10:16
created

AssetsInstallCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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\Bundle\CoreBundle\Command;
15
16
use InvalidArgumentException;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Filesystem\Filesystem;
23
use Symfony\Component\Finder\Finder;
24
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
25
26
/**
27
 * Command that places bundle web assets into a given directory.
28
 *
29
 * @author Fabien Potencier <[email protected]>
30
 */
31
class AssetsInstallCommand extends Command
32
{
33
    /**
34
     * @var Filesystem
35
     */
36
    private $filesystem;
37
38
    /**
39
     * @var ZikulaHttpKernelInterface
40
     */
41
    private $kernel;
42
43
    public function __construct(
44
        Filesystem $filesystem,
45
        ZikulaHttpKernelInterface $kernel
46
    ) {
47
        $this->filesystem = $filesystem;
48
        $this->kernel = $kernel;
49
        parent::__construct('assets:install');
50
    }
51
52
    protected function configure()
53
    {
54
        $this
55
            ->setName('assets:install')
56
            ->setDefinition([
57
                new InputArgument('target', InputArgument::OPTIONAL, 'The target directory', 'web'),
58
            ])
59
            ->addOption('symlink', null, InputOption::VALUE_NONE, 'Symlinks the assets instead of copying it')
60
            ->addOption('relative', null, InputOption::VALUE_NONE, 'Make relative symlinks')
61
            ->setDescription('Installs web assets under a public web directory')
62
            ->setHelp(<<<'EOT'
63
The <info>%command.name%</info> command installs bundle assets into a given
64
directory (e.g. the web directory).
65
66
<info>php %command.full_name% web</info>
67
68
A "modules" and "themes" directory will be created inside the target directory, and the
69
"Resources/public" directory of each bundle will be copied into it.
70
71
To create a symlink to each bundle instead of copying its assets, use the
72
<info>--symlink</info> option:
73
74
<info>php %command.full_name% web --symlink</info>
75
76
To make symlink relative, add the <info>--relative</info> option:
77
78
<info>php %command.full_name% web --symlink --relative</info>
79
80
EOT
81
            );
82
    }
83
84
    /**
85
     * @throws InvalidArgumentException When the target directory does not exist or symlink cannot be used
86
     */
87
    protected function execute(InputInterface $input, OutputInterface $output)
88
    {
89
        $targetArg = rtrim($input->getArgument('target'), '/');
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('target') can also be of type string[]; however, parameter $str of rtrim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
        $targetArg = rtrim(/** @scrutinizer ignore-type */ $input->getArgument('target'), '/');
Loading history...
90
        if (!is_dir($targetArg)) {
91
            throw new InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $input->getArgument('target')));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('target') can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            throw new InvalidArgumentException(sprintf('The target directory "%s" does not exist.', /** @scrutinizer ignore-type */ $input->getArgument('target')));
Loading history...
92
        }
93
        if (!function_exists('symlink') && $input->getOption('symlink')) {
94
            throw new InvalidArgumentException('The symlink() function is not available on your system. You need to install the assets without the --symlink option.');
95
        }
96
        $array = [
97
            'bundle' => $this->kernel->getJustBundles(),
98
            'module' => $this->kernel->getModules(),
99
            'theme' => $this->kernel->getThemes(),
100
        ];
101
        foreach ($array as $type => $bundles) {
102
            // Create the bundles directory otherwise symlink will fail.
103
            $this->filesystem->mkdir($targetArg . "/{$type}s/", 0777);
104
            $output->writeln(sprintf('Installing assets using the <comment>%s</comment> option', $input->getOption('symlink') ? 'symlink' : 'hard copy'));
105
            foreach ($bundles as $bundle) {
106
                if (is_dir($originDir = $bundle->getPath() . '/Resources/public')) {
107
                    $bundlesDir = $targetArg . '/' . $type . 's/';
108
                    $targetDir = $bundlesDir . preg_replace('/' . $type . '$/', '', mb_strtolower($bundle->getName()));
109
                    $output->writeln(sprintf('Installing assets for <comment>%s</comment> into <comment>%s</comment>', $bundle->getNamespace(), $targetDir));
110
                    $this->filesystem->remove($targetDir);
111
                    if ($input->getOption('symlink')) {
112
                        if ($input->getOption('relative')) {
113
                            $relativeOriginDir = $this->filesystem->makePathRelative($originDir, realpath($bundlesDir));
114
                        } else {
115
                            $relativeOriginDir = $originDir;
116
                        }
117
                        $this->filesystem->symlink($relativeOriginDir, $targetDir);
118
                    } else {
119
                        $this->filesystem->mkdir($targetDir, 0777);
120
                        // We use a custom iterator to ignore VCS files
121
                        $this->filesystem->mirror($originDir, $targetDir, Finder::create()->in($originDir));
122
                    }
123
                }
124
            }
125
        }
126
    }
127
}
128