Macchiato is a small and highly experimental PHP library, built on top of the SpiderMonkey PECL extension, that compiles CoffeeScript code to JavaScript, or directly executes it.
Unfortunately the SpiderMonkey PECL extension cannot be considered stable at this point. Compiling CoffeeScript to JavaScript should not pose any problem, but code execution can lead to random segmentation fault errors.
Macchiato requires the CoffeeScript client compiler library in order to work, so you must pass a valid path or URI for the JavaScript file to the constructor method:
<?php
$pathToCoffeeScript = 'coffee-script.js';
$coffee = new Macchiato\CoffeeScript($pathToCoffeeScript);
You can pass your CoffeeScript code to Macchiato and compile it to JavaScript:
<?php
var_dump($coffee->compile('square = (x) -> x * x'));
/*
string(64) "var square;
square = function(x) {
return x * x;
};"
*/
It is also possible to execute your CoffeeScript snippets directly in PHP:
<?php
var_dump($coffee->execute("
square = (x) -> x * x
square 42
"));
// int(1764)
PHP classes and objects can be made explicitly available to CoffeeScript when executing scripts:
<?php
$context = $coffee->createContext();
$context->registerVariable('message', ($m = 'Hello there!'));
$context->registerFunction('alert', function($msg) { echo "$msg\n"; });
$coffee->execute('alert(message)', $context);
- PHP >= 5.3.0
- SpiderMonkey PECL >= 0.1.4 (beta)
The code for Macchiato is distributed under the terms of the MIT license (see LICENSE).