From c9cea71a9d22661285b322a8fc571d8491dbe915 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 24 Oct 2024 03:51:48 +0200 Subject: [PATCH] new Bootstrap API --- app/Bootstrap.php | 45 +++++++++++++++++++++++++++++++++------------ www/index.php | 4 ++-- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/Bootstrap.php b/app/Bootstrap.php index 7a39742..cb937ac 100644 --- a/app/Bootstrap.php +++ b/app/Bootstrap.php @@ -4,6 +4,7 @@ namespace App; +use Nette; use Nette\Bootstrap\Configurator; @@ -12,28 +13,48 @@ */ class Bootstrap { - public static function boot(): Configurator + private Configurator $configurator; + private string $rootDir; + + + public function __construct() { + $this->rootDir = dirname(__DIR__); + // The configurator is responsible for setting up the application environment and services. // Learn more at https://doc.nette.org/en/bootstrap - $configurator = new Configurator; - $appDir = dirname(__DIR__); + $this->configurator = new Configurator; + + // Set the directory for temporary files generated by Nette (e.g. compiled templates) + $this->configurator->setTempDirectory($this->rootDir . '/temp'); + } + + + public function bootWebApplication(): Nette\DI\Container + { + $this->initializeEnvironment(); + $this->setupContainer(); + return $this->configurator->createContainer(); + } + + public function initializeEnvironment(): void + { // Nette is smart, and the development mode turns on automatically, // or you can enable for a specific IP address it by uncommenting the following line: - // $configurator->setDebugMode('secret@23.75.345.200'); + // $this->configurator->setDebugMode('secret@23.75.345.200'); // Enables Tracy: the ultimate "swiss army knife" debugging tool. // Learn more about Tracy at https://tracy.nette.org - $configurator->enableTracy($appDir . '/log'); - - // Set the directory for temporary files generated by Nette (e.g. compiled templates) - $configurator->setTempDirectory($appDir . '/temp'); + $this->configurator->enableTracy($this->rootDir . '/log'); + } - // Add configuration files - $configurator->addConfig($appDir . '/config/common.neon'); - $configurator->addConfig($appDir . '/config/services.neon'); - return $configurator; + private function setupContainer(): void + { + // Load configuration files + $configDir = $this->rootDir . '/config'; + $this->configurator->addConfig($configDir . '/common.neon'); + $this->configurator->addConfig($configDir . '/services.neon'); } } diff --git a/www/index.php b/www/index.php index 55c17b1..a6612cf 100644 --- a/www/index.php +++ b/www/index.php @@ -8,10 +8,10 @@ } // Initialize the application environment -$configurator = App\Bootstrap::boot(); +$bootstrap = new App\Bootstrap; // Create the Dependency Injection container -$container = $configurator->createContainer(); +$container = $bootstrap->bootWebApplication(); // Start the application and handle the incoming request $application = $container->getByType(Nette\Application\Application::class);