DI ( Dependency Injection: Setter, Constructor, Method), AOP ( Aspect Oriented Programming ), Events support, xml, yaml, and annotations (including some JSR 250 and JSR 330, like @Configuration and @Bean ala java configuration) , lightweight, simple, and quick MVC ( Model View Controller ), syslog, tcp client and server, with non blocking sockets, custom error, signal, and exception handling through events. Needs PHP 5.3, very similar to seasar, spring ( java ) . Can be deployed as a PHAR file.
APACHE-2.0 License
Please contact me if you have any comments, doubts, or any kind of feedback. Send an email to: [email protected]
Checkout the api (phpdoc) at: http://marcelog.github.com/Ding/php-doc/html/index.html
In the homepage, you can find the user manual: http://marcelog.github.com/Ding
Ding is also the winner of the 2011 Binpress programming contest :)
See these articles for a complete application example:
Supported Dependency Injection annotations:
You can now easily install Ding by issuing:
or
just replace 1.6.3 by the release version you'd like to install :) See: http://pear.marcelog.name/
Remember that using annotations is completely optional. Not using them will benefit performance. Annotations CAN be cached. Use the 'annotations' cache with any of the implementations below ;)
See these: http://marcelog.github.com/articles/ding_component_bean_annotations.html http://marcelog.github.com/articles/ding_component_bean_annotations_di_dependency_injection.html
Ding supports this cache implementations out of the box.
IContainerAware: Whenever ding is going to instantiate a bean whose class implements IContainerAware interface, it will inject the container instace to this bean.
IBeanNameAware: Whenever ding is going to instantiate a bean whose class implements IBeanNameAware interface, it will inject the bean name.
IAspectManagerAware: Whenever ding is going to instantiate a bean whose class implements IAspectManagerAware interface, it will inject the current instance of the aspect manager (giving the ability to inject pointcuts, aspects, and general aop management).
IResourceLoaderAware: Whenever ding is going to instantiate a bean whose class implements IResourceLoaderAware interface, it will inject the instance of the resource loader in use, which is usually the container itself.
ILoggerAware: Whenever ding is going to instantiate a bean whose class implements ILoggerAware interface, it will inject the instance of the logger (log4php) in use by the container, so you can log there. The returned logger will be of class CLASS (where \ are replaced with .).
IReflectionFactoryAware: Whenever ding is going to instantiate a bean whose class implements IReflectionFactoryAware interface, it will inject the instance of the reflection factory in use.
You can easily hook in the bean lifecycle by just implementing one of:
If you want to provider beans yourself, just have a bean implement the Ding\Bean\IBeanDefinitionProvider interface, it will automatically be registered in the container and will be called whenever a new bean definition is demanded.
If you want to provide aspects and pointcuts, just implement one or all of:
The container natively supports events. This means that you can register beans (either programatically or via the xml, yaml, or annotations drivers) and then asynchronously trigger (dispatch) these events along some data object. The container will then instantiate the needed beans (the listeners) and notify them. Please see docs/examples/events for an example using xml, yaml, and annotations.
Aspects work via an implementation of the interceptor pattern. There are 2 available interceptors, both of them available in xml, yaml, and annotation drivers.
Method: Use this one to be called before every method execution (use proceed() to continue the chained execution), resuming your own afterwards.
Exception: Use this one to be called when an aspected method throws an exception.
Can apply aspects to parent classes
The mvc is work in progress, but you can sneak peek it in docs/examples/mvc. You will need a rewrite rule, like (for lighttpd):
url.rewrite-if-not-file = ( ".?(.)$" => "/example.php?$1", "" => "/example.php" )
For Apache: RewriteRule ^(.*)$ /example.php/$1
The example is mapped to /Some/Mapped/Path/MyController/some (hint: try it with ?optional=value). If you press the button, the form will be submitted and handled by another action that will print the same data, but now for the post instead of the initial get.
Also, if you want to trigger an exception to see the exception handler in action, point your browser to: /Some/Mapped/Path/MyController/someException
To try a redirect (no view render, just headers sent), point your browser to /Some/Mapped/Path/MyController/redirect This uses an http header 302.
To try an internal redirect (forward), point your browser to /Some/Mapped/Path/MyController/forward. This will internally forward the request to another controller/action.
To try annotated controllers (see below), point your browser to /Some/Mapped/Path/MyAnnotatedController/an.
You can use @Controller and @RequestMapping in your classes and not declare them in beans.xml file. see docs/examples/mvc/annotatedController
You can also use Twig to render the views. see docs/examples/mvc-twig You can also use Smarty to render the views. see docs/examples/mvc-smarty
You can hook before dispatching a request to a controller and also after. A dispatcher will chain all preHandle() and postHandle() from all the IHandlerInterceptor you configure.
A preHandle() interceptor returning true will continue the chain execution. To stop the chained execution, false can be returned, and also a ModelAndView (thus, triggering redirects, view renders, etc).
You can use the helper TCPClientHelper as just another bean. Via callbacks and combining the helpers for error and signal handling, you can easily create a very complete tcp client, using non blocking sockets. Available callbacks:
You can specify the connection timeout, the read timeout (both in milliseconds) and the minimum needed bytes in the socket before considering it as available data to be read.
see docs/examples/tcp for an example of use.
You can use the helper TCPServerHelper as just another bean. Via callbacks and combining the helpers for error and signal handling, you can easily create a very complete tcp server, using non blocking sockets. Available callbacks:
You can specify the connection timeout, the read timeout (both in milliseconds) and the minimum needed bytes in the socket before considering it as available data to be read.
This helper integrates PAMI (https://github.com/marcelog/PAMI) with Ding, offering a quick, easy, and effective way to access and manager asterisk installations via the asterisk manager interface (ami).
This helper integrates PAGI (https://github.com/marcelog/PAGI) with Ding, offering a quick, easy, and effective way to make agi (Asterisk Gateway Interface) applications (PrePaid systems, voicemail's, etc).
See: docs/examples/pagi
In order to run the example, you need something like this in your dialplan:
[default] exten => _X,1,AGI(/tmp/Ding/docs/examples/pagi/run.sh,a,b,c,d) exten => _X,n,Hangup
You need log4php (http://logging.apache.org/log4php/). Just make sure you copy it to the include_path and Ding will pick it up from there.