| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Settings\Source; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use MediaWiki\Settings\SettingsBuilderException; |
| 7 |
use ReflectionClass; |
| 8 |
use ReflectionException; |
| 9 |
use Stringable; |
| 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 |
class ReflectionSchemaSource implements Stringable, SettingsSource { |
| 39 |
use JsonSchemaTrait; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private $class; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private $includeDoc; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function __construct( string $class, bool $includeDoc = false ) { |
| 57 |
$this->class = $class; |
| 58 |
$this->includeDoc = $includeDoc; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function load(): array { |
| 65 |
return $this->loadAsComponents(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function loadAsComponents( bool $inlineReferences = false ): array { |
| 76 |
$schemas = []; |
| 77 |
$defs = []; |
| 78 |
$obsolete = []; |
| 79 |
|
| 80 |
try { |
| 81 |
$class = new ReflectionClass( $this->class ); |
| 82 |
foreach ( $class->getReflectionConstants() as $const ) { |
| 83 |
if ( !$const->isPublic() ) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
$name = $const->getName(); |
| 88 |
$schema = $const->getValue(); |
| 89 |
|
| 90 |
if ( !is_array( $schema ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
if ( isset( $schema['obsolete'] ) ) { |
| 95 |
$obsolete[ $name ] = $schema['obsolete']; |
| 96 |
continue; |
| 97 |
} |
| 98 |
|
| 99 |
if ( $this->includeDoc ) { |
| 100 |
$doc = $const->getDocComment(); |
| 101 |
if ( $doc ) { |
| 102 |
$schema['description'] = $this->normalizeComment( $doc ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if ( isset( $schema['dynamicDefault'] ) ) { |
| 107 |
$schema['dynamicDefault'] = |
| 108 |
$this->normalizeDynamicDefault( $name, $schema['dynamicDefault'] ); |
| 109 |
} |
| 110 |
|
| 111 |
$schema['default'] ??= null; |
| 112 |
|
| 113 |
$schema = self::normalizeJsonSchema( $schema, $defs, $this->class, $name, $inlineReferences ); |
| 114 |
|
| 115 |
$schemas[ $name ] = $schema; |
| 116 |
} |
| 117 |
} catch ( ReflectionException $e ) { |
| 118 |
throw new SettingsBuilderException( |
| 119 |
'Failed to load schema from class {class}', |
| 120 |
[ 'class' => $this->class ], |
| 121 |
0, |
| 122 |
$e |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
return [ |
| 127 |
'config-schema' => $schemas, |
| 128 |
'schema-definitions' => $defs, |
| 129 |
'obsolete-config' => $obsolete |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function loadAsSchema( bool $inlineReferences = false ): array { |
| 146 |
$info = $this->loadAsComponents( $inlineReferences ); |
| 147 |
$schema = [ |
| 148 |
'type' => 'object', |
| 149 |
'properties' => $info['config-schema'], |
| 150 |
]; |
| 151 |
|
| 152 |
if ( $info['schema-definitions'] ) { |
| 153 |
$schema['$defs'] = $info['schema-definitions']; |
| 154 |
} |
| 155 |
|
| 156 |
return $schema; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function __toString(): string { |
| 165 |
return 'class ' . $this->class; |
| 166 |
} |
| 167 |
|
| 168 |
private function normalizeComment( string $doc ) { |
| 169 |
$doc = preg_replace( '/^\s*\/\*+\s*|\s*\*+\/\s*$/', '', $doc ); |
| 170 |
$doc = preg_replace( '/^\s*\**$/m', " ", $doc ); |
| 171 |
$doc = preg_replace( '/^\s*\**[ \t]?/m', '', $doc ); |
| 172 |
return $doc; |
| 173 |
} |
| 174 |
|
| 175 |
private function normalizeDynamicDefault( string $name, $spec ) { |
| 176 |
if ( $spec === true ) { |
| 177 |
$spec = [ 'callback' => [ $this->class, "getDefault{$name}" ] ]; |
| 178 |
} |
| 179 |
|
| 180 |
if ( is_string( $spec ) ) { |
| 181 |
$spec = [ 'callback' => $spec ]; |
| 182 |
} |
| 183 |
|
| 184 |
if ( !isset( $spec['callback'] ) ) { |
| 185 |
$spec['callback'] = [ $this->class, "getDefault{$name}" ]; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
if ( $spec['callback'] instanceof Closure ) { |
| 190 |
throw new SettingsBuilderException( |
| 191 |
"dynamicDefaults callback for $name must be JSON serializable. " . |
| 192 |
"Closures are not supported." |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
if ( !is_callable( $spec['callback'] ) ) { |
| 197 |
$pretty = var_export( $spec['callback'], true ); |
| 198 |
$pretty = preg_replace( '/\s+/', ' ', $pretty ); |
| 199 |
|
| 200 |
throw new SettingsBuilderException( |
| 201 |
"dynamicDefaults callback for $name is not callable: " . |
| 202 |
$pretty |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
return $spec; |
| 207 |
} |
| 208 |
|
| 209 |
} |