Skip to content

Commit

Permalink
Smart class references (#69)
Browse files Browse the repository at this point in the history
* Implemented smart references

* Removed duplicate check on class generation

* Fixes

* Stan fix

* Reworked nesting

Co-authored-by: Dimannn <[email protected]>
  • Loading branch information
DimanKuskov and Dimannn authored May 6, 2020
1 parent 1a067e3 commit 4e31f35
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 134 deletions.
19 changes: 19 additions & 0 deletions src/CodeGenerator/Definitions/ClassDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace OnMoon\OpenApiServerBundle\CodeGenerator\Definitions;

use function Safe\substr;
use function strrpos;

class ClassDefinition
{
private string $className;
Expand Down Expand Up @@ -37,4 +40,20 @@ public function getFQCN() : string
{
return $this->namespace . '\\' . $this->className;
}

public static function fromFQCN(string $className) : ClassDefinition
{
$lastPart = strrpos($className, '\\');
if ($lastPart !== false) {
$namespace = substr($className, 0, $lastPart);
$name = substr($className, $lastPart + 1);
} else {
$namespace = '';
$name = $className;
}

return (new ClassDefinition())
->setNamespace($namespace)
->setClassName($name);
}
}
30 changes: 6 additions & 24 deletions src/CodeGenerator/InterfaceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@
use OnMoon\OpenApiServerBundle\Interfaces\RequestHandler;
use OnMoon\OpenApiServerBundle\Interfaces\ResponseDto;
use function count;
use function Safe\substr;
use function strrpos;

class InterfaceGenerator
{
private ClassDefinition $defaultDto;
private ClassDefinition $defaultResponseDto;
private ClassDefinition $defaultService;
private ClassDefinition $defaultHandler;

public function __construct()
{
$this->defaultDto = $this->getDefaultInterface(Dto::class);
$this->defaultResponseDto = $this->getDefaultInterface(ResponseDto::class);
$this->defaultService = $this->getDefaultInterface(RequestHandler::class);
$this->defaultDto = ClassDefinition::fromFQCN(Dto::class);
$this->defaultResponseDto = ClassDefinition::fromFQCN(ResponseDto::class);
$this->defaultHandler = ClassDefinition::fromFQCN(RequestHandler::class);
}

public function setAllInterfaces(GraphDefinition $graph) : void
{
$graph->getServiceSubscriber()->setImplements([
$this->getDefaultInterface(ApiLoader::class),
ClassDefinition::fromFQCN(ApiLoader::class),
]);

foreach ($graph->getSpecifications() as $specificationDefinition) {
Expand Down Expand Up @@ -69,28 +67,12 @@ public function setAllInterfaces(GraphDefinition $graph) : void
$service
->setResponseType($responseClass)
->setRequestType($operation->getRequest())
->setExtends($this->defaultService);
->setExtends($this->defaultHandler);
$operation->setRequestHandlerInterface($service);
}
}
}

public function getDefaultInterface(string $className) : ClassDefinition
{
$lastPart = strrpos($className, '\\');
if ($lastPart !== false) {
$namespace = substr($className, 0, $lastPart);
$name = substr($className, $lastPart + 1);
} else {
$namespace = '';
$name = $className;
}

return (new ClassDefinition())
->setNamespace($namespace)
->setClassName($name);
}

public function setChildrenRecursive(DtoDefinition $root, ClassDefinition $implements) : void
{
foreach ($root->getProperties() as $property) {
Expand Down
17 changes: 5 additions & 12 deletions src/CodeGenerator/NameGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

class NameGenerator
{
private const DTO_NAMESPACE = 'Dto';
private const REQUEST_SUFFIX = 'Request';
private const RESPONSE_SUFFIX = 'Response';
public const DTO_SUFFIX = 'Dto';
public const APIS_NAMESPACE = 'Apis';
private const DUPLICATE_PREFIX = 'Property';
private const DTO_NAMESPACE = 'Dto';
private const REQUEST_SUFFIX = 'Request';
private const RESPONSE_SUFFIX = 'Response';
public const DTO_SUFFIX = 'Dto';
public const APIS_NAMESPACE = 'Apis';

private NamingStrategy $naming;
private Httpstatus $httpstatus;
Expand Down Expand Up @@ -157,12 +156,6 @@ public function setTreePathsAndClassNames(DtoDefinition $root, string $namespace

$part = $this->naming->stringToNamespace($property->getClassPropertyName());
$subClassName = $this->naming->stringToNamespace($part . self::DTO_SUFFIX);

if ($subClassName === $className) {
//ToDo: check if more elegant way exists
$subClassName = self::DUPLICATE_PREFIX . $subClassName;
}

$subNamespace = $this->naming->buildNamespace($namespace, $part);
$subPath = $this->naming->buildPath($path, $part);
$this->setTreePathsAndClassNames($objectDefinition, $subNamespace, $subClassName, $subPath);
Expand Down
27 changes: 8 additions & 19 deletions src/CodeGenerator/PhpParserGenerators/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace OnMoon\OpenApiServerBundle\CodeGenerator\PhpParserGenerators;

use Exception;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\ClassDefinition;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\PropertyDefinition;
use OnMoon\OpenApiServerBundle\Types\ScalarTypesResolver;
use PhpParser\Builder\Namespace_;
use PhpParser\BuilderFactory;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Declare_;
Expand Down Expand Up @@ -41,35 +39,26 @@ public function __construct(BuilderFactory $factory, ScalarTypesResolver $typeRe
$this->fullDocs = $fullDocs;
}

public function use(Namespace_ $builder, string $parentNameSpace, ClassDefinition $class) : void
public function getTypeDocBlock(FileBuilder $builder, PropertyDefinition $definition) : string
{
if ($parentNameSpace === $class->getNamespace()) {
return;
}

$builder->addStmt($this->factory->use($class->getFQCN()));
}

public function getTypeDocBlock(PropertyDefinition $definition) : string
{
return $this->getTypeName($definition) .
return $this->getTypeName($builder, $definition) .
($definition->isArray() ? '[]' : '') .
($definition->isNullable() ? '|null' : '');
}

public function getTypePhp(PropertyDefinition $definition) : string
public function getTypePhp(FileBuilder $builder, PropertyDefinition $definition) : string
{
return ($definition->isNullable() ? '?' : '') .
($definition->isArray() ? 'array' : $this->getTypeName($definition));
($definition->isArray() ? 'array' : $this->getTypeName($builder, $definition));
}

public function getTypeName(PropertyDefinition $definition) : string
public function getTypeName(FileBuilder $builder, PropertyDefinition $definition) : string
{
$objectType = $definition->getObjectTypeDefinition();
$scalarType = $definition->getScalarTypeId();

if ($objectType !== null) {
return $objectType->getClassName();
return $builder->getReference($objectType);
}

if ($scalarType === null) {
Expand All @@ -95,11 +84,11 @@ public function getDocComment(array $lines) : string
return sprintf('/**%s */', $glued);
}

public function printFile(Namespace_ $fileBuilder) : string
public function printFile(FileBuilder $fileBuilder) : string
{
return (new Standard())->prettyPrintFile([
new Declare_([new DeclareDeclare('strict_types', new LNumber(1))]),
$fileBuilder->getNode(),
$fileBuilder->getNamespace()->getNode(),
]);
}
}
Loading

0 comments on commit 4e31f35

Please sign in to comment.