forked from fa/breadcrumb-the-shire
refactor: rename lib/ to core/ for clearer core-module separation
Rename the top-level lib/ directory to core/ so the project root immediately communicates which code is core platform and which lives in modules/. PHP namespaces (MintyPHP\*) are unchanged — only the Composer PSR-4 path mapping moves from lib/ to core/. Module-internal lib/ directories (modules/*/lib/) are untouched. Updated across the full stack: - composer.json PSR-4 mapping - bootstrap entry points (web/index.php, bin/*, tests/bootstrap.php) - tooling configs (phpstan.neon, phpunit.xml, php-cs-fixer) - 26 architecture contract tests - enforcement-policy, guard-catalog, quality-gates - all documentation (CLAUDE.md, README, 25 docs/, .agents/skills/) - bin/qa-extended.sh search paths - .claude/settings.local.json permission paths Workflow: .agents/runs/CORE-LIB-RENAME-001/ (Analyst → Planner → Executor → Code Review (4 findings fixed) → Security Review → Acceptance Test → Finalizer) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
108
core/App/Module/ModuleManifestSchemaValidator.php
Normal file
108
core/App/Module/ModuleManifestSchemaValidator.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\App\Module;
|
||||
|
||||
use Opis\JsonSchema\Errors\ErrorFormatter;
|
||||
use Opis\JsonSchema\Errors\ValidationError;
|
||||
use Opis\JsonSchema\JsonPointer;
|
||||
use Opis\JsonSchema\Validator;
|
||||
use RuntimeException;
|
||||
|
||||
final class ModuleManifestSchemaValidator
|
||||
{
|
||||
private const DEFAULT_SCHEMA_PATH = '.agents/contracts/module-manifest.schema.json';
|
||||
|
||||
/** @var array<string, object> */
|
||||
private static array $schemaCache = [];
|
||||
|
||||
public static function assertValid(
|
||||
array $rawManifest,
|
||||
string $moduleId,
|
||||
?string $manifestFile = null,
|
||||
?string $schemaFile = null
|
||||
): void {
|
||||
$schemaPath = self::resolveSchemaPath($schemaFile);
|
||||
$schema = self::loadSchema($schemaPath);
|
||||
|
||||
$payload = json_decode((string) json_encode($rawManifest), false);
|
||||
if (!is_object($payload)) {
|
||||
throw new RuntimeException("Manifest schema validation failed for module '{$moduleId}': manifest payload is not serializable.");
|
||||
}
|
||||
|
||||
$validator = new Validator();
|
||||
$result = $validator->validate($payload, $schema);
|
||||
if ($result->isValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$location = $manifestFile ?? ("modules/{$moduleId}/module.php");
|
||||
$message = self::formatValidationErrors($result->error());
|
||||
throw new RuntimeException(
|
||||
"Manifest schema validation failed for module '{$moduleId}' ({$location}): {$message}"
|
||||
);
|
||||
}
|
||||
|
||||
private static function resolveSchemaPath(?string $schemaFile): string
|
||||
{
|
||||
if ($schemaFile !== null && trim($schemaFile) !== '') {
|
||||
return $schemaFile;
|
||||
}
|
||||
|
||||
return dirname(__DIR__, 3) . '/' . self::DEFAULT_SCHEMA_PATH;
|
||||
}
|
||||
|
||||
private static function loadSchema(string $schemaPath): object
|
||||
{
|
||||
$normalizedPath = str_replace('\\', '/', $schemaPath);
|
||||
if (isset(self::$schemaCache[$normalizedPath])) {
|
||||
return self::$schemaCache[$normalizedPath];
|
||||
}
|
||||
|
||||
if (!is_file($schemaPath)) {
|
||||
throw new RuntimeException("Module manifest schema not found: {$schemaPath}");
|
||||
}
|
||||
|
||||
$rawSchema = file_get_contents($schemaPath);
|
||||
if ($rawSchema === false) {
|
||||
throw new RuntimeException("Unable to read module manifest schema: {$schemaPath}");
|
||||
}
|
||||
|
||||
$decodedSchema = json_decode($rawSchema);
|
||||
if (!is_object($decodedSchema)) {
|
||||
throw new RuntimeException("Invalid JSON schema in {$schemaPath}");
|
||||
}
|
||||
|
||||
self::$schemaCache[$normalizedPath] = $decodedSchema;
|
||||
return $decodedSchema;
|
||||
}
|
||||
|
||||
private static function formatValidationErrors(?ValidationError $error): string
|
||||
{
|
||||
if ($error === null) {
|
||||
return 'unknown schema validation error';
|
||||
}
|
||||
|
||||
$formatter = new ErrorFormatter();
|
||||
$messages = $formatter->formatFlat(
|
||||
$error,
|
||||
static function (ValidationError $validationError) use ($formatter): string {
|
||||
$path = JsonPointer::pathToString($validationError->data()->fullPath());
|
||||
$normalizedPath = $path === '' ? '/' : $path;
|
||||
$message = $formatter->formatErrorMessage($validationError);
|
||||
|
||||
return "{$normalizedPath}: {$message}";
|
||||
}
|
||||
);
|
||||
|
||||
$messages = array_values(array_unique(array_filter(
|
||||
$messages,
|
||||
static fn ($message): bool => is_string($message) && trim($message) !== ''
|
||||
)));
|
||||
|
||||
if ($messages === []) {
|
||||
return 'unknown schema validation error';
|
||||
}
|
||||
|
||||
return implode('; ', array_slice($messages, 0, 3));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user