Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom TSS Function for JavaScript files. #236

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/Module/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
39 changes: 39 additions & 0 deletions src/TSSFunction/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @description Insert the contents of a file into selected element.
* @author Chris Johnson <[email protected]>
* @copyright 2020 Chris Johnson <[email protected]>
* @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;
}
}
21 changes: 21 additions & 0 deletions tests/TransphpormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,27 @@ public function testJsonFile() {
unlink($file);
}

public function testFile() {
$data = <<<JS
let j = 0;
if (j < 4) {
console.log('j' + " is less than 4");
}
JS;

$file = __DIR__ . 'data.file';
file_put_contents($file, $data);

$xml = "<script></script>";
$tss = 'script { content: file("' . $file . '"); }';

$template = new \Transphporm\Builder($xml, $tss);

$this->assertEquals($this->stripTabs("<script>$data</script>"), $this->stripTabs($template->output($data)->body));

unlink($file);
}

public function testRoot() {
$xml = "
<div></div>
Expand Down