5.11. Using S2Container with Symfony

5.11.1. Startup of Symfony

Please create Symfony projects, application, and a module.

5.11.2. S2Container Setup

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.

  • config/s2.php
<?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';

5.11.3. Creation of action

actions.class.php generated at the time of modular creation is edited as follows.

  • module/actions/actions.class.php
<?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();
    }
}
  1. s2.php common setting file is read
  2. An alias defines S2ApplicationContext using use (not required)
  3. Import Logic directory
  4. Hoge component is used in an action method. If taking out a component by a get method, Singleton S2Container managed inside S2ApplicationContext is used.

Hoge class used in an action class is created in lib/logic directory of a module directory.

  • module/lib/logic/Hoge.php
<?php
class Hoge {
    public function greeting() {
        return 'Hello World !!';
    }
}

As follows, edit the template file of index action.

  • module/template/indexSuccess.php
greeting : <?php echo $greeting; ?> <br>

If index page is accessed, it will be displayed as "greeting : Hello World !!".

5.11.4. Using Mock

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.