rougin /
combustor
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Rougin\Combustor\Commands; |
||
| 4 | |||
| 5 | use Symfony\Component\Console\Input\InputOption; |
||
| 6 | use Symfony\Component\Console\Input\InputArgument; |
||
| 7 | use Symfony\Component\Console\Input\InputInterface; |
||
| 8 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 9 | |||
| 10 | use Rougin\Combustor\Common\File; |
||
| 11 | use Rougin\Combustor\Common\Tools; |
||
| 12 | use Rougin\Combustor\Validator\ModelValidator; |
||
| 13 | use Rougin\Combustor\Generator\ModelGenerator; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Create Model Command |
||
| 17 | * |
||
| 18 | * Generates a Wildfire or Doctrine-based model for CodeIgniter |
||
| 19 | * |
||
| 20 | * @package Combustor |
||
| 21 | * @author Rougin Gutib <[email protected]> |
||
| 22 | */ |
||
| 23 | class CreateModelCommand extends AbstractCommand |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $command = 'model'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Sets the configurations of the specified command. |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | 15 | protected function configure() |
|
| 36 | { |
||
| 37 | 10 | $this |
|
| 38 | 15 | ->setName('create:model') |
|
| 39 | 15 | ->setDescription('Creates a new model') |
|
| 40 | 15 | ->addArgument( |
|
| 41 | 15 | 'name', |
|
| 42 | 15 | InputArgument::REQUIRED, |
|
| 43 | 5 | 'Name of the table' |
|
| 44 | 15 | )->addOption( |
|
| 45 | 15 | 'camel', |
|
| 46 | 15 | null, |
|
| 47 | 15 | InputOption::VALUE_NONE, |
|
| 48 | 5 | 'Uses the camel case naming convention' |
|
| 49 | 15 | )->addOption( |
|
| 50 | 15 | 'lowercase', |
|
| 51 | 15 | null, |
|
| 52 | 15 | InputOption::VALUE_NONE, |
|
| 53 | 5 | 'Keeps the first character of the name to lowercase' |
|
| 54 | 10 | ); |
|
| 55 | 15 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Executes the command. |
||
| 59 | * |
||
| 60 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 61 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
| 62 | * @return object|\Symfony\Component\Console\Output\OutputInterface |
||
| 63 | */ |
||
| 64 | 12 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 65 | { |
||
| 66 | 12 | $fileName = ucfirst(singular($input->getArgument('name'))); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 67 | |||
| 68 | 12 | $path = APPPATH . 'models' . DIRECTORY_SEPARATOR . $fileName . '.php'; |
|
|
0 ignored issues
–
show
|
|||
| 69 | |||
| 70 | $info = [ |
||
| 71 | 12 | 'name' => $fileName, |
|
| 72 | 12 | 'type' => 'model', |
|
| 73 | 4 | 'path' => $path |
|
| 74 | 8 | ]; |
|
| 75 | |||
| 76 | 12 | $validator = new ModelValidator($input->getOption('camel'), $info); |
|
| 77 | |||
| 78 | 12 | if ($validator->fails()) { |
|
| 79 | 3 | $message = $validator->getMessage(); |
|
| 80 | |||
| 81 | 3 | return $output->writeln('<error>' . $message . '</error>'); |
|
| 82 | } |
||
| 83 | |||
| 84 | $data = [ |
||
| 85 | 9 | 'file' => $info, |
|
| 86 | 9 | 'isCamel' => $input->getOption('camel'), |
|
| 87 | 9 | 'name' => $input->getArgument('name'), |
|
| 88 | 9 | 'type' => $validator->getLibrary() |
|
| 89 | 6 | ]; |
|
| 90 | |||
| 91 | 9 | $generator = new ModelGenerator($this->describe, $data); |
|
| 92 | |||
| 93 | 9 | $result = $generator->generate(); |
|
| 94 | 9 | $model = $this->renderer->render('Model.tpl', $result); |
|
| 95 | 9 | $message = 'The model "' . $fileName . '" has been created successfully!'; |
|
| 96 | |||
| 97 | 9 | $file = new File($path); |
|
| 98 | |||
| 99 | 9 | $file->putContents($model); |
|
| 100 | 9 | $file->close(); |
|
| 101 | |||
| 102 | 9 | return $output->writeln('<info>' . $message . '</info>'); |
|
| 103 | } |
||
| 104 | } |
||
| 105 |