Skip to content

Commit

Permalink
update server handle content type
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Sep 14, 2024
1 parent 38f9218 commit 6250ad3
Show file tree
Hide file tree
Showing 28 changed files with 1,198 additions and 20 deletions.
11 changes: 6 additions & 5 deletions src/Generator/Server/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use PSX\Api\Generator\Server\Dto\File;
use PSX\Api\OperationInterface;
use PSX\Schema\ContentType;
use PSX\Schema\Generator;
use PSX\Schema\GeneratorInterface as SchemaGeneratorInterface;
use PSX\Schema\Type\ReferenceType;
Expand Down Expand Up @@ -118,7 +119,7 @@ protected function generateFooter(File $file): string
return $controller;
}

protected function generateArgumentPath(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string
protected function generateArgumentPath(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
{
if ($argumentType instanceof ReferenceType) {
$type = 'Model\\' . $type;
Expand All @@ -131,7 +132,7 @@ protected function generateArgumentPath(string $rawName, string $variableName, s
}
}

protected function generateArgumentQuery(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string
protected function generateArgumentQuery(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
{
if ($argumentType instanceof ReferenceType) {
$type = 'Model\\' . $type;
Expand All @@ -144,7 +145,7 @@ protected function generateArgumentQuery(string $rawName, string $variableName,
}
}

protected function generateArgumentHeader(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string
protected function generateArgumentHeader(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
{
if ($argumentType instanceof ReferenceType) {
$type = 'Model\\' . $type;
Expand All @@ -157,7 +158,7 @@ protected function generateArgumentHeader(string $rawName, string $variableName,
}
}

protected function generateArgumentBody(string $variableName, string $type, TypeInterface $argumentType): string
protected function generateArgumentBody(string $variableName, string $type, TypeInterface|ContentType $argumentType): string
{
if ($argumentType instanceof ReferenceType) {
$type = 'Model\\' . $type;
Expand All @@ -166,7 +167,7 @@ protected function generateArgumentBody(string $variableName, string $type, Type
return '#[Body] ' . $type . ' $' . $variableName;
}

protected function generateMethod(string $operationName, OperationInterface $operation, array $arguments, string $type, TypeInterface $returnType): string
protected function generateMethod(string $operationName, OperationInterface $operation, array $arguments, string $type, TypeInterface|ContentType $returnType): string
{
if ($returnType instanceof ReferenceType) {
$type = 'Model\\' . $type;
Expand Down
33 changes: 23 additions & 10 deletions src/Generator/Server/ServerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PSX\Api\Operation\ArgumentInterface;
use PSX\Api\OperationInterface;
use PSX\Api\SpecificationInterface;
use PSX\Schema\ContentType;
use PSX\Schema\DefinitionsInterface;
use PSX\Schema\Exception\TypeNotFoundException;
use PSX\Schema\Generator;
Expand Down Expand Up @@ -149,15 +150,15 @@ abstract protected function getFileExtension(): string;

abstract protected function generateHeader(File $file, array $imports): string;
abstract protected function generateFooter(File $file): string;
abstract protected function generateArgumentPath(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string;
abstract protected function generateArgumentQuery(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string;
abstract protected function generateArgumentHeader(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string;
abstract protected function generateArgumentBody(string $variableName, string $type, TypeInterface $argumentType): string;
abstract protected function generateArgumentPath(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string;
abstract protected function generateArgumentQuery(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string;
abstract protected function generateArgumentHeader(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string;
abstract protected function generateArgumentBody(string $variableName, string $type, TypeInterface|ContentType $argumentType): string;

/**
* @param array<string> $arguments
*/
abstract protected function generateMethod(string $operationName, OperationInterface $operation, array $arguments, string $type, TypeInterface $returnType): string;
abstract protected function generateMethod(string $operationName, OperationInterface $operation, array $arguments, string $type, TypeInterface|ContentType $returnType): string;

protected function buildControllerFileName(string $name): string
{
Expand All @@ -184,7 +185,7 @@ protected function buildFolderStructure(SpecificationInterface $specification):
* @throws InvalidTypeException
* @throws TypeNotFoundException
*/
protected function newType(TypeInterface $type, DefinitionsInterface $definitions): Dto\Type
protected function newType(TypeInterface|ContentType $type, DefinitionsInterface $definitions): Dto\Type
{
if ($type instanceof ReferenceType) {
// in case we have a reference type we take a look at the reference, normally this is a struct type but in
Expand All @@ -199,9 +200,17 @@ protected function newType(TypeInterface $type, DefinitionsInterface $definition
}
}

if ($type instanceof ContentType) {
$dataType = $this->typeGenerator->getContentType($type);
$docType = $dataType;
} else {
$dataType = $this->typeGenerator->getType($type);
$docType = $this->typeGenerator->getDocType($type);
}

return new Dto\Type(
$this->typeGenerator->getType($type),
$this->typeGenerator->getDocType($type),
$dataType,
$docType
);
}

Expand Down Expand Up @@ -242,13 +251,17 @@ private function generateControllerFile(File $file, SpecificationInterface $spec
$args[] = $this->generateArgumentBody($variableName, $type->type, $argumentType);
}

$this->resolveImport($argumentType, $imports);
if ($argumentType instanceof TypeInterface) {
$this->resolveImport($argumentType, $imports);
}
}

$returnType = $operation->getReturn()->getSchema();
$type = $this->newType($returnType, $specification->getDefinitions());

$this->resolveImport($returnType, $imports);
if ($returnType instanceof TypeInterface) {
$this->resolveImport($returnType, $imports);
}

$controller.= $this->generateMethod($operationName, $operation, $args, $type->type, $returnType);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Generator/Server/TypeScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PSX\Api\Generator\Server\Dto\Folder;
use PSX\Api\OperationInterface;
use PSX\Api\SpecificationInterface;
use PSX\Schema\ContentType;
use PSX\Schema\Generator;
use PSX\Schema\GeneratorInterface as SchemaGeneratorInterface;
use PSX\Schema\TypeInterface;
Expand Down Expand Up @@ -106,27 +107,27 @@ protected function generateFooter(File $file): string
return $controller;
}

protected function generateArgumentPath(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string
protected function generateArgumentPath(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
{
return '@Param(\'' . $rawName . '\') ' . $variableName . ': ' . $type;
}

protected function generateArgumentQuery(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string
protected function generateArgumentQuery(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
{
return '@Query(\'' . $rawName . '\') ' . $variableName . ': ' . $type;
}

protected function generateArgumentHeader(string $rawName, string $variableName, string $type, TypeInterface $argumentType): string
protected function generateArgumentHeader(string $rawName, string $variableName, string $type, TypeInterface|ContentType $argumentType): string
{
return '@Headers(\'' . $rawName . '\') ' . $variableName . ': ' . $type;
}

protected function generateArgumentBody(string $variableName, string $type, TypeInterface $argumentType): string
protected function generateArgumentBody(string $variableName, string $type, TypeInterface|ContentType $argumentType): string
{
return '@Body() ' . $variableName . ': ' . $type;
}

protected function generateMethod(string $operationName, OperationInterface $operation, array $arguments, string $type, TypeInterface $returnType): string
protected function generateMethod(string $operationName, OperationInterface $operation, array $arguments, string $type, TypeInterface|ContentType $returnType): string
{
$methodName = ucfirst(strtolower($operation->getMethod()));

Expand Down
12 changes: 12 additions & 0 deletions tests/Generator/Server/PHPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ public function testGenerateCollection()
$this->assertFileExists($target . '/src/Controller/Bar.php');
$this->assertFileExists($target . '/src/Model/Entry.php');
}

public function testGenerateContentType()
{
$generator = new PHP();

$result = $generator->generate($this->getSpecificationContentType());
$target = __DIR__ . '/resource/php_content_type';

$this->writeChunksToFolder($result, $target);

$this->assertFileExists($target . '/src/Controller/App.php');
}
}
12 changes: 12 additions & 0 deletions tests/Generator/Server/TypeScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ public function testGenerateCollection()
$this->assertFileExists($target . '/src/controller/bar.controller.ts');
$this->assertFileExists($target . '/src/dto/Entry.ts');
}

public function testGenerateContentType()
{
$generator = new TypeScript();

$result = $generator->generate($this->getSpecificationContentType());
$target = __DIR__ . '/resource/typescript_content_type';

$this->writeChunksToFolder($result, $target);

$this->assertFileExists($target . '/src/controller/app.controller.ts');
}
}
7 changes: 7 additions & 0 deletions tests/Generator/Server/resource/php_content_type/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
APP_URL=http://127.0.0.1/projects/psx/public/
APP_ENV=dev
APP_DEBUG=on
APP_CONNECTION=mysql://root:test1234@localhost/psx
APP_MAILER=native://default
SDKGEN_CLIENT_ID=
SDKGEN_CLIENT_SECRET=
Loading

0 comments on commit 6250ad3

Please sign in to comment.