DerivativeResourceLoaderContext.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * @author Kunal Mehta
  20. */
  21. /**
  22. * A mutable version of ResourceLoaderContext.
  23. *
  24. * Allows changing specific properties of a context object,
  25. * without changing the main one. Inspired by MediaWiki's DerivativeContext.
  26. *
  27. * @ingroup ResourceLoader
  28. * @since 1.24
  29. */
  30. class DerivativeResourceLoaderContext extends ResourceLoaderContext {
  31. const INHERIT_VALUE = -1;
  32. /**
  33. * @var ResourceLoaderContext
  34. */
  35. private $context;
  36. /** @var int|array */
  37. protected $modules = self::INHERIT_VALUE;
  38. protected $language = self::INHERIT_VALUE;
  39. protected $direction = self::INHERIT_VALUE;
  40. protected $skin = self::INHERIT_VALUE;
  41. protected $user = self::INHERIT_VALUE;
  42. protected $debug = self::INHERIT_VALUE;
  43. protected $only = self::INHERIT_VALUE;
  44. protected $version = self::INHERIT_VALUE;
  45. protected $raw = self::INHERIT_VALUE;
  46. protected $contentOverrideCallback = self::INHERIT_VALUE;
  47. public function __construct( ResourceLoaderContext $context ) {
  48. $this->context = $context;
  49. }
  50. public function getModules() {
  51. if ( $this->modules === self::INHERIT_VALUE ) {
  52. return $this->context->getModules();
  53. }
  54. return $this->modules;
  55. }
  56. /**
  57. * @param string[] $modules
  58. */
  59. public function setModules( array $modules ) {
  60. $this->modules = $modules;
  61. }
  62. public function getLanguage() {
  63. if ( $this->language === self::INHERIT_VALUE ) {
  64. return $this->context->getLanguage();
  65. }
  66. return $this->language;
  67. }
  68. /**
  69. * @param string $language
  70. */
  71. public function setLanguage( $language ) {
  72. $this->language = $language;
  73. // Invalidate direction since it is based on language
  74. $this->direction = null;
  75. $this->hash = null;
  76. }
  77. public function getDirection() {
  78. if ( $this->direction === self::INHERIT_VALUE ) {
  79. return $this->context->getDirection();
  80. }
  81. if ( $this->direction === null ) {
  82. $this->direction = Language::factory( $this->getLanguage() )->getDir();
  83. }
  84. return $this->direction;
  85. }
  86. /**
  87. * @param string $direction
  88. */
  89. public function setDirection( $direction ) {
  90. $this->direction = $direction;
  91. $this->hash = null;
  92. }
  93. public function getSkin() {
  94. if ( $this->skin === self::INHERIT_VALUE ) {
  95. return $this->context->getSkin();
  96. }
  97. return $this->skin;
  98. }
  99. /**
  100. * @param string $skin
  101. */
  102. public function setSkin( $skin ) {
  103. $this->skin = $skin;
  104. $this->hash = null;
  105. }
  106. public function getUser() {
  107. if ( $this->user === self::INHERIT_VALUE ) {
  108. return $this->context->getUser();
  109. }
  110. return $this->user;
  111. }
  112. /**
  113. * @param string|null $user
  114. */
  115. public function setUser( $user ) {
  116. $this->user = $user;
  117. $this->hash = null;
  118. $this->userObj = null;
  119. }
  120. public function getDebug() {
  121. if ( $this->debug === self::INHERIT_VALUE ) {
  122. return $this->context->getDebug();
  123. }
  124. return $this->debug;
  125. }
  126. /**
  127. * @param bool $debug
  128. */
  129. public function setDebug( $debug ) {
  130. $this->debug = $debug;
  131. $this->hash = null;
  132. }
  133. public function getOnly() {
  134. if ( $this->only === self::INHERIT_VALUE ) {
  135. return $this->context->getOnly();
  136. }
  137. return $this->only;
  138. }
  139. /**
  140. * @param string|null $only
  141. */
  142. public function setOnly( $only ) {
  143. $this->only = $only;
  144. $this->hash = null;
  145. }
  146. public function getVersion() {
  147. if ( $this->version === self::INHERIT_VALUE ) {
  148. return $this->context->getVersion();
  149. }
  150. return $this->version;
  151. }
  152. /**
  153. * @param string|null $version
  154. */
  155. public function setVersion( $version ) {
  156. $this->version = $version;
  157. $this->hash = null;
  158. }
  159. public function getRaw() {
  160. if ( $this->raw === self::INHERIT_VALUE ) {
  161. return $this->context->getRaw();
  162. }
  163. return $this->raw;
  164. }
  165. /**
  166. * @param bool $raw
  167. */
  168. public function setRaw( $raw ) {
  169. $this->raw = $raw;
  170. }
  171. public function getRequest() {
  172. return $this->context->getRequest();
  173. }
  174. public function getResourceLoader() {
  175. return $this->context->getResourceLoader();
  176. }
  177. public function getContentOverrideCallback() {
  178. if ( $this->contentOverrideCallback === self::INHERIT_VALUE ) {
  179. return $this->context->getContentOverrideCallback();
  180. }
  181. return $this->contentOverrideCallback;
  182. }
  183. /**
  184. * @see self::getContentOverrideCallback
  185. * @since 1.32
  186. * @param callable|null|int $callback As per self::getContentOverrideCallback,
  187. * or self::INHERIT_VALUE
  188. */
  189. public function setContentOverrideCallback( $callback ) {
  190. $this->contentOverrideCallback = $callback;
  191. }
  192. }