-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…mper BUPH-143 | add API classes and Dumper
- Loading branch information
Showing
23 changed files
with
908 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.