Skip to content

Commit 07881e4

Browse files
committed
Implemented PostProcessorInterface to apply changes to the schema after the generation process.
Moved serialization logic from model template into a post processor
1 parent f87b9b4 commit 07881e4

File tree

11 files changed

+190
-40
lines changed

11 files changed

+190
-40
lines changed

src/Model/GeneratorConfiguration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use PHPModelGenerator\Utils\ClassNameGenerator;
1414
use PHPModelGenerator\Utils\ClassNameGeneratorInterface;
1515
use PHPModelGenerator\Exception\ErrorRegistryException;
16-
use PHPModelGenerator\Exception\ValidationException;
1716

1817
/**
1918
* Class GeneratorConfiguration

src/Model/MethodInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\Model;
6+
7+
interface MethodInterface
8+
{
9+
/**
10+
* Returns the code of the method including the function signature
11+
*
12+
* @return string
13+
*/
14+
public function getCode(): string;
15+
}

src/Model/Property/Serializer/TransformingFilterSerializer.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPMicroTemplate\Exception\UndefinedSymbolException;
1010
use PHPMicroTemplate\Render;
1111
use PHPModelGenerator\Model\GeneratorConfiguration;
12+
use PHPModelGenerator\Model\MethodInterface;
1213
use PHPModelGenerator\PropertyProcessor\Filter\TransformingFilterInterface;
1314
use PHPModelGenerator\Utils\RenderHelper;
1415

@@ -17,48 +18,51 @@
1718
*
1819
* @package PHPModelGenerator\Model\Property\Serializer
1920
*/
20-
class TransformingFilterSerializer
21+
class TransformingFilterSerializer implements MethodInterface
2122
{
2223
/** @var string */
2324
protected $propertyName;
2425
/** @var TransformingFilterInterface */
2526
protected $filter;
2627
/** @var array */
2728
private $filterOptions;
29+
/** @var GeneratorConfiguration */
30+
private $generatorConfiguration;
2831

2932
/**
3033
* TransformingFilterSerializer constructor.
3134
*
3235
* @param string $propertyName
3336
* @param TransformingFilterInterface $filter
3437
* @param array $filterOptions
38+
* @param GeneratorConfiguration $generatorConfiguration
3539
*/
3640
public function __construct(
3741
string $propertyName,
3842
TransformingFilterInterface $filter,
39-
array $filterOptions
43+
array $filterOptions,
44+
GeneratorConfiguration $generatorConfiguration
4045
) {
4146
$this->propertyName = $propertyName;
4247
$this->filter = $filter;
4348
$this->filterOptions = $filterOptions;
49+
$this->generatorConfiguration = $generatorConfiguration;
4450
}
4551

4652
/**
47-
* @param GeneratorConfiguration $generatorConfiguration
48-
*
4953
* @return string
5054
*
5155
* @throws FileSystemException
5256
* @throws SyntaxErrorException
5357
* @throws UndefinedSymbolException
5458
*/
55-
public function getSerializer(GeneratorConfiguration $generatorConfiguration): string
59+
public function getCode(): string
5660
{
5761
return (new Render(join(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', '..', 'Templates']) . DIRECTORY_SEPARATOR))
5862
->renderTemplate(
5963
DIRECTORY_SEPARATOR . 'Serializer' . DIRECTORY_SEPARATOR . 'TransformingFilterSerializer.phptpl',
6064
[
61-
'viewHelper' => new RenderHelper($generatorConfiguration),
65+
'viewHelper' => new RenderHelper($this->generatorConfiguration),
6266
'property' => $this->propertyName,
6367
'serializerClass' => $this->filter->getSerializer()[0],
6468
'serializerMethod' => $this->filter->getSerializer()[1],

src/Model/RenderJob.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPModelGenerator\Exception\FileSystemException;
1010
use PHPModelGenerator\Exception\RenderException;
1111
use PHPModelGenerator\Exception\ValidationException;
12+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
1213
use PHPModelGenerator\Utils\RenderHelper;
1314

1415
/**
@@ -52,6 +53,16 @@ public function __construct(
5253
$this->initialClass = $initialClass;
5354
}
5455

56+
/**
57+
* @param PostProcessorInterface[] $postProcessors
58+
*/
59+
public function postProcess(array $postProcessors)
60+
{
61+
foreach ($postProcessors as $postProcessor) {
62+
$postProcessor->process($this->schema);
63+
}
64+
}
65+
5566
/**
5667
* Execute the render job and render the class
5768
*
@@ -136,9 +147,7 @@ protected function renderClass(GeneratorConfiguration $generatorConfiguration):
136147
'namespace' => $namespace,
137148
'use' => 'use ' . join(";\nuse ", array_unique($use)) . ';',
138149
'class' => $this->className,
139-
'baseValidators' => $this->schema->getBaseValidators(),
140-
'properties' => $this->schema->getProperties(),
141-
'customSerializer' => $this->schema->getCustomSerializer(),
150+
'schema' => $this->schema,
142151
'generatorConfiguration' => $generatorConfiguration,
143152
'viewHelper' => new RenderHelper($generatorConfiguration),
144153
'initialClass' => $this->initialClass,

src/Model/Schema.php

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace PHPModelGenerator\Model;
66

77
use PHPModelGenerator\Model\Property\PropertyInterface;
8-
use PHPModelGenerator\Model\Property\Serializer\TransformingFilterSerializer;
98
use PHPModelGenerator\Model\SchemaDefinition\SchemaDefinitionDictionary;
109
use PHPModelGenerator\Model\Validator\PropertyValidatorInterface;
1110
use PHPModelGenerator\Model\Validator\SchemaDependencyValidator;
@@ -22,8 +21,16 @@ class Schema
2221
protected $className;
2322
/** @var string */
2423
protected $classPath;
24+
25+
/** @var string[] */
26+
protected $traits = [];
27+
/** @var string[] */
28+
protected $interfaces = [];
2529
/** @var PropertyInterface[] The properties which are part of the class */
2630
protected $properties = [];
31+
/** @var MethodInterface[] */
32+
protected $methods = [];
33+
2734
/** @var PropertyValidatorInterface[] A Collection of validators which must be applied
2835
* before adding properties to the model
2936
*/
@@ -32,8 +39,6 @@ class Schema
3239
protected $usedClasses = [];
3340
/** @var SchemaNamespaceTransferDecorator[] */
3441
protected $namespaceTransferDecorators = [];
35-
/** @var TransformingFilterSerializer[] */
36-
protected $customSerializer = [];
3742

3843
/** @var SchemaDefinitionDictionary */
3944
protected $schemaDefinitionDictionary;
@@ -196,23 +201,63 @@ public function getUsedClasses(array $visitedSchema = []): array
196201
}
197202

198203
/**
199-
* @param string $property
200-
* @param TransformingFilterSerializer $serializer
204+
* @param string $methodKey An unique key in the scope of the schema to identify the method
205+
* @param MethodInterface $method
201206
*
202207
* @return $this
203208
*/
204-
public function addCustomSerializer(string $property, TransformingFilterSerializer $serializer): self
209+
public function addMethod(string $methodKey, MethodInterface $method): self
210+
{
211+
$this->methods[$methodKey] = $method;
212+
213+
return $this;
214+
}
215+
216+
/**
217+
* @return MethodInterface[]
218+
*/
219+
public function getMethods(): array
220+
{
221+
return $this->methods;
222+
}
223+
224+
/**
225+
* @return string[]
226+
*/
227+
public function getTraits(): array
205228
{
206-
$this->customSerializer[$property] = $serializer;
229+
return $this->traits;
230+
}
231+
232+
/**
233+
* @param string $trait
234+
* @return Schema
235+
*/
236+
public function addTrait(string $trait): self
237+
{
238+
$this->traits[] = $trait;
239+
$this->addUsedClass($trait);
207240

208241
return $this;
209242
}
210243

211244
/**
212-
* @return TransformingFilterSerializer[]
245+
* @return string[]
213246
*/
214-
public function getCustomSerializer(): array
247+
public function getInterfaces(): array
215248
{
216-
return $this->customSerializer;
249+
return $this->interfaces;
250+
}
251+
252+
/**
253+
* @param string $interface
254+
* @return Schema
255+
*/
256+
public function addInterface(string $interface): self
257+
{
258+
$this->interfaces[] = $interface;
259+
$this->addUsedClass($interface);
260+
261+
return $this;
217262
}
218263
}

src/ModelGenerator.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PHPModelGenerator\Exception\RenderException;
1010
use PHPModelGenerator\Exception\SchemaException;
1111
use PHPModelGenerator\Model\GeneratorConfiguration;
12+
use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessorInterface;
13+
use PHPModelGenerator\SchemaProcessor\PostProcessor\SerializationPostProcessor;
1214
use PHPModelGenerator\SchemaProcessor\RenderQueue;
1315
use PHPModelGenerator\SchemaProcessor\SchemaProcessor;
1416
use PHPModelGenerator\SchemaProvider\SchemaProviderInterface;
@@ -24,6 +26,8 @@ class ModelGenerator
2426
{
2527
/** @var GeneratorConfiguration */
2628
protected $generatorConfiguration;
29+
/** @var PostProcessorInterface[] */
30+
protected $postProcessors = [];
2731

2832
/**
2933
* Generator constructor.
@@ -33,6 +37,22 @@ class ModelGenerator
3337
public function __construct(GeneratorConfiguration $generatorConfiguration = null)
3438
{
3539
$this->generatorConfiguration = $generatorConfiguration ?? new GeneratorConfiguration();
40+
41+
if ($this->generatorConfiguration->hasSerializationEnabled()) {
42+
$this->addPostProcessor(new SerializationPostProcessor());
43+
}
44+
}
45+
46+
/**
47+
* @param PostProcessorInterface $postProcessor
48+
*
49+
* @return $this
50+
*/
51+
public function addPostProcessor(PostProcessorInterface $postProcessor): self
52+
{
53+
$this->postProcessors[] = $postProcessor;
54+
55+
return $this;
3656
}
3757

3858
/**
@@ -93,7 +113,7 @@ public function generateModels(SchemaProviderInterface $schemaProvider, string $
93113
}
94114

95115
// render all collected classes
96-
$renderQueue->execute($destination, $this->generatorConfiguration);
116+
$renderQueue->execute($destination, $this->generatorConfiguration, $this->postProcessors);
97117

98118
if ($this->generatorConfiguration->hasPrettyPrintEnabled()) {
99119
// @codeCoverageIgnoreStart

src/PropertyProcessor/Filter/FilterProcessor.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ public function process(
101101
$schema->addUsedClass($typeAfterFilter->getName());
102102
}
103103

104-
$schema->addCustomSerializer(
105-
$property->getAttribute(),
106-
new TransformingFilterSerializer($property->getAttribute(), $filter, $filterOptions)
104+
if ($generatorConfiguration->hasSerializationEnabled()) {
105+
$schema->addMethod(
106+
"serialize{$property->getAttribute()}",
107+
new TransformingFilterSerializer(
108+
$property->getAttribute(),
109+
$filter,
110+
$filterOptions,
111+
$generatorConfiguration
112+
)
107113
);
114+
}
108115
}
109116
}
110117
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\SchemaProcessor\PostProcessor;
6+
7+
use PHPModelGenerator\Model\Schema;
8+
9+
interface PostProcessorInterface
10+
{
11+
/**
12+
* Have fun doin' crazy stuff with the schema
13+
*
14+
* @param Schema $schema
15+
*/
16+
public function process(Schema $schema): void;
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPModelGenerator\SchemaProcessor\PostProcessor;
6+
7+
use JsonSerializable;
8+
use PHPModelGenerator\Interfaces\SerializationInterface;
9+
use PHPModelGenerator\Model\Schema;
10+
use PHPModelGenerator\Traits\SerializableTrait;
11+
12+
/**
13+
* Class SerializationPostProcessor
14+
*
15+
* @package PHPModelGenerator\SchemaProcessor\PostProcessor
16+
*/
17+
class SerializationPostProcessor implements PostProcessorInterface
18+
{
19+
public function process(Schema $schema): void
20+
{
21+
$schema
22+
->addTrait(SerializableTrait::class)
23+
->addInterface(JsonSerializable::class)
24+
->addInterface(SerializationInterface::class);
25+
}
26+
}

src/SchemaProcessor/RenderQueue.php

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

1213
/**
1314
* Class RenderQueue
@@ -34,15 +35,20 @@ public function addRenderJob(RenderJob $renderJob): self
3435
/**
3536
* Render all collected jobs of the RenderProxy and clear the queue
3637
*
37-
* @param string $destination
38-
* @param GeneratorConfiguration $generatorConfiguration
38+
* @param string $destination
39+
* @param GeneratorConfiguration $generatorConfiguration
40+
* @param PostProcessorInterface[] $postProcessors
3941
*
4042
* @throws FileSystemException
4143
* @throws RenderException
4244
*/
43-
public function execute(string $destination, GeneratorConfiguration $generatorConfiguration): void
44-
{
45+
public function execute(
46+
string $destination,
47+
GeneratorConfiguration $generatorConfiguration,
48+
array $postProcessors
49+
): void {
4550
foreach ($this->jobs as $job) {
51+
$job->postProcess($postProcessors);
4652
$job->render($destination, $generatorConfiguration);
4753
}
4854

0 commit comments

Comments
 (0)