1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
<?php
namespace MediaWiki\Settings\Source;
use MediaWiki\Settings\SettingsBuilderException;
use Stringable;
use Wikimedia\AtEase\AtEase;
/**
* Settings loaded from a PHP file path as an array structure.
*
* @since 1.38
*/
class PhpSettingsSource implements Stringable, SettingsSource, SettingsIncludeLocator {
/**
* Path to the PHP file.
* @var string
*/
private $path;
/**
* @param string $path
*/
public function __construct( string $path ) {
$this->path = $path;
}
/**
* Loads an array structure from a PHP file and return
* for using with merge strategies to apply on configs.
*
* @throws SettingsBuilderException
* @return array
*/
public function load(): array {
// NOTE: try include first, for performance reasons. If all goes well, it will
// use the opcode cache, and will not touch the file system at all.
// So we should only go and look at the file system if the include fails.
$source = AtEase::quietCall( static function ( $path ) {
return include $path;
}, $this->path );
if ( $source === false ) {
if ( !file_exists( $this->path ) ) {
throw new SettingsBuilderException(
"'File: {path}' does not exist",
[ 'path' => $this->path ]
);
}
if ( !is_readable( $this->path ) ) {
throw new SettingsBuilderException(
"File '{path}' is not readable",
[ 'path' => $this->path ]
);
}
if ( is_dir( $this->path ) ) {
throw new SettingsBuilderException(
"'{path}' is a directory, not a file",
[ 'path' => $this->path ]
);
}
}
if ( !is_array( $source ) ) {
throw new SettingsBuilderException(
"File '{path}' did not return an array",
[ 'path' => $this->path ]
);
}
return $source;
}
/**
* Returns this file source as a string.
*
* @return string
*/
public function __toString(): string {
return $this->path;
}
public function locateInclude( string $location ): string {
return SettingsFileUtils::resolveRelativeLocation( $location, dirname( $this->path ) );
}
}
|