From 2451f65efc761b64a5547d5c8bc6c29418e741ab Mon Sep 17 00:00:00 2001 From: Jacques Marcotte Date: Tue, 6 Dec 2022 18:01:32 -0600 Subject: [PATCH] BUPH-146 | add support for source override behavior --- src/V1/Actor/Writer.php | 55 ++++++++++++++++--- src/V1/Actor/Writer.service.yml | 4 ++ src/V1/Actor/WriterInterface.php | 6 ++ .../SourceOverrideBehaviorDefault/.gitignore | 1 + .../SourceOverrideBehaviorInterface.php | 8 +++ .../v1/SourceOverrideBehaviorDefault/run_test | 37 +++++++++++++ ...verrideBehavior.buphalo.v1.fabrication.yml | 5 ++ .../src/SourceOverrideBehavior.php | 8 +++ .../templates/PrimaryActorName.php | 8 +++ .../templates/PrimaryActorNameInterface.php | 8 +++ .../SourceOverrideBehaviorInvalid/.gitignore | 1 + .../v1/SourceOverrideBehaviorInvalid/run_test | 37 +++++++++++++ ...verrideBehavior.buphalo.v1.fabrication.yml | 3 + .../src/SourceOverrideBehavior.php | 8 +++ .../templates/PrimaryActorName.php | 8 +++ .../v1/SourceOverrideBehaviorSkip/.gitignore | 1 + .../SourceOverrideBehaviorInterface.php | 8 +++ tests/v1/SourceOverrideBehaviorSkip/run_test | 39 +++++++++++++ ...verrideBehavior.buphalo.v1.fabrication.yml | 5 ++ .../src/SourceOverrideBehavior.php | 8 +++ .../templates/PrimaryActorName.php | 8 +++ .../templates/PrimaryActorNameInterface.php | 8 +++ .../v1/SourceOverrideBehaviorThrow/.gitignore | 1 + .../SourceOverrideBehaviorInterface.php | 8 +++ tests/v1/SourceOverrideBehaviorThrow/run_test | 37 +++++++++++++ ...verrideBehavior.buphalo.v1.fabrication.yml | 5 ++ .../src/SourceOverrideBehavior.php | 8 +++ .../templates/PrimaryActorName.php | 8 +++ .../templates/PrimaryActorNameInterface.php | 8 +++ .../v1/SourceOverrideBehaviorWrite/.gitignore | 1 + .../control/SourceOverrideBehavior.php | 8 +++ .../SourceOverrideBehaviorInterface.php | 8 +++ .../fab/SourceOverrideBehavior.php | 8 +++ tests/v1/SourceOverrideBehaviorWrite/run_test | 38 +++++++++++++ ...verrideBehavior.buphalo.v1.fabrication.yml | 5 ++ .../src/SourceOverrideBehavior.php | 8 +++ .../templates/PrimaryActorName.php | 8 +++ .../templates/PrimaryActorNameInterface.php | 8 +++ 38 files changed, 432 insertions(+), 9 deletions(-) create mode 100644 tests/v1/SourceOverrideBehaviorDefault/.gitignore create mode 100644 tests/v1/SourceOverrideBehaviorDefault/control/SourceOverrideBehaviorInterface.php create mode 100755 tests/v1/SourceOverrideBehaviorDefault/run_test create mode 100644 tests/v1/SourceOverrideBehaviorDefault/src/SourceOverrideBehavior.buphalo.v1.fabrication.yml create mode 100644 tests/v1/SourceOverrideBehaviorDefault/src/SourceOverrideBehavior.php create mode 100644 tests/v1/SourceOverrideBehaviorDefault/templates/PrimaryActorName.php create mode 100644 tests/v1/SourceOverrideBehaviorDefault/templates/PrimaryActorNameInterface.php create mode 100644 tests/v1/SourceOverrideBehaviorInvalid/.gitignore create mode 100755 tests/v1/SourceOverrideBehaviorInvalid/run_test create mode 100644 tests/v1/SourceOverrideBehaviorInvalid/src/SourceOverrideBehavior.buphalo.v1.fabrication.yml create mode 100644 tests/v1/SourceOverrideBehaviorInvalid/src/SourceOverrideBehavior.php create mode 100644 tests/v1/SourceOverrideBehaviorInvalid/templates/PrimaryActorName.php create mode 100644 tests/v1/SourceOverrideBehaviorSkip/.gitignore create mode 100644 tests/v1/SourceOverrideBehaviorSkip/control/SourceOverrideBehaviorInterface.php create mode 100755 tests/v1/SourceOverrideBehaviorSkip/run_test create mode 100644 tests/v1/SourceOverrideBehaviorSkip/src/SourceOverrideBehavior.buphalo.v1.fabrication.yml create mode 100644 tests/v1/SourceOverrideBehaviorSkip/src/SourceOverrideBehavior.php create mode 100644 tests/v1/SourceOverrideBehaviorSkip/templates/PrimaryActorName.php create mode 100644 tests/v1/SourceOverrideBehaviorSkip/templates/PrimaryActorNameInterface.php create mode 100644 tests/v1/SourceOverrideBehaviorThrow/.gitignore create mode 100644 tests/v1/SourceOverrideBehaviorThrow/control/SourceOverrideBehaviorInterface.php create mode 100755 tests/v1/SourceOverrideBehaviorThrow/run_test create mode 100644 tests/v1/SourceOverrideBehaviorThrow/src/SourceOverrideBehavior.buphalo.v1.fabrication.yml create mode 100644 tests/v1/SourceOverrideBehaviorThrow/src/SourceOverrideBehavior.php create mode 100644 tests/v1/SourceOverrideBehaviorThrow/templates/PrimaryActorName.php create mode 100644 tests/v1/SourceOverrideBehaviorThrow/templates/PrimaryActorNameInterface.php create mode 100644 tests/v1/SourceOverrideBehaviorWrite/.gitignore create mode 100644 tests/v1/SourceOverrideBehaviorWrite/control/SourceOverrideBehavior.php create mode 100644 tests/v1/SourceOverrideBehaviorWrite/control/SourceOverrideBehaviorInterface.php create mode 100644 tests/v1/SourceOverrideBehaviorWrite/fab/SourceOverrideBehavior.php create mode 100755 tests/v1/SourceOverrideBehaviorWrite/run_test create mode 100644 tests/v1/SourceOverrideBehaviorWrite/src/SourceOverrideBehavior.buphalo.v1.fabrication.yml create mode 100644 tests/v1/SourceOverrideBehaviorWrite/src/SourceOverrideBehavior.php create mode 100644 tests/v1/SourceOverrideBehaviorWrite/templates/PrimaryActorName.php create mode 100644 tests/v1/SourceOverrideBehaviorWrite/templates/PrimaryActorNameInterface.php diff --git a/src/V1/Actor/Writer.php b/src/V1/Actor/Writer.php index 6938121f..d1887eb3 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 74034216..88c027c7 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 24efc515..b9248434 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 00000000..44f14e18 --- /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 00000000..924baf0d --- /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 00000000..ade2b598 --- /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 00000000..ade2b598 --- /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 00000000..ade2b598 --- /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 00000000..ade2b598 --- /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 00000000..ade2b598 --- /dev/null +++ b/tests/v1/SourceOverrideBehaviorWrite/src/SourceOverrideBehavior.php @@ -0,0 +1,8 @@ +