|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Combustor\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
7
|
|
|
|
|
8
|
|
|
use Rougin\Combustor\Common\File; |
|
9
|
|
|
use Rougin\Combustor\Common\Tools; |
|
10
|
|
|
use Rougin\Combustor\Validator\ControllerValidator; |
|
11
|
|
|
use Rougin\Combustor\Generator\ControllerGenerator; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Create Controller Command |
|
15
|
|
|
* |
|
16
|
|
|
* Generates a Wildfire or Doctrine-based controller for CodeIgniter. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Combustor |
|
19
|
|
|
* @author Rougin Gutib <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class CreateControllerCommand extends AbstractCommand |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $command = 'controller'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Executes the command. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
32
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
33
|
|
|
* @return object|\Symfony\Component\Console\Output\OutputInterface |
|
34
|
|
|
*/ |
|
35
|
21 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
36
|
|
|
{ |
|
37
|
21 |
|
$fileName = ucfirst(plural($input->getArgument('name'))); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
21 |
|
if ($input->getOption('keep')) { |
|
40
|
12 |
|
$fileName = ucfirst($input->getArgument('name')); |
|
|
|
|
|
|
41
|
8 |
|
} |
|
42
|
|
|
|
|
43
|
21 |
|
$path = APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $fileName . '.php'; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$info = [ |
|
46
|
21 |
|
'name' => $fileName, |
|
47
|
21 |
|
'type' => 'controller', |
|
48
|
7 |
|
'path' => $path |
|
49
|
14 |
|
]; |
|
50
|
|
|
|
|
51
|
21 |
|
$validator = new ControllerValidator($input->getOption('camel'), $info); |
|
52
|
|
|
|
|
53
|
21 |
|
if ($validator->fails()) { |
|
54
|
12 |
|
$message = $validator->getMessage(); |
|
55
|
|
|
|
|
56
|
12 |
|
return $output->writeln('<error>' . $message . '</error>'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$data = [ |
|
60
|
12 |
|
'file' => $info, |
|
61
|
12 |
|
'isCamel' => $input->getOption('camel'), |
|
62
|
12 |
|
'name' => $input->getArgument('name'), |
|
63
|
12 |
|
'title' => strtolower($fileName), |
|
64
|
12 |
|
'type' => $validator->getLibrary() |
|
65
|
8 |
|
]; |
|
66
|
|
|
|
|
67
|
12 |
|
$generator = new ControllerGenerator($this->describe, $data); |
|
68
|
|
|
|
|
69
|
12 |
|
$result = $generator->generate(); |
|
70
|
12 |
|
$controller = $this->renderer->render('Controller.tpl', $result); |
|
71
|
12 |
|
$message = 'The controller "' . $fileName . '" has been created successfully!'; |
|
72
|
|
|
|
|
73
|
12 |
|
$file = new File($path); |
|
74
|
|
|
|
|
75
|
12 |
|
$file->putContents($controller); |
|
76
|
12 |
|
$file->close(); |
|
77
|
|
|
|
|
78
|
12 |
|
return $output->writeln('<info>' . $message . '</info>'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|