1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\command; |
3
|
|
|
|
4
|
|
|
use keeko\tools\command\AbstractKeekoCommand; |
5
|
|
|
use keeko\tools\model\Project; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use keeko\tools\generator\ember\EmberModelGenerator; |
10
|
|
|
use keeko\framework\utils\NameUtils; |
11
|
|
|
use keeko\tools\ui\EmberModelsUI; |
12
|
|
|
use phootwork\file\File; |
13
|
|
|
|
14
|
|
|
class GenerateEmberModelsCommand extends AbstractKeekoCommand { |
15
|
|
|
|
16
|
|
|
private $prj; |
17
|
|
|
|
18
|
|
|
protected function configure() { |
19
|
|
|
$this |
20
|
|
|
->setName('generate:ember:models') |
21
|
|
|
->setDescription('Generates ember models') |
22
|
|
|
->addOption( |
23
|
|
|
'package', |
24
|
|
|
'', |
25
|
|
|
InputOption::VALUE_OPTIONAL, |
26
|
|
|
'The package from which the models should be generated' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
$this->configureGenerateOptions(); |
30
|
|
|
|
31
|
|
|
parent::configure(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) { |
35
|
|
|
$ui = new EmberModelsUI($this); |
36
|
|
|
$ui->show(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
40
|
|
|
$project = $this->getProject(); |
41
|
|
|
$package = $project->getPackage(); |
42
|
|
|
if ($package->getType() != 'keeko-module') { |
43
|
|
|
throw new \RuntimeException('Package must be of type `keeko-module`'); |
44
|
|
|
} |
45
|
|
|
$module = $project->getPackage()->getKeeko()->getModule(); |
46
|
|
|
$this->modelService->read($project); |
47
|
|
|
$models = $this->modelService->getModels(); |
48
|
|
|
$generator = new EmberModelGenerator($this->service, $project); |
49
|
|
|
$output = $this->io->getOutput(); |
50
|
|
|
|
51
|
|
|
foreach ($models as $model) { |
52
|
|
|
$contents = $generator->generate($model); |
53
|
|
|
$filename = sprintf('%s/ember/app/models/%s/%s.js', $this->project->getRootPath(), |
54
|
|
|
$module->getSlug(), NameUtils::dasherize($model->getPhpName())); |
55
|
|
|
$file = new File($filename); |
56
|
|
|
$file->write($contents); |
57
|
|
|
$output->writeln(sprintf('Model <info>%s</info> written at <info>%s</info>', |
58
|
|
|
$model->getOriginCommonName(), $filename)); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function getProject($packageName = null) { |
|
|
|
|
63
|
|
|
if ($this->prj === null) { |
64
|
|
|
if ($packageName === null) { |
65
|
|
|
$input = $this->io->getInput(); |
66
|
|
|
$packageName = $input->getOption('package'); |
67
|
|
|
if (empty($packageName)) { |
68
|
|
|
$packageName = $this->package->getFullName(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($this->package->getFullName() == $packageName) { |
73
|
|
|
$this->prj = $this->project; |
74
|
|
|
} else { |
75
|
|
|
$path = $this->getPackagePath($packageName); |
76
|
|
|
$this->prj = new Project($path); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
return $this->prj; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function getPackagePath($packageName) { |
83
|
|
|
if ($this->package->getFullName() == $packageName) { |
84
|
|
|
return dirname($this->project->getComposerFileName()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return 'vendor/' . $packageName; |
88
|
|
|
} |
89
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.