Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added caching setting to GeneratorConfiguration #86

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Model/GeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class GeneratorConfiguration
protected $errorRegistryClass = ErrorRegistryException::class;
/** @var bool */
protected $serialization = false;
/** @var bool */
protected $cacheEnabled = true;

/** @var ClassNameGeneratorInterface */
protected $classNameGenerator;
Expand Down Expand Up @@ -254,6 +256,18 @@ public function setImplicitNull(bool $allowImplicitNull): self
return $this;
}

public function setCacheEnabled(bool $cacheEnabled): self
{
$this->cacheEnabled = $cacheEnabled;

return $this;
}

public function isCacheEnabled(): bool
{
return $this->cacheEnabled;
}

private function initFilter(): void
{
$this
Expand Down
5 changes: 4 additions & 1 deletion src/Model/SchemaDefinition/SchemaDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public function resolveReference(

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

if (!$this->resolvedPaths->offsetExists($key)) {
$isCacheEnabled = $this->schemaProcessor->getGeneratorConfiguration()->isCacheEnabled();
$isResolvedPath = $this->resolvedPaths->offsetExists($key);

if (!$isCacheEnabled || !$isResolvedPath) {
// create a dummy entry for the path first. If the path is used recursive the recursive usages will point
// to the currently created property
$this->resolvedPaths->offsetSet($key, null);
Expand Down
8 changes: 7 additions & 1 deletion tests/AbstractPHPModelGeneratorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ protected function generateClassFromFile(
bool $originalClassNames = false,
bool $implicitNull = true,
string $schemaProviderClass = RecursiveDirectoryProvider::class,
bool $cacheEnabled = true,
): string {
return $this->generateClass(
file_get_contents($this->getSchemaFilePath($file)),
$generatorConfiguration,
$originalClassNames,
$implicitNull,
$schemaProviderClass,
$cacheEnabled
);
}

Expand All @@ -156,6 +158,7 @@ protected function generateClassFromFileTemplate(
bool $escape = true,
bool $implicitNull = true,
string $schemaProviderClass = RecursiveDirectoryProvider::class,
bool $cacheEnabled = true,
): string {
return $this->generateClass(
sprintf(
Expand All @@ -170,6 +173,7 @@ protected function generateClassFromFileTemplate(
false,
$implicitNull,
$schemaProviderClass,
$cacheEnabled
);
}

Expand All @@ -186,10 +190,12 @@ protected function generateClass(
bool $originalClassNames = false,
bool $implicitNull = true,
string $schemaProviderClass = RecursiveDirectoryProvider::class,
bool $cacheEnabled = true,
): string {
$generatorConfiguration = ($generatorConfiguration ?? (new GeneratorConfiguration())->setCollectErrors(false))
->setImplicitNull($implicitNull)
->setOutputEnabled(false);
->setOutputEnabled(false)
->setCacheEnabled($cacheEnabled);

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

Expand Down
37 changes: 37 additions & 0 deletions tests/Objects/ReferencePropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPModelGenerator\Exception\ErrorRegistryException;
use PHPModelGenerator\Exception\FileSystemException;
use PHPModelGenerator\Exception\Object\RequiredValueException;
use PHPModelGenerator\Exception\ValidationException;
use PHPModelGenerator\Exception\RenderException;
use PHPModelGenerator\Exception\SchemaException;
Expand Down Expand Up @@ -547,4 +548,40 @@ public function invalidValuesForMultiplePropertiesWithIdenticalReferenceDataProv
],
];
}

/**
* @throws FileSystemException
* @throws RenderException
* @throws SchemaException
*/
public function testValidCreateObjectRequiredAndOptionalPropertiesWithDefinitionValue(): void
{
$className = $this->generateClassFromFile(
'RequiredAndOptionalPropertiesWithDefinitionValue.json',
cacheEnabled: false,
);

$object = new $className(['requiredProperty' => 'red']);

$this->assertSame('red', $object->getRequiredProperty());
$this->assertNull($object->getOptionalProperty());
}

/**
* @throws FileSystemException
* @throws RenderException
* @throws SchemaException
*/
public function testInvalidCreateObjectRequiredAndOptionalPropertiesWithDefinitionValue(): void
{
$className = $this->generateClassFromFile(
'RequiredAndOptionalPropertiesWithDefinitionValue.json',
cacheEnabled: true,
);

$this->expectException(RequiredValueException::class);
$this->expectExceptionMessage('Missing required value for optionalProperty');

new $className(['requiredProperty' => 'red']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"definitions": {
"forOptionalAndRequiredAtTheSameTime": {
"type": "string"
}
},
"type": "object",
"properties": {
"requiredProperty": {
"$ref": "#/definitions/forOptionalAndRequiredAtTheSameTime"
},
"optionalProperty": {
"$ref": "#/definitions/forOptionalAndRequiredAtTheSameTime"
}
},
"required": [
"requiredProperty"
]
}