Skip to content

Commit

Permalink
Merge pull request #75 from neighborhoods/BUPH-143-api-objects-and-du…
Browse files Browse the repository at this point in the history
…mper

BUPH-143 | add API classes and Dumper
  • Loading branch information
jpmarcotte committed Feb 10, 2022
2 parents e02e3d8 + 418f8d8 commit aaf0c67
Show file tree
Hide file tree
Showing 23 changed files with 908 additions and 20 deletions.
27 changes: 15 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/V1/AnnotationProcessors/SimpleString.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class SimpleString implements V1\AnnotationProcessorInterface
getAnnotationProcessorContext as public;
}

public const CONTEXT_KEY_STRING = 'string';

public function getReplacement(): string
{
$staticContextRecord = $this->getAnnotationProcessorContext()->getStaticContextRecord();

return $staticContextRecord['string'];
return $staticContextRecord[self::CONTEXT_KEY_STRING];
}


Expand Down
5 changes: 3 additions & 2 deletions src/V1/AnnotationProcessors/SymfonyExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class SymfonyExpression implements V1\AnnotationProcessorInterface
getAnnotationProcessorContext as public;
}

public const CONTEXT_KEY_EXPRESSION = 'expression';

public function getReplacement(): string
{
$context = $this->getAnnotationProcessorContext()->getStaticContextRecord();


$expressionLanguage = new ExpressionLanguage();
return (string) $expressionLanguage->evaluate(
$context['expression'],
$context[self::CONTEXT_KEY_EXPRESSION],
[
'context' => $this->getAnnotationProcessorContext()
]
Expand Down
84 changes: 84 additions & 0 deletions src/V1/Api/Dumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Api;

use Neighborhoods\Buphalo;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml;

class Dumper implements DumperInterface
{
/** @var Filesystem */
private $filesystem;

/** @var string */
private $basePath;

public function __construct(string $basePath)
{
$this->setBasePath($basePath);

$this->setFilesystem(new Filesystem());
}

public function dumpFile(FabricationFileInterface $fabricationFile): Dumper
{
$this->getFilesystem()->dumpFile($this->buildFilePath($fabricationFile), $this->dump($fabricationFile));

return $this;
}

private function buildFilePath(FabricationFileInterface $fabricationFile): string
{
return sprintf(
implode(DIRECTORY_SEPARATOR, ['%s', '%s', '%s']) . '.%s',
$this->getBasePath(),
$fabricationFile->getRelativePath(),
$fabricationFile->getPrimaryActorName(),
Buphalo\V1\FabricationFileInterface::FILE_EXTENSION_FABRICATION
);
}

public function dump(FabricationFileInterface $fabricationFile): string
{
$data = \json_decode(\json_encode($fabricationFile), true);

// Not strictly necessary for ephemeral files, but these keys would be ignored by Buphalo anyway
unset(
$data[FabricationFileInterface::PROP_RELATIVE_PATH],
$data[FabricationFileInterface::PROP_PRIMARY_ACTOR_NAME]
);

return Yaml\Yaml::dump($data, PHP_INT_MAX, 2);
}

public function setFilesystem(Filesystem $filesystem): void
{
$this->filesystem = $filesystem;
}

private function getFilesystem(): Filesystem
{
if ($this->filesystem === null) {
throw new \LogicException('Dumper filesystem has not been set.');
}

return $this->filesystem;
}

public function setBasePath(string $basePath): void
{
$this->basePath = $basePath;
}

private function getBasePath(): string
{
if ($this->basePath === null) {
throw new \LogicException('Dumper basePath has not been set.');
}

return $this->basePath;
}
}
12 changes: 12 additions & 0 deletions src/V1/Api/DumperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Api;

interface DumperInterface
{
public function dumpFile(FabricationFileInterface $fabricationFile): Dumper;

public function dump(FabricationFileInterface $fabricationFile): string;
}
104 changes: 104 additions & 0 deletions src/V1/Api/FabricationFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Api;

use Neighborhoods\Buphalo\V1\StringMapInterface;

class FabricationFile implements FabricationFileInterface
{
/** @var StringMapInterface */
private $preferred_template_trees;

/** @var FabricationFile\Actor\MapInterface */
private $actors;

/** @var string */
private $relative_path;

/** @var string */
private $primary_actor_name;

public function getPreferredTemplateTrees(): StringMapInterface
{
if ($this->preferred_template_trees === null) {
throw new \LogicException('preferred_template_trees has not been set');
}

return $this->preferred_template_trees;
}

public function setPreferredTemplateTrees(StringMapInterface $preferred_template_trees): FabricationFileInterface
{
if ($this->preferred_template_trees !== null) {
throw new \LogicException('preferred_template_trees has already been set');
}

$this->preferred_template_trees = $preferred_template_trees;
return $this;
}

public function hasPreferredTemplateTrees(): bool
{
return $this->preferred_template_trees !== null;
}


public function getActors(): FabricationFile\Actor\MapInterface
{
if ($this->actors === null) {
throw new \LogicException('actors has not been set');
}

return $this->actors;
}

public function setActors(FabricationFile\Actor\MapInterface $actors): FabricationFileInterface
{
if ($this->actors !== null) {
throw new \LogicException('actors has already been set');
}

$this->actors = $actors;
return $this;
}

public function getRelativePath(): string
{
if ($this->relative_path === null) {
throw new \LogicException('FabricationFile relativePath has not been set.');
}

return $this->relative_path;
}

public function setRelativePath(string $relative_path): FabricationFileInterface
{
$this->relative_path = $relative_path;

return $this;
}

public function getPrimaryActorName(): string
{
if ($this->primary_actor_name === null) {
throw new \LogicException('FabricationFile primaryActorName has not been set.');
}

return $this->primary_actor_name;
}

public function setPrimaryActorName(string $primary_actor_name): FabricationFileInterface
{
$this->primary_actor_name = $primary_actor_name;

return $this;
}

public function jsonSerialize()
{
return get_object_vars($this);
}

}
Loading

0 comments on commit aaf0c67

Please sign in to comment.