Services can be obtained using a service reference or the container.
The following example demonstrates how services can be obtain using references:
<?php use function ICanBoogie\Service\ref; $reference = ref('a_callable_service'); $result = $reference(1, 2, 3); $reference = ref('a_service'); $service = $reference->resolve(); $service->do_something();
The following example demonstrates how a service can be obtained using the container itself:
<?php /* @var $app \ICanBoogie\Application */ /* @var $container \Symfony\Component\DependencyInjection\Container */ $container = $app->container; $service = $container->get('a_service'); $service->do_something();
Obtaining services bound to the application
Usually, ICanBoogie's components add getters to
ICanBoogie\Application instances through the
prototype system, which means you can access the initial request using
or the session using
$app->session. Services defined this way are automatically accessible through
the container as well, which means they can be used as references
ref('session') or obtained
through the container
Obtaining config parameters
All application config parameters are available as container parameters e.g.
Services are defined using
services.yml files in
config folders. They are collected when it's
time to create the container, just like regular configuration files.
The tests included in this package showcase how
services.yml files can be defined in
default/config. Components and modules can use this feature to register their own services and
make them available to the application automatically.
About the container proxy
The following example demonstrates how the service provider and the service container can be obtained:
<?php use ICanBoogie\Service\ServiceProvider; /* @var $proxy \ICanBoogie\Binding\SymfonyDependencyInjection\ContainerProxy */ $proxy = ServiceProvider::defined(); $container = $proxy->container;
Configuring the container
The container is configured using
container configuration fragments:
<?php // config/container.php namespace ICanBoogie\Binding\SymfonyDependencyInjection; return [ ContainerConfig::USE_CACHING => false, ContainerConfig::EXTENSIONS => [ [ Extension\ApplicationExtension::class, 'from' ] ] ];
Defining container extensions
Container extensions are defined using
EXTENSIONS. Use an array of key/value pairs where key
is an optional identifier and value a callable that constructs the extension.
The package requires PHP 5.6 or later.
The recommended way to install this package is through Composer:
$ composer require icanboogie/bind-symfony-dependency-injection
Cloning the repository
The package is available on GitHub, its repository can be cloned with the following command line:
$ git clone https://github.com/ICanBoogie/bind-symfony-dependency-injection.git
The package is documented as part of the ICanBoogie framework documentation. You can
generate the documentation for the package and its dependencies with the
make doc command. The
documentation is generated in the
build/docs directory. ApiGen is required.
The directory can later be cleaned with the
make clean command.
The test suite is ran with the
make test command. PHPUnit and
Composer need to be globally available to run the suite. The command
installs dependencies as required. The
make test-coverage command runs test suite and also creates
an HTML coverage report in
build/coverage. The directory can later be cleaned with the
make clean command.
The package is continuously tested by Travis CI.
icanboogie/bind-symfony-dependency-injection is licensed under the New BSD License - See the *LICENSE file for details.