|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright Skywire. All rights reserved. |
| 5 | + * See LICENSE.txt for license details. |
| 6 | + * |
| 7 | + * @author Skywire Core Team |
| 8 | + * @copyright Copyright (c) 2021 Skywire (http://www.skywire.co.uk) |
| 9 | + */ |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace MageGen\Generator; |
| 13 | + |
| 14 | +use DOMDocument; |
| 15 | +use DOMElement; |
| 16 | +use MageGen\Writer\ModuleFile; |
| 17 | +use Nette\PhpGenerator\ClassType; |
| 18 | +use Twig\Environment; |
| 19 | + |
| 20 | + |
| 21 | +class SchemaGenerator |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var Environment |
| 25 | + */ |
| 26 | + protected $twig; |
| 27 | + |
| 28 | + /** |
| 29 | + * DiGenerator constructor. |
| 30 | + * |
| 31 | + * @param Environment $twig |
| 32 | + */ |
| 33 | + public function __construct(Environment $twig) |
| 34 | + { |
| 35 | + $this->twig = $twig; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Create a file if it doesn't exist and return the path |
| 40 | + * |
| 41 | + * @param string $vendor |
| 42 | + * @param string $module |
| 43 | + * @param ModuleFile $writer |
| 44 | + * |
| 45 | + * @return string |
| 46 | + * @throws \Twig\Error\LoaderError |
| 47 | + * @throws \Twig\Error\RuntimeError |
| 48 | + * @throws \Twig\Error\SyntaxError |
| 49 | + */ |
| 50 | + public function createSchemaFile(string $vendor, string $module, ModuleFile $writer): string |
| 51 | + { |
| 52 | + $etcPath = 'etc'; |
| 53 | + |
| 54 | + return $writer->writeFile( |
| 55 | + $vendor, |
| 56 | + $module, |
| 57 | + $etcPath, |
| 58 | + 'db_schema.xml', |
| 59 | + $this->twig->render('module/db_schema.xml.twig') |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public function addEntity(string $schemaFilePath, string $classFqn) |
| 64 | + { |
| 65 | + $dom = new DOMDocument(); |
| 66 | + $dom->preserveWhiteSpace = false; |
| 67 | + $dom->formatOutput = true; |
| 68 | + $dom->loadXML(file_get_contents($schemaFilePath)); |
| 69 | + |
| 70 | + $resourceFqn = str_replace('\\Model\\', '\\Model\\ResourceModel\\', $classFqn); |
| 71 | + [$tableName, $idField] = $this->getTableAndIdField($resourceFqn); |
| 72 | + if (!$tableName) { |
| 73 | + throw new \RuntimeException("Table name and id could not be determined for resource '$resourceFqn'"); |
| 74 | + } |
| 75 | + |
| 76 | + $tableNode = $this->getTableNode($dom, $tableName); |
| 77 | + if (!$tableNode) { |
| 78 | + $tableNode = $dom->createElement('table'); |
| 79 | + $tableNode->setAttribute('name', $tableName); |
| 80 | + $tableNode->setAttribute('resource', 'default'); |
| 81 | + $tableNode->setAttribute('engine', 'innodb'); |
| 82 | + $dom->documentElement->appendChild($tableNode); |
| 83 | + |
| 84 | + // Add primary key |
| 85 | + $primaryNode = $dom->createElement('column'); |
| 86 | + $primaryNode->setAttribute('xsi:type', 'int'); |
| 87 | + $primaryNode->setAttribute('name', $idField); |
| 88 | + $primaryNode->setAttribute('unsigned', 'true'); |
| 89 | + $primaryNode->setAttribute('nullable', 'false'); |
| 90 | + $primaryNode->setAttribute('identity', 'true'); |
| 91 | + $primaryNode->setAttribute('comment', 'Primary Key'); |
| 92 | + $tableNode->appendChild($primaryNode); |
| 93 | + |
| 94 | + $constraintNode = $dom->createElement('constraint'); |
| 95 | + $constraintNode->setAttribute('xsi:type', 'primary'); |
| 96 | + $constraintNode->setAttribute('referenceId', 'PRIMARY'); |
| 97 | + $constraintColumnNode = $dom->createElement('column'); |
| 98 | + $constraintColumnNode->setAttribute('name', $idField); |
| 99 | + $constraintNode->appendChild($constraintColumnNode); |
| 100 | + $tableNode->appendChild($constraintNode); |
| 101 | + } |
| 102 | + |
| 103 | + // TODO Add properties |
| 104 | + |
| 105 | +// $preferenceNode = $dom->createElement('preference'); |
| 106 | +// $preferenceNode->setAttribute('for', $interface); |
| 107 | +// $preferenceNode->setAttribute('type', $model); |
| 108 | +// $dom->documentElement->appendChild($preferenceNode); |
| 109 | + |
| 110 | + $xml = $dom->saveXML(); |
| 111 | + |
| 112 | + file_put_contents($schemaFilePath, $xml); |
| 113 | + } |
| 114 | + |
| 115 | + protected function getTableNode(DOMDocument $dom, string $tableName): ?DOMElement |
| 116 | + { |
| 117 | + $tables = $dom->getElementsByTagName('table'); |
| 118 | + |
| 119 | + $tables = array_filter( |
| 120 | + iterator_to_array($tables), |
| 121 | + function (DOMElement $node) use($tableName) { |
| 122 | + return $node->getAttribute('name') === $tableName; |
| 123 | + } |
| 124 | + ); |
| 125 | + |
| 126 | + if (empty($tables)) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + return array_shift($tables); |
| 131 | + } |
| 132 | + |
| 133 | + protected function getTableAndIdField(string $resourceFqn): array |
| 134 | + { |
| 135 | + $resourceClass = ClassType::withBodiesFrom($resourceFqn); |
| 136 | + $body = $resourceClass->getMethod('_construct')->getBody(); |
| 137 | + |
| 138 | + preg_match_all("/'\w*'/", $body, $matches); |
| 139 | + |
| 140 | + if (!empty($matches)) { |
| 141 | + return array_map( |
| 142 | + function (string $match) { |
| 143 | + return trim($match, "'"); |
| 144 | + }, |
| 145 | + $matches[0] |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + return []; |
| 150 | + } |
| 151 | +} |
0 commit comments