1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\generator\ember; |
3
|
|
|
|
4
|
|
|
use keeko\tools\generator\ActionNameGenerator; |
5
|
|
|
use keeko\tools\model\Relationship; |
6
|
|
|
use Propel\Generator\Model\Table; |
7
|
|
|
|
8
|
|
|
class EmberAbilitiesGenerator extends AbstractEmberGenerator { |
9
|
|
|
|
10
|
|
|
private $template = 'Ember.computed(function() { |
11
|
|
|
return this.get(\'session\').hasPermission(\'%s\', \'%s\'); |
12
|
|
|
})'; |
13
|
|
|
|
14
|
|
|
public function generate(Table $model) { |
15
|
|
|
$class = new EmberClassGenerator('Ability'); |
16
|
|
|
$class->addImport('Ember', 'ember'); |
17
|
|
|
$class->addImport('{ Ability }', 'ember-can'); |
18
|
|
|
|
19
|
|
|
// actions |
20
|
|
|
$this->generateActions($class, $model); |
21
|
|
|
|
22
|
|
|
// relationships |
23
|
|
|
$this->generateRelationships($class, $model); |
24
|
|
|
|
25
|
|
|
return $class->generate(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function generateActions(EmberClassGenerator $class, Table $model) { |
29
|
|
|
$types = $model->isReadOnly() ? ['read', 'list'] : ['read', 'list', 'create', 'update', 'delete']; |
30
|
|
|
$packageName = $this->getPackage()->getFullName(); |
31
|
|
|
|
32
|
|
|
foreach ($types as $type) { |
33
|
|
|
$actionName = ActionNameGenerator::generate($type, $model); |
34
|
|
|
$prop = sprintf('can%s', ucfirst($type)); |
35
|
|
|
$value = sprintf($this->template, $packageName, $actionName); |
36
|
|
|
$class->setProperty($prop, $value); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function generateRelationships(EmberClassGenerator $class, Table $model) { |
41
|
|
|
$relationships = $this->modelService->getRelationships($model); |
42
|
|
|
$packageName = $this->getPackage()->getFullName(); |
43
|
|
|
|
44
|
|
|
foreach ($relationships->getAll() as $relationship) { |
45
|
|
|
$types = $relationship->getType() == Relationship::ONE_TO_ONE |
46
|
|
|
? ['read', 'update'] : ['read', 'update', 'add', 'remove']; |
47
|
|
|
|
48
|
|
|
foreach ($types as $type) { |
49
|
|
|
$prop = sprintf('can%s%s', ucfirst($type), $relationship->getRelatedName()); |
50
|
|
|
$actionName = ActionNameGenerator::generateRelationship($type, $relationship); |
51
|
|
|
$value = sprintf($this->template, $packageName, $actionName); |
52
|
|
|
$class->setProperty($prop, $value); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
} |