Issues (590)

src/Console/UpgraderCommand.php (1 issue)

1
<?php
2
3
namespace Bdf\Prime\Console;
4
5
use Bdf\Prime\Schema\RepositoryUpgraderResolver;
6
use Bdf\Prime\Schema\StructureUpgraderResolverInterface;
7
use Bdf\Prime\ServiceLocator;
8
use Bdf\Util\Console\BdfStyle;
9
use Bdf\Util\File\ClassFileLocator;
10
use Symfony\Component\Console\Attribute\AsCommand;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * UpgraderCommand
19
 */
20
#[AsCommand('prime:upgrade', 'Upgrade schema from mappers')]
21
class UpgraderCommand extends Command
22
{
23
    protected static $defaultName = 'prime:upgrade';
24
25
    private StructureUpgraderResolverInterface $resolver;
26
27
    /**
28
     * UpgraderCommand constructor.
29
     *
30
     * @param StructureUpgraderResolverInterface|ServiceLocator $resolver
31
     */
32 4
    public function __construct($resolver)
33
    {
34 4
        if ($resolver instanceof ServiceLocator) {
35 4
            $resolver = new RepositoryUpgraderResolver($resolver);
36
        }
37
38 4
        $this->resolver = $resolver;
39
40 4
        parent::__construct(static::$defaultName);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 4
    protected function configure()
47
    {
48 4
        $this
49 4
            ->setDescription('Upgrade schema from mappers')
50 4
            ->addOption('execute', null, InputOption::VALUE_NONE, 'Lance les requetes d\'upgrade')
51 4
            ->addOption('useDrop', null, InputOption::VALUE_NONE, 'Lance les requetes de alter avec des drop')
52 4
            ->addOption('force', null, InputOption::VALUE_NONE, 'Force l\'utilisation du schema manager des mappers l\'ayant désactivé')
53 4
            ->addArgument('path', InputArgument::REQUIRED, 'Model path')
54 4
        ;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 4
    protected function execute(InputInterface $input, OutputInterface $output): int
61
    {
62 4
        $io = new BdfStyle($input, $output);
63
64 4
        $useDrop      = $io->option('useDrop');
65 4
        $executeQuery = $io->option('execute');
66 4
        $force        = $io->option('force');
67 4
        $nbWarning    = 0;
68
69 4
        foreach ((new ClassFileLocator(realpath($io->argument('path')))) as $classInfo) {
70 4
            $className = $classInfo->getClass();
71
72
            // Get the upgrader from the mapper class name
73 4
            $schema = $this->resolver->resolveByMapperClass($className, $force);
74
75
            // walk on mapper only
76 4
            if (!$schema) {
77 4
                $io->debug("{$className} is not mapper class");
78 4
                continue;
79
            }
80
81 4
            $queries = $schema->diff($useDrop);
82
83 4
            if (!$queries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $queries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
84 2
                $io->line("<comment>{$className}</comment> is up to date");
85 2
                continue;
86
            }
87
88 3
            $io->line("<comment>{$className}</comment> <info>needs upgrade</info>");
89
90 3
            foreach ($queries as $query) {
91 3
                $nbWarning++;
92
93 3
                $io->line(is_string($query) ? $query : json_encode($query));
94
            }
95
96 3
            if ($executeQuery) {
97 1
                $io->line('launching query ', ' ');
98
99
                try {
100 1
                    $schema->migrate($useDrop);
101
102 1
                    $io->info('[OK]');
103
                } catch (\Throwable $e) {
104
                    $io->alert($e->getMessage());
105
                }
106
            }
107
108 3
            $io->newLine();
109
        }
110
111 4
        $io->info('Found ' . $nbWarning . ' upgrade(s)');
112
113 4
        return 0;
114
    }
115
}
116