diff --git a/CHANGELOG.md b/CHANGELOG.md index ec9d354..8e4177a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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//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 (`/sympress-kernel--`) 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. diff --git a/src/Admin/PackageManagerActions.php b/src/Admin/PackageManagerActions.php index fc43d1a..b05a404 100644 --- a/src/Admin/PackageManagerActions.php +++ b/src/Admin/PackageManagerActions.php @@ -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 diff --git a/src/Console/ConfiguredBuilder.php b/src/Console/ConfiguredBuilder.php new file mode 100644 index 0000000..f90ae44 --- /dev/null +++ b/src/Console/ConfiguredBuilder.php @@ -0,0 +1,33 @@ +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(); + } +} diff --git a/src/Console/ContainerDumpCommand.php b/src/Console/ContainerDumpCommand.php index 3586d5c..792ab41 100644 --- a/src/Console/ContainerDumpCommand.php +++ b/src/Console/ContainerDumpCommand.php @@ -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()); diff --git a/src/Console/DebugContainerCommand.php b/src/Console/DebugContainerCommand.php index 8d620da..aec23b0 100644 --- a/src/Console/DebugContainerCommand.php +++ b/src/Console/DebugContainerCommand.php @@ -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; @@ -21,6 +22,8 @@ )] final class DebugContainerCommand extends Command { + private ?ContainerBuilder $builder = null; + public function __construct( private readonly Container $container, ) { @@ -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 */ private function serviceIds(): array { @@ -98,7 +106,7 @@ private function serviceIds(): array return $ids; } - $ids = $this->container->builder()->getServiceIds(); + $ids = $this->builder()->getServiceIds(); sort($ids); return $ids; @@ -107,7 +115,7 @@ private function serviceIds(): array /** @return list */ 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); @@ -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)); @@ -191,7 +199,7 @@ private function describeAlias(string $id, Alias $alias): string /** @return list */ private function parameters(): array { - $builder = $this->container->builder(); + $builder = $this->builder(); $parameters = array_keys($builder->getParameterBag()->all()); sort($parameters); @@ -202,7 +210,7 @@ private function parameters(): array /** @return list */ private function envVars(): array { - $builder = $this->container->builder(); + $builder = $this->builder(); $envs = []; foreach ($builder->getParameterBag()->all() as $value) { diff --git a/src/Console/LintContainerCommand.php b/src/Console/LintContainerCommand.php index 06f3ed9..04a528f 100644 --- a/src/Console/LintContainerCommand.php +++ b/src/Console/LintContainerCommand.php @@ -24,15 +24,8 @@ public function __construct( protected function execute(InputInterface $input, OutputInterface $output): int { - if ($this->container->runtimeContainer() !== null) { - $output->writeln('The container is valid.'); - - return Command::SUCCESS; - } - try { - $builder = $this->container->builder(); - $builder->compile(true); + ConfiguredBuilder::of($this->container)->compile(true); } catch (\Throwable $throwable) { $output->writeln(sprintf('%s', $throwable->getMessage())); diff --git a/src/Discovery/BundleDiscovery.php b/src/Discovery/BundleDiscovery.php index 5c77e5d..a706fdf 100644 --- a/src/Discovery/BundleDiscovery.php +++ b/src/Discovery/BundleDiscovery.php @@ -45,6 +45,7 @@ final class BundleDiscovery /** * @param list|array $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|null $bundlesConfiguration Already loaded config/bundles.php; required here when omitted. */ public function __construct( private readonly ActivePackageResolver $resolver, @@ -52,6 +53,7 @@ public function __construct( 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); @@ -166,7 +168,7 @@ private function configuredBundles(): array return []; } - $configuration = require $configFile; + $configuration = $this->bundlesConfiguration ?? require $configFile; if (!is_array($configuration)) { return []; diff --git a/src/Kernel/AbstractKernel.php b/src/Kernel/AbstractKernel.php index d9e0ca4..e6f08ff 100644 --- a/src/Kernel/AbstractKernel.php +++ b/src/Kernel/AbstractKernel.php @@ -270,6 +270,7 @@ public function discoverBundles(): BundleRegistry $this->projectDir, $this->environment, $this->getCacheDir(), + $this->configuration()->bundlesConfiguration(), ))->discover(); $this->bundles = $this->bundleMap($this->bundleRegistry); diff --git a/src/Kernel/KernelConfigurationResolver.php b/src/Kernel/KernelConfigurationResolver.php index acf2a38..0103cd7 100644 --- a/src/Kernel/KernelConfigurationResolver.php +++ b/src/Kernel/KernelConfigurationResolver.php @@ -12,6 +12,8 @@ final class KernelConfigurationResolver /** @var list|null */ private ?array $packagePrefixes = null; private ?string $cacheDir = null; + /** @var array|null */ + private ?array $bundlesConfiguration = null; public function __construct( private readonly string $projectDir, @@ -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; } @@ -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 + */ + 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 */ public function configDirectories(BundleRegistry $bundles): array { diff --git a/tests/Admin/PackageManagerPageTest.php b/tests/Admin/PackageManagerPageTest.php index ffa3725..a89d18d 100644 --- a/tests/Admin/PackageManagerPageTest.php +++ b/tests/Admin/PackageManagerPageTest.php @@ -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 { @@ -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, ]; @@ -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'], ); @@ -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'); diff --git a/tests/Console/WarmCacheInspectionTest.php b/tests/Console/WarmCacheInspectionTest.php new file mode 100644 index 0000000..851d57e --- /dev/null +++ b/tests/Console/WarmCacheInspectionTest.php @@ -0,0 +1,62 @@ +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 $input */ + private function runCommand(DebugContainerCommand|ContainerDumpCommand $command, array $input): string + { + $tester = new CommandTester($command); + self::assertSame(0, $tester->execute($input)); + + return $tester->getDisplay(); + } +} diff --git a/tests/Kernel/ConfigPrecedenceTest.php b/tests/Kernel/ConfigPrecedenceTest.php index b5ff313..46e0ed7 100644 --- a/tests/Kernel/ConfigPrecedenceTest.php +++ b/tests/Kernel/ConfigPrecedenceTest.php @@ -11,6 +11,7 @@ use SymPress\Kernel\Kernel\AbstractKernel; use SymPress\Kernel\Location\Locations; use SymPress\Kernel\SiteConfig; +use SymPress\Kernel\Tests\Support\TestSiteConfig; use SymPress\Kernel\WpContext; use Symfony\Component\Filesystem\Filesystem; @@ -32,6 +33,24 @@ public function testKernelLogsDirPointsToProjectVarLog(): void self::assertSame($projectDir . '/var/log', $container->builder()->getParameter('kernel.logs_dir')); } + public function testColdBootRequiresBundlesConfigurationOnce(): void + { + $projectDir = $this->tmpPath('project'); + mkdir("{$projectDir}/config", 0777, true); + // A bundles.php that declares a function would fatal on a second require. + file_put_contents("{$projectDir}/config/bundles.php", "force(WpContext::CORE)) extends AbstractKernel { + }; + $container = $kernel->createContainer(); + + try { + $kernel->configureContainer($container->builder(), $container, $kernel->discoverBundles()); + self::assertSame(1, $GLOBALS['kernel_test_bundles_loads']); + } finally { + unset($GLOBALS['kernel_test_bundles_loads']); + } + } + protected function tearDown(): void { if ($this->paths === []) {