wiki.techinc.nl/maintenance/generateConfigSchemaYaml.php
daniel 2fe23d6860 Use class constants to define config schema, rather than config-schema.yaml
Instead of maintaining the config schema as a yaml file, we
maintain it as a set of constants in a class. From the information in
these constants, we can generate a JSON schema (yaml) file, and an
php file containing optimized arrays for fast loading.

Advantages:
- PHP doc available to IDEs. The generated markdown file is no longer
  needed.
- Can use PHP constants when defining default values.

NOTE: needs backport to 1.38

Change-Id: I663c08b8a200644cbe7e5f65c20f1592a4f3974d
2022-03-17 21:20:03 +01:00

59 lines
1.5 KiB
PHP

<?php
use Symfony\Component\Yaml\Yaml;
require_once __DIR__ . '/Maintenance.php';
require_once __DIR__ . '/includes/ConfigSchemaDerivativeTrait.php';
/**
* Maintenance script that generates a YAML file containing a JSON Schema representation
* of the config schema.
*
* @ingroup Maintenance
*/
class GenerateConfigSchemaYaml extends Maintenance {
use ConfigSchemaDerivativeTrait;
/** @var string */
private const DEFAULT_OUTPUT_PATH = __DIR__ . '/../docs/config-schema.yaml';
public function __construct() {
parent::__construct();
$this->addDescription( 'Generate config-schema.yaml' );
$this->addOption(
'output',
'Output file. Default: ' . self::DEFAULT_OUTPUT_PATH,
false,
true
);
}
public function execute() {
$schemas = $this->loadSchema();
// Cast empty arrays to objects if they are declared to be of type object.
// This ensures they get represented in yaml as {} rather than [].
foreach ( $schemas as &$sch ) {
if ( isset( $sch['default'] ) && isset( $sch['type'] ) ) {
$types = (array)$sch['type'];
if ( $sch['default'] === [] && in_array( 'object', $types ) ) {
$sch['default'] = new stdClass();
}
}
}
$yamlFlags = Yaml::DUMP_OBJECT_AS_MAP
| Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK
| Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE;
$array = [ 'config-schema' => $schemas ];
$yaml = Yaml::dump( $array, 4, 4, $yamlFlags );
$this->writeOutput( self::DEFAULT_OUTPUT_PATH, $yaml );
}
}
$maintClass = GenerateConfigSchemaYaml::class;
require_once RUN_MAINTENANCE_IF_MAIN;