Mapleの森を歩く(2): 「include_once」でgrepすると...

Mapleの森を歩く(1)で

  1. 動的にクラスファイルをインクルード
  2. 動的にクラスを生成
  3. コンテナに生成したインスタンスを登録

というステップに関心を持ちました。


それで、考えていると、ふと閃きました。
「include_onceをしてから動的にクラスを生成しているケースが他にもあるのでは?」

いつものようにgrepをしてみると...

$ cd ~/public_html/php/maple/maple-3.0.0
$ grep -nr -A3 "include_once" . 
./maple/core/Controller.class.php:132:                  include_once($filename);
./maple/core/Controller.class.php-133-
./maple/core/Controller.class.php-134-                  $instance =& new $className();
./maple/core/Controller.class.php-135-
--
./maple/core/FilterChain.class.php:101:         include_once($filename);
./maple/core/FilterChain.class.php-102-
./maple/core/FilterChain.class.php-103-         $filter =& new $className();
./maple/core/FilterChain.class.php-104-
--
./maple/core/ActionChain.class.php:112:         include_once($filename);
./maple/core/ActionChain.class.php-113-
./maple/core/ActionChain.class.php-114-         $action =& new $className();
./maple/core/ActionChain.class.php-115-
--
./maple/core/LogFactory.class.php:93:           include_once($filename);
./maple/core/LogFactory.class.php-94-
./maple/core/LogFactory.class.php-95-           $logger =& new $className();
./maple/core/LogFactory.class.php-96-
--
./maple/core/ConverterManager.class.php:106:                    include_once($filename);
./maple/core/ConverterManager.class.php-107-
./maple/core/ConverterManager.class.php-108-                    $converter =& new $className();
./maple/core/ConverterManager.class.php-109-
--
./maple/core/ValidatorManager.class.php:182:                    include_once($filename);
./maple/core/ValidatorManager.class.php-183-
./maple/core/ValidatorManager.class.php-184-                    $validator =& new $className();
./maple/core/ValidatorManager.class.php-185-

予想通りでした。
このパターンはMapleの中で結構使われていて、動的にクラスをロードしなければならない所で利用されているようです。
開発者が書くactionクラスも、このメカニズムでロードされるのですね。

DIコンテナの本当に核になる部分の実装というのは、案外シンプルだったのには驚きました。
流行るもっていうのは、やっぱりシンプルなんですね。