Skip to content

Commit fcf6992

Browse files
committed
new Bootstrap API
1 parent 58d380b commit fcf6992

File tree

2 files changed

+96
-30
lines changed

2 files changed

+96
-30
lines changed

application/cs/bootstrap.texy

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,44 @@ use Nette\Bootstrap\Configurator;
2020

2121
class Bootstrap
2222
{
23-
public static function boot(): Configurator
23+
private Configurator $configurator;
24+
private string $rootDir;
25+
26+
public function __construct()
2427
{
2528
$this->rootDir = dirname(__DIR__);
26-
$configurator = new Configurator;
27-
//$this->configurator->setDebugMode('[email protected]');
28-
$this->configurator->enableTracy($this->rootDir . '/log');
29+
// Konfigurátor je zodpovědný za nastavení prostředí aplikace a služeb.
30+
$this->configurator = new Configurator;
31+
// Nastaví adresář pro dočasné soubory generované Nette (např. zkompilované šablony)
2932
$this->configurator->setTempDirectory($this->rootDir . '/temp');
33+
}
34+
35+
public function bootWebApplication(): Nette\DI\Container
36+
{
37+
$this->initializeEnvironment();
38+
$this->setupContainer();
39+
return $this->configurator->createContainer();
40+
}
41+
42+
private function initializeEnvironment(): void
43+
{
44+
// Nette je chytré a vývojový režim se zapíná automaticky,
45+
// nebo jej můžete povolit pro konkrétní IP adresu odkomentováním následujícího řádku:
46+
// $this->configurator->setDebugMode('[email protected]');
47+
48+
// Aktivuje Tracy: ultimátní "švýcarský nůž" pro ladění.
49+
$this->configurator->enableTracy($this->rootDir . '/log');
50+
51+
// RobotLoader: automaticky načítá všechny třídy ve zvoleném adresáři
3052
$this->configurator->createRobotLoader()
3153
->addDirectory(__DIR__)
3254
->register();
55+
}
56+
57+
private function setupContainer(): void
58+
{
59+
// Načte konfigurační soubory
3360
$this->configurator->addConfig($this->rootDir . '/config/common.neon');
34-
return $configurator;
3561
}
3662
}
3763
```
@@ -40,16 +66,15 @@ class Bootstrap
4066
index.php
4167
=========
4268

43-
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:
69+
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:
4470

4571
```php
46-
// inicializace prostředí + získání objektu Configurator
47-
$configurator = App\Bootstrap::boot();
48-
// vytvoření DI kontejneru
49-
$container = $this->configurator->createContainer();
72+
$bootstrap = new App\Bootstrap;
73+
// Inicializace prostředí + vytvoření DI kontejneru
74+
$container = $bootstrap->bootWebApplication();
5075
// DI kontejner vytvoří objekt Nette\Application\Application
5176
$application = $container->getByType(Nette\Application\Application::class);
52-
// spuštění Nette aplikace
77+
// Spuštění aplikace Nette a zpracování příchozího požadavku
5378
$application->run();
5479
```
5580

@@ -236,13 +261,21 @@ $this->configurator->addServices([
236261
Odlišné prostředí
237262
=================
238263

239-
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.
264+
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.
240265

241266
```php
242-
public static function bootForTests(): Configurator
267+
public function bootTestEnvironment(): Nette\DI\Container
243268
{
244-
$configurator = self::boot();
245269
Tester\Environment::setup(); // inicializace Nette Testeru
246-
return $configurator;
270+
$this->setupContainer();
271+
return $this->configurator->createContainer();
272+
}
273+
274+
public function bootConsoleApplication(): Nette\DI\Container
275+
{
276+
$this->configurator->setDebugMode(false);
277+
$this->initializeEnvironment();
278+
$this->setupContainer();
279+
return $this->configurator->createContainer();
247280
}
248281
```

application/en/bootstrap.texy

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,44 @@ use Nette\Bootstrap\Configurator;
2020

2121
class Bootstrap
2222
{
23-
public static function boot(): Configurator
23+
private Configurator $configurator;
24+
private string $rootDir;
25+
26+
public function __construct()
2427
{
2528
$this->rootDir = dirname(__DIR__);
26-
$configurator = new Configurator;
27-
//$this->configurator->setDebugMode('[email protected]');
28-
$this->configurator->enableTracy($this->rootDir . '/log');
29+
// The configurator is responsible for setting up the application environment and services.
30+
$this->configurator = new Configurator;
31+
// Set the directory for temporary files generated by Nette (e.g. compiled templates)
2932
$this->configurator->setTempDirectory($this->rootDir . '/temp');
33+
}
34+
35+
public function bootWebApplication(): Nette\DI\Container
36+
{
37+
$this->initializeEnvironment();
38+
$this->setupContainer();
39+
return $this->configurator->createContainer();
40+
}
41+
42+
private function initializeEnvironment(): void
43+
{
44+
// Nette is smart, and the development mode turns on automatically,
45+
// or you can enable for a specific IP address it by uncommenting the following line:
46+
// $this->configurator->setDebugMode('[email protected]');
47+
48+
// Enables Tracy: the ultimate "swiss army knife" debugging tool.
49+
$this->configurator->enableTracy($this->rootDir . '/log');
50+
51+
// RobotLoader: autoloads all classes in the given directory
3052
$this->configurator->createRobotLoader()
3153
->addDirectory(__DIR__)
3254
->register();
55+
}
56+
57+
private function setupContainer(): void
58+
{
59+
// Load configuration files
3360
$this->configurator->addConfig($this->rootDir . '/config/common.neon');
34-
return $configurator;
3561
}
3662
}
3763
```
@@ -40,16 +66,15 @@ class Bootstrap
4066
index.php
4167
=========
4268

43-
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:
69+
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:
4470

4571
```php
46-
// initialize the environment + get Configurator object
47-
$configurator = App\Bootstrap::boot();
48-
// create a DI container
49-
$container = $this->configurator->createContainer();
72+
$bootstrap = new App\Bootstrap;
73+
// Initialize the environment + create a DI container
74+
$container = $bootstrap->bootWebApplication();
5075
// DI container creates a Nette\Application\Application object
5176
$application = $container->getByType(Nette\Application\Application::class);
52-
// start Nette application
77+
// Start the Nette application and handle the incoming request
5378
$application->run();
5479
```
5580

@@ -236,13 +261,21 @@ $this->configurator->addServices([
236261
Different Environments
237262
======================
238263

239-
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.
264+
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.
240265

241266
```php
242-
public static function bootForTests(): Configurator
267+
public function bootTestEnvironment(): Nette\DI\Container
243268
{
244-
$configurator = self::boot();
245269
Tester\Environment::setup(); // Nette Tester initialization
246-
return $configurator;
270+
$this->setupContainer();
271+
return $this->configurator->createContainer();
272+
}
273+
274+
public function bootConsoleApplication(): Nette\DI\Container
275+
{
276+
$this->configurator->setDebugMode(false);
277+
$this->initializeEnvironment();
278+
$this->setupContainer();
279+
return $this->configurator->createContainer();
247280
}
248281
```

0 commit comments

Comments
 (0)