Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderLessVarFileModule.php
1 <?php
2
3 /**
4 * Subclass with context specific LESS variables
5 */
6 class ResourceLoaderLessVarFileModule extends ResourceLoaderFileModule {
7 protected $lessVariables = [];
8
9 /**
10 * @inheritDoc
11 */
12 public function __construct(
13 $options = [],
14 $localBasePath = null,
15 $remoteBasePath = null
16 ) {
17 if ( isset( $options['lessMessages'] ) ) {
18 $this->lessVariables = $options['lessMessages'];
19 }
20 parent::__construct( $options, $localBasePath, $remoteBasePath );
21 }
22
23 /**
24 * @inheritDoc
25 */
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 );
29 }
30
31 /**
32 * Exclude a set of messages from a JSON string representation
33 *
34 * @param string $blob
35 * @param array $exclusions
36 * @return object $blob
37 */
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 ) {
42 unset( $data[$key] );
43 }
44 return (object)$data;
45 }
46
47 /**
48 * @inheritDoc
49 */
50 protected function getMessageBlob( ResourceLoaderContext $context ) {
51 $blob = parent::getMessageBlob( $context );
52 return json_encode( $this->excludeMessagesFromBlob( $blob, $this->lessVariables ) );
53 }
54
55 /**
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
59 * of CSS Janus.
60 *
61 * @param string $msg
62 * @return string wrapped LESS variable definition
63 */
64 private static function wrapAndEscapeMessage( $msg ) {
65 return str_replace( "'", "\'", CSSMin::serializeStringValue( $msg ) );
66 }
67
68 /**
69 * Get language-specific LESS variables for this module.
70 *
71 * @param ResourceLoaderContext $context
72 * @return array LESS variables
73 */
74 protected function getLessVars( ResourceLoaderContext $context ) {
75 $blob = parent::getMessageBlob( $context );
76 $lessMessages = $this->excludeMessagesFromBlob( $blob, $this->messages );
77
78 $vars = parent::getLessVars( $context );
79 foreach ( $lessMessages as $msgKey => $value ) {
80 $vars['msg-' . $msgKey] = self::wrapAndEscapeMessage( $value );
81 }
82 return $vars;
83 }
84 }