phpBB

Development Wiki

Difference between revisions of "Dependency Injection Container"

From phpBB Development Wiki

(DIC documentation.)
 
(Extensions)
 
(2 intermediate revisions by 2 users not shown)
Line 15: Line 15:
 
== Extensions ==
 
== Extensions ==
  
phpBB extensions are also able to define services. This is important if they want to provide cron tasks or any other service. By creating a config/services.yml file within the extension directory, that file will automatically get loaded into the container.
+
phpBB extensions are also able to define services. This is important if they want to provide cron tasks or any other service. By creating a config/services.yml file within the extension directory, that file will automatically get loaded into the container. See the page on [[Dependency list|dependencies]] for more information.
  
== Tags ==
+
== Example usage (PHP) ==
 +
To access services, use get(service name), to access variables, use getParameter(parameter name)<br />
 +
See common.php and config/*.yml for more examples of usage.<br />
  
The container allows attaching tags to services. These tags are used to get a collection of certain types of services and then using that collection. The following tags are currently supported:
+
:$request = $phpbb_container->get('request');<br />
 
+
:$user = $phpbb_container->get('user');<br />
* '''cron.task:''' Cron task, which is automatically executed when it is ready to be run.
+
:$auth = $phpbb_container->get('auth');<br />
 +
:$db = $phpbb_container->get('dbal.conn');<br />
 +
:$template = $phpbb_container->get('template');<br />
 +
:<br />
 +
:$phpbb_root_path = $phpbb_container->getParameter('core.root_path');<br />
 +
:$php_ext= $phpbb_container->getParameter('core.php_ext');<br />
  
 
== Documentation ==
 
== Documentation ==

Latest revision as of 20:06, 5 February 2015

What is it?

A dependency injection container is an object that manages services and their creation. A service is an object that does something; it provides a service. Services can depend on other services, and the container has the job of wiring them up.

You can think of the container as "an array of objects on steroids". It knows about a number of objects and allows you to retrieve them. It will only create the objects you ask for, so it is lazy in a sense.

phpBB uses the Symfony2 Dependency Injection component.

Configuration

In order to create them, the container needs to know about how services should be created. You do that by configuring it. phpBB uses a YAML configuration format to do this. All configuration files are within the config directory.

The entry point is services.yml. It imports some other files and then defines the top level services. Modifications to phpBB's core will likely involve changes to one of the service configuration files.

Extensions

phpBB extensions are also able to define services. This is important if they want to provide cron tasks or any other service. By creating a config/services.yml file within the extension directory, that file will automatically get loaded into the container. See the page on dependencies for more information.

Example usage (PHP)

To access services, use get(service name), to access variables, use getParameter(parameter name)
See common.php and config/*.yml for more examples of usage.

$request = $phpbb_container->get('request');
$user = $phpbb_container->get('user');
$auth = $phpbb_container->get('auth');
$db = $phpbb_container->get('dbal.conn');
$template = $phpbb_container->get('template');

$phpbb_root_path = $phpbb_container->getParameter('core.root_path');
$php_ext= $phpbb_container->getParameter('core.php_ext');

Documentation

For further reading, check out the following resources: