1 | <?php |
||
14 | class Module extends ExternalModule |
||
15 | { |
||
16 | /** LESS mixin declaration pattern */ |
||
17 | const P_IMPORT_DECLARATION = '/@import\s+(\'|\")(?<path>[^\'\"]+)(\'|\");/'; |
||
18 | |||
19 | /** @var \lessc LESS compiler */ |
||
20 | protected $less; |
||
21 | |||
22 | /** @var array Collection of LESS variables */ |
||
23 | protected $variables = []; |
||
24 | |||
25 | /** @var array Collection of LESS mixins */ |
||
26 | protected $mixins = []; |
||
27 | |||
28 | /** @var string Cached LESS code */ |
||
29 | protected $lessCode; |
||
30 | |||
31 | /** SamsonFramework load preparation stage handler */ |
||
32 | 4 | public function prepare() |
|
40 | |||
41 | /** |
||
42 | * Recursively replace @import in content of the LESS file |
||
43 | * |
||
44 | * @param string $resource Resource full path |
||
45 | * @param string $content less file content |
||
46 | * |
||
47 | * @return string Content of LESS file with included @imported resources |
||
48 | * @throws ResourceNotFound If importing resource could not be found |
||
49 | */ |
||
50 | 3 | protected function readImport($resource, $content) |
|
51 | { |
||
52 | // Rewrite imports |
||
53 | 3 | $matches = []; |
|
54 | 3 | if (preg_match_all(self::P_IMPORT_DECLARATION, $content, $matches)) { |
|
55 | 2 | for ($i=0, $size = count($matches[0]); $i < $size; $i++) { |
|
56 | // Build absolute path to imported resource |
||
57 | 2 | $path = dirname($resource).DIRECTORY_SEPARATOR.$matches['path'][$i]; |
|
58 | |||
59 | // Append .less extension according to standard |
||
60 | 2 | if (false === ($path = realpath(is_file($path)?$path:$path.'.less'))) { |
|
61 | 1 | throw new ResourceNotFound('Cannot import file: '.$matches['path'][$i]); |
|
62 | } |
||
63 | |||
64 | // Replace path in LESS @import command with recursive call to this function |
||
65 | 1 | $content = str_replace($matches[0][$i], $this->readImport($path, file_get_contents($path)), $content); |
|
66 | 1 | } |
|
67 | 1 | } |
|
68 | |||
69 | 2 | return $content; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * LESS resource compiler. |
||
74 | * |
||
75 | * @param string $resource Resource full path |
||
76 | * @param string $extension Resource extension |
||
77 | * @param string $content Compiled output resource content |
||
78 | * |
||
79 | * @throws \Exception |
||
80 | */ |
||
81 | 3 | public function compiler($resource, &$extension, &$content) |
|
100 | } |
||
101 |