Skip to content

Commit 014b1d8

Browse files
authored
Merge pull request #22 from wol-soft/PostProcessorHooks
Post Processor pre/post execution hooks
2 parents 41ad8e5 + b80597d commit 014b1d8

14 files changed

+68
-44
lines changed

docs/source/generator/postProcessor.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ By default additional properties are not included in serialized models. If the *
148148
Custom Post Processors
149149
----------------------
150150

151-
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.
151+
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.
152152

153153
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:
154154

@@ -157,9 +157,9 @@ A custom post processor which adds a custom trait to the generated model (eg. a
157157
namespace MyApp\Model\Generator\PostProcessor;
158158
159159
use MyApp\Model\ActiveRecordTrait;
160-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
160+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
161161
162-
class ActiveRecordPostProcessor implements PostProcessorInterface
162+
class ActiveRecordPostProcessor extends PostProcessor
163163
{
164164
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
165165
{
@@ -188,3 +188,5 @@ What can you do inside your custom post processor?
188188
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.
189189

190190
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>`__
191+
192+
To execute code before/after the processing of the schemas override the methods **preProcess** and **postProcess** inside your custom post processor.

src/Model/RenderJob.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PHPModelGenerator\Exception\ValidationException;
1212
use PHPModelGenerator\Model\Validator\AbstractComposedPropertyValidator;
1313
use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookResolver;
14-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
14+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
1515
use PHPModelGenerator\Utils\RenderHelper;
1616

1717
/**
@@ -51,7 +51,7 @@ public function __construct(
5151
}
5252

5353
/**
54-
* @param PostProcessorInterface[] $postProcessors
54+
* @param PostProcessor[] $postProcessors
5555
* @param GeneratorConfiguration $generatorConfiguration
5656
*/
5757
public function postProcess(array $postProcessors, GeneratorConfiguration $generatorConfiguration)

src/ModelGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPModelGenerator\Exception\SchemaException;
1111
use PHPModelGenerator\Model\GeneratorConfiguration;
1212
use PHPModelGenerator\SchemaProcessor\PostProcessor\Internal\CompositionValidationPostProcessor;
13-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
13+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
1414
use PHPModelGenerator\SchemaProcessor\PostProcessor\SerializationPostProcessor;
1515
use PHPModelGenerator\SchemaProcessor\RenderQueue;
1616
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
@@ -27,7 +27,7 @@ class ModelGenerator
2727
{
2828
/** @var GeneratorConfiguration */
2929
protected $generatorConfiguration;
30-
/** @var PostProcessorInterface[] */
30+
/** @var PostProcessor[] */
3131
protected $postProcessors = [];
3232

3333
/**
@@ -48,11 +48,11 @@ public function __construct(GeneratorConfiguration $generatorConfiguration = nul
4848
}
4949

5050
/**
51-
* @param PostProcessorInterface $postProcessor
51+
* @param PostProcessor $postProcessor
5252
*
5353
* @return $this
5454
*/
55-
public function addPostProcessor(PostProcessorInterface $postProcessor): self
55+
public function addPostProcessor(PostProcessor $postProcessor): self
5656
{
5757
$this->postProcessors[] = $postProcessor;
5858

src/SchemaProcessor/PostProcessor/AdditionalPropertiesAccessorPostProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
2828
*/
29-
class AdditionalPropertiesAccessorPostProcessor implements PostProcessorInterface
29+
class AdditionalPropertiesAccessorPostProcessor extends PostProcessor
3030
{
3131
/** @var bool */
3232
private $addForModelsWithoutAdditionalPropertiesDefinition;

src/SchemaProcessor/PostProcessor/Internal/CompositionValidationPostProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
1212
use PHPModelGenerator\Model\Validator\AbstractComposedPropertyValidator;
1313
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
14-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
14+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
1515
use PHPModelGenerator\SchemaProcessor\PostProcessor\RenderedMethod;
1616
use PHPModelGenerator\Utils\RenderHelper;
1717

@@ -25,7 +25,7 @@
2525
*
2626
* @package PHPModelGenerator\SchemaProcessor\PostProcessor\Internal
2727
*/
28-
class CompositionValidationPostProcessor implements PostProcessorInterface
28+
class CompositionValidationPostProcessor extends PostProcessor
2929
{
3030
/**
3131
* @param Schema $schema

src/SchemaProcessor/PostProcessor/PopulatePostProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
1515
*/
16-
class PopulatePostProcessor implements PostProcessorInterface
16+
class PopulatePostProcessor extends PostProcessor
1717
{
1818
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
1919
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\SchemaProcessor\PostProcessor;
6+
7+
use PHPModelGenerator\Model\GeneratorConfiguration;
8+
use PHPModelGenerator\Model\Schema;
9+
10+
abstract class PostProcessor
11+
{
12+
/**
13+
* Have fun doin' crazy stuff with the schema
14+
*
15+
* @param Schema $schema
16+
* @param GeneratorConfiguration $generatorConfiguration
17+
*/
18+
abstract public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void;
19+
20+
/**
21+
* Overwrite this function to execute code before the schemas are processed by the post processor
22+
*/
23+
public function preProcess(): void
24+
{
25+
}
26+
27+
/**
28+
* Overwrite this function to execute code after the schemas are processed by the post processor
29+
*/
30+
public function postProcess(): void
31+
{
32+
}
33+
}

src/SchemaProcessor/PostProcessor/PostProcessorInterface.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/SchemaProcessor/PostProcessor/SerializationPostProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
1919
*/
20-
class SerializationPostProcessor implements PostProcessorInterface
20+
class SerializationPostProcessor extends PostProcessor
2121
{
2222
/**
2323
* Add serialization support to the provided schema

src/SchemaProcessor/RenderQueue.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PHPModelGenerator\Exception\RenderException;
99
use PHPModelGenerator\Model\GeneratorConfiguration;
1010
use PHPModelGenerator\Model\RenderJob;
11-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
11+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
1212

1313
/**
1414
* Class RenderQueue
@@ -36,17 +36,25 @@ public function addRenderJob(RenderJob $renderJob): self
3636
* Render all collected jobs of the RenderQueue and clear the queue
3737
*
3838
* @param GeneratorConfiguration $generatorConfiguration
39-
* @param PostProcessorInterface[] $postProcessors
39+
* @param PostProcessor[] $postProcessors
4040
*
4141
* @throws FileSystemException
4242
* @throws RenderException
4343
*/
4444
public function execute(GeneratorConfiguration $generatorConfiguration, array $postProcessors): void {
45+
foreach ($postProcessors as $postProcessor) {
46+
$postProcessor->preProcess();
47+
}
48+
4549
foreach ($this->jobs as $job) {
4650
$job->postProcess($postProcessors, $generatorConfiguration);
4751
$job->render($generatorConfiguration);
4852
}
4953

54+
foreach ($postProcessors as $postProcessor) {
55+
$postProcessor->postProcess();
56+
}
57+
5058
$this->jobs = [];
5159
}
5260
}

tests/Basic/BasicSchemaGenerationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PHPModelGenerator\Model\Schema;
1414
use PHPModelGenerator\ModelGenerator;
1515
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
16-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
16+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
1717
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
1818

1919
/**
@@ -66,7 +66,7 @@ public function testGetterAndSetterAreGeneratedForMutableObjects(bool $implicitN
6666
public function testSetterLogicIsNotExecutedWhenValueIsIdentical(): void
6767
{
6868
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
69-
$modelGenerator->addPostProcessor(new class () implements PostProcessorInterface {
69+
$modelGenerator->addPostProcessor(new class () extends PostProcessor {
7070
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
7171
{
7272
$schema->addSchemaHook(new class () implements SetterBeforeValidationHookInterface {

tests/Basic/SchemaHookTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPModelGenerator\SchemaProcessor\Hook\SchemaHookInterface;
1616
use PHPModelGenerator\SchemaProcessor\Hook\SetterAfterValidationHookInterface;
1717
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
18-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
18+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
1919
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
2020

2121
/**
@@ -184,7 +184,7 @@ public function setterAfterValidationHookDataProvider(): array
184184
protected function addSchemaHook(SchemaHookInterface $schemaHook): void
185185
{
186186
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator) use ($schemaHook): void {
187-
$modelGenerator->addPostProcessor(new class ($schemaHook) implements PostProcessorInterface {
187+
$modelGenerator->addPostProcessor(new class ($schemaHook) extends PostProcessor {
188188
private $schemaHook;
189189

190190
public function __construct(SchemaHookInterface $schemaHook)

tests/PostProcessor/AdditionalPropertiesAccessorPostProcessorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use PHPModelGenerator\ModelGenerator;
1717
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
1818
use PHPModelGenerator\SchemaProcessor\PostProcessor\AdditionalPropertiesAccessorPostProcessor;
19-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
19+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
2020
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
2121

2222
/**
@@ -295,7 +295,7 @@ public function testSetterSchemaHooksAreResolvedInSetAdditionalProperties(): voi
295295
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
296296
$modelGenerator
297297
->addPostProcessor(new AdditionalPropertiesAccessorPostProcessor())
298-
->addPostProcessor(new class () implements PostProcessorInterface {
298+
->addPostProcessor(new class () extends PostProcessor {
299299
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
300300
{
301301
$schema->addSchemaHook(new class () implements SetterBeforeValidationHookInterface {

tests/PostProcessor/PopulatePostProcessorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use PHPModelGenerator\SchemaProcessor\Hook\SetterAfterValidationHookInterface;
1818
use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
1919
use PHPModelGenerator\SchemaProcessor\PostProcessor\PopulatePostProcessor;
20-
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
20+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
2121
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
2222

2323
class PopulatePostProcessorTest extends AbstractPHPModelGeneratorTest
@@ -205,7 +205,7 @@ public function testSetterBeforeValidationHookInsidePopulateIsResolved(): void
205205
{
206206
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
207207
$modelGenerator->addPostProcessor(new PopulatePostProcessor());
208-
$modelGenerator->addPostProcessor(new class () implements PostProcessorInterface {
208+
$modelGenerator->addPostProcessor(new class () extends PostProcessor {
209209
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
210210
{
211211
$schema->addSchemaHook(new class () implements SetterBeforeValidationHookInterface {
@@ -249,7 +249,7 @@ public function testSetterAfterValidationHookInsidePopulateIsResolved(
249249
{
250250
$this->modifyModelGenerator = function (ModelGenerator $modelGenerator): void {
251251
$modelGenerator->addPostProcessor(new PopulatePostProcessor());
252-
$modelGenerator->addPostProcessor(new class () implements PostProcessorInterface {
252+
$modelGenerator->addPostProcessor(new class () extends PostProcessor {
253253
public function process(Schema $schema, GeneratorConfiguration $generatorConfiguration): void
254254
{
255255
$schema->addSchemaHook(new class () implements SetterAfterValidationHookInterface {

0 commit comments

Comments
 (0)