Completed
Pull Request — master (#20)
by Pavel
10:22
created

GenerateCommandCommand::getPathForCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Question\Question;
9
10
/**
11
 * GenerateCommandCommand
12
 */
13
class GenerateCommandCommand extends Command
14
{
15
    /**
16
     * Configuration of command
17
     */
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('generate:command')
22
            ->setDescription('Generate new command')
23
            ->setHelp('<info>php partisan generate:command</info>')
24
        ;
25
    }
26
27
    /**
28
     * Execute method of command
29
     *
30
     * @param InputInterface $input
31
     * @param OutputInterface $output
32
     *
33
     * @return void
34
     * @throws \Exception
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $output->writeln(['<comment>Welcome to the command generator</comment>']);
39
40
        $helper   = $this->getHelper('question');
41
        $question = new Question('<info>Please enter the name of the command class: </info>', 'DefaultCommand');
42
        $question->setValidator(function ($answer) {
43
            if ('Command' !== substr($answer, -7)) {
44
                throw new \RunTimeException('The name of the command should be suffixed with \'Command\'');
45
            }
46
47
            if (true === file_exists($this->getPathForCommand($answer))) {
48
                throw new \RunTimeException('This command already exists');
49
            }
50
51
            return $answer;
52
        });
53
54
        $commandClass = $helper->ask($input, $output, $question);
55
        $commandName  = $this->colonize($commandClass);
56
        $path         = $this->getPathForCommand($commandClass);
57
        $placeHolders = [
58
            '<class>',
59
            '<name>',
60
        ];
61
        $replacements = [
62
            $commandClass,
63
            $commandName,
64
        ];
65
66
        $this->generateCode($placeHolders, $replacements, 'CommandTemplate.tpl', $path);
67
68
        $output->writeln(sprintf('Generated new command class to "<info>%s</info>"', realpath($path)));
69
70
        return;
71
    }
72
73
    /**
74
     * Generate code
75
     *
76
     * @param array  $placeHolders
77
     * @param array  $replacements
78
     * @param string $templateName
79
     * @param string $resultPath
80
     * @return bool|int
81
     */
82 View Code Duplication
    public function generateCode($placeHolders, $replacements, $templateName, $resultPath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $templatePath = CODE_TEMPLATE_PATH . '/' . $templateName;
85
        if (false === file_exists($templatePath)) {
86
            throw new \RunTimeException(sprintf('Not found template %s', $templatePath));
87
        }
88
89
        $template = file_get_contents($templatePath);
90
91
        $code = str_replace($placeHolders, $replacements, $template);
92
93
        return file_put_contents($resultPath, $code);
94
    }
95
96
    /**
97
     * @param string $commandClass
98
     * @return string
99
     * @throws \Exception
100
     */
101
    private function getPathForCommand($commandClass)
102
    {
103
        $dir = rtrim(COMMANDS_PATH, '/');
104
        if (!file_exists($dir)) {
105
            throw new \Exception(sprintf('Commands directory "%s" does not exist.', $dir));
106
        }
107
108
        return $dir . '/'.$commandClass.'.php';
109
    }
110
111
    /**
112
     * Colonize command name
113
     *
114
     * @param $word
115
     * @return string
116
     */
117
    private function colonize($word)
118
    {
119
        $word = str_replace('Command', '', $word);
120
121
        return  strtolower(preg_replace('/[^A-Z^a-z^0-9]+/',':',
122
            preg_replace('/([a-zd])([A-Z])/','\1:\2',
123
                preg_replace('/([A-Z]+)([A-Z][a-z])/','\1:\2',$word))));
124
    }
125
}
126