diff --git a/src/ConfigLoader.php b/src/ConfigLoader.php index 0de4e64..636522f 100644 --- a/src/ConfigLoader.php +++ b/src/ConfigLoader.php @@ -5,23 +5,17 @@ namespace Luzrain\PhpRunnerBundle; use Symfony\Component\Config\ConfigCache; -use Symfony\Component\Config\Resource\FileResource; +use Symfony\Component\Config\Resource\DirectoryResource; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; -/** - * @psalm-suppress PropertyNotSetInConstructor - */ final class ConfigLoader implements CacheWarmerInterface { - private array $config; + private array $config = []; private ConfigCache $cache; - private string $yamlConfigFilePath; - public function __construct(string $projectDir, string $cacheDir, bool $isDebug) + public function __construct(private string $projectDir, string $cacheDir, bool $isDebug) { - $this->yamlConfigFilePath = \sprintf('%s/config/packages/phprunner.yaml', $projectDir); - $cacheConfigFilePath = \sprintf('%s/phprunner_config.cache.php', $cacheDir); - $this->cache = new ConfigCache($cacheConfigFilePath, $isDebug); + $this->cache = new ConfigCache(\sprintf('%s/phprunner_config.cache.php', $cacheDir), $isDebug); } public function isOptional(): bool @@ -31,64 +25,30 @@ public function isOptional(): bool public function warmUp(string $cacheDir, string $buildDir = null): array { - $resources = \is_file($this->yamlConfigFilePath) ? [new FileResource($this->yamlConfigFilePath)] : []; - $this->cache->write(\sprintf('config, true)), $resources); + $metadata = new DirectoryResource(\sprintf('%s/config/packages', $this->projectDir), '/phprunner/'); + $this->cache->write(\sprintf('config, true)), [$metadata]); return []; } - public function warmUpInFork(KernelFactory $kernelFactory): void - { - if (\pcntl_fork() === 0) { - $kernelFactory->createKernel()->boot(); - exit; - } else { - \pcntl_wait($status); - unset($status); - } - } - - public function isFresh(): bool - { - return $this->cache->isFresh(); - } - - /** - * @psalm-suppress UnresolvableInclude - * @psalm-suppress RedundantPropertyInitializationCheck - */ - private function getConfigCache(): array - { - return $this->config ??= require $this->cache->getPath(); - } - public function setConfig(array $config): void { - $this->config[0] = $config; - } - - public function setProcessConfig(array $config): void - { - $this->config[1] = $config; - } - - public function setSchedulerConfig(array $config): void - { - $this->config[2] = $config; + $this->config = $config; } - public function getConfig(): array + public function getConfig(KernelFactory|null $kernelFactory = null): array { - return $this->getConfigCache()[0]; - } - - public function getProcessConfig(): array - { - return $this->getConfigCache()[1]; - } + // Warm up cache if no fresh config found (do it in a forked process as the main process should not boot kernel) + if ($this->cache->isFresh() === false && $kernelFactory !== null) { + if (\pcntl_fork() === 0) { + $kernelFactory->createKernel()->boot(); + exit; + } else { + \pcntl_wait($status); + unset($status); + } + } - public function getSchedulerConfig(): array - { - return $this->getConfigCache()[2]; + return require $this->cache->getPath(); } } diff --git a/src/KernelFactory.php b/src/KernelFactory.php index a3803c6..45ea8ea 100644 --- a/src/KernelFactory.php +++ b/src/KernelFactory.php @@ -8,13 +8,10 @@ final readonly class KernelFactory { - private string $projectDir; private string $environment; private bool $isDebug; + private string $projectDir; - /** - * @psalm-suppress InvalidPropertyAssignmentValue - */ public function __construct(private \Closure $app, private array $args, array $options) { $this->projectDir = $options['project_dir']; diff --git a/src/Runner.php b/src/Runner.php index 6db89bc..c766840 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -23,14 +23,7 @@ public function run(): int isDebug: $this->kernelFactory->isDebug(), ); - // Warm up cache if no phprunner fresh config found (do it in a forked process as the main process should not boot kernel) - if (!$configLoader->isFresh()) { - $configLoader->warmUpInFork($this->kernelFactory); - } - - $config = $configLoader->getConfig(); - //$schedulerConfig = $configLoader->getSchedulerConfig(); - //$processConfig = $configLoader->getProcessConfig(); + $config = $configLoader->getConfig($this->kernelFactory); $phpRunner = new PhpRunner( pidFile: $config['pid_file'], @@ -52,36 +45,5 @@ public function run(): int } return $phpRunner->run(); - - - if (!empty($schedulerConfig)) { - new SchedulerWorker( - kernelFactory: $this->kernelFactory, - user: $config['user'], - group: $config['group'], - schedulerConfig: $schedulerConfig, - ); - } - - if ($config['reload_strategy']['file_monitor']['active'] && $this->kernelFactory->isDebug()) { - new FileMonitorWorker( - user: $config['user'], - group: $config['group'], - sourceDir: $config['reload_strategy']['file_monitor']['source_dir'], - filePattern: $config['reload_strategy']['file_monitor']['file_pattern'], - ); - } - - if (!empty($processConfig)) { - new SupervisorWorker( - kernelFactory: $this->kernelFactory, - user: $config['user'], - group: $config['group'], - processConfig: $processConfig, - ); - } - - - return 0; } }