1
|
|
|
<?php |
2
|
|
|
namespace Atog\Api\Console; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Command\Command; |
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ModelCommand |
11
|
|
|
* @package Atog\Console |
12
|
|
|
*/ |
13
|
|
|
class ModelCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Command Config |
17
|
|
|
*/ |
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
$this |
21
|
|
|
->setName('create:model') |
22
|
|
|
->setDescription('Create a new Model') |
23
|
|
|
->addArgument( |
24
|
|
|
'namespace', |
25
|
|
|
InputArgument::REQUIRED, |
26
|
|
|
'Namespace in Dot-Notation e.g. Acme.Api' |
27
|
|
|
) |
28
|
|
|
->addArgument( |
29
|
|
|
'name', |
30
|
|
|
InputArgument::REQUIRED, |
31
|
|
|
'Name of the Model' |
32
|
|
|
) |
33
|
|
|
; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Execute command |
38
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
39
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
// args |
45
|
|
|
$namespace = str_replace('.', '\\', $input->getArgument('namespace')); |
46
|
|
|
$name = $input->getArgument('name'); |
47
|
|
|
$path = $this->getApplication()->modelPath(); |
|
|
|
|
48
|
|
|
$file = $path . str_plural($name) . '.php'; |
49
|
|
|
|
50
|
|
|
// only write file if path exists and file does not exist yet |
51
|
|
|
if (file_exists($path) && !file_exists($file)) { |
52
|
|
|
// write file |
53
|
|
|
$endpoint = fopen($file, "w+"); |
54
|
|
|
fwrite($endpoint, $this->template($namespace, $name)); |
55
|
|
|
fclose($endpoint); |
56
|
|
|
|
57
|
|
|
// output |
58
|
|
|
$output->writeln("<info>Created Model: $name</info>"); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// error |
63
|
|
|
$output->writeln("<error>Could not create file $file at $path</error>"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* File Template |
68
|
|
|
* @param string $namespace |
69
|
|
|
* @param string $name |
70
|
|
|
* @param string $modelNS |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function template($namespace, $name, $modelNS = "Models") |
74
|
|
|
{ |
75
|
|
|
$className = str_plural($name); |
76
|
|
|
$namespace = "{$namespace}\\{$modelNS}"; |
77
|
|
|
|
78
|
|
|
return <<< EOT |
79
|
|
|
<?php |
80
|
|
|
namespace $namespace; |
81
|
|
|
|
82
|
|
|
use Atog\Api\Model; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Class $className |
86
|
|
|
* @package $namespace |
87
|
|
|
*/ |
88
|
|
|
class $className extends Model |
89
|
|
|
{ |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
EOT; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.