-
Notifications
You must be signed in to change notification settings - Fork 2
/
CoffeescriptGenerator.php
69 lines (57 loc) · 1.92 KB
/
CoffeescriptGenerator.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
<?php
/*
* This file is part of the SuperTheme extension by Comolo.
*
* Copyright (C) 2017 Comolo GmbH
*
* @author Hendrik Obermayer <https://github.com/henobi>
* @copyright 2017 Comolo GmbH <https://www.comolo.de/>
* @license LGPL
*/
namespace Comolo\SuperThemeBundle\Module;
use CoffeeScript\Compiler as CoffeeScriptCompiler;
use CoffeeScript\Init as CoffeeScriptInit;
/**
* Class GenerateCoffeescript.
*
* @author Hendrik Obermayer <https://github.com/henobi>
*/
class CoffeescriptGenerator extends AssetGenerator
{
protected function filesCollector()
{
$arrLayoutFiles = $this->sortArrayValues(
(array) unserialize($this->layoutModel->external_js),
$this->layoutModel->external_js_order
);
$arrPageFiles = $this->sortArrayValues(
(array) unserialize($this->pageModel->external_js),
$this->pageModel->external_js_order
);
return $this->combineArrayValues($arrLayoutFiles, $arrPageFiles);
}
protected function assetCompiler($strSourcePath)
{
// Target File
$strJSFile = 'assets/js/js-'.md5_file($strSourcePath).'.js';
if (!file_exists(TL_ROOT.'/'.$strJSFile)) {
$strJs = '';
if (substr($strSourcePath, -7) != '.coffee') {
$strJs = file_get_contents(TL_ROOT.'/'.$strSourcePath);
}
else {
// require classes
CoffeeScriptInit::load();
// Compile
$strCoffee = file_get_contents($strSourcePath);
$strJs = CoffeeScriptCompiler::compile($strCoffee, array('filename' => $strSourcePath));
}
$this->writeAndCompressAsset(TL_ROOT.'/'.$strJSFile, $strJs);
}
return array($strJSFile, null);
}
protected function addAssetToPage($filePath)
{
$GLOBALS['TL_BODY'][] = '<script src="'.$filePath.'"></script>';
}
}