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

WIP Add support for with_items for rendering multiple files from one #43

Draft
wants to merge 1 commit into
base: 0.4.x
Choose a base branch
from
Draft
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
43 changes: 28 additions & 15 deletions src/Types/Confd/Confd.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use my127\Workspace\Expression\Expression;
use my127\Workspace\Path\Path;
use my127\Workspace\Twig\Loader\Filesystem;
use Symfony\Component\Config\Definition\Exception\Exception;
use Twig_Environment;

class Confd
Expand Down Expand Up @@ -32,27 +32,40 @@ public function __construct(Path $path, Definition $definition, Twig_Environment
public function apply(): void
{
foreach ($this->definition->getTemplates() as $path) {
if (isset($path['with_items'])) {
if (!is_iterable($path['with_items'])) {
throw new Exception("Expected with_items to be iterable");
}

if (isset($path['when']) && $this->expression->evaluate($path['when']) === false) {
continue;
}

if (is_string($path)) {
$src = $path.'.twig';
$dst = $this->resolveDstFromSrc($src);
foreach ($path['with_items'] as $key => $item) {
$this->applyPath($path, ['key' => $key, 'item' => $item]);
}
} else {
$src = $path['src'].'.twig';
$dst = isset($path['dst']) ? $this->path->getRealPath($path['dst']) : $this->resolveDstFromSrc($src);
$this->applyPath($path);
}
}
}

$dir = dirname($dst);
private function applyPath(string $path, array $values = []): void {
if (isset($path['when']) && $this->expression->evaluate($path['when'], $values) === false) {
return;
}

if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
if (is_string($path)) {
$src = $path.'.twig';
$dst = $this->resolveDstFromSrc($src);
} else {
$src = $path['src'].'.twig';
$dst = isset($path['dst']) ? $this->path->getRealPath($path['dst']) : $this->resolveDstFromSrc($src);
Copy link
Contributor Author

@andytson-inviqa andytson-inviqa Apr 9, 2020

Choose a reason for hiding this comment

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

@dcole-inviqa is there any way to delay src/dst's = expression filtering to here so the values can be used in evaluating?

}

file_put_contents($dst, $this->twig->render($src));
$dir = dirname($dst);

if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}

file_put_contents($dst, $this->twig->render($src, $values));
}

private function resolveDstFromSrc(string $path): string
Expand Down