| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Settings\Source; |
| 4 |
|
| 5 |
use MediaWiki\Settings\SettingsBuilderException; |
| 6 |
use Stringable; |
| 7 |
use Wikimedia\AtEase\AtEase; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class PhpSettingsSource implements Stringable, SettingsSource, SettingsIncludeLocator { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private $path; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function __construct( string $path ) { |
| 25 |
$this->path = $path; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function load(): array { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
$source = AtEase::quietCall( static function ( $path ) { |
| 41 |
return include $path; |
| 42 |
}, $this->path ); |
| 43 |
|
| 44 |
if ( $source === false ) { |
| 45 |
if ( !file_exists( $this->path ) ) { |
| 46 |
throw new SettingsBuilderException( |
| 47 |
"'File: {path}' does not exist", |
| 48 |
[ 'path' => $this->path ] |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
if ( !is_readable( $this->path ) ) { |
| 53 |
throw new SettingsBuilderException( |
| 54 |
"File '{path}' is not readable", |
| 55 |
[ 'path' => $this->path ] |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
if ( is_dir( $this->path ) ) { |
| 60 |
throw new SettingsBuilderException( |
| 61 |
"'{path}' is a directory, not a file", |
| 62 |
[ 'path' => $this->path ] |
| 63 |
); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if ( !is_array( $source ) ) { |
| 68 |
throw new SettingsBuilderException( |
| 69 |
"File '{path}' did not return an array", |
| 70 |
[ 'path' => $this->path ] |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
return $source; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function __toString(): string { |
| 83 |
return $this->path; |
| 84 |
} |
| 85 |
|
| 86 |
public function locateInclude( string $location ): string { |
| 87 |
return SettingsFileUtils::resolveRelativeLocation( $location, dirname( $this->path ) ); |
| 88 |
} |
| 89 |
|
| 90 |
} |