From 38344c453991559fe0c8ef4d920141f33b605196 Mon Sep 17 00:00:00 2001 From: Christoph Kappestein <christoph.kappestein@gmail.com> Date: Sun, 13 Oct 2024 01:42:46 +0200 Subject: [PATCH] update seo and generator --- www/resources/template/generator.php | 23 +++++++- www/resources/template/generator_overview.php | 6 +- www/resources/template/inc/header.php | 9 +-- www/src/Controller/Changelog.php | 1 + www/src/Controller/Developer.php | 1 + www/src/Controller/Ecosystem.php | 1 + www/src/Controller/Example.php | 1 + www/src/Controller/Faq.php | 1 + www/src/Controller/Generator.php | 56 +++++++++++++++++-- www/src/Controller/Hash.php | 1 + www/src/Controller/Specification.php | 1 + 11 files changed, 85 insertions(+), 16 deletions(-) diff --git a/www/resources/template/generator.php b/www/resources/template/generator.php index da0a972..b3ec340 100644 --- a/www/resources/template/generator.php +++ b/www/resources/template/generator.php @@ -19,11 +19,30 @@ <textarea id="schema" name="schema" rows="24" class="form-control"><?php echo htmlspecialchars($schema); ?></textarea> </div> <input type="submit" value="Generate" class="btn btn-primary"> - <input type="submit" value="Download" class="btn btn-secondary"> </form> </div> <div class="col-6"> - <div><pre><code class="<?php echo $type ?? ''; ?>"><?php echo isset($output) ? htmlspecialchars($output) : ''; ?></code></pre></div> + <?php if(isset($output)): ?> + <form method="post" action="<?php echo $router->getAbsolutePath([\App\Controller\Generator::class, 'download'], ['type' => $type]); ?>"> + <input type="hidden" name="namespace" value="<?php echo htmlspecialchars($namespace ?? ''); ?>"> + <input type="hidden" name="schema" value="<?php echo htmlspecialchars($schema); ?>"> + <input type="submit" value="Download" class="btn btn-primary"> + </form> + <hr> + <?php if ($output instanceof stdClass): ?> + <?php foreach ($output as $fileName => $chunk): ?> + <div class="psx-object"> + <h1><?php echo $fileName; ?></h1> + <div class="example-box"><pre><code class="<?php echo $type; ?>"><?php echo htmlspecialchars($chunk); ?></code></pre></div> + </div> + <?php endforeach; ?> + <?php else: ?> + <div class="psx-object"> + <h1>Output</h1> + <div class="example-box"><pre><code class="<?php echo $type; ?>"><?php echo htmlspecialchars($output); ?></code></pre></div> + </div> + <?php endif; ?> + <?php endif; ?> </div> </div> </div> diff --git a/www/resources/template/generator_overview.php b/www/resources/template/generator_overview.php index 31d1851..6014e4c 100644 --- a/www/resources/template/generator_overview.php +++ b/www/resources/template/generator_overview.php @@ -9,8 +9,10 @@ <div class="container"> <h1 class="display-4">DTO Generator</h1> - <p>fsdfsdf</p> - + <p class="lead">This list gives you access to our reference code generator implementation. + For more advanced integration options like an REST API, CLI or GitHub action please take a look + at the <a href="https://sdkgen.app/">SDKgen project</a>. + </p> <div class="row"> <?php foreach ($types as $chunk): ?> <div class="col-6"> diff --git a/www/resources/template/inc/header.php b/www/resources/template/inc/header.php index 109c6ec..01becd5 100644 --- a/www/resources/template/inc/header.php +++ b/www/resources/template/inc/header.php @@ -3,9 +3,9 @@ <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> - <meta name="description" content="TypeSchema is a JSON format to describe data models in a language neutral format. A TypeSchema can be easily transformed into specific code for almost any programming language."> - <meta name="keywords" content="JSON Schema, TypeSchema, Data, Model, Specification, Code Generation"> - <title>TypeSchema</title> + <meta name="description" content="TypeSchema is a JSON specification to describe data models in a programming language neutral format."> + <meta name="keywords" content="JSON Schema, TypeSchema, Data, Model, Specification, Code Generation, Code Generator"> + <title><?php if(isset($title)): ?><?php echo $title; ?><?php else: ?>TypeSchema<?php endif; ?></title> <link rel="preload" href="<?php echo $base; ?>/css/app.min.css" as="style" /> <link rel="preload" href="<?php echo $base; ?>/js/app.min.js" as="script" /> <link rel="stylesheet" href="<?php echo $base; ?>/css/app.min.css"> @@ -33,9 +33,6 @@ function gtag(){dataLayer.push(arguments);} <li class="nav-item"> <a class="nav-link" href="<?php echo $router->getAbsolutePath([\App\Controller\Generator::class, 'show']); ?>">Generator</a> </li> - <li class="nav-item"> - <a class="nav-link" href="<?php echo $router->getAbsolutePath([\App\Controller\Developer::class, 'show']); ?>">Developer</a> - </li> <li class="nav-item"> <a class="nav-link" href="<?php echo $router->getAbsolutePath([\App\Controller\Ecosystem::class, 'show']); ?>">Ecosystem</a> </li> diff --git a/www/src/Controller/Changelog.php b/www/src/Controller/Changelog.php index a3bcf79..c00cfb6 100644 --- a/www/src/Controller/Changelog.php +++ b/www/src/Controller/Changelog.php @@ -30,6 +30,7 @@ public function __construct(ReverseRouter $reverseRouter, SchemaManagerInterface public function show(): mixed { $data = [ + 'title' => 'Changelog | TypeSchema', 'method' => explode('::', __METHOD__), 'left' => $this->getLeft(), 'right' => $this->getRight(), diff --git a/www/src/Controller/Developer.php b/www/src/Controller/Developer.php index 5d4e046..bc0c49d 100644 --- a/www/src/Controller/Developer.php +++ b/www/src/Controller/Developer.php @@ -22,6 +22,7 @@ public function __construct(ReverseRouter $reverseRouter) public function show(): mixed { $data = [ + 'title' => 'Developer | TypeSchema', 'method' => explode('::', __METHOD__), ]; diff --git a/www/src/Controller/Ecosystem.php b/www/src/Controller/Ecosystem.php index 94e5b57..54ead2b 100644 --- a/www/src/Controller/Ecosystem.php +++ b/www/src/Controller/Ecosystem.php @@ -22,6 +22,7 @@ public function __construct(ReverseRouter $reverseRouter) public function show(): mixed { $data = [ + 'title' => 'Ecosystem | TypeSchema', 'method' => explode('::', __METHOD__), ]; diff --git a/www/src/Controller/Example.php b/www/src/Controller/Example.php index d4d2980..e82afcd 100644 --- a/www/src/Controller/Example.php +++ b/www/src/Controller/Example.php @@ -55,6 +55,7 @@ public function show(string $type): mixed } $data = [ + 'title' => TypeName::getDisplayName($type) . ' Example | TypeSchema', 'method' => explode('::', __METHOD__), 'parameters' => ['type' => $type], 'type' => TypeName::getDisplayName($type), diff --git a/www/src/Controller/Faq.php b/www/src/Controller/Faq.php index 7aa6cbf..5eb0170 100644 --- a/www/src/Controller/Faq.php +++ b/www/src/Controller/Faq.php @@ -22,6 +22,7 @@ public function __construct(ReverseRouter $reverseRouter) public function show(): mixed { $data = [ + 'title' => 'FAQ | TypeSchema', 'method' => explode('::', __METHOD__), ]; diff --git a/www/src/Controller/Generator.php b/www/src/Controller/Generator.php index 419406a..b7a2bc5 100644 --- a/www/src/Controller/Generator.php +++ b/www/src/Controller/Generator.php @@ -7,24 +7,24 @@ use PSX\Api\Attribute\Get; use PSX\Api\Attribute\Path; use PSX\Api\Attribute\Post; +use PSX\Framework\Config\ConfigInterface; use PSX\Framework\Controller\ControllerAbstract; use PSX\Framework\Http\Writer\Template; use PSX\Framework\Loader\ReverseRouter; +use PSX\Http\Environment\HttpResponse; use PSX\Http\Exception\BadRequestException; +use PSX\Http\Writer\File; +use PSX\Schema\Generator\Code\Chunks; use PSX\Schema\Generator\Config; +use PSX\Schema\Generator\FileAwareInterface; use PSX\Schema\GeneratorFactory; use PSX\Schema\Parser\TypeSchema; use PSX\Schema\SchemaManagerInterface; class Generator extends ControllerAbstract { - private ReverseRouter $reverseRouter; - private SchemaManagerInterface $schemaManager; - - public function __construct(ReverseRouter $reverseRouter, SchemaManagerInterface $schemaManager) + public function __construct(private ReverseRouter $reverseRouter, private SchemaManagerInterface $schemaManager, private ConfigInterface $config) { - $this->reverseRouter = $reverseRouter; - $this->schemaManager = $schemaManager; } #[Get] @@ -37,6 +37,7 @@ public function show(): mixed } $data = [ + 'title' => 'DTO Generator | TypeSchema', 'method' => explode('::', __METHOD__), 'schema' => $this->getSchema(), 'types' => array_chunk($types, 9, true), @@ -55,6 +56,7 @@ public function showType(string $type): mixed } $data = [ + 'title' => TypeName::getDisplayName($type) . ' DTO Generator | TypeSchema', 'method' => explode('::', __METHOD__), 'parameters' => ['type' => $type], 'schema' => $this->getSchema(), @@ -92,6 +94,7 @@ public function generate(string $type, Generate $generate): mixed } $data = [ + 'title' => TypeName::getDisplayName($type) . ' DTO Generator | TypeSchema', 'method' => explode('::', __METHOD__), 'parameters' => ['type' => $type], 'namespace' => $namespace, @@ -105,6 +108,47 @@ public function generate(string $type, Generate $generate): mixed return new Template($data, $templateFile, $this->reverseRouter); } + #[Post] + #[Path('/generator/:type/download')] + public function download(string $type, Generate $generate): mixed + { + $namespace = $generate->getNamespace(); + $schema = $generate->getSchema() ?? throw new \RuntimeException('Provided no schema'); + + $config = new Config(); + if (!empty($namespace)) { + $config->put(Config::NAMESPACE, $namespace); + } + + if (!in_array($type, GeneratorFactory::getPossibleTypes())) { + throw new BadRequestException('Provided an invalid type'); + } + + try { + $result = (new TypeSchema($this->schemaManager))->parse($schema); + $generator = (new GeneratorFactory())->getGenerator($type, $config); + + $output = $generator->generate($result); + + if ($output instanceof Chunks && $generator instanceof FileAwareInterface) { + $zipFile = $this->config->get('psx_path_cache') . '/typeschema_gen_' . sha1($schema) . '.zip'; + + $result = new Chunks(); + foreach ($output as $identifier => $code) { + $result->append($generator->getFileName($identifier), $generator->getFileContent($code)); + } + + $result->writeToZip($zipFile); + + return new File($zipFile, 'typeschema_' . $type . '.zip', 'application/zip'); + } else { + return new HttpResponse(200, ['Content-Type' => 'text/plain'], (string) $output); + } + } catch (\Throwable $e) { + return new HttpResponse(500, ['Content-Type' => 'text/plain'], $e->getMessage()); + } + } + private function getSchema(): string { return <<<'JSON' diff --git a/www/src/Controller/Hash.php b/www/src/Controller/Hash.php index 7420eb1..78de9e0 100644 --- a/www/src/Controller/Hash.php +++ b/www/src/Controller/Hash.php @@ -29,6 +29,7 @@ public function __construct(ReverseRouter $reverseRouter, SchemaManagerInterface public function show(): mixed { $data = [ + 'title' => 'Hash | TypeSchema', 'method' => explode('::', __METHOD__), 'schema' => $this->getSchema(), ]; diff --git a/www/src/Controller/Specification.php b/www/src/Controller/Specification.php index 82ca570..18d2a26 100644 --- a/www/src/Controller/Specification.php +++ b/www/src/Controller/Specification.php @@ -22,6 +22,7 @@ public function __construct(ReverseRouter $reverseRouter) public function show(): mixed { $data = [ + 'title' => 'Specification | TypeSchema', 'method' => explode('::', __METHOD__), ];