-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PHPCoffeeScriptFilter by PHP composer package coffeescript/cof…
…feescript
- Loading branch information
1 parent
3f7e1af
commit 95aabba
Showing
3 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |