1 | <?php |
||
28 | abstract class BaseGenerator |
||
29 | { |
||
30 | /** |
||
31 | * @var Session |
||
32 | */ |
||
33 | private $session; |
||
34 | |||
35 | protected $schema; |
||
36 | protected $relation; |
||
37 | protected $filename; |
||
38 | protected $namespace; |
||
39 | protected $flexible_container; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Constructor |
||
44 | * |
||
45 | * @access public |
||
46 | * @param Session $session |
||
47 | * @param string $schema |
||
48 | * @param string $relation |
||
49 | * @param string $filename |
||
50 | * @param string $namespace |
||
51 | * @param $flexible_container |
||
52 | */ |
||
53 | public function __construct(Session $session, $schema, $relation, $filename, $namespace, $flexible_container = null) |
||
54 | { |
||
55 | $this->session = $session; |
||
56 | $this->schema = $schema; |
||
57 | $this->relation = $relation; |
||
58 | $this->filename = $filename; |
||
59 | $this->namespace = $namespace; |
||
60 | $this->flexible_container = $flexible_container; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * outputFileCreation |
||
65 | * |
||
66 | * Output what the generator will do. |
||
67 | * |
||
68 | * @access protected |
||
69 | * @param array $output |
||
70 | * @return BaseGenerator $this |
||
71 | */ |
||
72 | protected function outputFileCreation(array &$output) |
||
73 | { |
||
74 | if (file_exists($this->filename)) { |
||
75 | $output[] = ['status' => 'ok', 'operation' => 'overwriting', 'file' => $this->filename]; |
||
76 | } else { |
||
77 | $output[] = ['status' => 'ok', 'operation' => 'creating', 'file' => $this->filename]; |
||
78 | } |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * setSession |
||
85 | * |
||
86 | * Set the session. |
||
87 | * |
||
88 | * @access protected |
||
89 | * @param Session $session |
||
90 | * @return BaseGenerator $this |
||
91 | */ |
||
92 | protected function setSession(Session $session) |
||
93 | { |
||
94 | $this->session = $session; |
||
95 | |||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * getSession |
||
101 | * |
||
102 | * Return the session is set. Throw an exception otherwise. |
||
103 | * |
||
104 | * @access protected |
||
105 | * @throws GeneratorException |
||
106 | * @return Session |
||
107 | */ |
||
108 | protected function getSession() |
||
109 | { |
||
110 | if ($this->session === null) { |
||
111 | throw new GeneratorException(sprintf("Session is not set.")); |
||
112 | } |
||
113 | |||
114 | return $this->session; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * getInspector |
||
119 | * |
||
120 | * Shortcut to session's inspector client. |
||
121 | * |
||
122 | * @access protected |
||
123 | * @return Inspector |
||
124 | */ |
||
125 | protected function getInspector() |
||
126 | { |
||
127 | return $this->getSession()->getClientUsingPooler('inspector', null); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * generate |
||
132 | * |
||
133 | * Called to generate the file. |
||
134 | * Possible options are: |
||
135 | * * force: true if files can be overwritten, false otherwise |
||
136 | * |
||
137 | * @access public |
||
138 | * @param ParameterHolder $input |
||
139 | * @param array $output |
||
140 | * @throws GeneratorException |
||
141 | * @return array $output |
||
142 | */ |
||
143 | abstract public function generate(ParameterHolder $input, array $output = []); |
||
144 | |||
145 | /** |
||
146 | * getCodeTemplate |
||
147 | * |
||
148 | * Return the code template for files to be generated. |
||
149 | * |
||
150 | * @access protected |
||
151 | * @return string |
||
152 | */ |
||
153 | abstract protected function getCodeTemplate(); |
||
154 | |||
155 | /** |
||
156 | * mergeTemplate |
||
157 | * |
||
158 | * Merge templates with given values. |
||
159 | * |
||
160 | * @access protected |
||
161 | * @param array $variables |
||
162 | * @return string |
||
163 | */ |
||
164 | protected function mergeTemplate(array $variables) |
||
176 | |||
177 | /** |
||
178 | * saveFile |
||
179 | * |
||
180 | * Write the generated content to a file. |
||
181 | * |
||
182 | * @access protected |
||
183 | * @param string $filename |
||
184 | * @param string $content |
||
185 | * @throws GeneratorException |
||
186 | * @return BaseGenerator $this |
||
187 | */ |
||
188 | protected function saveFile($filename, $content) |
||
212 | |||
213 | /** |
||
214 | * checkOverwrite |
||
215 | * |
||
216 | * Check if the file exists and if it the write is forced. |
||
217 | * |
||
218 | * @access protected |
||
219 | * @param ParameterHolder $input |
||
220 | * @throws GeneratorException |
||
221 | * @return BaseGenerator $this |
||
222 | */ |
||
223 | protected function checkOverwrite(ParameterHolder $input) |
||
231 | } |
||
232 |