#!/usr/bin/env php * * This file is part of a Moko Consulting project. * * SPDX-License-Identifier: GPL-3.0-or-later * * FILE INFORMATION * DEFGROUP: MokoStandards.Scripts.Plugin * INGROUP: MokoStandards * REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform * PATH: /plugin_validate.php * BRIEF: Validate a project's structure and standards using the auto-detected or specified plugin */ declare(strict_types=1); // Autoload dependencies require_once __DIR__ . '/../vendor/autoload.php'; use MokoEnterprise\PluginFactory; use MokoEnterprise\AuditLogger; use MokoEnterprise\MetricsCollector; /** * Display usage information */ function showUsage(): void { echo << null, 'project_type' => null, 'config_file' => null, 'json_output' => true, 'verbose' => false, 'help' => false, ]; for ($i = 1; $i < count($argv); $i++) { switch ($argv[$i]) { case '--project-path': $options['project_path'] = $argv[++$i] ?? null; break; case '--project-type': $options['project_type'] = $argv[++$i] ?? null; break; case '--config': $options['config_file'] = $argv[++$i] ?? null; break; case '--json': $options['json_output'] = true; break; case '--verbose': $options['verbose'] = true; break; case '--help': case '-h': $options['help'] = true; break; default: fwrite(STDERR, "Unknown option: {$argv[$i]}\n"); exit(2); } } return $options; } /** * Load project configuration from file */ function loadConfig(?string $configFile): array { if ($configFile === null || !file_exists($configFile)) { return []; } $content = file_get_contents($configFile); $config = json_decode($content, true); if (json_last_error() !== JSON_ERROR_NONE) { fwrite(STDERR, "Error parsing configuration file: " . json_last_error_msg() . "\n"); exit(2); } return $config; } /** * Output validation results */ function outputResults(array $result, bool $jsonOutput, bool $verbose): int { if ($jsonOutput) { echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n"; } else { echo "\n=== Project Validation Results ===\n\n"; echo "Project Type: " . ($result['project_type'] ?? 'Unknown') . "\n"; echo "Project Path: " . ($result['project_path'] ?? 'Unknown') . "\n"; echo "Status: " . ($result['valid'] ? 'VALID' : 'INVALID') . "\n"; echo "Validation Score: " . ($result['score'] ?? 0) . "/100\n\n"; if (!empty($result['errors'])) { echo "ERRORS:\n"; foreach ($result['errors'] as $error) { echo " ✗ " . (is_array($error) ? ($error['message'] ?? $error) : $error) . "\n"; } echo "\n"; } if (!empty($result['warnings'])) { echo "WARNINGS:\n"; foreach ($result['warnings'] as $warning) { echo " ⚠ " . (is_array($warning) ? ($warning['message'] ?? $warning) : $warning) . "\n"; } echo "\n"; } if (empty($result['errors']) && empty($result['warnings'])) { echo "✓ No issues found!\n\n"; } if ($verbose && !empty($result['details'])) { echo "DETAILS:\n"; print_r($result['details']); } } return $result['valid'] ? 0 : 1; } /** * Main execution */ function main(array $argv): int { $options = parseArguments($argv); if ($options['help']) { showUsage(); return 0; } // Validate required arguments if ($options['project_path'] === null) { fwrite(STDERR, "Error: --project-path is required\n\n"); showUsage(); return 2; } $projectPath = realpath($options['project_path']); if ($projectPath === false || !is_dir($projectPath)) { fwrite(STDERR, "Error: Project path does not exist or is not a directory: {$options['project_path']}\n"); return 2; } // Load configuration $projectConfig = loadConfig($options['config_file']); try { // Create factory and plugin $logger = new AuditLogger('plugin_validate'); $metricsCollector = new MetricsCollector(); $factory = new PluginFactory($logger, $metricsCollector); // Get the appropriate plugin if ($options['project_type'] !== null) { $plugin = $factory->create($options['project_type']); $projectType = $options['project_type']; } else { $plugin = $factory->createForProject($projectPath); $projectType = $plugin ? $plugin->getProjectType() : null; } if ($plugin === null) { $error = $options['project_type'] !== null ? "Plugin not found for project type: {$options['project_type']}" : "Could not auto-detect project type for: {$projectPath}"; $result = [ 'valid' => false, 'project_path' => $projectPath, 'project_type' => $projectType, 'errors' => [$error], 'warnings' => [], 'score' => 0, 'timestamp' => date('c'), ]; outputResults($result, $options['json_output'], $options['verbose']); return 2; } // Run validation $validation = $plugin->validateProject($projectConfig, $projectPath); // Prepare result $result = [ 'valid' => $validation['valid'] ?? false, 'project_type' => $projectType, 'project_path' => $projectPath, 'plugin_name' => $plugin->getPluginName(), 'plugin_version' => $plugin->getPluginVersion(), 'errors' => $validation['errors'] ?? [], 'warnings' => $validation['warnings'] ?? [], 'score' => $validation['score'] ?? ($validation['valid'] ? 100 : 0), 'timestamp' => date('c'), ]; if ($options['verbose']) { $result['details'] = $validation; } return outputResults($result, $options['json_output'], $options['verbose']); } catch (\Exception $e) { fwrite(STDERR, "Error: " . $e->getMessage() . "\n"); if ($options['verbose']) { fwrite(STDERR, $e->getTraceAsString() . "\n"); } return 2; } } // Execute exit(main($argv));