forked from fa/breadcrumb-the-shire
- ConsoleApplication.discoverCommands() scans lib/Console/Commands/ automatically; adding a new command no longer requires editing bin/console - Command base class provides bootstrapApp(), bootstrapModuleApp(), requireBinScript() and projectRoot() helpers — eliminates fragile 4-level relative require_once paths - Per-command --help support via optional usage() method on Command - Docker-compose scheduler updated to use bin/console scheduler:run - Old bin scripts now emit deprecation hint to stderr when called directly Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
91 lines
2.9 KiB
PHP
91 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* CLI script to run a module's deactivation handler.
|
|
*
|
|
* Usage:
|
|
* php bin/module-deactivate.php <module-id> --confirm
|
|
* php bin/module-deactivate.php <module-id> --dry-run
|
|
*
|
|
* The module does NOT need to be in the enabled list — the manifest is loaded
|
|
* directly from modules/<id>/module.php.
|
|
*/
|
|
|
|
require_once __DIR__ . '/module-cli-bootstrap.php';
|
|
|
|
function moduleDeactivateRun(): int
|
|
{
|
|
return moduleCliRunStep('module-deactivate', static function (): int {
|
|
global $argv;
|
|
|
|
$moduleId = $argv[1] ?? '';
|
|
$flags = array_slice($argv, 2);
|
|
|
|
if ($moduleId === '' || $moduleId === '--help') {
|
|
fwrite(STDOUT, "Usage: php bin/module-deactivate.php <module-id> --confirm|--dry-run\n");
|
|
return $moduleId === '--help' ? 0 : 1;
|
|
}
|
|
|
|
$confirm = in_array('--confirm', $flags, true);
|
|
$dryRun = in_array('--dry-run', $flags, true);
|
|
|
|
if (!$confirm && !$dryRun) {
|
|
fwrite(STDERR, "Error: must pass --confirm to execute or --dry-run to validate.\n");
|
|
return 1;
|
|
}
|
|
|
|
$projectRoot = moduleCliProjectRoot();
|
|
$manifestFile = $projectRoot . '/modules/' . $moduleId . '/module.php';
|
|
|
|
if (!is_file($manifestFile)) {
|
|
fwrite(STDERR, "Error: module manifest not found at {$manifestFile}\n");
|
|
return 1;
|
|
}
|
|
|
|
$raw = require $manifestFile;
|
|
if (!is_array($raw)) {
|
|
fwrite(STDERR, "Error: manifest must return an array.\n");
|
|
return 1;
|
|
}
|
|
|
|
$manifest = \MintyPHP\App\Module\ModuleManifest::fromArray($raw, dirname($manifestFile));
|
|
|
|
$handlerClass = $manifest->deactivationHandler;
|
|
if ($handlerClass === null) {
|
|
fwrite(STDOUT, "Module '{$moduleId}' has no deactivation_handler declared — nothing to do.\n");
|
|
return 0;
|
|
}
|
|
|
|
if (!class_exists($handlerClass)) {
|
|
fwrite(STDERR, "Error: deactivation handler class '{$handlerClass}' not found.\n");
|
|
return 1;
|
|
}
|
|
|
|
$handler = new $handlerClass();
|
|
if (!$handler instanceof \MintyPHP\App\Module\Contracts\ModuleDeactivationHandler) {
|
|
fwrite(STDERR, "Error: '{$handlerClass}' does not implement ModuleDeactivationHandler.\n");
|
|
return 1;
|
|
}
|
|
|
|
if ($dryRun) {
|
|
fwrite(STDOUT, "Dry-run: handler '{$handlerClass}' found and valid for module '{$moduleId}'.\n");
|
|
return 0;
|
|
}
|
|
|
|
fwrite(STDOUT, "Running deactivation handler for module '{$moduleId}'...\n");
|
|
|
|
$container = app();
|
|
$handler->deactivate($container);
|
|
|
|
fwrite(STDOUT, "Deactivation handler for module '{$moduleId}' completed.\n");
|
|
return 0;
|
|
});
|
|
}
|
|
|
|
if (PHP_SAPI === 'cli' && realpath((string) ($_SERVER['SCRIPT_FILENAME'] ?? '')) === __FILE__) {
|
|
fwrite(STDERR, "Hint: prefer `php bin/console module:deactivate` instead.\n");
|
|
exit(moduleDeactivateRun());
|
|
}
|