1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\command; |
3
|
|
|
|
4
|
|
|
use keeko\framework\utils\NameUtils; |
5
|
|
|
use keeko\tools\generator\ember\EmberModelGenerator; |
6
|
|
|
use keeko\tools\model\Project; |
7
|
|
|
use keeko\tools\ui\EmberUI; |
8
|
|
|
use phootwork\file\File; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use phootwork\lang\Text; |
12
|
|
|
|
13
|
|
|
class GenerateEmberModelsCommand extends AbstractEmberCommand { |
14
|
|
|
|
15
|
|
|
protected function configure() { |
16
|
|
|
$this |
17
|
|
|
->setName('generate:ember:models') |
18
|
|
|
->setDescription('Generates ember models'); |
19
|
|
|
|
20
|
|
|
parent::configure(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) { |
24
|
|
|
$ui = new EmberUI($this); |
25
|
|
|
$ui->show(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
29
|
|
|
$project = $this->getProject(); |
30
|
|
|
$package = $project->getPackage(); |
31
|
|
|
if ($package->getType() != 'keeko-module') { |
32
|
|
|
throw new \RuntimeException('Package must be of type `keeko-module`'); |
33
|
|
|
} |
34
|
|
|
$module = $project->getPackage()->getKeeko()->getModule(); |
35
|
|
|
$this->modelService->read($project); |
36
|
|
|
$models = $this->modelService->getModels(); |
37
|
|
|
$generator = new EmberModelGenerator($this->service, $project); |
38
|
|
|
$output = $this->io->getOutput(); |
39
|
|
|
|
40
|
|
|
foreach ($models as $model) { |
41
|
|
|
$contents = $generator->generate($model); |
42
|
|
|
$filename = sprintf('%s/ember/app/models/%s/%s.js', $this->project->getRootPath(), |
43
|
|
|
str_replace('.', '/', $module->getSlug()), NameUtils::dasherize($model->getPhpName())); |
44
|
|
|
$file = new File($filename); |
45
|
|
|
$overwrite = true; |
46
|
|
|
if ($file->exists()) { |
47
|
|
|
$contents = new Text($file->read()); |
48
|
|
|
$overwrite = !$contents->startsWith('// overwrite: false'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($overwrite) { |
52
|
|
|
$file->write($contents); |
|
|
|
|
53
|
|
|
$output->writeln(sprintf('Model <info>%s</info> written at <info>%s</info>', |
54
|
|
|
$model->getOriginCommonName(), $filename)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.