forked from leafo/lessphp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.php
76 lines (66 loc) · 1.56 KB
/
start.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
72
73
74
75
76
<?php
require 'lessc.inc.php';
$config = Config::get('less');
$imports = function($file) use (&$imports)
{
$paths = array();
preg_match_all('/@import\s+"(?P<imports>[^";]+)"/ism', File::get($file), $matches);
foreach ($matches['imports'] as $import)
{
$path = dirname($file) . '/' . $import;
if (File::exists($path) || File::exists($path .= '.less'))
{
$paths[] = $path;
$paths = array_merge($paths, $imports($path));
}
}
return $paths;
};
$compile = function($input_file, $output_file) use ($imports)
{
try
{
$latest = File::modified($input_file);
foreach ($imports($input_file) as $import)
{
$import_modified = File::modified($import);
$latest = $import_modified > $latest ? $import_modified : $latest;
}
if (! File::exists($output_file) || $latest > File::modified($output_file))
{
$cache = lessc::cexecute($input_file);
File::put($output_file, $cache['compiled']);
}
}
catch (Exception $ex)
{
exit('lessc fatal error:<br />' . $ex->getMessage());
}
};
if (isset($config['directories']))
{
foreach ($config['directories'] as $less_dir => $css_dir)
{
$less_dir = rtrim($less_dir, '/') . '/';
foreach (glob($less_dir . '*.[Ll][Ee][Ss][Ss]') as $less)
{
$css = rtrim($css_dir, '/') . '/' . basename($less, '.less') . '.css';
$compile($less, $css);
}
}
}
if (isset($config['files']))
{
foreach ($config['files'] as $less => $css)
{
$compile($less, $css);
}
}
if (isset($config['snippets']))
{
$less = new lessc();
foreach ($config['snippets'] as $snippet => $css)
{
File::put($css, $less->parse($snippet));
}
}