|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Combustor\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Rougin\Describe\Describe; |
|
6
|
|
|
use Rougin\Combustor\Common\Tools; |
|
7
|
|
|
use Rougin\Combustor\Common\Inflector; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* View Generator |
|
11
|
|
|
* |
|
12
|
|
|
* Generates CodeIgniter-based views. |
|
13
|
|
|
* |
|
14
|
|
|
* @package Combustor |
|
15
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class ViewGenerator implements GeneratorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Rougin\Describe\Describe |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $describe; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $data = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \Rougin\Describe\Describe $describe |
|
31
|
|
|
* @param array $data |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public function __construct(Describe $describe, array $data) |
|
34
|
|
|
{ |
|
35
|
3 |
|
$this->describe = $describe; |
|
36
|
3 |
|
$this->data = $data; |
|
37
|
3 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Prepares the data before generation. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $data |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function prepareData(array &$data) |
|
46
|
|
|
{ |
|
47
|
|
|
$bootstrap = [ |
|
48
|
3 |
|
'button' => 'btn btn-default', |
|
49
|
3 |
|
'buttonPrimary' => 'btn btn-primary', |
|
50
|
3 |
|
'formControl' => 'form-control', |
|
51
|
3 |
|
'formGroup' => 'form-group col-lg-12 col-md-12 col-sm-12 col-xs-12', |
|
52
|
3 |
|
'label' => 'control-label', |
|
53
|
3 |
|
'table' => 'table table table-striped table-hover', |
|
54
|
|
|
'textRight' => 'text-right' |
|
55
|
3 |
|
]; |
|
56
|
|
|
|
|
57
|
3 |
|
if ($data['isBootstrap']) { |
|
58
|
3 |
|
$data['bootstrap'] = $bootstrap; |
|
59
|
3 |
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
$data['camel'] = []; |
|
62
|
3 |
|
$data['underscore'] = []; |
|
63
|
3 |
|
$data['foreignKeys'] = []; |
|
64
|
3 |
|
$data['primaryKeys'] = []; |
|
65
|
|
|
|
|
66
|
3 |
|
$data['plural'] = Inflector::plural($data['name']); |
|
67
|
3 |
|
$data['singular'] = Inflector::singular($data['name']); |
|
68
|
|
|
|
|
69
|
3 |
|
$data['primaryKey'] = 'get_'.$this->describe->getPrimaryKey( |
|
70
|
3 |
|
$data['name'] |
|
71
|
3 |
|
); |
|
72
|
|
|
|
|
73
|
|
|
// Workaround... |
|
74
|
3 |
|
if ($data['primaryKey'] == 'get_') { |
|
75
|
|
|
$data['primaryKey'] = 'get_'.$this->describe->getPrimaryKey( |
|
76
|
|
|
Inflector::singular($data['name']) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
if ($this->data['isCamel']) { |
|
81
|
3 |
|
$data['primaryKey'] = Inflector::camelize($data['primaryKey']); |
|
82
|
3 |
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
$data['columns'] = $this->describe->getTable( |
|
85
|
3 |
|
$data['name'] |
|
86
|
3 |
|
); |
|
87
|
|
|
|
|
88
|
|
|
// Workaround... |
|
89
|
3 |
|
if (empty($data['columns'])) { |
|
90
|
|
|
$data['columns'] = $this->describe->getTable( |
|
91
|
|
|
Inflector::singular($data['name']) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
3 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Generates set of code based on data. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function generate() |
|
102
|
|
|
{ |
|
103
|
3 |
|
$this->prepareData($this->data); |
|
104
|
|
|
|
|
105
|
3 |
|
foreach ($this->data['columns'] as $column) { |
|
106
|
3 |
|
$field = strtolower($column->getField()); |
|
107
|
3 |
|
$accessor = 'get_'.$field; |
|
108
|
3 |
|
$mutator = 'set_'.$field; |
|
109
|
|
|
|
|
110
|
3 |
|
$this->data['camel'][$field] = [ |
|
111
|
3 |
|
'field' => lcfirst(Inflector::camelize($field)), |
|
112
|
3 |
|
'accessor' => lcfirst(Inflector::camelize($accessor)), |
|
113
|
3 |
|
'mutator' => lcfirst(Inflector::camelize($mutator)) |
|
114
|
3 |
|
]; |
|
115
|
|
|
|
|
116
|
3 |
|
$this->data['underscore'][$field] = [ |
|
117
|
3 |
|
'field' => lcfirst(Inflector::underscore($field)), |
|
118
|
3 |
|
'accessor' => lcfirst(Inflector::underscore($accessor)), |
|
119
|
3 |
|
'mutator' => lcfirst(Inflector::underscore($mutator)) |
|
120
|
3 |
|
]; |
|
121
|
|
|
|
|
122
|
3 |
|
if ($column->isForeignKey()) { |
|
123
|
|
|
$referencedTable = Tools::stripTableSchema( |
|
124
|
|
|
$column->getReferencedTable() |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$this->data['foreignKeys'][$field] = Inflector::plural( |
|
128
|
|
|
$referencedTable |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
$singular = $field.'_singular'; |
|
132
|
|
|
|
|
133
|
|
|
$this->data['foreignKeys'][$singular] = Inflector::singular( |
|
134
|
|
|
$referencedTable |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
$this->data['primaryKeys'][$field] = 'get_'. |
|
138
|
|
|
$this->describe->getPrimaryKey($referencedTable); |
|
139
|
|
|
|
|
140
|
|
View Code Duplication |
if ($this->data['isCamel']) { |
|
|
|
|
|
|
141
|
|
|
$this->data['primaryKeys'][$field] = Inflector::camelize( |
|
142
|
|
|
$this->data['primaryKeys'][$field] |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
3 |
|
} |
|
147
|
|
|
|
|
148
|
3 |
|
return $this->data; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.