|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\generator\ember; |
|
3
|
|
|
|
|
4
|
|
|
use phootwork\collection\ArrayList; |
|
5
|
|
|
use phootwork\collection\Map; |
|
6
|
|
|
use gossi\codegen\utils\Writer; |
|
7
|
|
|
|
|
8
|
|
|
class EmberClassGenerator { |
|
9
|
|
|
|
|
10
|
|
|
private $name; |
|
|
|
|
|
|
11
|
|
|
private $parent; |
|
12
|
|
|
private $imports; |
|
13
|
|
|
private $properties; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($parent = null) { |
|
16
|
|
|
$this->imports = new ArrayList(); |
|
17
|
|
|
$this->properties = new Map(); |
|
18
|
|
|
|
|
19
|
|
|
if ($parent === null) { |
|
20
|
|
|
$parent = 'Ember.Object'; |
|
21
|
|
|
$this->addImport('Ember', 'ember'); |
|
22
|
|
|
} |
|
23
|
|
|
$this->parent = $parent; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function addImport($import, $from) { |
|
27
|
|
|
$this->imports->add(['import' => $import, 'from' => $from]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setProperty($property, $value) { |
|
31
|
|
|
$this->properties->set($property, $value); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function generate() { |
|
35
|
|
|
$writer = new Writer(); |
|
36
|
|
|
|
|
37
|
|
|
// add imports first |
|
38
|
|
|
foreach ($this->imports as $import) { |
|
39
|
|
|
$writer->writeln(sprintf('import %s from \'%s\';', $import['import'], $import['from'])); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($this->imports->size() > 0) { |
|
43
|
|
|
$writer->writeln(''); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// class signature |
|
47
|
|
|
$writer->writeln(sprintf('export default %s.extend({', $this->parent)); |
|
48
|
|
|
$writer->indent(); |
|
49
|
|
|
|
|
50
|
|
|
// properties |
|
51
|
|
|
$propper = new Writer(); |
|
52
|
|
|
foreach ($this->properties as $prop => $value) { |
|
53
|
|
|
$propper->writeln(sprintf('%s: %s,', $prop, $value)); |
|
54
|
|
|
} |
|
55
|
|
|
$writer->writeln(rtrim($propper->getContent(), ", \n")); |
|
56
|
|
|
|
|
57
|
|
|
// class foot |
|
58
|
|
|
$writer->outdent(); |
|
59
|
|
|
$writer->writeln('});'); |
|
60
|
|
|
|
|
61
|
|
|
return $writer->getContent(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.