|
1
|
|
|
<?php |
|
2
|
|
|
namespace gossi\codegen\generator; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\codegen\config\CodeGeneratorConfig; |
|
5
|
|
|
use gossi\codegen\model\GenerateableInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Code generator |
|
9
|
|
|
* |
|
10
|
|
|
* Generates code for any generateable model |
|
11
|
|
|
* |
|
12
|
|
|
* @author Thomas Gossmann |
|
13
|
|
|
*/ |
|
14
|
|
|
class CodeGenerator { |
|
15
|
|
|
|
|
16
|
|
|
const SORT_USESTATEMENTS_DEFAULT = 'default'; |
|
17
|
|
|
|
|
18
|
|
|
const SORT_CONSTANTS_DEFAULT = 'default'; |
|
19
|
|
|
|
|
20
|
|
|
const SORT_PROPERTIES_DEFAULT = 'default'; |
|
21
|
|
|
|
|
22
|
|
|
const SORT_METHODS_DEFAULT = 'default'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var CodeGeneratorConfig */ |
|
25
|
|
|
protected $config; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
* @param CodeGeneratorConfig|array $config |
|
30
|
|
|
*/ |
|
31
|
8 |
|
public function __construct($config = null) { |
|
32
|
8 |
|
$this->configure($config); |
|
33
|
8 |
|
$this->generator = new ModelGenerator($this->config); |
|
|
|
|
|
|
34
|
8 |
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
protected function configure($config = null) { |
|
37
|
2 |
|
if (is_array($config)) { |
|
38
|
1 |
|
$this->config = new CodeGeneratorConfig($config); |
|
39
|
1 |
|
} else if ($config instanceof CodeGeneratorConfig) { |
|
40
|
1 |
|
$this->config = $config; |
|
41
|
|
|
} else { |
|
42
|
1 |
|
$this->config = new CodeGeneratorConfig(); |
|
43
|
|
|
} |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the used configuration |
|
48
|
|
|
* |
|
49
|
|
|
* @return CodeGeneratorConfig |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function getConfig() { |
|
52
|
1 |
|
return $this->config; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Generates code from a given model |
|
57
|
|
|
* |
|
58
|
|
|
* @param GenerateableInterface $model |
|
59
|
|
|
* @return string the generated code |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function generate(GenerateableInterface $model) { |
|
62
|
|
|
// if ($this->config->getGenerateDocblock()) { |
|
|
|
|
|
|
63
|
|
|
// $model->generateDocblock(); |
|
64
|
|
|
// } |
|
65
|
|
|
|
|
66
|
6 |
|
$generator = new ModelGenerator($this->config); |
|
67
|
6 |
|
return $generator->generate($model); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: