Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderLessVarFileModule.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * Module augmented with context-specific LESS variables.
23 *
24 * @ingroup ResourceLoader
25 * @since 1.32
26 */
27 class ResourceLoaderLessVarFileModule extends ResourceLoaderFileModule {
28 protected $lessVariables = [];
29
30 /**
31 * @inheritDoc
32 */
33 public function __construct(
34 $options = [],
35 $localBasePath = null,
36 $remoteBasePath = null
37 ) {
38 if ( isset( $options['lessMessages'] ) ) {
39 $this->lessVariables = $options['lessMessages'];
40 }
41 parent::__construct( $options, $localBasePath, $remoteBasePath );
42 }
43
44 /**
45 * @inheritDoc
46 */
47 public function getMessages() {
48 // Overload so MessageBlobStore can detect updates to messages and purge as needed.
49 return array_merge( $this->messages, $this->lessVariables );
50 }
51
52 /**
53 * Exclude a set of messages from a JSON string representation
54 *
55 * @param string $blob
56 * @param array $exclusions
57 * @return object $blob
58 */
59 protected function excludeMessagesFromBlob( $blob, $exclusions ) {
60 $data = json_decode( $blob, true );
61 // unset the LESS variables so that they are not forwarded to JavaScript
62 foreach ( $exclusions as $key ) {
63 unset( $data[$key] );
64 }
65 return (object)$data;
66 }
67
68 /**
69 * @inheritDoc
70 */
71 protected function getMessageBlob( ResourceLoaderContext $context ) {
72 $blob = parent::getMessageBlob( $context );
73 return json_encode( $this->excludeMessagesFromBlob( $blob, $this->lessVariables ) );
74 }
75
76 /**
77 * Takes a message and wraps it in quotes for compatibility with LESS parser
78 * (ModifyVars) method so that the variable can be loaded and made available to stylesheets.
79 * Note this does not take care of CSS escaping. That will be taken care of as part
80 * of CSS Janus.
81 *
82 * @param string $msg
83 * @return string wrapped LESS variable definition
84 */
85 private static function wrapAndEscapeMessage( $msg ) {
86 return str_replace( "'", "\'", CSSMin::serializeStringValue( $msg ) );
87 }
88
89 /**
90 * Get language-specific LESS variables for this module.
91 *
92 * @param ResourceLoaderContext $context
93 * @return array LESS variables
94 */
95 protected function getLessVars( ResourceLoaderContext $context ) {
96 $blob = parent::getMessageBlob( $context );
97 $lessMessages = $this->excludeMessagesFromBlob( $blob, $this->messages );
98
99 $vars = parent::getLessVars( $context );
100 foreach ( $lessMessages as $msgKey => $value ) {
101 $vars['msg-' . $msgKey] = self::wrapAndEscapeMessage( $value );
102 }
103 return $vars;
104 }
105 }