Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of phpunit/php-text-template.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianBergmann\Template;
11
 
12
use function array_merge;
13
use function file_exists;
14
use function file_get_contents;
15
use function file_put_contents;
16
use function sprintf;
17
use function str_replace;
18
 
19
final class Template
20
{
21
    /**
22
     * @var string
23
     */
24
    private $template = '';
25
 
26
    /**
27
     * @var string
28
     */
29
    private $openDelimiter;
30
 
31
    /**
32
     * @var string
33
     */
34
    private $closeDelimiter;
35
 
36
    /**
37
     * @var array
38
     */
39
    private $values = [];
40
 
41
    /**
42
     * @throws InvalidArgumentException
43
     */
44
    public function __construct(string $file = '', string $openDelimiter = '{', string $closeDelimiter = '}')
45
    {
46
        $this->setFile($file);
47
 
48
        $this->openDelimiter  = $openDelimiter;
49
        $this->closeDelimiter = $closeDelimiter;
50
    }
51
 
52
    /**
53
     * @throws InvalidArgumentException
54
     */
55
    public function setFile(string $file): void
56
    {
57
        $distFile = $file . '.dist';
58
 
59
        if (file_exists($file)) {
60
            $this->template = file_get_contents($file);
61
        } elseif (file_exists($distFile)) {
62
            $this->template = file_get_contents($distFile);
63
        } else {
64
            throw new InvalidArgumentException(
65
                sprintf(
66
                    'Failed to load template "%s"',
67
                    $file
68
                )
69
            );
70
        }
71
    }
72
 
73
    public function setVar(array $values, bool $merge = true): void
74
    {
75
        if (!$merge || empty($this->values)) {
76
            $this->values = $values;
77
        } else {
78
            $this->values = array_merge($this->values, $values);
79
        }
80
    }
81
 
82
    public function render(): string
83
    {
84
        $keys = [];
85
 
86
        foreach ($this->values as $key => $value) {
87
            $keys[] = $this->openDelimiter . $key . $this->closeDelimiter;
88
        }
89
 
90
        return str_replace($keys, $this->values, $this->template);
91
    }
92
 
93
    /**
94
     * @codeCoverageIgnore
95
     */
96
    public function renderTo(string $target): void
97
    {
98
        if (!file_put_contents($target, $this->render())) {
99
            throw new RuntimeException(
100
                sprintf(
101
                    'Writing rendered result to "%s" failed',
102
                    $target
103
                )
104
            );
105
        }
106
    }
107
}