Completed
Push — master ( 550846...8c8483 )
by Anton
09:53
created

ModuleCommand::execute()   B

Complexity

Conditions 3
Paths 14

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 14
nop 2
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command\Generate;
8
9
use Bluzman\Generator\GeneratorException;
10
use Bluzman\Input\InputArgument;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Generate Module Structure
17
 *
18
 * @package  Bluzman\Command
19
 */
20
class ModuleCommand extends AbstractGenerateCommand
21
{
22
    /**
23
     * Command configuration
24
     */
25
    protected function configure()
26
    {
27
        $this
28
            // the name of the command (the part after "bin/bluzman")
29
            ->setName('generate:module')
30
            // the short description shown while running "php bin/bluzman list"
31
            ->setDescription('Generate a new module')
32
            // the full command description shown when running the command with
33
            // the "--help" option
34
            ->setHelp('This command allows you to generate a module structure')
35
        ;
36
37
        $this->addModuleArgument();
38
39
        $controller = new InputArgument(
40
            'controller',
41
            InputArgument::OPTIONAL|InputArgument::IS_ARRAY,
42
            'Controller name(s)'
43
        );
44
45
        $this->getDefinition()->addArgument($controller);
46
    }
47
48
    /**
49
     * @param InputInterface $input
50
     * @param OutputInterface $output
51
     * @return void
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        try {
56
            $this->write("Running <info>generate:module</info> command");
57
58
            $module = $input->getArgument('module');
59
60
            $argument = $this->getDefinition()->getArgument('module');
61
            $argument->validate($module);
62
63
            // create main folder and subfolders
64
            $this->generate($input, $output);
65
66
            // verify it
67
            $this->verify($input, $output);
68
69
            $this->write("Module <info>$module</info> has been successfully created.");
70
71
            // create controllers
72
            $controllers = $input->getArgument('controller') ?? [];
73
74
            $command = $this->getApplication()->find('generate:controller');
75
76
            foreach ($controllers as $controller) {
77
                $arguments = [
78
                    'command' => 'generate:controller',
79
                    'module' => $module,
80
                    'controller' => $controller
81
                ];
82
                $greetInput = new ArrayInput($arguments);
83
                $command->run($greetInput, $output);
84
            }
85
86
        } catch (\Exception $e) {
87
            $this->error("ERROR: {$e->getMessage()}");
88
        }
89
    }
90
91
    /**
92
     * @param InputInterface $input
93
     * @param OutputInterface $output
94
     * @return void
95
     */
96
    protected function generate(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
    {
98
        $this->addSubFolders(
99
            $this->getApplication()->getModulePath($input->getArgument('module')),
100
            [
101
                'controllers',
102
                'views'
103
            ]
104
        );
105
    }
106
107
    /**
108
     * @param $path
109
     * @param array $subFolders
110
     */
111
    protected function addSubFolders($path, array $subFolders = [])
112
    {
113
        if (!$this->getFs()->exists($path)) {
114
            $this->getFs()->mkdir($path);
115
        }
116
117
        foreach ($subFolders as $subFolderName) {
118
            $subFolderPath = $path . DIRECTORY_SEPARATOR . $subFolderName;
119
            if ($this->getFs()->exists($subFolderPath)) {
120
                $this->comment("Directory <info>$subFolderPath</info> already exists");
121
            } else {
122
                $this->getFs()->mkdir($subFolderPath, 0755);
123
                $this->getFs()->touch([$subFolderPath . DIRECTORY_SEPARATOR . '.keep']);
124
            }
125
        }
126
    }
127
128
    /**
129
     * @param InputInterface $input
130
     * @param OutputInterface $output
131
     * @return void
132
     * @throws GeneratorException
133
     */
134
    public function verify(InputInterface $input, OutputInterface $output)
135
    {
136
        $modulePath = $this->getApplication()->getModulePath($input->getArgument('module'));
137
138
        $paths = [
139
            $modulePath,
140
            $modulePath . DS . 'controllers',
141
            $modulePath . DS . 'views'
142
        ];
143
144
        foreach ($paths as $path) {
145
            if (!$this->getFs()->exists($path)) {
146
                throw new GeneratorException("Directory `$path` is not exists");
147
            }
148
        }
149
    }
150
}
151