after-update.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. // Template for MANAGE_UPDATE_POST_HOOK.
  4. //
  5. // Runs after every successful update - even when no migration ran. Runs
  6. // AFTER the migrations and is skipped if a migration failed.
  7. //
  8. // Register it in manage-client/config.php:
  9. //
  10. // define("MANAGE_UPDATE_POST_HOOK", [
  11. // "file" => MANAGE_APP_ROOT . "/includes/after-update.php",
  12. // "callback" => "myProjectAfterUpdate",
  13. // ]);
  14. //
  15. // This file must be part of the release package, otherwise it's missing
  16. // after the first update.
  17. /**
  18. * @param array $context app_root, instance, from_version, to_version,
  19. * backup_dir, run_id, migrations, pdo if configured
  20. */
  21. function myProjectAfterUpdate(array $context): void
  22. {
  23. // 1. Discard compiled templates and caches.
  24. foreach (glob($context["app_root"] . "/data/cache/*.php") ?: [] as $file) {
  25. @unlink($file);
  26. }
  27. // 2. Create directories a new release requires.
  28. $newDir = $context["app_root"] . "/data/exports";
  29. if (!is_dir($newDir) && !mkdir($newDir, 02775, true) && !is_dir($newDir)) {
  30. throw new RuntimeException("Could not create directory: " . $newDir);
  31. }
  32. // 3. Log for traceability.
  33. @file_put_contents(
  34. $context["app_root"] . "/data/update-history.log",
  35. sprintf(
  36. "%s %s -> %s (migrations: %s)%s",
  37. date(DATE_ATOM),
  38. $context["from_version"] !== "" ? $context["from_version"] : "unknown",
  39. $context["to_version"],
  40. $context["migrations"] === [] ? "none" : implode(", ", $context["migrations"]),
  41. PHP_EOL,
  42. ),
  43. FILE_APPEND | LOCK_EX,
  44. );
  45. // To report failure: throw an exception, return false, or
  46. // return ["success" => false, "error" => "…"].
  47. }