summaryrefslogtreecommitdiff
path: root/src/TemplateModule.php
blob: 9bbb6f92789a47e518383184b79378d27772d45a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php

namespace PHPIAC\Modules;

use PHPIAC\Connection;
use PHPIAC\Module\Module;
use PHPIAC\Modules\Support\HandlesFiles;
use PHPIAC\Modules\Support\HasPermissions;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

class TemplateModule extends Module
{
    use HasPermissions, HandlesFiles;

    protected string $src;
    protected string $dest;
    protected array $vars;

    protected bool $force = false;

    /**
     * @inheritDoc
     */
    public function checkState(): bool
    {
        if ($this->force) {
            return false;
            // TODO: ContentsEqual?();
        }

        return $this->fileExists($this->dest);
    }

    /**
     * @inheritDoc
     */
    public function execute(): void
    {
        $loader = new FilesystemLoader(dirname($this->src));
        $twig = new Environment($loader);
        $rendered = $twig->render(basename($this->src), $this->vars);

        Connection::put($this->dest, $rendered);

        Connection::exec($this->getPermissions($this->dest));
    }
}