Skip to content

Commit

Permalink
Merge pull request #39 from onmoon/add-property-dto-hook
Browse files Browse the repository at this point in the history
Add property dto hook
  • Loading branch information
DimanKuskov authored Mar 1, 2020
2 parents 10931fd + dafbfd0 commit 6126a0f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ The following events are available:
This event allows you to modify the definitions of the generated Parameters DTO's customizing the generated code.
The parametersType() method returns either "path" or "query" and indicates what part of the RequestDTO is the
ParameterDTO generated for.
- `OnMoon\OpenApiServerBundle\Event\CodeGenerator\PropertyDtoGenerationEvent`

The PropertyDtoGenerationEvent event occurs before any of the DTOs nested in the Request Body or Response are generated.
This event allows you to modify the definitions of the generated nested DTOs that are used in the Request Body
and Response DTOs, customizing the generated code.
- `OnMoon\OpenApiServerBundle\Event\CodeGenerator\RequestBodyDtoGenerationEvent`

The RequestBodyDtoGenerationEvent event occurs before the request body dto is generated.
Expand Down
3 changes: 3 additions & 0 deletions src/CodeGenerator/Dto/PhpParserDtoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OnMoon\OpenApiServerBundle\Event\CodeGenerator\ClassPropertyGenerationEvent;
use OnMoon\OpenApiServerBundle\Event\CodeGenerator\ConstructorParameterGenerationEvent;
use OnMoon\OpenApiServerBundle\Event\CodeGenerator\GetterMethodGenerationEvent;
use OnMoon\OpenApiServerBundle\Event\CodeGenerator\PropertyDtoGenerationEvent;
use OnMoon\OpenApiServerBundle\Event\CodeGenerator\SetterMethodGenerationEvent;
use OnMoon\OpenApiServerBundle\Interfaces\Dto;
use OnMoon\OpenApiServerBundle\Interfaces\ResponseDto;
Expand Down Expand Up @@ -287,6 +288,7 @@ public function generateDtoClassGraph(SchemaBasedDtoDefinition $definition) : ar
$propertyDtoDefinition->makeImmutable() :
$propertyDtoDefinition->makeMutable();

$this->eventDispatcher->dispatch(new PropertyDtoGenerationEvent($propertyDtoDefinition));
$generatedClassGraph = $this->generatePropertyDto($propertyDtoDefinition);

$generatedClasses = array_merge($generatedClasses, $generatedClassGraph->getClassGraph());
Expand Down Expand Up @@ -318,6 +320,7 @@ public function generateDtoClassGraph(SchemaBasedDtoDefinition $definition) : ar
$propertyDtoDefinition->makeImmutable() :
$propertyDtoDefinition->makeMutable();

$this->eventDispatcher->dispatch(new PropertyDtoGenerationEvent($propertyDtoDefinition));
$generatedClassGraph = $this->generatePropertyDto($propertyDtoDefinition);

$generatedClasses = array_merge($generatedClasses, $generatedClassGraph->getClassGraph());
Expand Down
31 changes: 31 additions & 0 deletions src/Event/CodeGenerator/PropertyDtoGenerationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace OnMoon\OpenApiServerBundle\Event\CodeGenerator;

use OnMoon\OpenApiServerBundle\CodeGenerator\Dto\Definitions\PropertyDtoDefinition;
use Symfony\Contracts\EventDispatcher\Event;

/**
* The PropertyDtoGenerationEvent event occurs before any
* of the DTOs nested in the Request Body or Response are generated.
*
* This event allows you to modify the definitions of the generated
* nested DTOs that are used in the Request Body and Response DTOs,
* customizing the generated code.
*/
class PropertyDtoGenerationEvent extends Event
{
private PropertyDtoDefinition $definition;

public function __construct(PropertyDtoDefinition $definition)
{
$this->definition = $definition;
}

public function definition() : PropertyDtoDefinition
{
return $this->definition;
}
}

0 comments on commit 6126a0f

Please sign in to comment.