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..00f3745 --- /dev/null +++ b/src/TSSFunction/File.php @@ -0,0 +1,36 @@ +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/DateFormatTest.php b/tests/DateFormatTest.php index c009d07..6245cc9 100644 --- a/tests/DateFormatTest.php +++ b/tests/DateFormatTest.php @@ -120,7 +120,7 @@ public function testWeeksAgo() { } public function testWeeksin() { - $this->assertEquals('in 2 weeks', $this->relative('+2 weeks')); + $this->assertEquals('in 3 weeks', $this->relative('+3 weeks')); } public function testMonthsAgo() { diff --git a/tests/TransphpormTest.php b/tests/TransphpormTest.php index 501a782..27461d8 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 = "