39 protected $origin = self::ORIGIN_CORE_SITEWIDE;
115 $this->localBasePath = static::extractLocalBasePath( $options,
$localBasePath );
117 $this->definition = $options;
124 if ( $this->definition ===
null ) {
129 $this->definition =
null;
131 if ( isset( $options[
'data'] ) ) {
133 $data = json_decode( file_get_contents( $dataPath ),
true );
134 $options = array_merge( $data, $options );
144 $prefix = isset( $options[
'prefix'] ) && $options[
'prefix'];
145 $selector = isset( $options[
'selector'] ) && $options[
'selector'];
147 && $options[
'selectorWithoutVariant'];
149 && $options[
'selectorWithVariant'];
152 throw new InvalidArgumentException(
153 "Given 'selectorWithoutVariant' but no 'selectorWithVariant'."
157 throw new InvalidArgumentException(
158 "Given 'selectorWithVariant' but no 'selectorWithoutVariant'."
162 throw new InvalidArgumentException(
163 "Incompatible 'selector' and 'selectorWithVariant'+'selectorWithoutVariant' given."
167 throw new InvalidArgumentException(
168 "None of 'prefix', 'selector' or 'selectorWithVariant'+'selectorWithoutVariant' given."
172 foreach ( $options as $member => $option ) {
176 if ( !is_array( $option ) ) {
177 throw new InvalidArgumentException(
178 "Invalid list error. '$option' given, array expected."
181 if ( !isset( $option[
'default'] ) ) {
183 $option = [
'default' => $option ];
185 foreach ( $option as $skin => $data ) {
186 if ( !is_array( $data ) ) {
187 throw new InvalidArgumentException(
188 "Invalid list error. '$data' given, array expected."
192 $this->{$member} = $option;
196 $this->{$member} = (bool)$option;
200 case 'selectorWithoutVariant':
201 case 'selectorWithVariant':
202 $this->{$member} = (string)$option;
206 $this->selectorWithoutVariant = $this->selectorWithVariant = (string)$option;
251 if ( $this->imageObjects ===
null ) {
253 $this->imageObjects = [];
255 if ( !isset( $this->imageObjects[$skin] ) ) {
256 $this->imageObjects[$skin] = [];
257 if ( !isset( $this->images[$skin] ) ) {
258 $this->images[$skin] = $this->images[
'default'] ?? [];
260 foreach ( $this->images[$skin] as
$name => $options ) {
261 $fileDescriptor = is_array( $options ) ? $options[
'file'] : $options;
263 $allowedVariants = array_merge(
264 ( is_array( $options ) && isset( $options[
'variants'] ) ) ? $options[
'variants'] : [],
267 if ( isset( $this->variants[$skin] ) ) {
268 $variantConfig = array_intersect_key(
269 $this->variants[$skin],
270 array_fill_keys( $allowedVariants,
true )
280 $this->localBasePath,
284 $this->imageObjects[$skin][$image->getName()] = $image;
288 return $this->imageObjects[$skin];
299 if ( $this->globalVariants ===
null ) {
301 $this->globalVariants = [];
303 if ( !isset( $this->globalVariants[$skin] ) ) {
304 $this->globalVariants[$skin] = [];
305 if ( !isset( $this->variants[$skin] ) ) {
306 $this->variants[$skin] = $this->variants[
'default'] ?? [];
308 foreach ( $this->variants[$skin] as
$name =>
$config ) {
310 $this->globalVariants[$skin][] =
$name;
315 return $this->globalVariants[$skin];
333 $selectors[
'selectorWithoutVariant'],
340 $rules[] =
"$selector {\n\t$declarations\n}";
342 foreach ( $image->getVariants() as $variant ) {
345 $selectors[
'selectorWithVariant'],
349 '{variant}' => $variant,
352 $rules[] =
"$selector {\n\t$declarations\n}";
356 $style = implode(
"\n", $rules );
357 return [
'all' => $style ];
377 $imageDataUri = $this->useDataURI ? $image->
getDataUri(
$context, $variant,
'original' ) :
false;
378 $primaryUrl = $imageDataUri ?: $image->
getUrl(
$context, $script, $variant,
'original' );
383 return implode(
"\n\t", $declarations );
402 "background-image: $fallbackUrl;",
403 "background-image: linear-gradient(transparent, transparent), $primaryUrl;",
422 $summary = parent::getDefinitionSummary(
$context );
430 'selectorWithoutVariant',
431 'selectorWithVariant',
433 $options[$member] = $this->{$member};
437 'options' => $options,
452 $files[] = $image->getPath(
$context );
454 $files = array_values( array_unique( $files ) );
455 return array_map( [ __CLASS__,
'safeFileHash' ], $files );
464 return $path->getLocalPath();
467 return "{$this->localBasePath}/$path";
485 if ( array_key_exists(
'localBasePath', $options ) ) {
496 return self::LOAD_STYLES;
static buildUrlValue( $url)
Build a CSS 'url()' value for the given URL, quoting parentheses (and other funny characters) and esc...
Context object that contains information about the state of a specific ResourceLoader web request.
An object to represent a path to a JavaScript/CSS file, along with a remote and local base path,...
Module for generated and embedded images.
getImage( $name, ResourceLoaderContext $context)
Get a ResourceLoaderImage object for given image.
loadFromDefinition()
Parse definition and external JSON data, if referenced.
ResourceLoaderImage[][] null $imageObjects
getStyles(ResourceLoaderContext $context)
string $localBasePath
Local base path, see __construct()
__construct( $options=[], $localBasePath=null)
Constructs a new module from an options array.
getDefinitionSummary(ResourceLoaderContext $context)
Get the definition summary for this module.
static extractLocalBasePath( $options, $localBasePath=null)
Extract a local base path from module definition information.
getSelectors()
Get CSS selector templates used by this module.
array null $globalVariants
getCssDeclarations( $primary, $fallback)
SVG support using a transparent gradient to guarantee cross-browser compatibility (browsers able to u...
getFileHashes(ResourceLoaderContext $context)
Helper method for getDefinitionSummary.
getPrefix()
Get CSS class prefix used by this module.
string null $defaultColor
getStyleDeclarations(ResourceLoaderContext $context, ResourceLoaderImage $image, $script, $variant=null)
This method must not be used by getDefinitionSummary as doing so would cause an infinite loop (we use...
getGlobalVariants(ResourceLoaderContext $context)
Get list of variants in this module that are 'global', i.e., available for every image regardless of ...
getImages(ResourceLoaderContext $context)
Get ResourceLoaderImage objects for all images.
Class encapsulating an image used in a ResourceLoaderImageModule.
getUrl(ResourceLoaderContext $context, $script, $variant, $format)
Get the load.php URL that will produce this image.
getDataUri(ResourceLoaderContext $context, $variant, $format)
Get the data: URI that will produce this image.
Abstraction for ResourceLoader modules, with name registration and maxage functionality.
string null $name
Module name.
getSource()
Get the source of this module.
getName()
Get this module's name.