From a6a1cf3684207cb007bfa7a1194b028cec90e1c5 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Wed, 7 Oct 2020 15:47:17 -0500 Subject: [PATCH 1/2] Custom TSS Function for JavaScript files. - Sometimes it is helpful to be able to insert the contents of a file into a template without any special handling, for example, for external JavaScript code. --- src/Module/Functions.php | 1 + src/TSSFunction/File.php | 39 +++++++++++++++++++++++++++++++++++++++ tests/TransphpormTest.php | 21 +++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/TSSFunction/File.php 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 = "
From 6d06a86e8adff7d24c61223ff8bf95b00d18fd71 Mon Sep 17 00:00:00 2001 From: Chris Johnson Date: Sat, 10 Oct 2020 12:28:41 -0500 Subject: [PATCH 2/2] Rename package for local use without VCS nonsense. See https://github.com/ClaimLynx/wiki/wiki/Custom-Transphporm. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",