4.4. Aspect using S2Container

4.4.1. Using S2ApplicationContext

When Applying aspect to a component using S2ApplicationContext, there are two methods, setting up an automatic aspect or setting up comment annotation in each class.

Automatic Aspect. 

If using the automatic aspect function of S2ApplictionContext, an aspect is set up by an S2ApplictionContext::registerAspect method.
As an example, you can apply aspect to the following Service class.

class Service {
    public function add($a, $b) {
        return $a + $b;
    }
}

Service class is imported by S2ApplicationContext::import method. A container is generated by S2ApplicationContext::create method and Service component is taken out by a getComponent method.

<?php
require_once('S2Container/S2Container.php');
seasar\container\S2ApplicationContext::import(dirname(__FILE__) . '/classes');
seasar\container\S2ApplicationContext::registerAspect('new seasar\aop\interceptor\TraceInterceptor', '/^Service$/', '^add$');
$container = seasar\container\S2ApplicationContext::create();
$service   = $container->getComponent('Service');

print get_class($service) . PHP_EOL;
$result = $service->add(2, 3);

You can specify the component name to which an aspect is applied, Interceptor and Pointcut by the argument of an S2ApplicationContext::registerAspect method.


Setup by Comment Annotation. 

@S2Aspect annotation is used when setting up an aspect by comment annotation. @S2Aspect annotation can be described in a class or a method.

As an example, it is as follows when describing annotation in a service class.

/**
 * @S2Aspect('interceptor' => 'new seasar\aop\interceptor\TraceInterceptor',
 *           'pointcut'    => '^add$')
 */
class Service {
    public function add($a, $b) {
        return $a + $b;
    }
}

It is as follows when describing annotation in a method. "pointcut" is omissible.

class Service {
    /**
     * @S2Aspect('interceptor' => 'new seasar\aop\interceptor\TraceInterceptor')
     */
    public function add($a, $b) {
        return $a + $b;
    }
}

Service class is imported by the S2ApplicationContext::import method. A container is generated by the S2ApplicationContext::create method, and Service component is taken out by the getComponent method from the container.

<?php
require_once('S2Container/S2Container.php');
seasar\container\S2ApplicationContext::import(dirname(__FILE__) . '/classes');
$container = seasar\container\S2ApplicationContext::create();
$service   = $container->getComponent('Service');

print get_class($service) . PHP_EOL;
$result = $service->add(2, 3);

4.4.2. Using S2ContainerFactory

If using S2ContainerFactory, aspect setting is performed using a aspect tag in DICON file which is a definition file of S2Container.
An aspect is applied to service class as an example.

class Service {
    public function add($a, $b) {
        return $a + $b;
    }
}

An aspect tag is added to a component tag. The name of Interceptor component is described by a body of a aspect tag.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN"
"http://www.seasar.org/dtd/components21.dtd">
<components>
    <component name="trace" class="seasar\aop\interceptor::TraceInterceptor"/>
    <component class="Service">
        <aspect pointcut="^add$">trace</aspect>
    </component>
</components>

A container is generated using the above DICON file by the create method of S2ContainerFactory.

<?php
require_once('S2Container/S2Container.php');
seasar\util\ClassLoader::import(dirname(__FILE__) . '/classes');
$container = seasar\container\factory\S2ContainerFactory::create('/path/to/diconファイル');
$service   = $container->getComponent('Service');

print get_class($service) . PHP_EOL;
$result = $service->add(2, 3);


© Copyright The Seasar Foundation and the others 2005-2010, all rights reserved.