diff --git a/n98-magerun2.yaml b/n98-magerun2.yaml index e004ceb..f2e4d27 100644 --- a/n98-magerun2.yaml +++ b/n98-magerun2.yaml @@ -4,6 +4,7 @@ autoloaders: commands: customCommands: - Hypernode\Magento\Command\Hypernode\Performance\PerformanceCommand + - Hypernode\Magento\Command\Hypernode\Config\CheckPerformanceConfigCommand passwordCracker: rulesDirs: diff --git a/src/Hypernode/Magento/Command/Hypernode/Config/CheckPerformanceConfigCommand.php b/src/Hypernode/Magento/Command/Hypernode/Config/CheckPerformanceConfigCommand.php new file mode 100644 index 0000000..13f73a5 --- /dev/null +++ b/src/Hypernode/Magento/Command/Hypernode/Config/CheckPerformanceConfigCommand.php @@ -0,0 +1,118 @@ +setName('hypernode:check-performance-config') + ->setDescription('Creates a little report if the magento configuration is optimal based on Redis, Varnish, and more'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->detectMagento($output); + if (!$this->initMagento()) { + return 1; + } + + $output->writeln('Checking Magento Performance Configuration...'); + + // Fetch configuration values + $config = $this->getMagentoConfig(); + + $checks = [ + 'cache.frontend.page_cache' => [ + 'current' => $config['cache']['cache.frontend.page_cache'] ?? 'Unknown', + 'optimal' => 'Magento\Framework\Cache\Backend\Redis', + ], + 'session.save' => [ + 'current' => $config['cache']['session.save'] ?? 'Unknown', + 'optimal' => 'redis', + ], + 'cache.frontend.default.backend' => [ + 'current' => $config['cache']['cache.frontend.default.backend'] ?? 'Unknown', + 'optimal' => 'Magento\Framework\Cache\Backend\Redis', + ], + 'http_cache_hosts' => [ + 'current' => $config['cache']['http_cache_hosts'] ?? 'Unknown', + 'optimal' => 'varnish:6081', + ], + 'deploy:mode' => [ + 'current' => $config['deploy_mode'] ?? 'Unknown', + 'optimal' => 'production', + ], + ]; + + // Create a table to display results + $table = new Table($output); + $table->setHeaders(['Configuration', 'Current Value', 'Optimal Value']); + + foreach ($checks as $key => $check) { + $current = $check['current']; + $optimal = $check['optimal']; + $color = $current === $optimal ? 'green' : 'red'; + + $table->addRow([ + $key, + "$current", + $optimal, + ]); + } + + $table->render(); + + $output->writeln('Performance configuration check completed.'); + return 0; + } + + private function getMagentoConfig(): array + { + // Load the env.php file + $envFilePath = BP . '/app/etc/env.php'; + if (!file_exists($envFilePath)) { + throw new \RuntimeException('env.php file not found.'); + } + + $envConfig = include $envFilePath; + + // Fetch deployment mode + $deployMode = \Magento\Framework\App\State::MODE_DEFAULT; + if (isset($envConfig['MAGE_MODE'])) { + $deployMode = $envConfig['MAGE_MODE']; + } + + // Fetch cache configurations + $cacheConfig = [ + 'cache.frontend.page_cache' => $envConfig['cache']['frontend']['page_cache']['backend'] ?? 'Unknown', + 'session.save' => $envConfig['session']['save'] ?? 'Unknown', + 'cache.frontend.default.backend' => $envConfig['cache']['frontend']['default']['backend'] ?? 'Unknown', + 'http_cache_hosts' => isset($envConfig['http_cache_hosts'][0]) + ? ($envConfig['http_cache_hosts'][0]['host'] . ':' . $envConfig['http_cache_hosts'][0]['port']) + : 'Unknown',]; + + return [ + 'cache' => $cacheConfig, + 'deploy_mode' => $deployMode, + ]; + } +} + diff --git a/src/Hypernode/Magento/Command/Hypernode/Performance/PerformanceCommand.php b/src/Hypernode/Magento/Command/Hypernode/Performance/PerformanceCommand.php index 81e2e1f..8dd55c4 100644 --- a/src/Hypernode/Magento/Command/Hypernode/Performance/PerformanceCommand.php +++ b/src/Hypernode/Magento/Command/Hypernode/Performance/PerformanceCommand.php @@ -85,7 +85,7 @@ protected function configure() * * @return bool|void */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { // setting the options - hypernode demands it $this->_options = $input->getOptions(); @@ -120,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output) 'Use argument --sitemap to specify sitemap when using Magento 2.' ); - return; + return 1; } if ($this->_options['silent']) { @@ -170,6 +170,8 @@ protected function execute(InputInterface $input, OutputInterface $output) } } } + + return 0; } /**