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

BUPH-146 | add support for source override behavior #84

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
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
55 changes: 46 additions & 9 deletions src/V1/Actor/Writer.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
<?php

declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Actor;

use LogicException;
use Neighborhoods\Buphalo\V1\Actor;
use Symfony\Component\Filesystem\Filesystem;

use function dirname;

class Writer implements WriterInterface
{
use Actor\Template\Compiler\AwareTrait;

protected $filesystem;
private $filesystem;

private $source_override_behavior;

public function write(): WriterInterface
{
$actor = $this->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;
}

Expand All @@ -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;
}
}
4 changes: 4 additions & 0 deletions src/V1/Actor/Writer.service.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
parameters:
env(Neighborhoods_Buphalo_V1_Actor_WriterInterface__SourceOverrideBehavior): !php/const \Neighborhoods\Buphalo\V1\Actor\WriterInterface::SOURCE_OVERRIDE_BEHAVIOR_DEFAULT
Copy link

@dreinhuber dreinhuber Dec 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it this ensures the default is always being used and line 3 then overrides the default if a variable is provided?

Since it doesn't show in the conversation view, line 3 is:

Neighborhoods\Buphalo\V1\Actor\WriterInterface.SourceOverrideBehavior: '%env(Neighborhoods_Buphalo_V1_Actor_WriterInterface__SourceOverrideBehavior)%'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. It's not a super-intuitive notation, but as far as I can tell, this is how Symfony handles defaults for environment variables that might not be set. And it makes it so that the code in Buphalo doesn't really have to rely on a "default" and can instead be explicit and know the value will be set.

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
public: false
shared: false
calls:
- [setFilesystem, ['@Symfony\Component\Filesystem\Filesystem']]
- [setSourceOverrideBehavior, ['%Neighborhoods\Buphalo\V1\Actor\WriterInterface.SourceOverrideBehavior%']]
6 changes: 6 additions & 0 deletions src/V1/Actor/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/v1/SourceOverrideBehaviorDefault/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fab/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTest;

interface SourceOverrideBehaviorInterface
{
}
37 changes: 37 additions & 0 deletions tests/v1/SourceOverrideBehaviorDefault/run_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env php
<?php
declare(strict_types=1);
error_reporting(E_ALL);

$PWD = __DIR__;

// Must load autoloader before including any Annotation Processors
if (file_exists($autoloaderFilePath = dirname($PWD, 5) . '/autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once $autoloaderFilePath;
} elseif (file_exists($autoloaderFilePath = dirname($PWD, 3) . '/vendor/autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once $autoloaderFilePath;
} else {
throw new RuntimeException('Unable to find the Composer autoloader.');
}

// Include Annotation Processors for Buphalo
// None for this test

// Add exec envs
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__SourceDirectoryPath=$PWD/src");
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__FabricationDirectoryPath=$PWD/fab");
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__NamespacePrefix=Neighborhoods\\BuphaloTest");
putenv("Neighborhoods_Buphalo_V1_TemplateTree_Map_Builder_FactoryInterface__TemplateTreeDirectoryPaths=$PWD/templates");

// Include Buphalo
require_once($PWD . '/../../../bin/v1/buphalo.php');

// Diff the two directories
exec("diff -r $PWD/control/ $PWD/fab/", $output, $return);

if ($return) {
echo(implode(PHP_EOL, $output) . PHP_EOL);
die(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
actors:
<PrimaryActorName>.php:
template: PrimaryActorName.php
<PrimaryActorName>Interface.php:
template: PrimaryActorNameInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTest;

class SourceOverrideBehavior
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTemplateTree;

class PrimaryActorName
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTemplateTree;

interface PrimaryActorNameInterface
{
}
1 change: 1 addition & 0 deletions tests/v1/SourceOverrideBehaviorInvalid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fab/*
37 changes: 37 additions & 0 deletions tests/v1/SourceOverrideBehaviorInvalid/run_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env php
<?php
declare(strict_types=1);
error_reporting(E_ALL);

$PWD = __DIR__;

// Must load autoloader before including any Annotation Processors
if (file_exists($autoloaderFilePath = dirname($PWD, 5) . '/autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once $autoloaderFilePath;
} elseif (file_exists($autoloaderFilePath = dirname($PWD, 3) . '/vendor/autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once $autoloaderFilePath;
} else {
throw new RuntimeException('Unable to find the Composer autoloader.');
}

// Include Annotation Processors for Buphalo
// None for this test

// Add exec envs
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__SourceDirectoryPath=$PWD/src");
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__FabricationDirectoryPath=$PWD/fab");
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__NamespacePrefix=Neighborhoods\\BuphaloTest");
putenv("Neighborhoods_Buphalo_V1_TemplateTree_Map_Builder_FactoryInterface__TemplateTreeDirectoryPaths=$PWD/templates");
putenv("Neighborhoods_Buphalo_V1_Actor_WriterInterface__SourceOverrideBehavior=invalid");

// Include Buphalo
try {
require_once($PWD . '/../../../bin/v1/buphalo.php');
} catch (LogicException $e) {
exit;
}

echo "Failed to catch Logic Exception" . PHP_EOL;
die(1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
actors:
<PrimaryActorName>.php:
template: PrimaryActorName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTest;

class SourceOverrideBehavior
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTemplateTree;

class PrimaryActorName
{
}
1 change: 1 addition & 0 deletions tests/v1/SourceOverrideBehaviorSkip/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fab/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTest;

interface SourceOverrideBehaviorInterface
{
}
39 changes: 39 additions & 0 deletions tests/v1/SourceOverrideBehaviorSkip/run_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env php
<?php
declare(strict_types=1);
error_reporting(E_ALL);

$PWD = __DIR__;

// Must load autoloader before including any Annotation Processors
if (file_exists($autoloaderFilePath = dirname($PWD, 5) . '/autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once $autoloaderFilePath;
} elseif (file_exists($autoloaderFilePath = dirname($PWD, 3) . '/vendor/autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once $autoloaderFilePath;
} else {
throw new RuntimeException('Unable to find the Composer autoloader.');
}

// Include Annotation Processors for Buphalo
// None for this test

// Add exec envs
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__SourceDirectoryPath=$PWD/src");
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__FabricationDirectoryPath=$PWD/fab");
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__NamespacePrefix=Neighborhoods\\BuphaloTest");
putenv("Neighborhoods_Buphalo_V1_TemplateTree_Map_Builder_FactoryInterface__TemplateTreeDirectoryPaths=$PWD/templates");
putenv("Neighborhoods_Buphalo_V1_Actor_WriterInterface__SourceOverrideBehavior=skip");


// Include Buphalo
require_once($PWD . '/../../../bin/v1/buphalo.php');

// Diff the two directories
exec("diff -r $PWD/control/ $PWD/fab/", $output, $return);

if ($return) {
echo(implode(PHP_EOL, $output) . PHP_EOL);
die(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
actors:
<PrimaryActorName>.php:
template: PrimaryActorName.php
<PrimaryActorName>Interface.php:
template: PrimaryActorNameInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTest;

class SourceOverrideBehavior
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTemplateTree;

class PrimaryActorName
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTemplateTree;

interface PrimaryActorNameInterface
{
}
1 change: 1 addition & 0 deletions tests/v1/SourceOverrideBehaviorThrow/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fab/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\BuphaloTest;

interface SourceOverrideBehaviorInterface
{
}
37 changes: 37 additions & 0 deletions tests/v1/SourceOverrideBehaviorThrow/run_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env php
<?php
declare(strict_types=1);
error_reporting(E_ALL);

$PWD = __DIR__;

// Must load autoloader before including any Annotation Processors
if (file_exists($autoloaderFilePath = dirname($PWD, 5) . '/autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once $autoloaderFilePath;
} elseif (file_exists($autoloaderFilePath = dirname($PWD, 3) . '/vendor/autoload.php')) {
/** @noinspection PhpIncludeInspection */
require_once $autoloaderFilePath;
} else {
throw new RuntimeException('Unable to find the Composer autoloader.');
}

// Include Annotation Processors for Buphalo
// None for this test

// Add exec envs
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__SourceDirectoryPath=$PWD/src");
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__FabricationDirectoryPath=$PWD/fab");
putenv("Neighborhoods_Buphalo_V1_TargetApplication_BuilderInterface__NamespacePrefix=Neighborhoods\\BuphaloTest");
putenv("Neighborhoods_Buphalo_V1_TemplateTree_Map_Builder_FactoryInterface__TemplateTreeDirectoryPaths=$PWD/templates");
putenv("Neighborhoods_Buphalo_V1_Actor_WriterInterface__SourceOverrideBehavior=throw");

// Include Buphalo
try {
require_once($PWD . '/../../../bin/v1/buphalo.php');
} catch (RuntimeException $e) {
exit;
}

echo "Failed to catch Logic Exception" . PHP_EOL;
die(1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
actors:
<PrimaryActorName>.php:
template: PrimaryActorName.php
<PrimaryActorName>Interface.php:
template: PrimaryActorNameInterface.php
Loading