|
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
|
|
|
$models = $this->getModels(); |
|
47
|
|
|
$generator = new EmberModelGenerator($this->service, $project); |
|
48
|
|
|
$output = $this->io->getOutput(); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($models as $model) { |
|
51
|
|
|
$contents = $generator->generate($model); |
|
52
|
|
|
$filename = sprintf('%s/ember/app/models/%s/%s.js', $this->project->getRootPath(), |
|
53
|
|
|
$module->getSlug(), NameUtils::dasherize($model->getPhpName())); |
|
54
|
|
|
$file = new File($filename); |
|
55
|
|
|
$file->write($contents); |
|
56
|
|
|
$output->writeln(sprintf('Model <info>%s</info> written at <info>%s</info>', |
|
57
|
|
|
$model->getOriginCommonName(), $filename)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function getModels() { |
|
62
|
|
|
$input = $this->io->getInput(); |
|
63
|
|
|
$project = $this->getProject(); |
|
64
|
|
|
if ($project->hasSchemaFile()) { |
|
65
|
|
|
$input->setOption('schema', $project->getSchemaFileName()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$models = []; |
|
69
|
|
|
$database = $this->modelService->getDatabase(); |
|
70
|
|
|
foreach ($database->getTables() as $table) { |
|
71
|
|
|
if ($table->getNamespace() == $database->getNamespace() && !$table->isCrossRef()) { |
|
72
|
|
|
$models[] = $table; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $models; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function getProject($packageName = null) { |
|
|
|
|
|
|
80
|
|
|
if ($this->prj === null) { |
|
81
|
|
|
if ($packageName === null) { |
|
82
|
|
|
$input = $this->io->getInput(); |
|
83
|
|
|
$packageName = $input->getOption('package'); |
|
84
|
|
|
if (empty($packageName)) { |
|
85
|
|
|
$packageName = $this->package->getFullName(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($this->package->getFullName() == $packageName) { |
|
90
|
|
|
$this->prj = $this->project; |
|
91
|
|
|
} else { |
|
92
|
|
|
$path = $this->getPackagePath($packageName); |
|
93
|
|
|
$this->prj = new Project($path); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
return $this->prj; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function getPackagePath($packageName) { |
|
100
|
|
|
if ($this->package->getFullName() == $packageName) { |
|
101
|
|
|
return dirname($this->project->getComposerFileName()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return 'vendor/' . $packageName; |
|
105
|
|
|
} |
|
106
|
|
|
} |
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
@returnannotation as described here.