source: branches/version-2_13-dev/data/class/plugin/SC_Plugin_Installer.php @ 22978

Revision 22978, 8.0 KB checked in by adachi, 11 years ago (diff)

#2308 開発合宿(2013/06)プラグイン改善分をマージ

RevLine 
[22630]1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23class SC_Plugin_Installer {
24   
[22673]25    protected $exec_func;
26   
[22661]27    protected $plugin_code;
28   
[22630]29    protected $arrPlugin;
30   
31    protected $arrInstallData;
32   
[22673]33    public function __construct($exec_func, $arrPlugin) {
34        $this->exec_func   = $exec_func;
35        $this->plugin_code = $arrPlugin['plugin_code'];
[22661]36        $this->arrPlugin   = $arrPlugin;
37        $this->arrInstallData = array();
38        $this->arrInstallData['sql'] = array();
39        $this->arrInstallData['copy_file'] = array();
40        $this->arrInstallData['copy_direcrtory'] = array();
[22682]41        $this->arrInstallData['insert'] = array();
42        $this->arrInstallData['update'] = array();
[22661]43        $this->arrInstallData['remove_file'] = array();
44        $this->arrInstallData['remove_directory'] = array();
[22630]45    }
46   
[22676]47    public function execPlugin() {
[22673]48        $this->log("start");
49       
50        $plugin_code = $this->arrPlugin['plugin_code'];
51
[22682]52
[22673]53        // テーブル作成SQLなどを実行
54        $arrSql = $this->arrInstallData['sql'];
[22682]55        $arrErr = array();
56
57        // SQLの検証
58        foreach ($arrSql as $sql) {
59            $this->log("verify sql: " . $sql['sql']);
60            $error_message = $this->verifySql($sql['sql'], $sql['params']);
61            if (!is_null($error_message)) {
62                $this->log("verify sql: invalid sql " . $sql['sql']);
[22683]63                $this->log("verify sql: $error_message");
[22682]64                $arrErr[] = $error_message;
65            }
66        }
[22673]67       
[22682]68        if (count($arrErr) > 0) {
69            return $arrErr;
70        }
71       
72        $objQuery =& SC_Query_Ex::getSingletonInstance();
73       
74        // SQLの実行
[22673]75        foreach ($arrSql as $sql) {
76            $this->log("exec sql: " . $sql['sql']);
77            $objQuery->query($sql['sql'], $sql['params']);
78        }
79       
[22683]80        $arrInsertQuery = $this->arrInstallData['insert'];
81        foreach ($arrInsertQuery as $query) {
82            $objQuery->insert(
83                    $query['table'],
84                    $query['arrVal'],
85                    $query['arrSql'],
86                    $query['arrSqlVal'],
87                    $query['form'],
88                    $query['arrFromVal']
89            );
90        }
91       
92        $arrUpdateQuery = $this->arrInstallData['update'];
93        foreach ($arrUpdateQuery as $query) {
94            $objQuery->update(
95                    $query['table'],
96                    $query['arrVal'],
97                    $query['where'],
98                    $query['arrWhereVal'],
99                    $query['arrRawSql'],
100                    $query['arrRawSqlVal']
101            );
102        }
103       
[22673]104        // プラグインのディレクトリコピー
105        $arrCopyDirectories = $this->arrInstallData['copy_directory'];
106
107        foreach ($arrCopyDirectories as $directory) {
108            $this->log("exec dir copy: " . $directory['src'] . ' -> ' . $directory['dist']);
109            // ディレクトリコピー -> HTML配下とDATA配下を別関数にする
110            SC_Utils::copyDirectory(
111                    PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $directory['src'],
112                    PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $directory['dist']);
113        }
114
115        // プラグインのファイルコピー
116        $arrCopyFiles = $this->arrInstallData['copy_file'];
117
118        foreach ($arrCopyFiles as $file) {
119            $this->log("exec file copy: " . $file['src'] . ' -> ' . $file['dist']);
120            // ファイルコピー
121            copy(PLUGIN_UPLOAD_REALDIR . $plugin_code . DIRECTORY_SEPARATOR . $file['src'],
122                 PLUGIN_HTML_REALDIR   . $plugin_code . DIRECTORY_SEPARATOR . $file['dist']);
123        }
124       
125        $this->log("end");         
126    }
127
[22662]128    public function copyFile($src, $dist) {
[22661]129        $this->arrInstallData['copy_file'][] = array(
[22662]130            'src'  => $src,
[22646]131            'dist' => $dist
132        );
[22630]133    }
[22661]134 
[22662]135    public function copyDirectory($src, $dist) {
[22661]136        $this->arrInstallData['copy_directory'][] = array(
[22662]137            'src'  => $src,
[22646]138            'dist' => $dist
139        );       
140    }
[22661]141   
[22662]142    public function removeFile($dist) {
[22661]143        $this->arrInstallData['remove_file'][] = array(
144            'dist' => $dist
145        );
[22630]146    }
147   
[22662]148    public function removeDirectory($dist) {
[22661]149       $this->arrInstallData['remove_file'][] = array(
150            'dist' => $dist
151        );     
152    }
153
[22652]154    public function sql($sql, array $params = array()) {
[22661]155        $this->arrInstallData['sql'][] = array(
[22630]156            'sql'    => $sql,
157            'params' => $params
158        );
159    }
[22665]160   
[22673]161    protected function log($msg) {
[22676]162        $msg = sprintf("%s %s: %s", $this->plugin_code, $this->exec_func, $msg);
[22673]163        GC_Utils::gfPrintLog($msg, PLUGIN_LOG_REALFILE);
164    }
[22675]165   
166    /**
167     * カラム追加クエリの追加
168     *
169     * @param type $table
170     * @param type $col
171     * @param type $type
172     */
[22710]173    function sqlAterTableAddColumn($table_name, $col_name, $col_type) {
[22683]174        $sql = "ALTER TABLE $table_name ADD $col_name $col_type ";
[22675]175        $this->sql($sql);
176    }
177   
178    /**
179     * カラム削除クエリの追加
180     *
181     * @param type $table
182     * @param type $col
183     * @param type $type
184     */
[22710]185    function sqlAterTableDropColumn($table_name, $col_name) {
[22683]186        $sql = "ALTER TABLE $table_name DROP $col_name";
[22675]187        $this->sql($sql);
188    }
189   
190   
191    function sqlInsert($table, $arrVal, $arrSql = array(), $arrSqlVal = array(), $from = '', $arrFromVal = array()) {
[22683]192        $this->arrInstallData['insert'][] = array(
193            'table' => $table,
194            'arrVal' => $arrVal,
195            'arrSql' => $arrSql,
196            'arrSqlVal' => $arrSqlVal,
197            'form' =>$from,
198            'arrFromVal' => $arrFromVal
[22675]199        );
200    }
201   
202    function sqlUpdate($table, $arrVal, $where = '', $arrWhereVal = array(), $arrRawSql = array(), $arrRawSqlVal = array()) {
[22683]203        $this->arrInstallData['update'][] = array(
204            'table' => $table,
205            'arrVal' => $arrVal,
206            'where' => $where,
207            'arrWhereVal' => $arrWhereVal,
208            'arrRawSql' =>$arrRawSql,
209            'arrRawSqlVal' => $arrRawSqlVal
[22675]210        );
[22682]211    }
212   
213    /**
214     *
215     * @param string $sql
216     * @param type $params
217     */
218    protected function verifySql($sql, $params) {
219        // FIXME $paramsのチェックも行いたい.
220        $objQuery =& SC_Query_Ex::getSingletonInstance();
221       
222        // force runを有効にし, システムエラーを回避する
223        $objQuery->force_run = true;
224
225        // prepareでSQLを検証
226        $sth = $objQuery->prepare($sql);
227
228        if (PEAR::isError($sth)) {
229            $error_message = $sth->message . ":" . $sth->userinfo;
[22699]230            $objQuery->force_run = false;
231            return $error_message;
[22682]232        }
[22699]233       
[22728]234        //$ret = $sth->execute($params);
235        //if (PEAR::isError($ret)) {
236        //    $error_message = $ret->message . ":" . $ret->userinfo;
237        //    $objQuery->force_run = false;
238        //    return $error_message;           
239        //}
[22699]240       
241        $sth->free();
[22682]242        // force_runをもとに戻す.
243        $objQuery->force_run = false;
244       
245        return $error_message;
246    }
[22675]247}
Note: See TracBrowser for help on using the repository browser.