-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,18 +20,44 @@ use Nette\Bootstrap\Configurator; | |
|
||
class Bootstrap | ||
{ | ||
public static function boot(): Configurator | ||
private Configurator $configurator; | ||
private string $rootDir; | ||
|
||
public function __construct() | ||
{ | ||
$this->rootDir = dirname(__DIR__); | ||
$configurator = new Configurator; | ||
//$this->configurator->setDebugMode('[email protected]'); | ||
$this->configurator->enableTracy($this->rootDir . '/log'); | ||
// Konfigurátor je zodpovědný za nastavení prostředí aplikace a služeb. | ||
$this->configurator = new Configurator; | ||
// Nastaví adresář pro dočasné soubory generované Nette (např. zkompilované šablony) | ||
$this->configurator->setTempDirectory($this->rootDir . '/temp'); | ||
} | ||
|
||
public function bootWebApplication(): Nette\DI\Container | ||
{ | ||
$this->initializeEnvironment(); | ||
$this->setupContainer(); | ||
return $this->configurator->createContainer(); | ||
} | ||
|
||
private function initializeEnvironment(): void | ||
{ | ||
// Nette je chytré a vývojový režim se zapíná automaticky, | ||
// nebo jej můžete povolit pro konkrétní IP adresu odkomentováním následujícího řádku: | ||
// $this->configurator->setDebugMode('[email protected]'); | ||
|
||
// Aktivuje Tracy: ultimátní "švýcarský nůž" pro ladění. | ||
$this->configurator->enableTracy($this->rootDir . '/log'); | ||
|
||
// RobotLoader: automaticky načítá všechny třídy ve zvoleném adresáři | ||
$this->configurator->createRobotLoader() | ||
->addDirectory(__DIR__) | ||
->register(); | ||
} | ||
|
||
private function setupContainer(): void | ||
{ | ||
// Načte konfigurační soubory | ||
$this->configurator->addConfig($this->rootDir . '/config/common.neon'); | ||
return $configurator; | ||
} | ||
} | ||
``` | ||
|
@@ -40,16 +66,15 @@ class Bootstrap | |
index.php | ||
========= | ||
|
||
Prvotní soubor je v případě webových aplikací `index.php`, který se nachází ve veřejném adresáři `www/`. Ten si nechá od třídy Bootstrap inicializovat prostředí a vrátit `$configurator` a následně vyrobí DI kontejner. Poté z něj získá službu `Application`, kterou spustí webovou aplikaci: | ||
Prvotní soubor je v případě webových aplikací `index.php`, který se nachází ve veřejném adresáři `www/`. Ten si nechá od třídy Bootstrap inicializovat prostředí a vyrobit DI kontejner. Poté z něj získá službu `Application`, která spustí webovou aplikaci: | ||
|
||
```php | ||
// inicializace prostředí + získání objektu Configurator | ||
$configurator = App\Bootstrap::boot(); | ||
// vytvoření DI kontejneru | ||
$container = $this->configurator->createContainer(); | ||
$bootstrap = new App\Bootstrap; | ||
// Inicializace prostředí + vytvoření DI kontejneru | ||
$container = $bootstrap->bootWebApplication(); | ||
// DI kontejner vytvoří objekt Nette\Application\Application | ||
$application = $container->getByType(Nette\Application\Application::class); | ||
// spuštění Nette aplikace | ||
// Spuštění aplikace Nette a zpracování příchozího požadavku | ||
$application->run(); | ||
``` | ||
|
||
|
@@ -236,13 +261,21 @@ $this->configurator->addServices([ | |
Odlišné prostředí | ||
================= | ||
|
||
Nebojte se upravit třídu Bootstrap podle svých potřeb. Metodě `boot()` můžete přidat parametry pro rozlišení webových projektů nebo doplnit další metody, například `bootForTests()`, která inicializuje prostředí pro jednotkové testy, `bootForCli()` pro skripty volané z příkazové řádky atd. | ||
Nebojte se upravit třídu Bootstrap podle svých potřeb. Metodě `bootWebApplication()` můžete přidat parametry pro rozlišení webových projektů. Nebo můžeme doplnit další metody, například `bootTestEnvironment()`, která inicializuje prostředí pro jednotkové testy, `bootConsoleApplication()` pro skripty volané z příkazové řádky atd. | ||
|
||
```php | ||
public static function bootForTests(): Configurator | ||
public function bootTestEnvironment(): Nette\DI\Container | ||
{ | ||
$configurator = self::boot(); | ||
Tester\Environment::setup(); // inicializace Nette Testeru | ||
return $configurator; | ||
$this->setupContainer(); | ||
return $this->configurator->createContainer(); | ||
} | ||
|
||
public function bootConsoleApplication(): Nette\DI\Container | ||
{ | ||
$this->configurator->setDebugMode(false); | ||
$this->initializeEnvironment(); | ||
$this->setupContainer(); | ||
return $this->configurator->createContainer(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,18 +20,44 @@ use Nette\Bootstrap\Configurator; | |
|
||
class Bootstrap | ||
{ | ||
public static function boot(): Configurator | ||
private Configurator $configurator; | ||
private string $rootDir; | ||
|
||
public function __construct() | ||
{ | ||
$this->rootDir = dirname(__DIR__); | ||
$configurator = new Configurator; | ||
//$this->configurator->setDebugMode('[email protected]'); | ||
$this->configurator->enableTracy($this->rootDir . '/log'); | ||
// The configurator is responsible for setting up the application environment and services. | ||
$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(); | ||
} | ||
|
||
private 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: | ||
// $this->configurator->setDebugMode('[email protected]'); | ||
|
||
// Enables Tracy: the ultimate "swiss army knife" debugging tool. | ||
$this->configurator->enableTracy($this->rootDir . '/log'); | ||
|
||
// RobotLoader: autoloads all classes in the given directory | ||
$this->configurator->createRobotLoader() | ||
->addDirectory(__DIR__) | ||
->register(); | ||
} | ||
|
||
private function setupContainer(): void | ||
{ | ||
// Load configuration files | ||
$this->configurator->addConfig($this->rootDir . '/config/common.neon'); | ||
return $configurator; | ||
} | ||
} | ||
``` | ||
|
@@ -40,16 +66,15 @@ class Bootstrap | |
index.php | ||
========= | ||
|
||
In the case of web applications, the initial file is `index.php`, which is located in the public directory `www/`. It lets the `Bootstrap` class to initialize the environment and return the `$configurator` which creates DI container. Then it obtains the `Application` service, that runs the web application: | ||
The initial file for web applications is `index.php`, located in the public directory `www/`. It uses the `Bootstrap` class to initialize the environment and create a DI container. Then, it obtains the `Application` service from the container, which launches the web application: | ||
|
||
```php | ||
// initialize the environment + get Configurator object | ||
$configurator = App\Bootstrap::boot(); | ||
// create a DI container | ||
$container = $this->configurator->createContainer(); | ||
$bootstrap = new App\Bootstrap; | ||
// Initialize the environment + create a DI container | ||
$container = $bootstrap->bootWebApplication(); | ||
// DI container creates a Nette\Application\Application object | ||
$application = $container->getByType(Nette\Application\Application::class); | ||
// start Nette application | ||
// Start the Nette application and handle the incoming request | ||
$application->run(); | ||
``` | ||
|
||
|
@@ -236,13 +261,21 @@ $this->configurator->addServices([ | |
Different Environments | ||
====================== | ||
|
||
Feel free to customize the `Bootstrap` class to suit your needs. You can add parameters to the `boot()` method to differentiate web projects, or add other methods, such as `bootForTests()`, which initializes the environment for unit tests, `bootForCli()` for scripts called from the command line, and so on. | ||
Don't hesitate to customize the `Bootstrap` class according to your needs. You can add parameters to the `bootWebApplication()` method to differentiate between web projects. Alternatively, you can add other methods, such as `bootTestEnvironment()` to initialize the environment for unit tests, `bootConsoleApplication()` for scripts called from the command line, and so on. | ||
|
||
```php | ||
public static function bootForTests(): Configurator | ||
public function bootTestEnvironment(): Nette\DI\Container | ||
{ | ||
$configurator = self::boot(); | ||
Tester\Environment::setup(); // Nette Tester initialization | ||
return $configurator; | ||
$this->setupContainer(); | ||
return $this->configurator->createContainer(); | ||
} | ||
|
||
public function bootConsoleApplication(): Nette\DI\Container | ||
{ | ||
$this->configurator->setDebugMode(false); | ||
$this->initializeEnvironment(); | ||
$this->setupContainer(); | ||
return $this->configurator->createContainer(); | ||
} | ||
``` |