diff --git a/composer.json b/composer.json index 7090ae4..e4c7bf9 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "level-2/transphporm", + "name": "clx/transphporm", "description": "A new approach at templating", "license": "BSD-2-Clause", "homepage": "https://github.com/Level-2/Transphporm", diff --git a/src/Module/Functions.php b/src/Module/Functions.php index 4a6dc72..1c836f2 100644 --- a/src/Module/Functions.php +++ b/src/Module/Functions.php @@ -22,6 +22,7 @@ public function load(\Transphporm\Config $config) { $functionSet->addFunction('template', $templateFunction); $functionSet->addFunction('json', new \Transphporm\TSSFunction\Json($baseDir)); $functionSet->addFunction('constant', new \Transphporm\TSSFunction\Constant()); + $functionSet->addFunction('file', new \Transphporm\TSSFunction\File($baseDir)); // Register HTML formatter here because it uses the template function $config->registerFormatter(new \Transphporm\Formatter\HTMLFormatter($templateFunction)); diff --git a/src/TSSFunction/File.php b/src/TSSFunction/File.php new file mode 100644 index 0000000..676e540 --- /dev/null +++ b/src/TSSFunction/File.php @@ -0,0 +1,39 @@ + + * @copyright 2020 Chris Johnson + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @version 1.0 + */ +namespace Transphporm\TSSFunction; + +class File implements \Transphporm\TSSFunction +{ + private $filePath; + + public function __construct(\Transphporm\FilePath $filePath) + { + $this->filePath = $filePath; + } + + /** + * @param array $args + * @param \DomElement|null $element + * + * @return array + * @throws \Exception + */ + public function run(array $args, \DomElement $element = null) + { + $fileContents = $args[0]; + + $path = $this->filePath->getFilePath($fileContents); + if (!file_exists($path)) { + throw new \Exception('File does not exist at: ' . $path); + } + $fileContents = file_get_contents($path); + + return $fileContents; + } +} diff --git a/tests/TransphpormTest.php b/tests/TransphpormTest.php index d6574d9..19e6e71 100644 --- a/tests/TransphpormTest.php +++ b/tests/TransphpormTest.php @@ -1436,6 +1436,27 @@ public function testJsonFile() { unlink($file); } + public function testFile() { + $data = <<assertEquals($this->stripTabs(""), $this->stripTabs($template->output($data)->body)); + + unlink($file); + } + public function testRoot() { $xml = "