Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
ResourceLoaderFileModuleWithLessVars | |
0.00% |
0 / 12 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getLessVars | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments; |
4 | |
5 | use LogicException; |
6 | use MediaWiki\ResourceLoader as RL; |
7 | |
8 | /** |
9 | * Like a normal file module, but can provide dynamically evaluated LESS variables |
10 | * via a callback, defined in the field 'lessCallback'. As with JS callbacks, the result |
11 | * should not depend on anything but the ResourceLoader context. |
12 | * |
13 | * Basically, this reimplements the old ResourceLoaderGetLessVars hook. |
14 | */ |
15 | class ResourceLoaderFileModuleWithLessVars extends RL\FileModule { |
16 | |
17 | /** @var callable|null */ |
18 | protected $lessCallback; |
19 | |
20 | /** |
21 | * @param array $options See RL\FileModule. Also: |
22 | * - lessCallback: callable which takes a RL\Context |
23 | * and returns an array of LESS variables (name => value). |
24 | * The variables must not depend on anything other than the context. |
25 | * @param string|null $localBasePath See RL\FileModule. |
26 | * @param string|null $remoteBasePath See RL\FileModule. |
27 | */ |
28 | public function __construct( |
29 | array $options = [], $localBasePath = null, $remoteBasePath = null |
30 | ) { |
31 | parent::__construct( $options, $localBasePath, $remoteBasePath ); |
32 | if ( isset( $options['lessCallback'] ) ) { |
33 | $this->lessCallback = $options['lessCallback']; |
34 | } |
35 | } |
36 | |
37 | /** @inheritDoc */ |
38 | protected function getLessVars( RL\Context $context ) { |
39 | $lessVars = parent::getLessVars( $context ); |
40 | if ( $this->lessCallback ) { |
41 | if ( !is_callable( $this->lessCallback ) ) { |
42 | $msg = "Invalid 'lessCallback' for module '{$this->getName()}'."; |
43 | $this->getLogger()->error( $msg ); |
44 | throw new LogicException( $msg ); |
45 | } |
46 | $lessVars = array_merge( $lessVars, ( $this->lessCallback )( $context ) ); |
47 | } |
48 | return $lessVars; |
49 | } |
50 | |
51 | } |