SiG Staff Blog

福井と金沢にある株式会社SIG 総合研究所で働きたい方、ご連絡ください。

指示通りSkeletonを使ってサンプルサイト作ったけど、なんかワーニング出るやん!

Zend Framework2で急にワーニングがでた!何もしてないのになんで!?

指示通り作ったのにワーニングがでてる!

Zend Framework2に書いてある通り、 Skeltonを入れて、 サービスの登録とかを諸々したら画面が出る(出てた)んだけど

ある日、

Deprecated: You are retrieving the service locator from within the class Application\Controller\IndexController. Please be aware that ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. You will need to update your class to accept all dependencies at creation, either via constructor arguments or setters, and use a factory to perform the injections.

って文字が画面の上に出てる。 前までちゃんと動いていたのになんで???

Zendは3.0に向けて動いてるらしい

3.0からはZendもDIっぽくやっていくよ!って事らしく。

ServiceとかでServiceLocatorを使う時に、$this->serviceLocator の様に呼びださずに Module.phpのgetServiceConfigメソッドでServiceのクラス変数に値を入れるようにしなさい。 って事らしい。

$this->serviceLocatorみたいに呼びだせないようにするからね!って事だそうで。

いきなりDeprecated出るようなアップデートしないでYO

で、AwareInterfaceってなんぞ?

ControllerとかにLoggerを持っているか、ServiceManagerを表現するインターフェースのようで、

LoggerAwareInterfaceを持ってたらsetLogger()メソッドを持たすことを強要できるので、 Controllerクラスを作成するタイミングで、クラス変数とかに$loggerとかにセットしてあげればloggerが使えるという感じです。

ServiceManagerAwareInterfaceだったら、setServiceManager()メソッドをつけれるので、Controllerにサービスマネージャをセットできます。

という事らしい。

で、やってみた。

各コントローラのクラスにLoggerAwareInterface, ServiceManagerAwareInterfaceをimplementsする。

abstract class BaseController extends AbstractActionController implements LoggerAwareInterface, ServiceManagerAwareInterface
{

    /** 改行をBRに変更する */
    public $nl2br;

    /** ロガー(monolog) */
    protected $log;

    /** サービスマネージャ */
    protected $sm;

    /**
     * loggerの設定。initializerで設定
     */
    public function setLogger(Logger $log) {
        $this->log = $log;
    }

    /**
     * サービスマネージャの設定。initializerで設定
     */
    public function setServiceManager(ServiceManager $serviceManager) {
        $this->sm = $serviceManager;
    }
    /*
     * 諸々の処理
     */

で、初期設定をするためのクラスを設定する。

/**
 * コントローラーの初期設定を行う
 */
class ControllerInitializer implements InitializerInterface
{
    public function initialize($instance, ServiceLocatorInterface $serviceLocator)
    {
        //loggerの設定
        if ($instance instanceof LoggerAwareInterface){
            $instance->setlogger(\Application\Common\Log::createBasicLogger(Setting::APPLICATION_NAME));
        }

        //service managerの設定
        if ($instance instanceof ServiceManagerAwareInterface){
            $instance->setServiceManager($serviceLocator->getServiceLocator());
        }
    }
}

DIコンテナもどきなので、Module.php経由でコントローラを呼びだす設定の所に、 イニシャライズとして、さっき作ったクラスを指定する。

    /*
     * コントローラーの設定をする
     */
    public function getControllerConfig() {
        return array(
            'invokables' => array(
                'Application\Controller\BaseController' => 'Application\Controller\BaseController',
            ),
            'initializers' => array(
                'Application\Initializer\Controller\ControllerInitializer',
            ),
        );
    }

で、便利なん?

LoggerとかSMとかだとあんまり嬉しくないけど、ファイルに保存するクラスなどを

  • ローカル開発なら、ローカルに保存するクラス。
  • STGならS3に保存するクラス

などに差し替える事ができるので、便利ですね。