Skip to content

Commit 0b158f5

Browse files
kolodkinvalentinKolodkin Valentin
authored and
Kolodkin Valentin
committed
Added caching setting to GeneratorConfiguration
1 parent 789fe36 commit 0b158f5

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

src/Model/GeneratorConfiguration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class GeneratorConfiguration
4141
protected $errorRegistryClass = ErrorRegistryException::class;
4242
/** @var bool */
4343
protected $serialization = false;
44+
/** @var bool */
45+
protected $cacheEnabled = true;
4446

4547
/** @var ClassNameGeneratorInterface */
4648
protected $classNameGenerator;
@@ -254,6 +256,18 @@ public function setImplicitNull(bool $allowImplicitNull): self
254256
return $this;
255257
}
256258

259+
public function setCacheEnabled(bool $cacheEnabled): self
260+
{
261+
$this->cacheEnabled = $cacheEnabled;
262+
263+
return $this;
264+
}
265+
266+
public function isCacheEnabled(): bool
267+
{
268+
return $this->cacheEnabled;
269+
}
270+
257271
private function initFilter(): void
258272
{
259273
$this

src/Model/SchemaDefinition/SchemaDefinition.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ public function resolveReference(
6767

6868
$key = implode('-', $originalPath);
6969

70-
if (!$this->resolvedPaths->offsetExists($key)) {
70+
$isCacheEnabled = $this->schemaProcessor->getGeneratorConfiguration()->isCacheEnabled();
71+
$isResolvedPath = $this->resolvedPaths->offsetExists($key);
72+
73+
if (!$isCacheEnabled || !$isResolvedPath) {
7174
// create a dummy entry for the path first. If the path is used recursive the recursive usages will point
7275
// to the currently created property
7376
$this->resolvedPaths->offsetSet($key, null);

tests/AbstractPHPModelGeneratorTestCase.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,15 @@ protected function generateClassFromFile(
130130
bool $originalClassNames = false,
131131
bool $implicitNull = true,
132132
string $schemaProviderClass = RecursiveDirectoryProvider::class,
133+
bool $cacheEnabled = true,
133134
): string {
134135
return $this->generateClass(
135136
file_get_contents($this->getSchemaFilePath($file)),
136137
$generatorConfiguration,
137138
$originalClassNames,
138139
$implicitNull,
139140
$schemaProviderClass,
141+
$cacheEnabled
140142
);
141143
}
142144

@@ -156,6 +158,7 @@ protected function generateClassFromFileTemplate(
156158
bool $escape = true,
157159
bool $implicitNull = true,
158160
string $schemaProviderClass = RecursiveDirectoryProvider::class,
161+
bool $cacheEnabled = true,
159162
): string {
160163
return $this->generateClass(
161164
sprintf(
@@ -170,6 +173,7 @@ protected function generateClassFromFileTemplate(
170173
false,
171174
$implicitNull,
172175
$schemaProviderClass,
176+
$cacheEnabled
173177
);
174178
}
175179

@@ -186,10 +190,12 @@ protected function generateClass(
186190
bool $originalClassNames = false,
187191
bool $implicitNull = true,
188192
string $schemaProviderClass = RecursiveDirectoryProvider::class,
193+
bool $cacheEnabled = true,
189194
): string {
190195
$generatorConfiguration = ($generatorConfiguration ?? (new GeneratorConfiguration())->setCollectErrors(false))
191196
->setImplicitNull($implicitNull)
192-
->setOutputEnabled(false);
197+
->setOutputEnabled(false)
198+
->setCacheEnabled($cacheEnabled);
193199

194200
$baseDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'PHPModelGeneratorTest';
195201

tests/Objects/ReferencePropertyTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPModelGenerator\Exception\ErrorRegistryException;
88
use PHPModelGenerator\Exception\FileSystemException;
9+
use PHPModelGenerator\Exception\Object\RequiredValueException;
910
use PHPModelGenerator\Exception\ValidationException;
1011
use PHPModelGenerator\Exception\RenderException;
1112
use PHPModelGenerator\Exception\SchemaException;
@@ -547,4 +548,40 @@ public function invalidValuesForMultiplePropertiesWithIdenticalReferenceDataProv
547548
],
548549
];
549550
}
551+
552+
/**
553+
* @throws FileSystemException
554+
* @throws RenderException
555+
* @throws SchemaException
556+
*/
557+
public function testValidCreateObjectRequiredAndOptionalPropertiesWithDefinitionValue(): void
558+
{
559+
$className = $this->generateClassFromFile(
560+
'RequiredAndOptionalPropertiesWithDefinitionValue.json',
561+
cacheEnabled: false,
562+
);
563+
564+
$object = new $className(['requiredProperty' => 'red']);
565+
566+
$this->assertSame('red', $object->getRequiredProperty());
567+
$this->assertNull($object->getOptionalProperty());
568+
}
569+
570+
/**
571+
* @throws FileSystemException
572+
* @throws RenderException
573+
* @throws SchemaException
574+
*/
575+
public function testInvalidCreateObjectRequiredAndOptionalPropertiesWithDefinitionValue(): void
576+
{
577+
$className = $this->generateClassFromFile(
578+
'RequiredAndOptionalPropertiesWithDefinitionValue.json',
579+
cacheEnabled: true,
580+
);
581+
582+
$this->expectException(RequiredValueException::class);
583+
$this->expectExceptionMessage('Missing required value for optionalProperty');
584+
585+
new $className(['requiredProperty' => 'red']);
586+
}
550587
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"definitions": {
3+
"forOptionalAndRequiredAtTheSameTime": {
4+
"type": "string"
5+
}
6+
},
7+
"type": "object",
8+
"properties": {
9+
"requiredProperty": {
10+
"$ref": "#/definitions/forOptionalAndRequiredAtTheSameTime"
11+
},
12+
"optionalProperty": {
13+
"$ref": "#/definitions/forOptionalAndRequiredAtTheSameTime"
14+
}
15+
},
16+
"required": [
17+
"requiredProperty"
18+
]
19+
}

0 commit comments

Comments
 (0)