The main function of S2ApplicationContext is listed below.
When S2Container is generated using S2ApplicationContext, the component definition by DICON file is not needed. When searching a class from a file system and a DICON file (.dicon) is found, it reads automatically it. A DICON file can be used as a configuration file which packed the component used frequently.
Import of a class is performed by the following method.
S2ApplicationContext::import($path, $namespace = array(), $strict = false, $pear = false, $recursive = true)
- 1st argument : path to direcotry searched
- 2nd argument : string or array of namespace
- 3rd argument : In the case of true, The namespace specified by $namespace is used. In the case of false, The searched subdirectory name is added to $namespace array one by one.
- 4th argument : In the case of true, $namespace is imploded by "_" (underscore). In the case of false, $namespace is imploded by "\"
- 5th argument : In the case of true, a directory is searched recursively. In the case of false, sub directories are not searched.
The class file (.php, .class.php) and DICON file (.dicon) in the directory specified by the 1st argument are imported. The path of a class name and a class file is acquired in import. This information is used when a class definition is required in autoload function. A class name becomes a portion except the extension of a class file name. As an example, when a class file name is "S2Container.php" and "S2Container.class.php", "S2Container" becomes a class name. The s2import function is also defined as shortcut to the S2ApplicationContext::import method.
Generation of S2Container instance is performed by the following method.
S2ApplicationContext::create($namespaces = array())
Generated S2Container has all the classes as a component which are imported by the import method. If the import method has not performed, S2Container instance without any component(empty container) is returned. When a DICON file is imported by the import method, S2Container is generated using the DICON file, and it is included as a child container.
If a namespace argument is passed, the S2Container instance with the component contained in specified namespace is returned. As an example, S2Container is generable as follows, if there is Foo class in global namespace (means without namespace) and Bar class in "example" namespace.
Foo class is created to path/to/classes/Foo.php.
<?php /** * @S2Component('name' => 'foo') */ class Foo {}Bar class is created to /path/to/classes/Bar.php.
<?php /** * @S2Component('name' => 'bar', 'namespace' => 'example') */ class Bar {}An execution script be as follows.
<?php require_once('S2Container/S2Container.php'); seasar\container\S2ApplicationContext::import('/path/to/classes'); // genareting global s2container $globalContainer = seasar\container\S2ApplicationContext::create(); $foo = $globalContainer->getComponent('Foo'); $bar = $globalContainer->getComponent('example.Bar'); // genarating example namespaced s2container $exampleContainer = seasar\container\S2ApplicationContext::create('example'); $foo = $exampleContainer->getComponent('Foo'); // exception is throwed because Foo component does not exists. $bar = $exampleContainer->getComponent('Bar');
Caution | |
---|---|
There is no relation in namespace which manages a child container by S2Container, and namespace of PHP. |
You can choose a part from the imported class or DICON file, and can generate S2Container.
setIncludePattern method definition.
S2ApplicationContext::setIncludePattern($pattern)The regular expression of an argument is used with a preg_match function. If you want to include the class which the name has ended by "Bean", an argument value is "/Bean$/". setIncludePattern method overwrites current patterns.
- The 1st argument : A regular expression string
addIncludePattern method definition.
S2ApplicationContext::addIncludePattern($pattern)addIncludePattern method adds a pattern to current patterns.
- The 1st argument : A regular expression string
setExcludePattern method definition.
S2ApplicationContext::setExcludePattern($pattern)The regular expression of an argument is used with a preg_match function. If you want to exclude the class which the name has started by "Abstract", an argument value is "/^Abstract/". setExcludePattern method overwrites current patterns.
- The 1st argument : A regular expression string
addExcludePattern method definition.
S2ApplicationContext::addEcludePattern($pattern)addExcludePattern method adds a pattern to current patterns.
- The 1st argument : A regular expression string
By a register method, a component can be registered individually.
S2ApplicationContext::register($info)
- The 1nd argument : class name | ReflectionClass | ComponentInfoDef instance
- return : ComponentInfoDef instance
It is as follows when registering a Service class as a component manually.
S2ApplicationContext::register('Service');
The s2component function is defined as shortcut to a register method. The usage of the s2component function is the same as a register method.
s2component('Service');
By ComponentInfoDef (the return value of the s2component function), you are able to add other component setup.
s2component('Service') ->setName('indexService') ->setInstance('singleton') ->setAutoBinding('auto') ->setNamespace('example') ->setConstructClosure(function(){return new Service;});
Caution | |
---|---|
A setup by annotation is not reflected in manual registration of a component. Moreover, autoload is not executed, either. |
An aspect is automatically applied to the component included by selection of the component.
S2ApplicationContext::registerAspect($interceptor, $componentPattern, $pointcut = null)
- The 1nd argument : Interceptor component name, Closure, or Expression
- The 2st argument : The regular expression string which carries out a pattern match at a component name or a component class name.
- The 3rd argument : Pointcut is specified by a regular expression character string.
- return : AspectInfoDef instance.
It sets up as follows to carry out the aspect of the "dao.interceptor" to the component which the component class name has ended by "Dao".
S2ApplicationContext::registerAspect('dao.interceptor', '/Dao$/');
The s2aspect function is defined as shortcut to a registerAspect method. The usage of the s2apsect function is the same as a registerAspect method.
s2aspect('dao.interceptor', '/Dao$/');
By AspectInfoDef (the return value of the s2aspet function), you are able to add other aspect setup.
s2aspect('dao.interceptor') ->setPattern('/Dao$/') ->setPointcut('/^find/');
In the following example, Closure is set as an interceptor. The MethodInvocation instance is passed to the first argument of Closure.
s2aspect() ->setPattern('/Service$/') ->setPointcut('/^add/') ->setInterceptor(function($invoker) { return $invoker->proceed() * 1.05; });
Caution | |
---|---|
An automatic aspect is applied only to the imported component. It is not applied to the component contained in the imported DICON file. |
Annotation is used when performing a detailed setup of a component, such as Dependency Injection setup or Aspect setup. The form of annotation is "@annotation name[()]". It starts by "@". An end is a space or a new-line. You can pass an argument to the annotation. The form of an argument is the same format as PHP Array.
Method annotation of a parent class.
By the default, method annotation of a parent class is disabled. You can set up it by the following method.
S2ApplicationContext::setReadParentAnnotation($val = true)
- The 1st argument : boolean, if setting true, method annotation of a parent class is enabled.
@S2Component annotation.
@S2Component annotation sets up component information.
- Notation of annotation : @S2Component
- Arguments
- name : name of component
- instance : Instance Type
- autoBinding : Binding Type
- available : uses as component or not (boolean default true)
- namespace : namespace used in S2Container
- Annotation point : class
- Example
/** * @S2Component('name' => 'hoge') */ class Hoge{} /** * @S2Component('name' => 'huga', * 'instance' => 'singleton', * 'autoBinding' => 'auto', * 'available' => true, * 'namespace' => 'example.service') */ class Huga{}
@S2Binding annotation.
@S2Binding annotation performs a manual injection setup.
- Notation of annotation : @S2Binding
- Arguments : The component name or PHP Expression
- Annotation point : pulbic property, setter method
- Example
class IndexAction { /** * @S2Binding('new HogeService') */ public $service; /** * @S2Binding('Hello World') */ public funcion setHello($val) { $this->hello = $val; } }
@S2Aspect annotation sets up aspect information.
- Notation of annotation : @S2Aspect
- Arguments
- interceptor : Interceptor component name, or PHP Expression
- pointcut : a pointcut is specified by a regular expression character string.
- Annotation point : class, method
- Example
/** * @S2Aspect('interceptor' => 'traceInterceptor', * 'pointcut' => '/.+Action/') */ class CdController { public function indexAction(){ . . . } } class DvdController { /** * @S2Aspect('interceptor' => 'new seasar\aop\interceptor\TraceInterceptor()') */ public funcion indexAction() { . . . } }
@S2Meta annotation.
@S2Meta annotation sets up meta information.
- Notation of annotation : @S2Meta
- Arguments
- name : The discernment name of meta-information
- value : value or PHP Expression
- Annotation point : class
- Example
/** * @S2Meta('nameA' => 'valueA', * 'nameB' => 'valueB') */ class CdController { public function indexAction(){ ・・・ } }
S2ApplicationContext generates and manages Singleton S2Container in a namespace unit. Singleton S2Container are held at S2ApplicationContext::$SINGLETON_CONTAINERS. Access to Singleton S2Container is performed by the following method.
get method (getComponent method).
S2ApplicationContext::get($key, $namespaces = array()) S2ApplicationContext::getComponent($key, $namespaces = array())
- The 1st argument : name of component
- the 2nd argument : namespace of S2Container
The get method is Alias of getComponent method. The component specified by the 1st argument is taken out. If Singleton S2Container of namespace specified by the 2nd argument has not been created, S2Continer is generated using S2ApplicationContext::create method. The s2get function is also defined as shortcut to the S2ApplicationContext::get method.
getComponentDef method.
S2ApplicationContext::getComponentDef($key, $namespaces = array())
- The 1st argument : name of component
- the 2nd argument : namespace of S2Container
The component definition specified by the 1st argument is taken out. If Singleton S2Container of namespace specified by the 2nd argument has not been created, S2Continer is generated using S2ApplicationContext::create method.
As an example, Foo class is in global namespace (without a namespace), and when Bar class is example namesace, each component can be taken out as follows.
Foo class is created to /path/to/classes/Foo.php.
<?php /** * @S2Component('name' => 'foo') */ class Foo {}Bar class is created to /path/to/classes/Bar.php.
<?php /** * @S2Component('name' => 'bar', 'namespace' => 'example') */ class Bar {}An execution script becomes the next.
<?php require_once('S2Container/S2Container.php'); use seasar\container\S2ApplicationContext as s2app; s2app::import('/path/to/classes'); // using global Singleton S2Container $globalFoo = s2app::get('Foo'); $globalBar = s2app::get('example.Bar'); // using example namespace Singleton S2Container $exampleFoo = s2app::get('Foo', 'example'); // Since a component does not exist, an exception is thrown. $exampleBar = s2app::get('Bar', 'example'); var_dump($globalBar === $exampleBar); // be false
Caution | |
---|---|
When a global Singleton container is generated, the container of example namespace is also generated, and it is held as a child container, but it is an instance different from the singleton container of example namespace. |
You can initialize information set with Import of Component, Selection of a component, Auto Aspect and Singleton Container by the init method of S2ApplicationContext.
S2ApplicationContext::init()
The s2init function is also defined as shortcut to the S2ApplicationContext::init method.
© Copyright The Seasar Foundation and the others 2005-2010, all rights reserved. |