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 EndpointCommand |
11
|
|
|
* @package Atog\Console |
12
|
|
|
*/ |
13
|
|
|
class EndpointCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Command Config |
17
|
|
|
*/ |
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
$this |
21
|
|
|
->setName('create:endpoint') |
22
|
|
|
->setDescription('Create a new Endpoint') |
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 Endpoint' |
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()->endpointPath(); |
|
|
|
|
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 Endpoint: $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 $endpointNS |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function template($namespace, $name, $endpointNS = "Endpoints") |
74
|
|
|
{ |
75
|
|
|
$className = str_plural($name); |
76
|
|
|
$endpointName = strtolower($name); |
77
|
|
|
$namespace = "{$namespace}\\{$endpointNS}"; |
78
|
|
|
|
79
|
|
|
return <<< EOT |
80
|
|
|
<?php |
81
|
|
|
namespace $namespace; |
82
|
|
|
|
83
|
|
|
use Atog\Api\Endpoint; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Class $className |
87
|
|
|
* @package $namespace |
88
|
|
|
*/ |
89
|
|
|
class $className extends Endpoint |
90
|
|
|
{ |
91
|
|
|
/** |
92
|
|
|
* @var string |
93
|
|
|
*/ |
94
|
|
|
protected \$endpoint = '$endpointName'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
EOT; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.