Debian Codegraph @

vendor/wikimedia/less.php/API.md

raw
1 Less.php API
2 ========
3
4 ## Basic use
5
6 #### Parse strings
7
8 ```php
9 $parser = new Less_Parser();
10 $parser->parse( '@color: #36c; .link { color: @color; } a { color: @color; }' );
11 $css = $parser->getCss();
12 ```
13
14 #### Parse files
15
16 The `parseFile()` function takes two parameters:
17
18 * The absolute path to a `.less` file.
19 * The base URL for any relative image or CSS references in the `.less` file,
20 typically the same directory that contains the `.less` file or a public equivalent.
21
22 ```php
23 $parser = new Less_Parser();
24 $parser->parseFile( '/var/www/mysite/bootstrap.less', 'https://example.org/mysite/' );
25 $css = $parser->getCss();
26 ```
27
28 #### Handle invalid syntax
29
30 An exception will be thrown if the compiler encounters invalid LESS.
31
32 ```php
33 try{
34 $parser = new Less_Parser();
35 $parser->parseFile( '/var/www/mysite/bootstrap.less', 'https://example.org/mysite/' );
36 $css = $parser->getCss();
37 } catch (Exception $e) {
38 echo $e->getMessage();
39 }
40 ```
41
42 #### Parse multiple inputs
43
44 Less.php can parse multiple input sources (e.g. files and/or strings) and generate a single CSS output.
45
46 ```php
47 $parser = new Less_Parser();
48 $parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
49 $parser->parse( '@color: #36c; .link { color: @color; } a { color: @color; }' );
50 $css = $parser->getCss();
51 ```
52
53 #### Metadata
54
55 Less.php keeps track of which `.less` files have been parsed, i.e. the input
56 file(s) and any direct and indirect imports.
57
58 ```php
59 $parser = new Less_Parser();
60 $parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
61 $css = $parser->getCss();
62 $files = $parser->getParsedFiles();
63 ```
64
65 #### Compress output
66
67 You can tell Less.php to remove comments and whitespace to generate minified CSS.
68
69 ```php
70 $options = [ 'compress' => true ];
71 $parser = new Less_Parser( $options );
72 $parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
73 $css = $parser->getCss();
74 ```
75
76 #### Get variables
77
78 You can use the `getVariables()` method to get an all variables defined and
79 their value in an associative array. Note that the input must be compiled first
80 by calling `getCss()`.
81
82 ```php
83 $parser = new Less_Parser;
84 $parser->parseFile( '/var/www/mysite/bootstrap.less');
85 $css = $parser->getCss();
86 $variables = $parser->getVariables();
87
88 ```
89
90 #### Set variables
91
92 Use the `ModifyVars()` method to inject additional variables, i.e. custom values
93 computed or accessed from your PHP code.
94
95 ```php
96 $parser = new Less_Parser();
97 $parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
98 $parser->ModifyVars( [ 'font-size-base' => '16px' ] );
99 $css = $parser->getCss();
100 ```
101
102 #### Import directories
103
104 By default, Less.php will look for imported files in the directory of the file passed to `parseFile()`.
105
106 If you use `parse()`, or if need to enable additional import directories, you can specify these by
107 calling `SetImportDirs()`.
108
109 ```php
110 $directories = [ '/var/www/mysite/bootstrap/' => '/mysite/bootstrap/' ];
111 $parser = new Less_Parser();
112 $parser->SetImportDirs( $directories );
113 $parser->parseFile( '/var/www/mysite/theme.less', '/mysite/' );
114 $css = $parser->getCss();
115 ```
116
117 ## Caching
118
119 Compiling LESS code into CSS can be a time-consuming process. It is recommended to cache your results.
120
121 #### Basic cache
122
123 Use the `Less_Cache` class to save and reuse the results of compiling LESS files.
124 This class will check the modified time and size of each LESS file (including imported files) and
125 either re-use or re-generate the CSS output accordingly.
126
127 The cache files are determinstically named, based on the full list of referenced LESS files and the metadata (file path, file mtime, file size) of each file. This means that each time a change is made, a different cache filename is used.
128
129 ```php
130 $lessFiles = [ '/var/www/mysite/bootstrap.less' => '/mysite/' ];
131 $options = [ 'cache_dir' => '/var/www/writable_folder' ];
132 $cssOutputFile = Less_Cache::Get( $lessFiles, $options );
133 $css = file_get_contents( '/var/www/writable_folder/' . $cssOutputFile );
134 ```
135
136 #### Caching with variables
137
138 Passing custom variables to `Less_Cache::Get()`:
139
140 ```php
141 $lessFiles = [ '/var/www/mysite/bootstrap.less' => '/mysite/' ];
142 $options = [ 'cache_dir' => '/var/www/writable_folder' ];
143 $variables = [ 'width' => '100px' ];
144 $cssOutputFile = Less_Cache::Get( $lessFiles, $options, $variables );
145 $css = file_get_contents( '/var/www/writable_folder/' . $cssOutputFile );
146 ```
147
148 #### Incremental caching
149
150 **Warning:** The incremental cache in Less_Parser is significantly slower and more memory-intense than `Less_Cache`! If you call Less.php during web requests, or otherwise call it repeatedly with files that might be unchanged, use `Less_Cache` instead.
151
152 You can turn off the incremental cache to save memory and disk space ([ref](https://github.com/wikimedia/less.php/issues/104)). Without incremental cache, cache misses may be slower.
153
154 ```php
155 // Disable incremental cache
156 $lessFiles = [ __DIR__ . '/assets/bootstrap.less' => '/mysite/assets/' ];
157 $options = [
158 'cache_dir' => '/var/www/writable_folder',
159 'cache_incremental' => false,
160 ];
161 $css = Less_Cache::Get( $lessFiles, $options );
162 ```
163
164 When you instantiate Less_Parser, and call `parseFile()` or `getCss()`, it is assumed that at least one input file has changed (i.e. the whole-output cache from Less_Cache was a cache miss). The incremental cache exists to speed up cache misses on very large code bases, and is enabled by default when you call Less_Cache.
165
166 It is inherent to the Less language that later imports may change variables or extend mixins used in earlier files, and that imports may reference variables defined by earlier imports. Thus one can't actually cache the CSS output of an import. All inputs needs to be re-compiled together to produce the correct CSS output. The incremental cache merely allows the `parseFile()` method to skip parsing for unchanged files (i.e. interpreting Less syntax into an object structure). The `getCss()` method will still traverse and compile the representation of all input files.
167
168 ## Source maps
169
170 Less.php supports v3 sourcemaps.
171
172 #### Inline
173
174 The sourcemap will be appended to the generated CSS file.
175
176 ```php
177 $options = [ 'sourceMap' => true ];
178 $parser = new Less_Parser($options);
179 $parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
180 $css = $parser->getCss();
181 ```
182
183 #### Saving to map file
184
185 ```php
186 $options = [
187 'sourceMap' => true,
188 'sourceMapWriteTo' => '/var/www/mysite/writable_folder/filename.map',
189 'sourceMapURL' => '/mysite/writable_folder/filename.map',
190 ];
191 $parser = new Less_Parser($options);
192 $parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
193 $css = $parser->getCss();
194 ```
195
196 ## Command line
197
198 An additional script has been included to use the Less.php compiler from the command line.
199 In its simplest invocation, you specify an input file and the compiled CSS is written to standard out:
200
201 ```
202 $ lessc input.less > output.css
203 ```
204
205 By using the `-w` flag you can watch a specified input file and have it compile as needed to the output file:
206
207 ```
208 $ lessc -w input.less output.css
209 ```
210
211 Errors from watch mode are written to standard out.
212
213 For more information, run `lessc --help`