Skip to content

Commit

Permalink
Refactor to always apply the current config when accessing prcessors
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann committed Apr 20, 2022
1 parent d7d4874 commit c7e261a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 36 deletions.
74 changes: 40 additions & 34 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,18 @@ public function setAnalyser(?AnalyserInterface $analyser): Generator
return $this;
}

public function getDefaultConfig(): array
{
return [
'operationId' => [
'hash' => true,
],
];
}

public function getConfig(): array
{
return $this->config + [
'operationId' => ['hash' => true],
];
return $this->config + $this->getDefaultConfig();
}

/**
Expand All @@ -205,38 +212,37 @@ public function setConfig(array $config): Generator
public function getProcessors(): array
{
if (null === $this->processors) {
$defaultProcessors = [
Processors\DocBlockDescriptions::class,
Processors\MergeIntoOpenApi::class,
Processors\MergeIntoComponents::class,
Processors\ExpandClasses::class,
Processors\ExpandInterfaces::class,
Processors\ExpandTraits::class,
Processors\ExpandEnums::class,
Processors\AugmentSchemas::class,
Processors\AugmentProperties::class,
Processors\BuildPaths::class,
Processors\AugmentParameters::class,
Processors\AugmentRefs::class,
Processors\MergeJsonContent::class,
Processors\MergeXmlContent::class,
Processors\OperationId::class,
Processors\CleanUnmerged::class,
$this->processors = [
new Processors\DocBlockDescriptions(),
new Processors\MergeIntoOpenApi(),
new Processors\MergeIntoComponents(),
new Processors\ExpandClasses(),
new Processors\ExpandInterfaces(),
new Processors\ExpandTraits(),
new Processors\ExpandEnums(),
new Processors\AugmentSchemas(),
new Processors\AugmentProperties(),
new Processors\BuildPaths(),
new Processors\AugmentParameters(),
new Processors\AugmentRefs(),
new Processors\MergeJsonContent(),
new Processors\MergeXmlContent(),
new Processors\OperationId(),
new Processors\CleanUnmerged(),
];
$this->processors = [];
$config = $this->getConfig();
foreach ($defaultProcessors as $processor) {
$rc = new \ReflectionClass($processor);
$this->processors[] = $instance = new $processor();

// optional config
$processorKey = lcfirst($rc->getShortName());
if (array_key_exists($processorKey, $config)) {
foreach ($config[$processorKey] as $name => $value) {
$setter = 'set' . ucfirst($name);
if (method_exists($instance, $setter)) {
$instance->{$setter}($value);
}
}

$config = $this->getConfig();
foreach ($this->processors as $processor) {
$rc = new \ReflectionClass($processor);

// apply config
$processorKey = lcfirst($rc->getShortName());
if (array_key_exists($processorKey, $config)) {
foreach ($config[$processorKey] as $name => $value) {
$setter = 'set' . ucfirst($name);
if (method_exists($processor, $setter)) {
$processor->{$setter}($value);
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public function testScanInvalidSource(): void
public function processorCases(): iterable
{
return [
[new OperationId(false), false],
[new OperationId(true), true],
[new OperationId(), true],
[new class(false) extends OperationId {
}, false],
];
Expand Down Expand Up @@ -116,4 +115,21 @@ public function testRemoveProcessorNotFound(): void
(new Generator())->removeProcessor(function () {
});
}

public function testConfig()
{
$assertOperationId = function (Generator $generator, bool $expected) {
foreach ($generator->getProcessors() as $processor) {
if ($processor instanceof OperationId) {
$this->assertEquals($expected, $processor->isHash());
}
}
};

$generator = new Generator();
$assertOperationId($generator, true);

$generator->setConfig(['operationId' => ['hash' => false]]);
$assertOperationId($generator, false);
}
}

0 comments on commit c7e261a

Please sign in to comment.