Let's install S2Container.php by PEAR. If installing with a full package, please unpack it as S2Container to the lib directory of a project directory. Next, create the following sample configuration file as s2.php to the config directory of a project directory.
<?php require_once('S2Container/S2Container.php'); /** S2Log setup */ seasar\Config::$LOG_LEVEL = seasar\log\impl\SimpleLogger::DEBUG; seasar\Config::$DEBUG_EVAL = false; seasar\Config::$DEBUG_VERBOSE = false; seasar\Config::$SIMPLE_LOG_FILE = SF_ROOT_DIR . '/log/s2.log'; /** S2Aop setup */ seasar\aop\Config::$CACHING = false; seasar\aop\Config::$CACHE_DIR = SF_ROOT_DIR . '/cache/s2aop';
actions.class.php generated at the time of modular creation is edited as follows.
<?php require_once(SF_ROOT_DIR . '/config/s2.php'); use seasar\container\S2ApplicationContext as s2app; s2app::import(dirname(dirname(__FILE__)) . '/lib/logic'); class defaultActions extends sfActions { public function executeIndex() { $this->greeting = s2app::get('Hoge')->greeting(); } }
Hoge class used in an action class is created in lib/logic directory of a module directory.
<?php class Hoge { public function greeting() { return 'Hello World !!'; } }
As follows, edit the template file of index action.
greeting : <?php echo $greeting; ?> <br>
If index page is accessed, it will be displayed as "greeting : Hello World !!".
As an example, MockInterceptor is applied to Hoge class, and the return value of greeting method is specified.
<?php class Hoge { /** * @S2Aspect('interceptor' => 'new seasar\aop\interceptor\MockInterceptor') * @S2Mock('return' => 'strval("Bye World !!")') */ public function greeting() { return 'Hello World !!'; } }
If index page is accessed, it will be displayed as "greeting : Bye World !!".
@S2Aspect annotation can also annotate on a class. Moreover, the same result can be obtained even if it uses the automatic aspect function of S2ApplicationContext. If using an automatic aspect function, registerAspect method is added to S2ApplicationContext setting part of an action class.
<?php require_once(SF_ROOT_DIR . '/config/s2.php'); use seasar\container\S2ApplicationContext as s2app; s2app::import(dirname(dirname(__FILE__)) . '/lib/logic'); s2app::registerAspect('new seasar\aop\interceptor\MockInterceptor', '/^Hoge$/'); class defaultActions extends sfActions { public function executeIndex() { $this->greeting = s2app::get('Hoge')->greeting(); } }
© Copyright The Seasar Foundation and the others 2005-2010, all rights reserved. |