From bab1a5b83b28ad86660cdba15faf9eed239e860e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 12 Jan 2024 00:30:03 +0000 Subject: [PATCH] continue implementations --- Helper/Php/createPhpFromJsonDb.php | 184 +++++++++++++++++++++++++++++ Helper/Php/langUsageInspector.php | 46 ++++++-- 2 files changed, 222 insertions(+), 8 deletions(-) create mode 100644 Helper/Php/createPhpFromJsonDb.php diff --git a/Helper/Php/createPhpFromJsonDb.php b/Helper/Php/createPhpFromJsonDb.php new file mode 100644 index 0000000..0d69ee6 --- /dev/null +++ b/Helper/Php/createPhpFromJsonDb.php @@ -0,0 +1,184 @@ + + */ +final class ItemMapper extends DataMapperFactory +{ + /** + * Columns. + * + * @var array + * @since 1.0.0 + */ + public const COLUMNS = [ + {$columns} + ]; + + /** + * Primary table. + * + * @var string + * @since 1.0.0 + */ + public const TABLE = '{$table}'; + + /** + * Primary field name. + * + * @var string + * @since 1.0.0 + */ + public const PRIMARYFIELD = '{$primaryfield}'; + + /** + * Has many relation. + * + * @var array + * @since 1.0.0 + */ + public const HAS_MANY = [ + ]; +} + +MAPPER; + +\file_put_contents($output . $name . 'Mapper.php', $mapper); + +//////////// Create model +$model = <<< MODEL + $this->id, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() : mixed + { + return $this->toArray(); + } +} + +MODEL; + +\file_put_contents($output . $name . '.php', $model); + +//////////// Create null model +$nullmodel = <<< NULLMODEL +id = $id; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() : mixed + { + return ['id' => $this->id]; + } +} + +NULLMODEL; + +\file_put_contents($output . 'Null' . $name . '.php', $nullmodel); \ No newline at end of file diff --git a/Helper/Php/langUsageInspector.php b/Helper/Php/langUsageInspector.php index 5334556..956e216 100644 --- a/Helper/Php/langUsageInspector.php +++ b/Helper/Php/langUsageInspector.php @@ -16,22 +16,22 @@ declare(strict_types=1); function printUsage() : void { - echo 'Usage: -d -m + echo 'Usage: -m '; -'; - echo "\t" . '-d Destination/output directory.' . "\n"; - echo "\t" . '-m Module directory.' . "\n"; + echo "\t" . '-m Module name.' . "\n"; } -$destination = ($key = \array_search('-d', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" '); -$modulePath = ($key = \array_search('-m', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" '); +// $destination = ($key = \array_search('-d', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" '); +$moduleName = ($key = \array_search('-m', $argv)) === false || $key === \count($argv) - 1 ? null : \trim($argv[$key + 1], '" '); -if (!isset($destination) || !isset($modulePath)) { +if (!isset($moduleName)) { \printUsage(); return; } +$modulePath = __DIR__ . '/../../../Modules/' . $moduleName; + $sources = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($modulePath)); $tpls = []; $langs = []; @@ -69,7 +69,8 @@ foreach($langs as $lang => $data) { $fileContent = \file_get_contents($tpl); foreach ($data as $key => $word) { - if (\stripos($fileContent, '$this->getHtml(\'' . $key . '\')') !== false + if ((\stripos($fileContent, '$this->getHtml(\'' . $key . '\')') !== false + || \stripos($fileContent, '$this->getHtml(\'' . $key . '\', \'' . $moduleName . '\', \'Backend\')') !== false) && ($key = \array_search($key, $unusedLanguage)) !== false ) { unset($unusedLanguage[$key]); @@ -81,3 +82,32 @@ foreach($langs as $lang => $data) { echo 'Language files have different length: ' . ($unequalLength ? 'yes' : 'no') . "\n"; echo 'Unused language components: ' . "\n"; \var_dump($unusedLanguage); + +$sources = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($modulePath)); +$tpls = []; +$langs = []; + +foreach ($sources as $source) { + if ($source->isFile() + && (($temp = \strlen($source->getPathname()) - \strlen('lang.php')) >= 0 && \strpos($source->getPathname(), 'lang.php', $temp) !== false) + && \strlen(\explode('.', \basename($source->getPathname()))[0]) === 2 + ) { + $file = \file_get_contents($source->getPathname()); + $lines = \explode("\n", $file); + $exclude = []; + + foreach ($lines as $line) { + foreach ($unusedLanguage as $unused) { + if (\strpos($line, ' \'' . $unused . '\' ') !== false + && \stripos($unused, ':') === false + ) { + continue 2; + } + } + + $exclude[] = $line; + } + + \file_put_contents($source->getPathname(), \implode("\n", $exclude)); + } +}