1 | <?php |
||
42 | class Creator |
||
43 | { |
||
44 | use EveApiCreatorTrait; |
||
45 | /** |
||
46 | * Creator constructor. |
||
47 | * |
||
48 | * @param \Twig_Environment $twig |
||
49 | * @param string $dir |
||
50 | * @param string $platform |
||
51 | */ |
||
52 | public function __construct(\Twig_Environment $twig, string $dir = __DIR__, $platform = 'MySql') |
||
53 | { |
||
54 | $this->setRelativeBaseDir($dir); |
||
55 | $this->setPlatform($platform); |
||
56 | $this->setTwig($twig); |
||
57 | $this->twigExtension = strtolower($platform) . '.sql.twig'; |
||
58 | } |
||
59 | /** |
||
60 | * @param EveApiEventInterface $event |
||
61 | * @param string $eventName |
||
62 | * @param MediatorInterface $yem |
||
63 | * |
||
64 | * @return EveApiEventInterface |
||
65 | * @throws \DomainException |
||
66 | * @throws \InvalidArgumentException |
||
67 | * @throws \LogicException |
||
68 | */ |
||
69 | public function createSql( |
||
70 | EveApiEventInterface $event, |
||
71 | string $eventName, |
||
72 | MediatorInterface $yem |
||
73 | ): EveApiEventInterface |
||
74 | { |
||
75 | $this->setYem($yem); |
||
76 | $data = $event->getData(); |
||
77 | $yem->triggerLogEvent('Yapeal.Log.log', |
||
78 | Logger::DEBUG, |
||
79 | $this->getReceivedEventMessage($data, $eventName, __CLASS__)); |
||
80 | // Only work with raw unaltered XML data. |
||
81 | if (false !== strpos($data->getEveApiXml(), '<?yapeal.parameters.json')) { |
||
82 | return $event->setHandledSufficiently(); |
||
83 | } |
||
84 | $this->sectionName = $data->getEveApiSectionName(); |
||
85 | $this->apiName = $data->getEveApiName(); |
||
86 | $outputFile = sprintf('%1$s%2$s/Create%3$s.%4$s.sql', |
||
87 | $this->getRelativeBaseDir(), |
||
88 | ucfirst($this->sectionName), |
||
89 | ucfirst($this->apiName), |
||
90 | $this->getPlatform()); |
||
91 | // Nothing to do if NOT overwriting and file exists. |
||
92 | if (false === $this->isOverwrite() && is_file($outputFile)) { |
||
93 | return $event->setHandledSufficiently(); |
||
94 | } |
||
95 | $sxi = new \SimpleXMLIterator($data->getEveApiXml()); |
||
96 | $this->tables = []; |
||
97 | $this->processValueOnly($sxi, $this->apiName); |
||
98 | $this->processRowset($sxi, $this->apiName); |
||
99 | $tCount = count($this->tables); |
||
100 | if (0 === $tCount) { |
||
101 | $mess = 'No SQL tables to create for'; |
||
102 | $yem->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($mess, $data)); |
||
103 | return $event->setHandledSufficiently(); |
||
104 | } |
||
105 | ksort($this->tables); |
||
106 | list($mSec, $sec) = explode(' ', microtime()); |
||
107 | $context = [ |
||
108 | 'className' => lcfirst($this->apiName), |
||
109 | 'tables' => $this->tables, |
||
110 | 'sectionName' => lcfirst($this->sectionName), |
||
111 | 'version' => gmdate('YmdHis', $sec) . substr($mSec, 1, 4) |
||
112 | ]; |
||
113 | $contents = $this->getContentsFromTwig($eventName, $data, $context); |
||
114 | if (false === $contents) { |
||
115 | return $event; |
||
116 | } |
||
117 | if (false === $this->safeFileWrite($outputFile, $contents)) { |
||
118 | $yem->triggerLogEvent($eventName, |
||
119 | Logger::WARNING, |
||
120 | $this->getFailedToWriteFileMessage($data, $outputFile)); |
||
121 | return $event; |
||
122 | } |
||
123 | return $event->setHandledSufficiently(); |
||
124 | } |
||
125 | /** |
||
126 | * @param string $platform |
||
127 | * |
||
128 | * @return self Fluent interface. |
||
129 | */ |
||
130 | public function setPlatform(string $platform) |
||
131 | { |
||
132 | $this->platform = $platform; |
||
133 | return $this; |
||
134 | } |
||
135 | /** |
||
136 | * @param \SimpleXMLIterator $sxi |
||
137 | * @param string $apiName |
||
138 | * @param string $xPath |
||
139 | */ |
||
140 | protected function processRowset(\SimpleXMLIterator $sxi, string $apiName, string $xPath = '//result/rowset') |
||
141 | { |
||
142 | $items = $sxi->xpath($xPath); |
||
143 | if (0 === count($items)) { |
||
144 | return; |
||
145 | } |
||
146 | foreach ($items as $ele) { |
||
147 | $rsName = ucfirst((string)$ele['name']); |
||
148 | $colNames = explode(',', (string)$ele['columns']); |
||
149 | $keyNames = explode(',', (string)$ele['key']); |
||
150 | $columns = []; |
||
151 | foreach ($keyNames as $keyName) { |
||
152 | $columns[$keyName] = $this->inferTypeFromName($keyName); |
||
153 | } |
||
154 | foreach ($colNames as $colName) { |
||
155 | $columns[$colName] = $this->inferTypeFromName($colName); |
||
156 | } |
||
157 | if ($this->hasOwner()) { |
||
158 | $columns['ownerID'] = 'BIGINT(20) UNSIGNED NOT NULL'; |
||
159 | } |
||
160 | uksort($columns, |
||
161 | function ($alpha, $beta) { |
||
162 | return strtolower($alpha) <=> strtolower($beta); |
||
163 | }); |
||
164 | if (0 === count($this->tables)) { |
||
165 | $this->tables[$apiName] = ['columns' => $columns, 'keys' => $this->getSqlKeys($keyNames)]; |
||
166 | } else { |
||
167 | $this->tables[$rsName] = ['columns' => $columns, 'keys' => $this->getSqlKeys($keyNames)]; |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | /** |
||
172 | * @param \SimpleXMLIterator $sxi |
||
173 | * @param string $tableName |
||
174 | * @param string $xpath |
||
175 | */ |
||
176 | protected function processValueOnly( |
||
177 | \SimpleXMLIterator $sxi, |
||
178 | string $tableName, |
||
179 | string $xpath = '//result/child::*[not(*|@*|self::dataTime)]' |
||
180 | ) { |
||
181 | $items = $sxi->xpath($xpath); |
||
182 | if (false === $items || 0 === count($items)) { |
||
183 | return; |
||
184 | } |
||
185 | /** |
||
186 | * @var \SimpleXMLElement[] $items |
||
187 | */ |
||
188 | $columns = []; |
||
189 | foreach ($items as $ele) { |
||
190 | $name = (string)$ele->getName(); |
||
191 | $columns[$name] = $this->inferTypeFromName($name, true); |
||
192 | } |
||
193 | if ($this->hasOwner()) { |
||
194 | $columns['ownerID'] = 'BIGINT(20) UNSIGNED NOT NULL'; |
||
195 | } |
||
196 | uksort($columns, |
||
197 | function ($alpha, $beta) { |
||
198 | return strtolower($alpha) <=> strtolower($beta); |
||
199 | }); |
||
200 | $keys = $this->getSqlKeys(); |
||
201 | if (0 !== count($keys)) { |
||
202 | $this->tables[$tableName] = ['columns' => $columns, 'keys' => $keys]; |
||
203 | } else { |
||
204 | $this->tables[$tableName] = ['columns' => $columns]; |
||
205 | } |
||
206 | } |
||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | private function getPlatform(): string |
||
214 | /** |
||
215 | * @param string[] $keyNames |
||
216 | * |
||
217 | * @return string[] |
||
218 | */ |
||
219 | private function getSqlKeys(array $keyNames = []): array |
||
226 | /** |
||
227 | * Used to determine if API is in section that has an owner. |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | private function hasOwner(): bool |
||
235 | /** |
||
236 | * Used to infer(choose) type from element or attribute's name. |
||
237 | * |
||
238 | * @param string $name Name of the element or attribute. |
||
239 | * @param bool $forValue Determines if returned type is going to be used for element or an attribute. |
||
240 | * |
||
241 | * @return string Returns the inferred type from the name. |
||
242 | */ |
||
243 | private function inferTypeFromName(string $name, bool $forValue = false): string |
||
274 | /** |
||
275 | * @var string $apiName |
||
276 | */ |
||
277 | private $apiName; |
||
278 | /** |
||
279 | * @var string $platform Sql connection platform being used. |
||
280 | */ |
||
281 | private $platform; |
||
282 | /** |
||
283 | * @var string $sectionName |
||
284 | */ |
||
285 | private $sectionName; |
||
286 | /** |
||
287 | * @var array $tables |
||
288 | */ |
||
289 | private $tables; |
||
290 | } |
||
291 |