Skip to content

Commit

Permalink
Merge pull request #22 from wol-soft/PostProcessorHooks
Browse files Browse the repository at this point in the history
Post Processor pre/post execution hooks
  • Loading branch information
wol-soft authored Feb 10, 2021
2 parents 41ad8e5 + b80597d commit 014b1d8
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 44 deletions.
8 changes: 5 additions & 3 deletions docs/source/generator/postProcessor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ By default additional properties are not included in serialized models. If the *
Custom Post Processors
----------------------

You can implement custom post processors to accomplish your tasks. Each post processor must implement the **PHPModelGenerator\\SchemaProcessor\\PostProcessor\\PostProcessorInterface**. If you have implemented a post processor add the post processor to your `ModelGenerator` and the post processor will be executed for each class.
You can implement custom post processors to accomplish your tasks. Each post processor must extend the class **PHPModelGenerator\\SchemaProcessor\\PostProcessor\\PostProcessor**. If you have implemented a post processor add the post processor to your `ModelGenerator` and the post processor will be executed for each class.

A custom post processor which adds a custom trait to the generated model (eg. a trait adding methods for an active record pattern implementation) may look like:

Expand All @@ -157,9 +157,9 @@ A custom post processor which adds a custom trait to the generated model (eg. a
namespace MyApp\Model\Generator\PostProcessor;
use MyApp\Model\ActiveRecordTrait;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
class ActiveRecordPostProcessor implements PostProcessorInterface
class ActiveRecordPostProcessor extends PostProcessor
{
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
Expand Down Expand Up @@ -188,3 +188,5 @@ What can you do inside your custom post processor?
If a setter for a property is called with the same value which is already stored internally (consequently no update of the property is required), the setters will return directly and as a result of that the setter hooks will not be executed.

This behaviour also applies also to properties changed via the *populate* method added by the `PopulatePostProcessor <#populatepostprocessor>`__ and the *setAdditionalProperty* method added by the `AdditionalPropertiesAccessorPostProcessor <#additionalpropertiesaccessorpostprocessor>`__

To execute code before/after the processing of the schemas override the methods **preProcess** and **postProcess** inside your custom post processor.
4 changes: 2 additions & 2 deletions src/Model/RenderJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PHPModelGenerator\Exception\ValidationException;
use PHPModelGenerator\Model\Validator\AbstractComposedPropertyValidator;
use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookResolver;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\Utils\RenderHelper;

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ public function __construct(
}

/**
* @param PostProcessorInterface[] $postProcessors
* @param PostProcessor[] $postProcessors
* @param GeneratorConfiguration $generatorConfiguration
*/
public function postProcess(array $postProcessors, GeneratorConfiguration $generatorConfiguration)
Expand Down
8 changes: 4 additions & 4 deletions src/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\GeneratorConfiguration;
use PHPModelGenerator\SchemaProcessor\PostProcessor\Internal\CompositionValidationPostProcessor;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\SchemaProcessor\PostProcessor\SerializationPostProcessor;
use PHPModelGenerator\SchemaProcessor\RenderQueue;
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
Expand All @@ -27,7 +27,7 @@ class ModelGenerator
{
/** @var GeneratorConfiguration */
protected $generatorConfiguration;
/** @var PostProcessorInterface[] */
/** @var PostProcessor[] */
protected $postProcessors = [];

/**
Expand All @@ -48,11 +48,11 @@ public function __construct(GeneratorConfiguration $generatorConfiguration = nul
}

/**
* @param PostProcessorInterface $postProcessor
* @param PostProcessor $postProcessor
*
* @return $this
*/
public function addPostProcessor(PostProcessorInterface $postProcessor): self
public function addPostProcessor(PostProcessor $postProcessor): self
{
$this->postProcessors[] = $postProcessor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
*/
class AdditionalPropertiesAccessorPostProcessor implements PostProcessorInterface
class AdditionalPropertiesAccessorPostProcessor extends PostProcessor
{
/** @var bool */
private $addForModelsWithoutAdditionalPropertiesDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
use PHPModelGenerator\Model\Validator\AbstractComposedPropertyValidator;
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\SchemaProcessor\PostProcessor\RenderedMethod;
use PHPModelGenerator\Utils\RenderHelper;

Expand All @@ -25,7 +25,7 @@
*
* @package PHPModelGenerator\SchemaProcessor\PostProcessor\Internal
*/
class CompositionValidationPostProcessor implements PostProcessorInterface
class CompositionValidationPostProcessor extends PostProcessor
{
/**
* @param Schema $schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
*/
class PopulatePostProcessor implements PostProcessorInterface
class PopulatePostProcessor extends PostProcessor
{
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
Expand Down
33 changes: 33 additions & 0 deletions src/SchemaProcessor/PostProcessor/PostProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types = 1);

namespace PHPModelGenerator\SchemaProcessor\PostProcessor;

use PHPModelGenerator\Model\GeneratorConfiguration;
use PHPModelGenerator\Model\Schema;

abstract class PostProcessor
{
/**
* Have fun doin' crazy stuff with the schema
*
* @param Schema $schema
* @param GeneratorConfiguration $generatorConfiguration
*/
abstract public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void;

/**
* Overwrite this function to execute code before the schemas are processed by the post processor
*/
public function preProcess(): void
{
}

/**
* Overwrite this function to execute code after the schemas are processed by the post processor
*/
public function postProcess(): void
{
}
}
19 changes: 0 additions & 19 deletions src/SchemaProcessor/PostProcessor/PostProcessorInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
*/
class SerializationPostProcessor implements PostProcessorInterface
class SerializationPostProcessor extends PostProcessor
{
/**
* Add serialization support to the provided schema
Expand Down
12 changes: 10 additions & 2 deletions src/SchemaProcessor/RenderQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PHPModelGenerator\Exception\RenderException;
use PHPModelGenerator\Model\GeneratorConfiguration;
use PHPModelGenerator\Model\RenderJob;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;

/**
* Class RenderQueue
Expand Down Expand Up @@ -36,17 +36,25 @@ public function addRenderJob(RenderJob $renderJob): self
* Render all collected jobs of the RenderQueue and clear the queue
*
* @param GeneratorConfiguration $generatorConfiguration
* @param PostProcessorInterface[] $postProcessors
* @param PostProcessor[] $postProcessors
*
* @throws FileSystemException
* @throws RenderException
*/
public function execute(GeneratorConfiguration $generatorConfiguration, array $postProcessors): void {
foreach ($postProcessors as $postProcessor) {
$postProcessor->preProcess();
}

foreach ($this->jobs as $job) {
$job->postProcess($postProcessors, $generatorConfiguration);
$job->render($generatorConfiguration);
}

foreach ($postProcessors as $postProcessor) {
$postProcessor->postProcess();
}

$this->jobs = [];
}
}
4 changes: 2 additions & 2 deletions tests/Basic/BasicSchemaGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PHPModelGenerator\Model\Schema;
use PHPModelGenerator\ModelGenerator;
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;

/**
Expand Down Expand Up @@ -66,7 +66,7 @@ public function testGetterAndSetterAreGeneratedForMutableObjects(bool $implicitN
public function testSetterLogicIsNotExecutedWhenValueIsIdentical(): void
{
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
$modelGenerator->addPostProcessor(new class () implements PostProcessorInterface {
$modelGenerator->addPostProcessor(new class () extends PostProcessor {
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
$schema->addSchemaHook(new class () implements SetterBeforeValidationHookInterface {
Expand Down
4 changes: 2 additions & 2 deletions tests/Basic/SchemaHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookInterface;
use PHPModelGenerator\SchemaProcessor\Hook\SetterAfterValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ public function setterAfterValidationHookDataProvider(): array
protected function addSchemaHook(SchemaHookInterface $schemaHook): void
{
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator) use ($schemaHook): void {
$modelGenerator->addPostProcessor(new class ($schemaHook) implements PostProcessorInterface {
$modelGenerator->addPostProcessor(new class ($schemaHook) extends PostProcessor {
private $schemaHook;

public function __construct(SchemaHookInterface $schemaHook)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PHPModelGenerator\ModelGenerator;
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\AdditionalPropertiesAccessorPostProcessor;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;

/**
Expand Down Expand Up @@ -295,7 +295,7 @@ public function testSetterSchemaHooksAreResolvedInSetAdditionalProperties(): voi
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
$modelGenerator
->addPostProcessor(new AdditionalPropertiesAccessorPostProcessor())
->addPostProcessor(new class () implements PostProcessorInterface {
->addPostProcessor(new class () extends PostProcessor {
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
$schema->addSchemaHook(new class () implements SetterBeforeValidationHookInterface {
Expand Down
6 changes: 3 additions & 3 deletions tests/PostProcessor/PopulatePostProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PHPModelGenerator\SchemaProcessor\Hook\SetterAfterValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PopulatePostProcessor;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;

class PopulatePostProcessorTest extends AbstractPHPModelGeneratorTest
Expand Down Expand Up @@ -205,7 +205,7 @@ public function testSetterBeforeValidationHookInsidePopulateIsResolved(): void
{
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
$modelGenerator->addPostProcessor(new PopulatePostProcessor());
$modelGenerator->addPostProcessor(new class () implements PostProcessorInterface {
$modelGenerator->addPostProcessor(new class () extends PostProcessor {
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
$schema->addSchemaHook(new class () implements SetterBeforeValidationHookInterface {
Expand Down Expand Up @@ -249,7 +249,7 @@ public function testSetterAfterValidationHookInsidePopulateIsResolved(
{
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
$modelGenerator->addPostProcessor(new PopulatePostProcessor());
$modelGenerator->addPostProcessor(new class () implements PostProcessorInterface {
$modelGenerator->addPostProcessor(new class () extends PostProcessor {
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
{
$schema->addSchemaHook(new class () implements SetterAfterValidationHookInterface {
Expand Down

0 comments on commit 014b1d8

Please sign in to comment.