File size: 2,721 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

declare(strict_types=1);

namespace Presentation\Commands;

use Easy\Container\Attributes\Inject;
use Nette\InvalidStateException;
use Nette\InvalidArgumentException as NetteInvalidArgumentException;
use Presentation\Commands\Generators\ModuleGenerator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

#[AsCommand(name: 'app:make:module')]
class MakeModuleCommand extends Command
{
    /**
     * @param string $rootDir 
     * @return void 
     * @throws InvalidArgumentException 
     */
    public function __construct(
        #[Inject('config.dirs.root')]
        private string $rootDir,
    ) {
        parent::__construct();
    }

    /**
     * @param InputInterface $input 
     * @param OutputInterface $output 
     * @return int 
     * @throws LogicException 
     * @throws InvalidArgumentException 
     * @throws RuntimeException 
     * @throws InvalidStateException 
     * @throws NetteInvalidArgumentException 
     */
    public function execute(
        InputInterface $input,
        OutputInterface $output
    ): int {
        $moduleName = $this->askModuleName($input, $output);

        $generator = new ModuleGenerator($this->rootDir);
        $generator->generate($moduleName);

        return Command::SUCCESS;
    }

    private function askModuleName(
        InputInterface $input,
        OutputInterface $output
    ): string {
        /** @var QuestionHelper $helper */
        $helper = $this->getHelper('question');

        $question = new Question('<info>Name of the Module</info>:' . PHP_EOL . '> ');
        $question->setNormalizer(function (string $value): string {
            return $value ? ucfirst(strtolower(trim($value))) : '';
        });

        $moduleName = null;
        while (!$moduleName) {
            $moduleName = $helper->ask($input, $output, $question);

            if (!$moduleName) {
                $output->writeln('<error>Module name is required</error>');
                $moduleName = null;
                continue;
            }

            if (file_exists($this->rootDir . '/src/' . $moduleName)) {
                $output->writeln('<error>Module already exists</error>');
                $moduleName = null;
                continue;
            }
        }

        return $moduleName;
    }
}