|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +declare(strict_types=1); |
| 5 | + |
| 6 | +// Locate the autoloader. Order matters: composer's bin proxy provides the |
| 7 | +// exact path; otherwise prefer the project the command is run FROM (cwd) — |
| 8 | +// with a path-repository symlink __DIR__ resolves inside the package source, |
| 9 | +// where the relative guesses would pick the wrong vendor tree. |
| 10 | +$candidates = [ |
| 11 | + $_composer_autoload_path ?? null, |
| 12 | + getcwd().'/vendor/autoload.php', |
| 13 | + __DIR__.'/../../../autoload.php', |
| 14 | + __DIR__.'/../vendor/autoload.php', |
| 15 | +]; |
| 16 | + |
| 17 | +foreach ($candidates as $autoload) { |
| 18 | + if ($autoload !== null && is_file($autoload)) { |
| 19 | + require $autoload; |
| 20 | + break; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +use StdOut\SimpleDataObjects\Support\CacheWarmer; |
| 25 | + |
| 26 | +$args = array_slice($argv, 1); |
| 27 | + |
| 28 | +if ($args === []) { |
| 29 | + fwrite(STDERR, "Pre-builds the metadata + compiled hydrator/serializer cache for every\n"); |
| 30 | + fwrite(STDERR, "concrete BaseData subclass found in the scanned sources.\n\n"); |
| 31 | + fwrite(STDERR, "Usage: sdo-warm <cache-dir> [<src-path> ...]\n\n"); |
| 32 | + fwrite(STDERR, "Without <src-path> arguments, the PSR-4 directories from ./composer.json\n"); |
| 33 | + fwrite(STDERR, "are scanned.\n\n"); |
| 34 | + fwrite(STDERR, "Example: vendor/bin/sdo-warm storage/framework/cache/data-objects app/Data\n"); |
| 35 | + exit(2); |
| 36 | +} |
| 37 | + |
| 38 | +$cacheDir = array_shift($args); |
| 39 | + |
| 40 | +if ($args === []) { |
| 41 | + $args = CacheWarmer::pathsFromComposer(getcwd().'/composer.json'); |
| 42 | + |
| 43 | + if ($args === []) { |
| 44 | + fwrite(STDERR, "No source paths given and no PSR-4 autoload directories found in ./composer.json.\n"); |
| 45 | + exit(2); |
| 46 | + } |
| 47 | + |
| 48 | + echo 'Scanning PSR-4 paths from composer.json: '.implode(', ', $args)."\n\n"; |
| 49 | +} |
| 50 | + |
| 51 | +try { |
| 52 | + $result = CacheWarmer::warm($cacheDir, $args); |
| 53 | +} catch (Throwable $e) { |
| 54 | + fwrite(STDERR, 'ERROR: '.$e->getMessage()."\n"); |
| 55 | + exit(1); |
| 56 | +} |
| 57 | + |
| 58 | +if ($result['warmed'] === [] && $result['skipped'] === []) { |
| 59 | + fwrite(STDERR, "WARNING: no concrete BaseData subclasses found under: ".implode(', ', $args)."\n"); |
| 60 | + fwrite(STDERR, "Check that the classes are autoloadable from the current working directory.\n"); |
| 61 | +} |
| 62 | + |
| 63 | +foreach ($result['warmed'] as $class) { |
| 64 | + echo " warmed {$class}\n"; |
| 65 | +} |
| 66 | + |
| 67 | +foreach ($result['skipped'] as $class) { |
| 68 | + echo " skipped {$class} (metadata not exportable — in-memory cache only)\n"; |
| 69 | +} |
| 70 | + |
| 71 | +printf( |
| 72 | + "\n%d class(es) warmed, %d skipped → %s\n", |
| 73 | + count($result['warmed']), |
| 74 | + count($result['skipped']), |
| 75 | + $cacheDir, |
| 76 | +); |
| 77 | + |
| 78 | +exit(0); |
0 commit comments