Skip to content

Commit

Permalink
Create PHPCoffeeScriptFilter by PHP composer package coffeescript/cof…
Browse files Browse the repository at this point in the history
…feescript
  • Loading branch information
janci authored and fprochazka committed Sep 6, 2015
1 parent 3f7e1af commit 95aabba
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
49 changes: 49 additions & 0 deletions WebLoader/Filter/PHPCoffeeScriptFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace WebLoader\Filter;

use CoffeeScript\Compiler;



/**
* Coffee script filter implements with composer php compiler
*
* @author Jan Svantner
*/
class PHPCoffeeScriptFilter
{

/**
* Invoke filter
*
* @param string
* @param \WebLoader\Compiler
* @param string
* @return string
*/
public function __invoke($code, \WebLoader\Compiler $loader, $file = NULL)
{
if (pathinfo($file, PATHINFO_EXTENSION) === 'coffee') {
$code = $this->compileCoffee($code, $file);
}

return $code;
}


/**
* @param $source $string
* @param $file bool|NULL
* @throws \WebLoader\WebLoaderException
* @return string
*/
public function compileCoffee($source, $file)
{
try {
return Compiler::compile($source, array('filename' => $file));
} catch (\Exception $e) {
throw new \WebLoader\WebLoaderException('CoffeeScript Filter Error: ' . $e->getMessage(), 0, $e);
}
}
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"suggest": {
"oyejorge/less.php": "LESS compiler written in PHP.",
"leafo/scssphp": "SCSS compiler written in PHP.",
"joseki/webloader-filters": "CSSMin & JSMin filters written in PHP."
"joseki/webloader-filters": "CSSMin & JSMin filters written in PHP.",
"coffeescript/coffeescript": "CoffeeScript compiler written in PHP."
},
"require-dev": {
"nette/application": "~2.3@dev",
Expand All @@ -50,6 +51,7 @@

"oyejorge/less.php": "~1.5",
"leafo/scssphp": "~0.1",
"coffeescript/coffeescript": "1.3.*",

"mockery/mockery": "0.7.*",
"phpunit/phpunit": "3.7.*",
Expand Down
47 changes: 47 additions & 0 deletions tests/Filter/PHPCoffeeScriptFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace WebLoader\Test\Filter;

use WebLoader\Filter\PHPCoffeeScriptFilter;

/**
* CompilerTest
*
* @author Jan Svantner
*/
class PHPCoffeeScriptFilterTest extends \PHPUnit_Framework_TestCase
{

/** @var PHPCoffeeScriptFilter */
private $object;

protected function setUp()
{
$this->object = new PHPCoffeeScriptFilter();
}

public function testSimpleLoadAndParse()
{
if (!class_exists('CoffeeScript\Compiler')) {
$this->markTestSkipped('Missing CoffeeScript compiler.');
}

$compiler = new PHPCoffeeScriptFilter();
$coffee = $compiler->compileCoffee("number = -42 if opposite", null);

$version = COFFEESCRIPT_VERSION;
$expected = <<<COFFEE
// Generated by CoffeeScript PHP {$version}
(function() {
var number;
if (opposite) {
number = -42;
}
}).call(this);
COFFEE;
$this->assertEquals($expected, $coffee);
}
}

0 comments on commit 95aabba

Please sign in to comment.