/
CoffeeScript.php
71 lines (55 loc) · 2.31 KB
/
CoffeeScript.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace samsonos\coffeescript;
use samson\core\ExternalModule;
use samson\resourcerouter;
/** Load CoffeeScript parser manually */
require 'src/CoffeeScript/Init.php';
/**
* Class for loading module into SamsonPHP
*
* @package samsonos/coffeescript
* @author Vitaly Iegorov <vitalyiegorov@gmail.com>
* @version 0.1
*/
class CoffeeScript extends ExternalModule
{
/** Identifier */
protected $id = 'coffescript';
/** @see ModuleConnector::init() */
public function init(array $params = array())
{
// Pointer to resourcerouter
$rr = m('resourcer');
// If we have coffee resource in project
if (isset($rr->cached['coffee'])) {
// Change coffee file to js and store it as current js resource
$newJS = str_replace('.coffee', '.js', str_replace(__SAMSON_PUBLIC_PATH, '', $rr->cached['coffee']));
// If .coffee resource has been updated
$file = & $rr->updated['coffee'];
if (isset($file)) try {
// Read coffee file
$coffee = file_get_contents($file);
// Initialize coffee compiler
\CoffeeScript\Init::load();
// Read updated .coffee resource file and compile it
$js = \CoffeeScript\Compiler::compile($coffee, array('filename' => $file));
// Compile coffee script to js and save to the same location
file_put_contents($file, $js);
}
catch( Exception $e){ e('Ошибка обработки CoffeeScript: '.$e->getMessage()); }
// If regular JS has been updated or coffee script has been updated
if (isset($rr->updated['js']) || isset($rr->updated['coffee'])) {
// Read gathered js
$oldJS = file_get_contents(str_replace(__SAMSON_PUBLIC_PATH, '', $rr->cached['js']));
// Read gathered coffee
$coffee = file_get_contents(str_replace(__SAMSON_PUBLIC_PATH, '', $rr->cached['coffee']));
// Concatenate regular js and compiled coffee script js to a new javascript file
file_put_contents($newJS, $oldJS.$coffee);
}
// Change old js resource to new one
//$rr->cached['js'] = $newJS;
}
// Call parent method
parent::init($params);
}
}