Issues (108)

src/Command/Generate/CrudCommand.php (4 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @copyright Bluz PHP Team
5
 * @link https://github.com/bluzphp/bluzman
6
 */
7
8
namespace Bluzman\Command\Generate;
9
10
use Bluzman\Input\InputArgument;
11
use Bluzman\Input\InputException;
12
use Bluzman\Generator;
13
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * ModelCommand
18
 *
19
 * @package  Bluzman\Command\Generate
20
 */
21
class CrudCommand extends AbstractGenerateCommand
22
{
23
    /**
24
     * Command configuration
25 15
     */
26
    protected function configure()
27
    {
28
        $this
29 15
            // the name of the command (the part after "bin/bluzman")
30
            ->setName('generate:crud')
31 15
            // the short description shown while running "php bin/bluzman list"
32
            ->setDescription('Generate a CRUD for model')
33
            // the full command description shown when running the command with
34 15
            // the "--help" option
35
            ->setHelp('This command allows you to generate CRUD files')
36
        ;
37 15
38 15
        $this->addModelArgument();
39 15
        $this->addModuleArgument(InputArgument::OPTIONAL);
40 15
        $this->addForceOption();
41
    }
42
43
    /**
44
     * @param InputInterface  $input
45
     * @param OutputInterface $output
46
     *
47
     * @return int
48
     * @throws \Bluzman\Generator\GeneratorException
49 4
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51 4
    {
52
        $this->write('Running <info>generate:crud</info> command');
53
        try {
54 4
            // validate
55 1
            $this->validateModelArgument();
56
            $this->validateModuleArgument();
57
58 1
            // generate directories and files
59
            $this->generate($input, $output);
60
61 1
            // verify it
62 3
            $this->verify($input, $output);
63 3
            return 0;
64
        } catch (InputException $e) {
65 4
            $this->error("ERROR: {$e->getMessage()}");
66
            return $e->getCode();
67
        }
68
    }
69
70
    /**
71
     * @param InputInterface  $input
72
     * @param OutputInterface $output
73
     *
74
     * @return void
75 1
     * @throws Generator\GeneratorException
76
     * @throws InputException
77 1
     */
78 1
    protected function generate(InputInterface $input, OutputInterface $output): void
79
    {
80
        $model = ucfirst($input->getArgument('model'));
81
        $module = $input->getArgument('module');
82 1
83 1
        // template data
84
        $data = [
85
            'model' => $model,
86
            'module' => $module
87 1
        ];
88 1
89 1
        // generate CRUD class
90
        $this->write(" |> Generate CRUD class <info>$model\\Crud</info>");
91 1
        $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Crud.php';
0 ignored issues
show
The constant Bluzman\Command\Generate\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
        $this->generateFile('CrudTemplate', $crudFile, $data);
93
94
        if ($module) {
95
            if (!$this->getApplication()->isModelExists($model)) {
96
                throw new InputException(
97
                    "Model $model is not exist, " .
98
                    "run command <question>bluzman generate:model $model</question> before"
99
                );
100
            }
101
102
            $this->write(" |> Generate CRUD controller <info>$module/controllers/crud.php</info>");
103
104
            $controllerFile = $this->getControllerPath($module, 'crud');
105
            $this->generateFile('CrudControllerTemplate', $controllerFile, $data);
106
107
            $this->write(" |> Generate CRUD view <info>$module/views/crud.phtml</info>");
108
109
            $tableInstance = $this->getTableInstance($model);
110
            $data['columns'] = $tableInstance::getMeta();
111
112 1
            $viewFile = $this->getViewPath($module, 'crud');
113
            $this->generateFile('CrudViewTemplate', $viewFile, $data);
114
        }
115
    }
116
117
    /**
118
     * @param InputInterface $input
119
     * @param OutputInterface $output
120 1
     * @return void
121
     * @throws \Bluzman\Generator\GeneratorException
122 1
     */
123 1
    public function verify(InputInterface $input, OutputInterface $output): void
124
    {
125 1
        $model = $input->getArgument('model');
126
        $module = $input->getArgument('module');
127
128 1
        $modelPath = $this->getApplication()->getModelPath($model);
129
130
        $paths = [
131 1
            $modelPath . DS . 'Crud.php',
0 ignored issues
show
The constant Bluzman\Command\Generate\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
132 1
        ];
133 1
134
        foreach ($paths as $path) {
135
            if (!$this->getFs()->exists($path)) {
136
                throw new Generator\GeneratorException("File `$path` is not exists");
137 1
            }
138
        }
139 1
140
        $this->write(" |> CRUD for <info>{$model}</info> has been successfully created.");
141
142
        if ($module) {
143
            $this->write(
144
                " |> <options=bold>Open page <info>/acl</info> in your browser " .
145 1
                "and set permission <info>Management</info> for <info>{$module}</info> module</>"
146
            );
147
        }
148
    }
149
}
150