Completed
Push — master ( 966759...8da8ca )
by Thomas
07:52
created

GenerateEmberModelsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
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) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
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
}