|
1
|
|
|
<?php |
|
2
|
|
|
namespace samsonphp\less; |
|
3
|
|
|
|
|
4
|
|
|
use samson\resourcer\ResourceRouter; |
|
5
|
|
|
use samsonframework\core\LoadableInterface; |
|
6
|
|
|
use samsonphp\event\Event; |
|
7
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* SamsonPHP LESS compiler module. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Vitaly Iegorov <[email protected]> |
|
13
|
|
|
* @author Nikita Kotenko <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class Module implements LoadableInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \lessc LESS compiler */ |
|
18
|
|
|
protected $less; |
|
19
|
|
|
|
|
20
|
|
|
/** SamsonFramework load preparation stage handler */ |
|
21
|
2 |
|
public function prepare() |
|
22
|
|
|
{ |
|
23
|
2 |
|
Event::subscribe(ResourceRouter::EVENT_CREATED, array($this, 'renderer')); |
|
24
|
|
|
|
|
25
|
2 |
|
$this->less = new \lessc; |
|
26
|
|
|
|
|
27
|
2 |
|
return true; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* New resource file update handler. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $type Resource type(extension) |
|
34
|
|
|
* @param string $content Resource content |
|
35
|
|
|
* @param string $file LESS file path |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \Exception |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function renderer($type, &$content, $file = '') |
|
40
|
|
|
{ |
|
41
|
|
|
// If CSS resource has been updated |
|
42
|
2 |
|
if ($type === 'css') { |
|
43
|
|
|
try { |
|
44
|
|
|
// Read updated CSS resource file and compile it |
|
45
|
2 |
|
$content = $this->less->compile($content); |
|
46
|
2 |
|
} catch (\Exception $e) { |
|
47
|
|
|
//$errorFile = 'cache/error_resourcer'.microtime(true).'.less'; |
|
48
|
|
|
//file_put_contents($errorFile, $content); |
|
49
|
1 |
|
throw new \Exception('Failed compiling LESS['.$file.']:'."\n".$e->getMessage()); |
|
50
|
|
|
} |
|
51
|
1 |
|
} |
|
52
|
1 |
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|