upgraded dependencies
This commit is contained in:
@@ -1,85 +1,76 @@
|
||||
<?php
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of the Text_Template package.
|
||||
* This file is part of phpunit/php-text-template.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Template;
|
||||
|
||||
/**
|
||||
* A simple template engine.
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
*/
|
||||
class Text_Template
|
||||
use function array_merge;
|
||||
use function file_exists;
|
||||
use function file_get_contents;
|
||||
use function file_put_contents;
|
||||
use function sprintf;
|
||||
use function str_replace;
|
||||
|
||||
final class Template
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $template = '';
|
||||
private $template = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $openDelimiter = '{';
|
||||
private $openDelimiter;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $closeDelimiter = '}';
|
||||
private $closeDelimiter;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $values = array();
|
||||
private $values = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $file
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function __construct($file = '', $openDelimiter = '{', $closeDelimiter = '}')
|
||||
public function __construct(string $file = '', string $openDelimiter = '{', string $closeDelimiter = '}')
|
||||
{
|
||||
$this->setFile($file);
|
||||
|
||||
$this->openDelimiter = $openDelimiter;
|
||||
$this->closeDelimiter = $closeDelimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the template file.
|
||||
*
|
||||
* @param string $file
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function setFile($file)
|
||||
public function setFile(string $file): void
|
||||
{
|
||||
$distFile = $file . '.dist';
|
||||
|
||||
if (file_exists($file)) {
|
||||
$this->template = file_get_contents($file);
|
||||
}
|
||||
|
||||
else if (file_exists($distFile)) {
|
||||
} elseif (file_exists($distFile)) {
|
||||
$this->template = file_get_contents($distFile);
|
||||
}
|
||||
|
||||
else {
|
||||
} else {
|
||||
throw new InvalidArgumentException(
|
||||
'Template file could not be loaded.'
|
||||
sprintf(
|
||||
'Failed to load template "%s"',
|
||||
$file
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets one or more template variables.
|
||||
*
|
||||
* @param array $values
|
||||
* @param bool $merge
|
||||
*/
|
||||
public function setVar(array $values, $merge = TRUE)
|
||||
public function setVar(array $values, bool $merge = true): void
|
||||
{
|
||||
if (!$merge || empty($this->values)) {
|
||||
$this->values = $values;
|
||||
@@ -88,14 +79,9 @@ class Text_Template
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the template and returns the result.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
public function render(): string
|
||||
{
|
||||
$keys = array();
|
||||
$keys = [];
|
||||
|
||||
foreach ($this->values as $key => $value) {
|
||||
$keys[] = $this->openDelimiter . $key . $this->closeDelimiter;
|
||||
@@ -105,31 +91,17 @@ class Text_Template
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the template and writes the result to a file.
|
||||
*
|
||||
* @param string $target
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function renderTo($target)
|
||||
public function renderTo(string $target): void
|
||||
{
|
||||
$fp = @fopen($target, 'wt');
|
||||
|
||||
if ($fp) {
|
||||
fwrite($fp, $this->render());
|
||||
fclose($fp);
|
||||
} else {
|
||||
$error = error_get_last();
|
||||
|
||||
if (!file_put_contents($target, $this->render())) {
|
||||
throw new RuntimeException(
|
||||
sprintf(
|
||||
'Could not write to %s: %s',
|
||||
$target,
|
||||
substr(
|
||||
$error['message'],
|
||||
strpos($error['message'], ':') + 2
|
||||
sprintf(
|
||||
'Writing rendered result to "%s" failed',
|
||||
$target
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
16
vendor/phpunit/php-text-template/src/exceptions/Exception.php
vendored
Normal file
16
vendor/phpunit/php-text-template/src/exceptions/Exception.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-text-template.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Template;
|
||||
|
||||
use Throwable;
|
||||
|
||||
interface Exception extends Throwable
|
||||
{
|
||||
}
|
14
vendor/phpunit/php-text-template/src/exceptions/InvalidArgumentException.php
vendored
Normal file
14
vendor/phpunit/php-text-template/src/exceptions/InvalidArgumentException.php
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-text-template.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Template;
|
||||
|
||||
final class InvalidArgumentException extends \InvalidArgumentException implements Exception
|
||||
{
|
||||
}
|
16
vendor/phpunit/php-text-template/src/exceptions/RuntimeException.php
vendored
Normal file
16
vendor/phpunit/php-text-template/src/exceptions/RuntimeException.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-text-template.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Template;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class RuntimeException extends InvalidArgumentException implements Exception
|
||||
{
|
||||
}
|
Reference in New Issue
Block a user