Skip to content

Commit

Permalink
Removed code used to generate same files as before refactoring (#54)
Browse files Browse the repository at this point in the history
* * Removed code used to generate same files and before refactoring
* Added full doc block option

* Fixed symfony serializer issue

Co-authored-by: Dimannn <[email protected]>
  • Loading branch information
DimanKuskov and Dimannn authored Apr 21, 2020
1 parent 6afa90b commit ba4a448
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 120 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ open_api_server:
root_path: %kernel.project_dir%/src/Generated
language_level: 7.4.0 # minimum PHP version the generated code should be compatible with
generated_dir_permissions: 0755 # permissions for the generated directories
full_doc_blocks: false # whether to generate DocBlocks for typed variables and params
specs:
petstore:
path: '../spec/petstore.yaml' # path to OpenApi specification
Expand Down
19 changes: 0 additions & 19 deletions src/CodeGenerator/ApiServerCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use OnMoon\OpenApiServerBundle\Event\CodeGenerator\ClassGraphReadyEvent;
use OnMoon\OpenApiServerBundle\Event\CodeGenerator\FilesReadyEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use function Safe\substr;

class ApiServerCodeGenerator
{
Expand Down Expand Up @@ -37,24 +36,6 @@ public function generate() : void
$this->interfaceGenerator->setAllInterfaces($graph);
$this->attributeGenerator->setAllAttributes($graph);
$this->nameGenerator->setAllNamesAndPaths($graph);
//ToDo: remove this loop
foreach ($graph->getSpecifications() as $specificationDefinition) {
foreach ($specificationDefinition->getOperations() as $operation) {
$request = $operation->getRequest();
if ($request === null) {
continue;
}

foreach ($request->getProperties() as $property) {
$object = $property->getObjectTypeDefinition();
if ($object === null) {
continue;
}

$this->nameGenerator->setTreePathsAndClassNames($object, $request->getNamespace(), substr($request->getClassName(), 0, -3) . $object->getClassName(), $request->getFilePath());
}
}
}

$this->eventDispatcher->dispatch(new ClassGraphReadyEvent($graph));

Expand Down
10 changes: 5 additions & 5 deletions src/CodeGenerator/PhpParserGenerators/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ abstract class CodeGenerator
protected BuilderFactory $factory;
protected ScalarTypesResolver $typeResolver;
protected string $languageLevel;
protected bool $fullDocs = false;
protected bool $fullDocs;

public function __construct(BuilderFactory $factory, ScalarTypesResolver $typeResolver, string $languageLevel)
public function __construct(BuilderFactory $factory, ScalarTypesResolver $typeResolver, string $languageLevel, bool $fullDocs)
{
$this->factory = $factory;
$this->typeResolver = $typeResolver;
$this->languageLevel = $languageLevel;
$this->fullDocs = $fullDocs;
}

public function use(Namespace_ $builder, string $parentNameSpace, ClassDefinition $class) : void
Expand Down Expand Up @@ -85,11 +86,10 @@ public function getDocComment(array $lines) : string
$glued = ' ' . trim($lines[0]);
} else {
$asteriskLines = array_map(
//ToDo: add space after * anyway after tests
static fn(string $line) : string => ' *' . (trim($line)?' ':'') . trim($line),
static fn(string $line) : string => ' * ' . trim($line),
$lines
);
$glued = PHP_EOL . implode(PHP_EOL, $asteriskLines) . PHP_EOL;
$glued = PHP_EOL . implode(PHP_EOL, $asteriskLines) . PHP_EOL;
}

return sprintf('/**%s */', $glued);
Expand Down
34 changes: 16 additions & 18 deletions src/CodeGenerator/PhpParserGenerators/DtoCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use PhpParser\Node\Stmt\Return_;
use function count;
use function Safe\sprintf;
use function str_replace;
use function strpos;

class DtoCodeGenerator extends CodeGenerator
{
Expand Down Expand Up @@ -51,15 +49,14 @@ public function generate(DtoDefinition $definition) : GeneratedFileDefinition
}

$classBuilder->addStmts($this->generateProperties($definition));
//ToDo: Move below after tests
if ($definition instanceof ResponseDtoDefinition) {
$classBuilder->addStmt($this->generateResponseCodeStaticMethod($definition));
}

$classBuilder->addStmts($this->generateConstructor($definition));
$classBuilder->addStmts($this->generateGetters($definition));
$classBuilder->addStmts($this->generateSetters($definition));

if ($definition instanceof ResponseDtoDefinition) {
$classBuilder->addStmt($this->generateResponseCodeStaticMethod($definition));
}

$fileBuilder = $fileBuilder->addStmt($classBuilder);

return new GeneratedFileDefinition(
Expand Down Expand Up @@ -176,20 +173,22 @@ private function generateClassProperty(PropertyDefinition $definition) : Propert
$docCommentLines[] = '';
}

//ToDo: remove this
if (strpos($definition->getClassPropertyName(), 'queryParameters') === false
&&
strpos($definition->getClassPropertyName(), 'pathParameters') === false
&&
strpos($definition->getClassPropertyName(), 'body') === false
) {
$supportSymfonySerializer = true;
/*
* Symfony serializer does not support property class definitions.
* ToDo: Remove this hack and phpstan ignores after serializer is no longer used.
*/

/** @phpstan-ignore-next-line */
if ($this->fullDocs || $definition->isArray() || $supportSymfonySerializer) {
$docCommentLines[] = sprintf(
'@var %s $%s ',
$this->getTypeDocBlock($definition),
$definition->getClassPropertyName()
);
}

/** @phpstan-ignore-next-line */
if (count($docCommentLines)) {
$property->setDocComment($this->getDocComment($docCommentLines));
}
Expand All @@ -199,11 +198,10 @@ private function generateClassProperty(PropertyDefinition $definition) : Propert

private function generateMethodParameter(PropertyDefinition $definition) : Param
{
//ToDo: Remove
return $this
->factory
->param($definition->getClassPropertyName())
->setType(str_replace('?', '', $this->getTypePhp($definition)));
->setType($this->getTypePhp($definition));
}

private function getAssignmentDefinition(string $name) : Assign
Expand Down Expand Up @@ -250,12 +248,12 @@ private function generateSetter(PropertyDefinition $definition) : Method
->addParam($this->generateMethodParameter($definition))
->addStmt($this->getAssignmentDefinition($definition->getClassPropertyName()))
->addStmt(new Return_(new Variable('this')));
//ToDo: remove

if ($this->fullDocs || $definition->isArray()) {
$blocks = [
sprintf(
'@param %s $%s',
str_replace('|null', '', $this->getTypeDocBlock($definition)),
$this->getTypeDocBlock($definition),
$definition->getClassPropertyName()
),
];
Expand Down
27 changes: 25 additions & 2 deletions src/CodeGenerator/PhpParserGenerators/InterfaceCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\GeneratedFileDefinition;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\GeneratedInterfaceDefinition;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\ServiceInterfaceDefinition;
use function count;
use function Safe\sprintf;

class InterfaceCodeGenerator extends CodeGenerator
Expand All @@ -31,26 +32,48 @@ public function generate(GeneratedInterfaceDefinition $definition) : GeneratedFi
if ($definition instanceof ServiceInterfaceDefinition) {
$methodBuilder = $this->factory->method($definition->getMethodName())->makePublic();
$request = $definition->getRequestType();
$docBlocks = [];

if ($request !== null) {
$this->use($fileBuilder, $definition->getNamespace(), $request);

$methodBuilder->addParam(
$this->factory->param('request')->setType($request->getClassName())
);
//ToDo: Add full docblock support
if ($this->fullDocs) {
$docBlocks[] = sprintf(
'@param %s $%s',
$request->getClassName(),
'request'
);
}
}

$response = $definition->getResponseType();
if ($response !== null) {
$this->use($fileBuilder, $definition->getNamespace(), $response);
$methodBuilder->setReturnType($response->getClassName());
if ($this->fullDocs) {
$docBlocks[] = sprintf(
'@return %s',
$response->getClassName()
);
}
} else {
$methodBuilder->setReturnType('void');
}

$description = $definition->getMethodDescription();
if ($description !== null) {
$methodBuilder->setDocComment($this->getDocComment([$description]));
if (count($docBlocks)) {
$docBlocks = [$description, '', ...$docBlocks];
} else {
$docBlocks[] = $description;
}
}

if (count($docBlocks) > 0) {
$methodBuilder->setDocComment($this->getDocComment($docBlocks));
}

$interfaceBuilder->addStmt($methodBuilder);
Expand Down
Loading

0 comments on commit ba4a448

Please sign in to comment.