DerivativeResourceLoaderContext.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Derivative context for ResourceLoader modules.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @author Kunal Mehta
  22. */
  23. /**
  24. * Allows changing specific properties of a context object,
  25. * without changing the main one. Inspired by DerivativeContext.
  26. *
  27. * @since 1.24
  28. */
  29. class DerivativeResourceLoaderContext extends ResourceLoaderContext {
  30. const INHERIT_VALUE = -1;
  31. /**
  32. * @var ResourceLoaderContext
  33. */
  34. private $context;
  35. protected $modules = self::INHERIT_VALUE;
  36. protected $language = self::INHERIT_VALUE;
  37. protected $direction = self::INHERIT_VALUE;
  38. protected $skin = self::INHERIT_VALUE;
  39. protected $user = self::INHERIT_VALUE;
  40. protected $debug = self::INHERIT_VALUE;
  41. protected $only = self::INHERIT_VALUE;
  42. protected $version = self::INHERIT_VALUE;
  43. protected $raw = self::INHERIT_VALUE;
  44. protected $contentOverrideCallback = self::INHERIT_VALUE;
  45. public function __construct( ResourceLoaderContext $context ) {
  46. $this->context = $context;
  47. }
  48. public function getModules() {
  49. if ( $this->modules === self::INHERIT_VALUE ) {
  50. return $this->context->getModules();
  51. }
  52. return $this->modules;
  53. }
  54. /**
  55. * @param string[] $modules
  56. */
  57. public function setModules( array $modules ) {
  58. $this->modules = $modules;
  59. }
  60. public function getLanguage() {
  61. if ( $this->language === self::INHERIT_VALUE ) {
  62. return $this->context->getLanguage();
  63. }
  64. return $this->language;
  65. }
  66. /**
  67. * @param string $language
  68. */
  69. public function setLanguage( $language ) {
  70. $this->language = $language;
  71. // Invalidate direction since it is based on language
  72. $this->direction = null;
  73. $this->hash = null;
  74. }
  75. public function getDirection() {
  76. if ( $this->direction === self::INHERIT_VALUE ) {
  77. return $this->context->getDirection();
  78. }
  79. if ( $this->direction === null ) {
  80. $this->direction = Language::factory( $this->getLanguage() )->getDir();
  81. }
  82. return $this->direction;
  83. }
  84. /**
  85. * @param string $direction
  86. */
  87. public function setDirection( $direction ) {
  88. $this->direction = $direction;
  89. $this->hash = null;
  90. }
  91. public function getSkin() {
  92. if ( $this->skin === self::INHERIT_VALUE ) {
  93. return $this->context->getSkin();
  94. }
  95. return $this->skin;
  96. }
  97. /**
  98. * @param string $skin
  99. */
  100. public function setSkin( $skin ) {
  101. $this->skin = $skin;
  102. $this->hash = null;
  103. }
  104. public function getUser() {
  105. if ( $this->user === self::INHERIT_VALUE ) {
  106. return $this->context->getUser();
  107. }
  108. return $this->user;
  109. }
  110. /**
  111. * @param string|null $user
  112. */
  113. public function setUser( $user ) {
  114. $this->user = $user;
  115. $this->hash = null;
  116. $this->userObj = null;
  117. }
  118. public function getDebug() {
  119. if ( $this->debug === self::INHERIT_VALUE ) {
  120. return $this->context->getDebug();
  121. }
  122. return $this->debug;
  123. }
  124. /**
  125. * @param bool $debug
  126. */
  127. public function setDebug( $debug ) {
  128. $this->debug = $debug;
  129. $this->hash = null;
  130. }
  131. public function getOnly() {
  132. if ( $this->only === self::INHERIT_VALUE ) {
  133. return $this->context->getOnly();
  134. }
  135. return $this->only;
  136. }
  137. /**
  138. * @param string|null $only
  139. */
  140. public function setOnly( $only ) {
  141. $this->only = $only;
  142. $this->hash = null;
  143. }
  144. public function getVersion() {
  145. if ( $this->version === self::INHERIT_VALUE ) {
  146. return $this->context->getVersion();
  147. }
  148. return $this->version;
  149. }
  150. /**
  151. * @param string|null $version
  152. */
  153. public function setVersion( $version ) {
  154. $this->version = $version;
  155. $this->hash = null;
  156. }
  157. public function getRaw() {
  158. if ( $this->raw === self::INHERIT_VALUE ) {
  159. return $this->context->getRaw();
  160. }
  161. return $this->raw;
  162. }
  163. /**
  164. * @param bool $raw
  165. */
  166. public function setRaw( $raw ) {
  167. $this->raw = $raw;
  168. }
  169. public function getRequest() {
  170. return $this->context->getRequest();
  171. }
  172. public function getResourceLoader() {
  173. return $this->context->getResourceLoader();
  174. }
  175. public function getContentOverrideCallback() {
  176. if ( $this->contentOverrideCallback === self::INHERIT_VALUE ) {
  177. return $this->context->getContentOverrideCallback();
  178. }
  179. return $this->contentOverrideCallback;
  180. }
  181. /**
  182. * @see self::getContentOverrideCallback
  183. * @since 1.32
  184. * @param callable|null|int $callback As per self::getContentOverrideCallback,
  185. * or self::INHERIT_VALUE
  186. */
  187. public function setContentOverrideCallback( $callback ) {
  188. $this->contentOverrideCallback = $callback;
  189. }
  190. }