Skip to content

Commit c52869f

Browse files
committed
Warm cache in batch
1 parent b84f994 commit c52869f

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/MarkClosedPluginAsAbandoned.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ public function __construct(
5252
private IOInterface $io,
5353
) {}
5454

55+
public function warmCache(CompletePackageInterface ...$packages): void
56+
{
57+
$slugs = array_map(
58+
fn (CompletePackageInterface $package): ?string => $this->slug(
59+
...$package->getDistUrls(),
60+
...$package->getSourceUrls(),
61+
),
62+
$packages,
63+
);
64+
$slugs = array_filter($slugs, static fn (?string $slug) => $slug !== null);
65+
66+
$slugCount = count($slugs);
67+
$message = sprintf(
68+
'Warming cache for WordPress.org closed plugin status for %d %s',
69+
$slugCount,
70+
$slugCount === 1 ? 'slug' : 'slugs',
71+
);
72+
$this->io->debug($message);
73+
74+
$this->client->warmCache(...$slugs);
75+
}
76+
5577
public function __invoke(CompletePackageInterface $package): void
5678
{
5779
$this->io->debug(

src/Plugin.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ public function uninstall(Composer $composer, IOInterface $io): void
4848
public static function getSubscribedEvents(): array
4949
{
5050
return [
51-
PackageEvents::PRE_PACKAGE_INSTALL => ['markClosedAsAbandoned', PHP_INT_MAX - 1000],
52-
PackageEvents::PRE_PACKAGE_UPDATE => ['markClosedAsAbandoned', PHP_INT_MAX - 1000],
51+
PackageEvents::PRE_PACKAGE_INSTALL => [
52+
['warmCache', PHP_INT_MAX - 500],
53+
['markClosedAsAbandoned', PHP_INT_MAX - 1000],
54+
],
55+
PackageEvents::PRE_PACKAGE_UPDATE => [
56+
['warmCache', PHP_INT_MAX - 500],
57+
['markClosedAsAbandoned', PHP_INT_MAX - 1000],
58+
],
5359

5460
ScriptEvents::POST_INSTALL_CMD => ['markClosedLockedPackagesIfNotAlready', PHP_INT_MAX - 1000],
5561
ScriptEvents::POST_UPDATE_CMD => ['markClosedLockedPackagesIfNotAlready', PHP_INT_MAX - 1000],
@@ -58,6 +64,22 @@ public static function getSubscribedEvents(): array
5864
];
5965
}
6066

67+
public function warmCache(PackageEvent $event): void
68+
{
69+
$packages = array_map(
70+
static fn ($operation) => match (true) {
71+
$operation instanceof InstallOperation => $operation->getPackage(),
72+
$operation instanceof UpdateOperation => $operation->getTargetPackage(),
73+
default => null,
74+
},
75+
$event->getOperations(),
76+
);
77+
$packages = array_filter($packages, static fn ($package) => $package instanceof CompletePackageInterface);
78+
$packages = array_filter($packages, static fn ($package) => ! $package->isAbandoned());
79+
80+
$this->markClosedAsAbandoned->warmCache(...$packages);
81+
}
82+
6183
public function markClosedAsAbandoned(PackageEvent $event): void
6284
{
6385
$operation = $event->getOperation();
@@ -95,6 +117,8 @@ public function markClosedLockedPackagesIfNotAlready(ScriptEvent $event): void
95117
fn ($package) => ! in_array($package->getPrettyName(), $this->marked, true),
96118
);
97119

120+
$this->markClosedAsAbandoned->warmCache(...$packages);
121+
98122
foreach ($packages as $package) {
99123
$this->marked[] = $package->getPrettyName();
100124
$this->markClosedAsAbandoned->__invoke($package);

src/WpOrg/Api/Client.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ public function __construct(
1818
private CacheInterface $cache,
1919
) {}
2020

21+
public function warmCache(string ...$slugs): void
22+
{
23+
$slugs = array_map('trim', $slugs);
24+
$slugs = array_filter($slugs, static fn (string $slug) => $slug !== '');
25+
26+
$promises = array_map(
27+
fn (string $slug) => $this->isClosedAsync($slug),
28+
$slugs,
29+
);
30+
31+
$this->loop->wait($promises);
32+
}
33+
2134
public function isClosed(string $slug): bool
2235
{
2336
$slug = trim($slug);

0 commit comments

Comments
 (0)