diff --git a/src/V1/Actor/Writer.php b/src/V1/Actor/Writer.php index 6938121..d1887eb 100644 --- a/src/V1/Actor/Writer.php +++ b/src/V1/Actor/Writer.php @@ -1,4 +1,5 @@ getActorTemplateCompiler()->getActorTemplateTokenizer()->getActor(); - if (!is_file($actor->getSourceFilePath())) { - $fabricationFilePath = $actor->getFabricationFilePath(); - $this->getFilesystem()->mkdir(dirname($fabricationFilePath)); - if (is_file($fabricationFilePath)) { - $message = sprintf('Actor with fabrication file path [%s] already exists.', $fabricationFilePath); - throw new LogicException($message); + if (is_file($actor->getSourceFilePath())) { + switch ($this->getSourceOverrideBehavior()) { + case WriterInterface::SOURCE_OVERRIDE_BEHAVIOR_SKIP: + return $this; + case WriterInterface::SOURCE_OVERRIDE_BEHAVIOR_THROW: + throw new \RuntimeException(sprintf('File `%s` already exists.', $actor->getSourceFilePath())); + case WriterInterface::SOURCE_OVERRIDE_BEHAVIOR_WRITE: + break; // continue on + default: + throw new \LogicException( + sprintf('Invalid Source-Override Behavior: `%s`', $this->getSourceOverrideBehavior()) + ); } - $compiledContents = $this->getActorTemplateCompiler()->getCompiledContents(); - file_put_contents($fabricationFilePath, $compiledContents); } + $fabricationFilePath = $actor->getFabricationFilePath(); + $this->getFilesystem()->mkdir(dirname($fabricationFilePath)); + if (is_file($fabricationFilePath)) { + $message = sprintf('Actor with fabrication file path [%s] already exists.', $fabricationFilePath); + throw new LogicException($message); + } + $compiledContents = $this->getActorTemplateCompiler()->getCompiledContents(); + file_put_contents($fabricationFilePath, $compiledContents); + return $this; } @@ -51,4 +68,24 @@ public function setFilesystem(Filesystem $filesystem): WriterInterface return $this; } + + public function setSourceOverrideBehavior($source_override_behavior): WriterInterface + { + if ($this->source_override_behavior !== null) { + throw new LogicException('Writer source_override_behavior is already set.'); + } + + $this->source_override_behavior = $source_override_behavior; + + return $this; + } + + private function getSourceOverrideBehavior() + { + if ($this->source_override_behavior === null) { + throw new \LogicException('Writer source_override_behavior has not been set.'); + } + + return $this->source_override_behavior; + } } diff --git a/src/V1/Actor/Writer.service.yml b/src/V1/Actor/Writer.service.yml index 7403421..88c027c 100644 --- a/src/V1/Actor/Writer.service.yml +++ b/src/V1/Actor/Writer.service.yml @@ -1,3 +1,6 @@ +parameters: + env(Neighborhoods_Buphalo_V1_Actor_WriterInterface__SourceOverrideBehavior): !php/const \Neighborhoods\Buphalo\V1\Actor\WriterInterface::SOURCE_OVERRIDE_BEHAVIOR_DEFAULT + Neighborhoods\Buphalo\V1\Actor\WriterInterface.SourceOverrideBehavior: '%env(Neighborhoods_Buphalo_V1_Actor_WriterInterface__SourceOverrideBehavior)%' services: Neighborhoods\Buphalo\V1\Actor\WriterInterface: class: Neighborhoods\Buphalo\V1\Actor\Writer @@ -5,3 +8,4 @@ services: shared: false calls: - [setFilesystem, ['@Symfony\Component\Filesystem\Filesystem']] + - [setSourceOverrideBehavior, ['%Neighborhoods\Buphalo\V1\Actor\WriterInterface.SourceOverrideBehavior%']] diff --git a/src/V1/Actor/WriterInterface.php b/src/V1/Actor/WriterInterface.php index 24efc51..b924843 100644 --- a/src/V1/Actor/WriterInterface.php +++ b/src/V1/Actor/WriterInterface.php @@ -7,6 +7,12 @@ interface WriterInterface { + public const SOURCE_OVERRIDE_BEHAVIOR_SKIP = 'skip'; + public const SOURCE_OVERRIDE_BEHAVIOR_WRITE = 'write'; + public const SOURCE_OVERRIDE_BEHAVIOR_THROW = 'throw'; + + public const SOURCE_OVERRIDE_BEHAVIOR_DEFAULT = self::SOURCE_OVERRIDE_BEHAVIOR_SKIP; + public function setActorTemplateCompiler(CompilerInterface $Compiler); public function write(): WriterInterface; diff --git a/tests/v1/SourceOverrideBehaviorDefault/.gitignore b/tests/v1/SourceOverrideBehaviorDefault/.gitignore new file mode 100644 index 0000000..44f14e1 --- /dev/null +++ b/tests/v1/SourceOverrideBehaviorDefault/.gitignore @@ -0,0 +1 @@ +fab/* diff --git a/tests/v1/SourceOverrideBehaviorDefault/control/SourceOverrideBehaviorInterface.php b/tests/v1/SourceOverrideBehaviorDefault/control/SourceOverrideBehaviorInterface.php new file mode 100644 index 0000000..924baf0 --- /dev/null +++ b/tests/v1/SourceOverrideBehaviorDefault/control/SourceOverrideBehaviorInterface.php @@ -0,0 +1,8 @@ +.php: + template: PrimaryActorName.php + Interface.php: + template: PrimaryActorNameInterface.php diff --git a/tests/v1/SourceOverrideBehaviorDefault/src/SourceOverrideBehavior.php b/tests/v1/SourceOverrideBehaviorDefault/src/SourceOverrideBehavior.php new file mode 100644 index 0000000..ade2b59 --- /dev/null +++ b/tests/v1/SourceOverrideBehaviorDefault/src/SourceOverrideBehavior.php @@ -0,0 +1,8 @@ +.php: + template: PrimaryActorName.php diff --git a/tests/v1/SourceOverrideBehaviorInvalid/src/SourceOverrideBehavior.php b/tests/v1/SourceOverrideBehaviorInvalid/src/SourceOverrideBehavior.php new file mode 100644 index 0000000..ade2b59 --- /dev/null +++ b/tests/v1/SourceOverrideBehaviorInvalid/src/SourceOverrideBehavior.php @@ -0,0 +1,8 @@ +.php: + template: PrimaryActorName.php + Interface.php: + template: PrimaryActorNameInterface.php diff --git a/tests/v1/SourceOverrideBehaviorSkip/src/SourceOverrideBehavior.php b/tests/v1/SourceOverrideBehaviorSkip/src/SourceOverrideBehavior.php new file mode 100644 index 0000000..ade2b59 --- /dev/null +++ b/tests/v1/SourceOverrideBehaviorSkip/src/SourceOverrideBehavior.php @@ -0,0 +1,8 @@ +.php: + template: PrimaryActorName.php + Interface.php: + template: PrimaryActorNameInterface.php diff --git a/tests/v1/SourceOverrideBehaviorThrow/src/SourceOverrideBehavior.php b/tests/v1/SourceOverrideBehaviorThrow/src/SourceOverrideBehavior.php new file mode 100644 index 0000000..ade2b59 --- /dev/null +++ b/tests/v1/SourceOverrideBehaviorThrow/src/SourceOverrideBehavior.php @@ -0,0 +1,8 @@ +.php: + template: PrimaryActorName.php + Interface.php: + template: PrimaryActorNameInterface.php diff --git a/tests/v1/SourceOverrideBehaviorWrite/src/SourceOverrideBehavior.php b/tests/v1/SourceOverrideBehaviorWrite/src/SourceOverrideBehavior.php new file mode 100644 index 0000000..ade2b59 --- /dev/null +++ b/tests/v1/SourceOverrideBehaviorWrite/src/SourceOverrideBehavior.php @@ -0,0 +1,8 @@ +