4 * Subclass with context specific LESS variables
6 class ResourceLoaderLessVarFileModule
extends ResourceLoaderFileModule
{
7 protected $lessVariables = [];
12 public function __construct(
14 $localBasePath = null,
15 $remoteBasePath = null
17 if ( isset( $options['lessMessages'] ) ) {
18 $this->lessVariables
= $options['lessMessages'];
20 parent
::__construct( $options, $localBasePath, $remoteBasePath );
26 public function getMessages() {
27 // Overload so MessageBlobStore can detect updates to messages and purge as needed.
28 return array_merge( $this->messages
, $this->lessVariables
);
32 * Exclude a set of messages from a JSON string representation
35 * @param array $exclusions
38 protected function excludeMessagesFromBlob( $blob, $exclusions ) {
39 $data = json_decode( $blob, true );
40 // unset the LESS variables so that they are not forwarded to JavaScript
41 foreach ( $exclusions as $key ) {
50 protected function getMessageBlob( ResourceLoaderContext
$context ) {
51 $blob = parent
::getMessageBlob( $context );
52 return json_encode( $this->excludeMessagesFromBlob( $blob, $this->lessVariables
) );
56 * Takes a message and wraps it in quotes for compatibility with LESS parser
57 * (ModifyVars) method so that the variable can be loaded and made available to stylesheets.
58 * Note this does not take care of CSS escaping. That will be taken care of as part
62 * @return string wrapped LESS variable definition
64 private static function wrapAndEscapeMessage( $msg ) {
65 return str_replace( "'", "\'", CSSMin
::serializeStringValue( $msg ) );
69 * Get language-specific LESS variables for this module.
71 * @param ResourceLoaderContext $context
72 * @return array LESS variables
74 protected function getLessVars( ResourceLoaderContext
$context ) {
75 $blob = parent
::getMessageBlob( $context );
76 $lessMessages = $this->excludeMessagesFromBlob( $blob, $this->messages
);
78 $vars = parent
::getLessVars( $context );
79 foreach ( $lessMessages as $msgKey => $value ) {
80 $vars['msg-' . $msgKey] = self
::wrapAndEscapeMessage( $value );