| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- declare(strict_types=1);
- // Template for MANAGE_UPDATE_POST_HOOK.
- //
- // Runs after every successful update - even when no migration ran. Runs
- // AFTER the migrations and is skipped if a migration failed.
- //
- // Register it in manage-client/config.php:
- //
- // define("MANAGE_UPDATE_POST_HOOK", [
- // "file" => MANAGE_APP_ROOT . "/includes/after-update.php",
- // "callback" => "myProjectAfterUpdate",
- // ]);
- //
- // This file must be part of the release package, otherwise it's missing
- // after the first update.
- /**
- * @param array $context app_root, instance, from_version, to_version,
- * backup_dir, run_id, migrations, pdo if configured
- */
- function myProjectAfterUpdate(array $context): void
- {
- // 1. Discard compiled templates and caches.
- foreach (glob($context["app_root"] . "/data/cache/*.php") ?: [] as $file) {
- @unlink($file);
- }
- // 2. Create directories a new release requires.
- $newDir = $context["app_root"] . "/data/exports";
- if (!is_dir($newDir) && !mkdir($newDir, 02775, true) && !is_dir($newDir)) {
- throw new RuntimeException("Could not create directory: " . $newDir);
- }
- // 3. Log for traceability.
- @file_put_contents(
- $context["app_root"] . "/data/update-history.log",
- sprintf(
- "%s %s -> %s (migrations: %s)%s",
- date(DATE_ATOM),
- $context["from_version"] !== "" ? $context["from_version"] : "unknown",
- $context["to_version"],
- $context["migrations"] === [] ? "none" : implode(", ", $context["migrations"]),
- PHP_EOL,
- ),
- FILE_APPEND | LOCK_EX,
- );
- // To report failure: throw an exception, return false, or
- // return ["success" => false, "error" => "…"].
- }
|