Completed
Push — master ( 8e8098...c27fcd )
by Craig
05:52
created

ZikulaExtensionInstallCommand::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
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 - 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\ExtensionsModule\Command;
15
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Zikula\Bundle\CoreBundle\Composer\MetaData;
22
use Zikula\ExtensionsModule\Constant;
23
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
24
use Zikula\ExtensionsModule\Event\ExtensionPostCacheRebuildEvent;
25
26
class ZikulaExtensionInstallCommand extends AbstractExtensionCommand
27
{
28
    protected static $defaultName = 'zikula:extension:install';
29
30
    protected function configure()
31
    {
32
        $this
33
            ->setDescription('Install a zikula extension (module or theme). You must run this command twice.')
34
            ->addArgument('bundle_name', InputArgument::REQUIRED, 'Bundle class name (e.g. ZikulaUsersModule)')
35
        ;
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output): int
39
    {
40
        $io = new SymfonyStyle($input, $output);
41
        $bundleName = $input->getArgument('bundle_name');
42
43
        if (false !== $this->isInstalled($bundleName)) {
0 ignored issues
show
Bug introduced by
It seems like $bundleName can also be of type null and string[]; however, parameter $bundleName of Zikula\ExtensionsModule\...nCommand::isInstalled() 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

43
        if (false !== $this->isInstalled(/** @scrutinizer ignore-type */ $bundleName)) {
Loading history...
44
            if ($input->isInteractive()) {
45
                $io->error('Extension is already installed but possibly inactive.');
46
            }
47
48
            return 1;
49
        }
50
51
        if (!$this->kernel->isBundle($bundleName)) {
0 ignored issues
show
Bug introduced by
It seems like $bundleName can also be of type null and string[]; however, parameter $name of Zikula\Bundle\CoreBundle...elInterface::isBundle() 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

51
        if (!$this->kernel->isBundle(/** @scrutinizer ignore-type */ $bundleName)) {
Loading history...
52
            $this->load($bundleName);
0 ignored issues
show
Bug introduced by
It seems like $bundleName can also be of type null and string[]; however, parameter $bundleName of Zikula\ExtensionsModule\...nInstallCommand::load() 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

52
            $this->load(/** @scrutinizer ignore-type */ $bundleName);
Loading history...
53
            $io->note(sprintf('%s is now prepared for installation. Run this command again to complete installation.', $bundleName));
54
55
            return 0;
56
        }
57
58
        /** @var $extension ExtensionEntity */
59
        $extension = $this->extensionRepository->findOneBy(['name' => $bundleName]);
60
        $unsatisfiedDependencies = $this->dependencyHelper->getUnsatisfiedExtensionDependencies($extension);
61
        $dependencyNames = [];
62
        foreach ($unsatisfiedDependencies as $dependency) {
63
            if (MetaData::DEPENDENCY_REQUIRED !== $dependency->getStatus()) {
64
                continue;
65
            }
66
            $dependencyNames[] = $dependency->getModname();
67
        }
68
        if (!empty($dependencyNames)) {
69
            $io->error(sprintf('Cannot install because this extension depends on other extensions. Please install the following extensions first: %s', implode(', ', $dependencyNames)));
70
71
            return 2;
72
        }
73
74
        if (false === $this->extensionHelper->install($extension)) {
75
            if ($input->isInteractive()) {
76
                $io->error('Could not install the extension');
77
            }
78
79
            return 3;
80
        }
81
82
        $this->eventDispatcher->dispatch(new ExtensionPostCacheRebuildEvent($this->kernel->getBundle($extension->getName()), $extension));
83
84
        if ($input->isInteractive()) {
85
            $io->success('Extension installed');
86
        }
87
88
        return 0;
89
    }
90
91
92
    private function load(string $bundleName): void
93
    {
94
        // load the extension into the modules table
95
        $this->reSync(false);
96
        if (!($extension = $this->extensionRepository->findOneBy(['name' => $bundleName]))) {
97
            throw new \Exception(sprintf('Could not find extension %s in the database', $bundleName));
98
        }
99
100
        // force the kernel to load the bundle
101
        $extension->setState(Constant::STATE_TRANSITIONAL);
102
        $this->extensionRepository->persistAndFlush($extension);
103
104
        // delete the container file
105
        $cacheDir = $this->kernel->getContainer()->getParameter('kernel.cache_dir');
106
        $containerClass = $this->kernel->getContainer()->getParameter('kernel.container_class');
107
        $containerFile = $cacheDir . '/' . $containerClass . '.php';
108
        $fs = new Filesystem();
109
        $fs->remove($containerFile);
110
        $this->kernel->getContainer()->get('cache_warmer')->warmUp($this->kernel->getCacheDir());
111
        $this->kernel->reboot(null);
112
    }
113
}
114