ResourceLoaderLessVarFileModule.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * Module augmented with context-specific LESS variables.
  22. *
  23. * @ingroup ResourceLoader
  24. * @since 1.32
  25. */
  26. class ResourceLoaderLessVarFileModule extends ResourceLoaderFileModule {
  27. protected $lessVariables = [];
  28. /**
  29. * @inheritDoc
  30. */
  31. public function __construct(
  32. $options = [],
  33. $localBasePath = null,
  34. $remoteBasePath = null
  35. ) {
  36. if ( isset( $options['lessMessages'] ) ) {
  37. $this->lessVariables = $options['lessMessages'];
  38. }
  39. parent::__construct( $options, $localBasePath, $remoteBasePath );
  40. }
  41. /**
  42. * @inheritDoc
  43. */
  44. public function getMessages() {
  45. // Overload so MessageBlobStore can detect updates to messages and purge as needed.
  46. return array_merge( $this->messages, $this->lessVariables );
  47. }
  48. /**
  49. * Exclude a set of messages from a JSON string representation
  50. *
  51. * @param string $blob
  52. * @param array $exclusions
  53. * @return object $blob
  54. */
  55. protected function excludeMessagesFromBlob( $blob, $exclusions ) {
  56. $data = json_decode( $blob, true );
  57. // unset the LESS variables so that they are not forwarded to JavaScript
  58. foreach ( $exclusions as $key ) {
  59. unset( $data[$key] );
  60. }
  61. return (object)$data;
  62. }
  63. /**
  64. * @inheritDoc
  65. */
  66. protected function getMessageBlob( ResourceLoaderContext $context ) {
  67. $blob = parent::getMessageBlob( $context );
  68. return json_encode( $this->excludeMessagesFromBlob( $blob, $this->lessVariables ) );
  69. }
  70. /**
  71. * Takes a message and wraps it in quotes for compatibility with LESS parser
  72. * (ModifyVars) method so that the variable can be loaded and made available to stylesheets.
  73. * Note this does not take care of CSS escaping. That will be taken care of as part
  74. * of CSS Janus.
  75. *
  76. * @param string $msg
  77. * @return string wrapped LESS variable definition
  78. */
  79. private static function wrapAndEscapeMessage( $msg ) {
  80. return str_replace( "'", "\'", CSSMin::serializeStringValue( $msg ) );
  81. }
  82. /**
  83. * Get language-specific LESS variables for this module.
  84. *
  85. * @param ResourceLoaderContext $context
  86. * @return array LESS variables
  87. */
  88. protected function getLessVars( ResourceLoaderContext $context ) {
  89. $blob = parent::getMessageBlob( $context );
  90. $lessMessages = $this->excludeMessagesFromBlob( $blob, $this->messages );
  91. $vars = parent::getLessVars( $context );
  92. foreach ( $lessMessages as $msgKey => $value ) {
  93. $vars['msg-' . $msgKey] = self::wrapAndEscapeMessage( $value );
  94. }
  95. return $vars;
  96. }
  97. }