Skip to main content
Schema · Revision #2

How do I change where the schema is loaded from?

How do I change where the schema is loaded from?

Fallback Schema Location

The default fallback schema location is: themes/{$theme}/blueprint/schema.json. It is the base version of the schema, one that has had no changes made via the editor.

setFallbackSchemaLoader allows you to change the location that the fallback version of the schema is located at.

For example:

\Aero\Blueprint\Facades\SchemaRepository::setFallbackSchemaLoader(function () {
    $path = base_path("modules/aerocargo/acme/schema.json");

    if (file_exists($path)) {
        return json_decode(file_get_contents($path), true);
    }

    return [];
});

Temp Schema Location

The default temp schema location is: blueprint/{$theme}/temp/versions/{$version}/schema.json. It is the version of the schema that is temporarily saved while changes are pending via the editor.

setTempSchemaLoader allows you to change the location that the temp versions of schemas are located at.

For example:

\Aero\Blueprint\Facades\SchemaRepository::setTempSchemaLoader(function (string $version) {
    $path = base_path("modules/aerocargo/acme/temp/versions/{$version}/schema.json");

    if (file_exists($path)) {
        return json_decode(file_get_contents($path), true);
    }

    return [];
});

Active Schema Location

The default active schema location is: blueprint/{$theme}/versions/{$version}/schema.json. It is the version of the schema currently active for the theme.

setActiveSchemaLoader allows you to change the location that the active versions of schemas are located at.

For example:

\Aero\Blueprint\Facades\SchemaRepository::setActiveSchemaLoader(function (string $version) {
    $path = base_path("modules/aerocargo/acme/versions/{$version}/schema.json");

    if (file_exists($path)) {
        return json_decode(file_get_contents($path), true);
    }

    return [];
});

If you move the location that the active schema is loaded from, you will likely need to also use setActiveSchemaPathResolver and setSaveSchemaHandler to handle saving the schema appropriately.

Or perhaps, you'd like to load the fallback schema as the active schema with only the templates from the actual active schema merged in:

\Aero\Blueprint\Facades\SchemaRepository::setActiveSchemaLoader(function (string $version, string $disk, string $path) {
    // This is the one from storage that is currently active
    $activeSchema = json_decode(Storage::disk($disk)->get($path), true) ?? [];

    // This is the preset schema (the one from the theme)
    $fallbackSchema = \Aero\Blueprint\Facades\SchemaRepository::getFallbackSchema();

    // Merge only the templates from the active schema into the fallback schema
    return array_merge($fallbackSchema, [
        'templates' => $activeSchema['templates'] ?? $fallbackSchema['templates'] ?? [],
    ]);
});

Saving Active Schema to Custom Location

setActiveSchemaPathResolver allows you to change the path that the active schema is considered to be located, it should be used in conjunction with setActiveSchemaLoader so that functionality such as saving collision checks still work.

For example:

\Aero\Blueprint\Facades\SchemaRepository::setActiveSchemaPathResolver(function (string $version) {
    return base_path("modules/aerocargo/acme/versions/{$version}/schema.json");
});

setSaveSchemaHandler allows you to change how the schema is saved, it should be used in conjunction with setActiveSchemaLoader so that the schema is saved correctly to the new location.

\Aero\Blueprint\Facades\SchemaRepository::setSaveSchemaHandler(function (string $version, array $schema) {
    \Illuminate\Support\Facades\File::put(
        base_path("modules/aerocargo/acme/versions/{$version}/schema.json"),
        json_encode($schema, JSON_PRETTY_PRINT)
    );
});