Completed
Pull Request — 4.0 (#4058)
by Kentaro
06:25
created

UpdateSchemaDoctrineCommand::removeOutputDir()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Command;
4
5
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
6
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand as BaseUpdateSchemaDoctrineCommand;
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
9
use Doctrine\ORM\Tools\SchemaTool;
10
use Eccube\Doctrine\ORM\Mapping\Driver\ReloadSafeAnnotationDriver;
11
use Eccube\Repository\PluginRepository;
12
use Eccube\Service\PluginService;
13
use Eccube\Service\SchemaService;
14
use Eccube\Util\StringUtil;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
20
/**
21
 * Command to generate the SQL needed to update the database schema to match
22
 * the current mapping information.
23
 */
24
class UpdateSchemaDoctrineCommand extends BaseUpdateSchemaDoctrineCommand
25
{
26
    /**
27
     * @var PluginRepository
28
     */
29
    protected $pluginRepository;
30
31
    /**
32
     * @var PluginService
33
     */
34
    protected $pluginService;
35
36
    /**
37
     * @var SchemaService
38
     */
39
    protected $schemaService;
40
41
    public function __construct(
42
        PluginRepository $pluginRepository,
43
        PluginService $pluginService,
44
        SchemaService $schemaService
45
    ) {
46
        parent::__construct();
47
        $this->pluginRepository = $pluginRepository;
48
        $this->pluginService = $pluginService;
49
        $this->schemaService = $schemaService;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    protected function configure()
56
    {
57
        parent::configure();
58
59
        $this
60
            ->setName('eccube:schema:update')
61
            ->setAliases(['doctrine:schema:update'])
62
            ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
63
            ->addOption('no-proxy', null, InputOption::VALUE_NONE, 'Does not use the proxy class and behaves the same as the original doctrine:schema:update command');
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    protected function execute(InputInterface $input, OutputInterface $output)
70
    {
71
        DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
0 ignored issues
show
Documentation introduced by
$this->getApplication() is of type object<Symfony\Component...nsole\Application>|null, but the function expects a object<Symfony\Bundle\Fr...le\Console\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
        $noProxy = true === $input->getOption('no-proxy');
73
        $dumpSql = true === $input->getOption('dump-sql');
74
        $force = true === $input->getOption('force');
75
76
        if ($noProxy || $dumpSql === false && $force === false) {
77
            return parent::execute($input, $output);
78
        }
79
80
        $tmpProxyOutputDir = sys_get_temp_dir().'/proxy_'.StringUtil::random(12);
81
        $tmpMetaDataOutputDir = sys_get_temp_dir().'/metadata_'.StringUtil::random(12);
82
83
        $generateAllFiles = [];
84
        try {
85
            // Generate proxy files of plugins
86
            $Plugins = $this->pluginRepository->findAll();
87
            foreach ($Plugins as $Plugin) {
88
                $config = ['code' => $Plugin->getCode()];
89
                $this->pluginService->generateProxyAndCallback(function ($generateFiles) use (&$generateAllFiles) {
90
                    $generateAllFiles = array_merge($generateAllFiles, $generateFiles);
91
                }, $Plugin, $config, false, $tmpProxyOutputDir);
92
            }
93
94
            $result = null;
95
            $command = $this;
96
97
            // Generate Doctrine metadata and execute schema command
98
            $this->schemaService->executeCallback(function (SchemaTool $schemaTool, array $metaData) use ($command, $input, $output, &$result) {
99
                $ui = new SymfonyStyle($input, $output);
100
                if (empty($metaData)) {
101
                    $ui->success('No Metadata Classes to process.');
102
                    $result = 0;
103
                } else {
104
                    $result = $command->executeSchemaCommand($input, $output, $schemaTool, $metaData, $ui);
105
                }
106
            }, $generateAllFiles, $tmpProxyOutputDir, $tmpMetaDataOutputDir);
107
108
            return $result;
109
        } finally {
110
            $this->removeOutputDir($tmpMetaDataOutputDir);
111
            $this->removeOutputDir($tmpProxyOutputDir);
112
        }
113
    }
114
115
    protected function removeOutputDir($outputDir)
116
    {
117
        if (file_exists($outputDir)) {
118
            foreach (glob("${outputDir}/*") as $f) {
119
                unlink($f);
120
            }
121
            rmdir($outputDir);
122
        }
123
    }
124
}
125