Loader (Bootstrap.php)
Class Bootstrap performs the following methods:
- Creating an application
__construct()
// Create app
$app = new Silex\Application();
$this->app = $app;
- Creating handlers middleware
_iniMiddlewares
- Configuration application
_iniConfig
- Initialization providers
_iniProviders
- Initialization services
_iniServices
и_extendServices
- Initialization controllers
_iniControllers
- Initialization application
_iniApplication
Creating handlers middleware
Create an event handler at different points of the request processing cycle.
- The middleware is run before the routing and the security:
$app->before(function (Request $request, Application $app) {
// ...
}, Application::EARLY_EVENT);
- The middleware is run after the routing and the security, but before the controller is executed:
$app->before(function (Request $request, Application $app) {
// ...
});
- An after application middleware allows you to tweak the Response before it is sent to the client:
$app->after(function (Request $request, Response $response) {
// ...
});
- A finish application middleware allows you to execute tasks after the Response has been sent to the client (like sending emails or logging):
$app->finish(function (Request $request, Response $response) {
// ...
// Warning: modifications to the Request or Response will be ignored
});
Configuration application
This defines the type of configuration:
// Set type configuration
$app['yml_config'] = FALSE;
The configuration is set using yml
file or using ini
file.
There are set values of the main parameters of your configuration that
can be used in your other configurations.
The parameter values can be accessed through $app['config']['parameters']
.
The values of the main parameters:
- Time zone (timezone)
- Language localization (locale)
- The color scheme of the site (scheme)
- The sign debug (debug)
- Environment (environment)
- Database Options (db.driver, db.path, db.name, db.models.path, db.models.namespace, db.connection.production, db.connection.development, db.connection.test)
- Email settings (mail.host, mail.port, mail.username, mail.password)
- PagerFanta parameters (pagerfanta.max_per_page, pagerfanta.proximity)
Initialization providers
Initialization providers also depends on the type of configuration.
Else $app['yml_config'] = TRUE;
then providers are set such as shown below.
if ($app['yml_config']) {
foreach ($app['config']['service_providers'] as $serviceProviderConfig) {
$app->register(
new $serviceProviderConfig['class'](
(!isset($serviceProviderConfig['construct_parameters'])) ?
null : $serviceProviderConfig['construct_parameters']
), (isset($serviceProviderConfig['parameters']) &&
null !== $serviceProviderConfig['parameters']) ?
$serviceProviderConfig['parameters'] : array()
);
}
}
Otherwise providers are set manually.
...
// Monolog
$app->register(new Silex\Provider\MonologServiceProvider(), array(
'monolog.logfile' => "{$app['config']['log_path']}/application.log",
'monolog.name' => 'APPLICATION'
));
...
Initialization services
With class MyService app/Services/MyService.php
installed services app/Services/My
.
This includes the following services:
- Working with arrays (
ArrayBox.php
) - Working with XML (
CrXml.php
) - Working with HTTP (
Http.php
) - Work with parameters (
ParamBox.php
) - Working with strings (
String.php
) - Common Functions (
System.php
)
With class Zf2Service app/Services/Zf2Service.php
installed services from
Zend Framework 2 vendor/zendframework
.
This includes the following services:
With metod _extendServices
of class Bootstrap loader app/Console/Bootstrap.php
extends the functionality of some services such as: $app['translator']
, $app['twig']
.
Initialization controllers
Creating and initializing the controllers takes place via a method _ iniControllers.
/**
* Initialization controllers
*
* @param Application $app
*/
private function _iniControllers(Application $app) {
...
// Load controllers
foreach ($app['config']['controllers'] as $controllerServiceName => $controllerClass) {
$app[$controllerServiceName] = $app->share(function () use ($app, $controllerClass) {
return new $controllerClass($app);
});
$controller = $app[$controllerServiceName];
}
}
List controllers classes stored in a file app/Resources/Config/parameters.yml
.
Initialization application
In this method, you create a directorys necessary for the functioning of the web application. This is the first time the console is started.
// Create application paths
$app['my']->get('config')->createAppPaths();