爬虫類嫌いのPython日記

爬虫類が大の苦手の筆者が、Pythonに挑戦。他にも、RubyやObjective-C、Google Appengine、Herokuなど色々とチャレンジしています。

Zend Frameworkのバッチ処理

改修案件で、PHPZend Frameworkを使用している環境でバッチ処理をする必要がありました。
ネットで調べてみると、色々な方法があるようですが、ViewのないActionを呼ぶと言うような実装は、やはりセキュリティ的にどうなのと思いました。
そこで、最良と思える方法を自分なりに考えてみました。

class Bootstrap {
    
    public function __construct() {
        set_include_path(
            getcwd().DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.
            'library'.DIRECTORY_SEPARATOR.PATH_SEPARATOR.
            getcwd().DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.
            'models'.DIRECTORY_SEPARATOR.PATH_SEPARATOR.
            getcwd().DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.
            'configs'.DIRECTORY_SEPARATOR
        );
        
        require_once 'Zend/Db/Table/Abstract.php';
        require_once 'Zend/Config/Ini.php';
        require_once 'Zend/Registry.php';
        require_once 'Master.php';
        
        $ini_path = '../configs/application.ini';
        $config	  = new Zend_Config_Ini($ini_path);
        $this->_initDatabase($config->db->db->adapter, $config->db->db->params);
    }
    
    public function __destruct() {
    }
    
    public function runApp() {
        
        /*
         * ここに処理を記述
         */
    }
    
    /*
     * データベースの初期化
     */
    private function _initDatabase($adapter, $params) {
        $db = Zend_Db::factory($adapter, $params);
        Zend_Registry::set('db' , $db);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
    }
}


$bootstrap = new Bootstrap();
try 
    $bootstrap->runApp();
}
catch (Exception $e) {
    echo $e->getMessage();
    exit;
}

Bootstrapクラスを作成し、それをrunAppしているだけです。

php ファイル名

コンソールから、実行可能ですし、cronで実行させる事も出来ます。