Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
where applicable.

## 1.1.18 — 2026-11-23

- Stop recompiling on every request when more than 32 bundle sets are live (multisite). Warm hits refresh the bundle-set metadata mtime at most hourly (not for a read-only prewarmed cache), and metadata beyond the 32-set cap is evicted only after 30 days without use. Before, the oldest set by creation time was evicted, so each recompile evicted another live set.
- Treat a symlinked implicit `var/cache/<env>/kernel` directory as untrusted and use the private fallback. Before, `resolve()` returned the symlink and every request failed with HTTP 500 after a full compile. An explicit `APP_CACHE_DIR` whose `kernel` entry is a symlink now fails in `CachePath::resolve()` with the existing diagnostic.
- Require `config/bundles.php` once per boot. A cold boot used to require it twice, so a file that declares a function or class failed with "Cannot redeclare". `BundleDiscovery` accepts the loaded configuration as an optional trailing constructor argument.
- On multisite, refuse to activate a "Network: true" plugin package from a site admin's package page, as core's `plugins.php` does. Activation also requires the `activate_plugin` meta capability for the entry.
- After a warm cache hit, `container:dump` and `debug:container --parameters/--tag/--types/--env-vars` show a freshly configured builder instead of the bootstrap stub. `lint:container` now always compiles the configured container instead of reporting "valid" whenever a runtime container exists.
- Let Dependabot update the pinned GitHub Actions (CI only).

## 1.1.17 — 2026-11-16

- Stop failing the boot when another local user pre-creates the predictable temporary cache root (`<tmp>/sympress-kernel-<uid>-<hash>`) as a directory or symlink. The kernel still never adopts such a path; it reuses or atomically creates a randomly suffixed private sibling (mode 0700, owned by the PHP user) and logs the switch.
Expand Down
19 changes: 18 additions & 1 deletion src/Admin/PackageManagerActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,24 @@ public function canRun(string $action, PackageMetadata $package): bool

return $action === 'activate'
&& $package->isPlugin()
&& current_user_can('activate_plugins');
&& current_user_can('activate_plugins')
&& current_user_can('activate_plugin', $package->entry())
&& !$this->networkOnlyOnSite($package);
}

/**
* activate_plugin() activates a "Network: true" plugin network-wide without a
* capability check; core's plugins.php only allows that from the network admin.
*/
private function networkOnlyOnSite(PackageMetadata $package): bool
{
if (!is_multisite() || is_network_admin()) {
return false;
}

$this->loadPluginAdminFunctions();

return is_network_only_plugin($package->entry());
}

public function isKnownAction(string $action): bool
Expand Down
33 changes: 33 additions & 0 deletions src/Console/ConfiguredBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace SymPress\Kernel\Console;

use SymPress\Kernel\Container;
use SymPress\Kernel\Kernel\KernelInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/** @internal */
final class ConfiguredBuilder
{
/**
* A warm cache hit skips configuration, leaving only the bootstrap builder. Inspection
* commands then configure a fresh builder on a clone, so the live container stays as is.
* shortcut: listeners of kernel.before_container_build/container_configured do not run
* for it; dispatch them here if a bundle starts registering definitions through them.
*/
public static function of(Container $container): ContainerBuilder
{
$builder = $container->builder();
$kernel = $container->has(Container::KERNEL_ID) ? $container->get(Container::KERNEL_ID) : null;
if ($builder->hasParameter('kernel.project_dir') || !$kernel instanceof KernelInterface) {
return $builder;
}

$fresh = clone $container;
$kernel->configureContainer($fresh->builder(), $fresh, $kernel->discoverBundles());

return $fresh->builder();
}
}
2 changes: 1 addition & 1 deletion src/Console/ContainerDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$format = $input->getOption('format');
$format = is_string($format) ? strtolower($format) : 'yaml';
$builder = $this->container->builder();
$builder = ConfiguredBuilder::of($this->container);

if ($format === 'yaml') {
$output->write((new YamlDumper($builder))->dump());
Expand Down
18 changes: 13 additions & 5 deletions src/Console/DebugContainerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Component\DependencyInjection\Reference;

Expand All @@ -21,6 +22,8 @@
)]
final class DebugContainerCommand extends Command
{
private ?ContainerBuilder $builder = null;

public function __construct(
private readonly Container $container,
) {
Expand Down Expand Up @@ -83,6 +86,11 @@ private function items(InputInterface $input): array
return $this->serviceIds();
}

private function builder(): ContainerBuilder
{
return $this->builder ??= ConfiguredBuilder::of($this->container);
}

/** @return list<string> */
private function serviceIds(): array
{
Expand All @@ -98,7 +106,7 @@ private function serviceIds(): array
return $ids;
}

$ids = $this->container->builder()->getServiceIds();
$ids = $this->builder()->getServiceIds();
sort($ids);

return $ids;
Expand All @@ -107,7 +115,7 @@ private function serviceIds(): array
/** @return list<string> */
private function taggedServices(string $tag, bool $types, bool $showArguments): array
{
$builder = $this->container->builder();
$builder = $this->builder();
$ids = array_keys($builder->findTaggedServiceIds($tag));
sort($ids);

Expand Down Expand Up @@ -149,7 +157,7 @@ private function describedServices(bool $showArguments): array

private function describeService(string $id, bool $showArguments): string
{
$builder = $this->container->builder();
$builder = $this->builder();

if ($builder->hasAlias($id)) {
return $this->describeAlias($id, $builder->getAlias($id));
Expand Down Expand Up @@ -191,7 +199,7 @@ private function describeAlias(string $id, Alias $alias): string
/** @return list<string> */
private function parameters(): array
{
$builder = $this->container->builder();
$builder = $this->builder();

$parameters = array_keys($builder->getParameterBag()->all());
sort($parameters);
Expand All @@ -202,7 +210,7 @@ private function parameters(): array
/** @return list<string> */
private function envVars(): array
{
$builder = $this->container->builder();
$builder = $this->builder();
$envs = [];

foreach ($builder->getParameterBag()->all() as $value) {
Expand Down
9 changes: 1 addition & 8 deletions src/Console/LintContainerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ public function __construct(

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($this->container->runtimeContainer() !== null) {
$output->writeln('<info>The container is valid.</info>');

return Command::SUCCESS;
}

try {
$builder = $this->container->builder();
$builder->compile(true);
ConfiguredBuilder::of($this->container)->compile(true);
} catch (\Throwable $throwable) {
$output->writeln(sprintf('<error>%s</error>', $throwable->getMessage()));

Expand Down
4 changes: 3 additions & 1 deletion src/Discovery/BundleDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ final class BundleDiscovery
/**
* @param list<string>|array<string> $packagePrefixes Optional package-name prefixes used to narrow discovery.
* @param string|null $cacheDir Resolved kernel cache directory; resolved again from the project when omitted.
* @param array<mixed, mixed>|null $bundlesConfiguration Already loaded config/bundles.php; required here when omitted.
*/
public function __construct(
private readonly ActivePackageResolver $resolver,
array $packagePrefixes = [],
private readonly ?string $projectDir = null,
private readonly ?string $environment = null,
private readonly ?string $cacheDir = null,
private readonly ?array $bundlesConfiguration = null,
) {

$this->packagePrefixes = $this->normalizePackagePrefixes($packagePrefixes);
Expand Down Expand Up @@ -166,7 +168,7 @@ private function configuredBundles(): array
return [];
}

$configuration = require $configFile;
$configuration = $this->bundlesConfiguration ?? require $configFile;

if (!is_array($configuration)) {
return [];
Expand Down
1 change: 1 addition & 0 deletions src/Kernel/AbstractKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public function discoverBundles(): BundleRegistry
$this->projectDir,
$this->environment,
$this->getCacheDir(),
$this->configuration()->bundlesConfiguration(),
))->discover();
$this->bundles = $this->bundleMap($this->bundleRegistry);

Expand Down
35 changes: 22 additions & 13 deletions src/Kernel/KernelConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class KernelConfigurationResolver
/** @var list<string>|null */
private ?array $packagePrefixes = null;
private ?string $cacheDir = null;
/** @var array<mixed, mixed>|null */
private ?array $bundlesConfiguration = null;

public function __construct(
private readonly string $projectDir,
Expand Down Expand Up @@ -101,19 +103,7 @@ public function knownEnvironments(): array
'staging',
'test',
];
$bundlesDefinition = sprintf('%s/config/bundles.php', $this->projectDir);

if (!is_file($bundlesDefinition)) {
return $this->normalizeKnownEnvironments($known);
}

$configuration = require $bundlesDefinition;

if (!is_array($configuration)) {
return $this->normalizeKnownEnvironments($known);
}

foreach ($configuration as $envs) {
foreach ($this->bundlesConfiguration() as $envs) {
if (!is_array($envs)) {
continue;
}
Expand All @@ -130,6 +120,25 @@ public function knownEnvironments(): array
return $this->normalizeKnownEnvironments($known);
}

/**
* config/bundles.php may declare functions or classes, so it is required once per
* resolver and shared by bundle discovery and the known environments.
*
* @return array<mixed, mixed>
*/
public function bundlesConfiguration(): array
{
if ($this->bundlesConfiguration !== null) {
return $this->bundlesConfiguration;
}

$file = sprintf('%s/config/bundles.php', $this->projectDir);
$configuration = is_file($file) ? require $file : [];
$this->bundlesConfiguration = is_array($configuration) ? $configuration : [];

return $this->bundlesConfiguration;
}

/** @return array<int, string> */
public function configDirectories(BundleRegistry $bundles): array
{
Expand Down
41 changes: 41 additions & 0 deletions tests/Admin/PackageManagerPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,27 @@ function activate_plugin(string $plugin, string $redirect = '', bool $networkWid
}
}

if (!function_exists('is_multisite')) {
function is_multisite(): bool
{
return (bool) ($GLOBALS['kernel_test_is_multisite'] ?? false);
}
}

if (!function_exists('is_network_admin')) {
function is_network_admin(): bool
{
return (bool) ($GLOBALS['kernel_test_is_network_admin'] ?? false);
}
}

if (!function_exists('is_network_only_plugin')) {
function is_network_only_plugin(string $plugin): bool
{
return in_array($plugin, $GLOBALS['kernel_test_network_only_plugins'] ?? [], true);
}
}

if (!function_exists('delete_plugins')) {
function delete_plugins(array $plugins): bool
{
Expand Down Expand Up @@ -186,6 +207,7 @@ protected function setUp(): void
$GLOBALS['kernel_test_admin_menu_pages'] = [];
$GLOBALS['kernel_test_admin_actions'] = [];
$GLOBALS['kernel_test_capabilities'] = [
'activate_plugin' => true,
'activate_plugins' => true,
'delete_plugins' => true,
];
Expand All @@ -202,6 +224,9 @@ protected function tearDown(): void
$GLOBALS['kernel_test_admin_menu_pages'],
$GLOBALS['kernel_test_capabilities'],
$GLOBALS['kernel_test_file_mod_allowed'],
$GLOBALS['kernel_test_is_multisite'],
$GLOBALS['kernel_test_is_network_admin'],
$GLOBALS['kernel_test_network_only_plugins'],
$GLOBALS['kernel_test_plugins_cache_cleaned'],
);

Expand Down Expand Up @@ -258,6 +283,22 @@ public function testActionsAreBlockedWhenWordPressDisallowsFileModifications():
);
}

public function testSiteAdminCannotActivateNetworkOnlyPlugin(): void
{
$package = $this->pluginPackage('/tmp/demo');
self::assertTrue($this->invokePageMethod('canRun', 'activate', $package));

$GLOBALS['kernel_test_is_multisite'] = true;
$GLOBALS['kernel_test_network_only_plugins'] = ['demo/demo.php'];
self::assertFalse($this->invokePageMethod('canRun', 'activate', $package));

$GLOBALS['kernel_test_is_network_admin'] = true;
self::assertTrue($this->invokePageMethod('canRun', 'activate', $package));

$GLOBALS['kernel_test_capabilities']['activate_plugin'] = false;
self::assertFalse($this->invokePageMethod('canRun', 'activate', $package));
}

public function testUnmanagedSymlinkDeleteIsRejected(): void
{
$target = $this->tmpPath('unmanaged-target');
Expand Down
62 changes: 62 additions & 0 deletions tests/Console/WarmCacheInspectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace SymPress\Kernel\Tests\Console;

use SymPress\Kernel\Bundle\BundleRegistry;
use SymPress\Kernel\Console\ContainerDumpCommand;
use SymPress\Kernel\Console\DebugContainerCommand;
use SymPress\Kernel\Console\LintContainerCommand;
use SymPress\Kernel\Tests\Kernel\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServicesResetter;

final class WarmCacheInspectionTest extends KernelTestCase
{
public function testInspectionCommandsSeeTheConfiguredContainerAfterAWarmHit(): void
{
$project = $this->tmpPath('warm-inspection');
$cold = $this->kernel($project);
$container = $cold->createContainer();
$cold->createRuntimeContainer($container, new BundleRegistry(), $cold->configureContainer($container->builder(), $container, new BundleRegistry()));
$kernel = $this->kernel($project);
$warm = $kernel->createContainer();
self::assertTrue($kernel->tryUseRuntimeContainer($warm, new BundleRegistry()));
self::assertFalse($warm->builder()->hasParameter('kernel.project_dir'));

$parameters = $this->runCommand(new DebugContainerCommand($warm), ['--parameters' => true]);
self::assertStringContainsString('kernel.project_dir', $parameters);
$types = $this->runCommand(new DebugContainerCommand($warm), ['--types' => true]);
self::assertStringContainsString('services_resetter class=' . ServicesResetter::class, $types);
self::assertStringContainsString(LintContainerCommand::class, $this->runCommand(new ContainerDumpCommand($warm), []));
self::assertSame(0, (new CommandTester(new LintContainerCommand($warm)))->execute([]));
self::assertFalse($warm->builder()->hasParameter('kernel.project_dir'));
}

public function testLintCompilesTheContainerEvenWithARuntimeContainer(): void
{
$project = $this->tmpPath('warm-lint');
$kernel = $this->kernel($project);
$container = $kernel->createContainer();
$kernel->createRuntimeContainer($container, new BundleRegistry(), $kernel->configureContainer($container->builder(), $container, new BundleRegistry()));
self::assertNotNull($container->runtimeContainer());
$container->builder()->register('broken.consumer', \stdClass::class)
->addArgument(new Reference('missing.service'))
->setPublic(true);

$tester = new CommandTester(new LintContainerCommand($container));
self::assertSame(1, $tester->execute([]));
self::assertStringContainsString('missing.service', $tester->getDisplay());
}

/** @param array<string, mixed> $input */
private function runCommand(DebugContainerCommand|ContainerDumpCommand $command, array $input): string
{
$tester = new CommandTester($command);
self::assertSame(0, $tester->execute($input));

return $tester->getDisplay();
}
}
Loading