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