1 | <?php |
||
13 | abstract class AbstractScaffoldingCommand extends Command |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $type; |
||
19 | |||
20 | /** |
||
21 | * Command Config |
||
22 | */ |
||
23 | 4 | protected function configure() |
|
24 | { |
||
25 | 4 | $this |
|
26 | 4 | ->setName('create:' . $this->type) |
|
27 | 4 | ->setDescription('Create a new ' . ucfirst($this->type)) |
|
28 | 4 | ->addArgument( |
|
29 | 4 | 'namespace', |
|
30 | 4 | InputArgument::REQUIRED, |
|
31 | 'Namespace in Dot-Notation e.g. Acme.Api' |
||
32 | 4 | ) |
|
33 | 4 | ->addArgument( |
|
34 | 4 | 'name', |
|
35 | 4 | InputArgument::REQUIRED, |
|
36 | 4 | 'Name of the ' . ucfirst($this->type) |
|
37 | 4 | ) |
|
38 | ; |
||
39 | 4 | } |
|
40 | |||
41 | /** |
||
42 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
43 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
44 | * @param string $path |
||
45 | */ |
||
46 | 4 | protected function createFileTemplate(InputInterface $input, OutputInterface $output, $path) |
|
47 | { |
||
48 | // args |
||
49 | 4 | $namespace = str_replace('.', '\\', $input->getArgument('namespace')); |
|
50 | 4 | $name = $input->getArgument('name'); |
|
51 | 4 | $filename = $path . str_plural($name) . '.php'; |
|
52 | |||
53 | // only write file if path exists and file does not exist yet |
||
54 | 4 | if (file_exists($path) && !file_exists($filename)) { |
|
55 | // write file |
||
56 | 4 | $file = fopen($filename, "w+"); |
|
57 | 4 | fwrite($file, $this->template($namespace, $name)); |
|
58 | 4 | fclose($file); |
|
59 | |||
60 | // output |
||
61 | 4 | $output->writeln("<info>Created {$this->type}</info>"); |
|
62 | 4 | return; |
|
63 | } |
||
64 | |||
65 | // error |
||
66 | 2 | $output->writeln("<error>Could not create file $name</error>"); |
|
67 | 2 | } |
|
68 | |||
69 | /** |
||
70 | * File Template |
||
71 | * @param string $namespace |
||
72 | * @param string $name |
||
73 | * @param string $topNSPart |
||
74 | * @return string |
||
75 | */ |
||
76 | abstract protected function template($namespace, $name, $topNSPart = ""); |
||
77 | } |
||
78 |