Installing and using the Ding DI ContainerTweet
Your Aspect Oriented Programming and Dependency Injection container for PHP
Ding is DI (Dependency Injection) container, featuring IoC (Inversion of Control), and AOP ( Aspect Orientend Programming) for PHP 5.3 (actually, it needs >= 5.3.3). It also supports some Event Driven programming too. It is based on The Spring Framework, and also has some of the features of the JSR-250 and the JSR-330.
Installing the container
First, a couple of useful links:
- http://marcelog.github.com/Ding/: The homepage, where you can find the user manual.
- http://pear.marcelog.name/: The PEAR channel
- http://packagist.org/packages/marcelog/ding: Packagist home
- https://github.com/marcelog/Ding/: The GitHub repository
- Full Ding examples
- http://ci.marcelog.name:8080/job/Ding/lastSuccessfulBuild/artifact/build/: The CI server, containing the latest successful built artifacts, phar and pear packages.
- http://pear.apache.org/log4php/: The log4php pear channel.
A brief parenthesis: Installing log4php (only needed if installing manually or using the phar)
Unless you are installing Ding via the pear channel, you need to install a dependency first. Ding uses log4php as its logging system, so let's install log4php via the pear channel:
Please also see the official installation guide of log4php.
Make sure the directory "log4php" inside the log4php distribution is in your include path when using Ding. If you get this error:
PHP Fatal error: Class 'Logger' not found
You need to include the logphp subdirectory correctly in your include_path.
You can download and install Ding in a number of ways, let's first examine them and then see how to bootstrap the container:
Option A: Installing and using the PHAR distribution
You can get the latest phar and pear package from the CI server. At this time, the latest version is 1.6.3, so the available artifact is named "Ding-1.6.3.phar". Let's fetch it:
Let's see how to use it:
That's it. You can now bootstrap the container and do your thing (see below).
Option B: Installing the PEAR distribution manually
As stated before, you can get the pear package at the CI server. You can download it and install it manually (again, 1.6.3 is the latest version at the time of this writing):
Option C: Installing via the PEAR Channel
A pear channel is available here. If you want to use it, just:
Either way, to use it, make sure log4php is in your include path. Ding will already be, since it's installed via pear:
That's it. You can now instantiate the container.
Option D: Getting the source, installing from github
You can download the ZIP file from github, and also, you can clone the repository:
When installed this way, you need to make sure you set the include path into the /src/mg subdirectory inside the root tree, so the PSR-0 autoloader would load from the src/mg/Ding subdirectory.
Option E: Use composer
Just add the package "marcelog/ding":
Packagist URL: http://packagist.org/packages/marcelog/ding
Instantiating the container
To instantiate the container, you need to pass a set of configuration options:
The complete list of configuration options is here. Let's try some basic stuff first. We will configure the 3 drivers for bean definitions (you need at least 1, and there are up to 3 drivers available). Each driver will provide bean definitions from a specific source and you can mix them as you like:
- Annotation: Will scan the given set of directories for annotated classes that define components (beans).
- Xml: Given a set of directories and a set of filenames to look for, will read the bean definitions from them.
- Yaml: Same as Xml but with Yaml files.
The 'bdef' (from Bean Definition) section of the configuration will configure the drivers:
Getting beans from the container for your PHP application
That's it! Here's how to get beans from the container:
If you happen to run the code above, you will notice that log4php is outputing to the console. This is because it's probably not configured. You can configure log4php on your own (see this) or have ding do it on its own, by passing an option to the container with the log4php configuration file you want to use (in this case, an .ini file, but you can use xml):
A sample log4php.properties ini file:
Everything's now ready to start using AOP, DI, and Events in your PHP Applications
Enough to get you started. As you can see there are many different ways to get you started using Ding. In another article, I'll talk about the bean definitions themselves, what are they, and what can they do exactly for your benefit (this is quite a long article already!). Cheers!