Completed
Push — master ( 63765d...3a9527 )
by Vitaly
02:14
created

Module::renderer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 3
crap 3
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