]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Updater/AbstractUpdater.php
9a2d9dab22a045ee19c6605dd6bbcc50f633d44c
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette abstract script updater
10 * Copyright © 2013 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.6dev - 2013-07-21
38 namespace Galette\Updater
;
44 * Galette abstract updater script
47 * @name AbstractUpdater
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2013 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since Available since 0.7.6dev - 2013-07-21
55 abstract class AbstractUpdater
57 const REPORT_SUCCESS
= 0;
58 const REPORT_ERROR
= 1;
59 const REPORT_WARNING
= 2;
61 protected $sql_scripts = null;
62 protected $db_version = null;
63 private $engines = array(
64 Db
::MYSQL
=> Db
::MYSQL
,
65 Db
::PGSQL
=> Db
::PGSQL
,
69 private $report = array();
74 public function __construct()
76 if ($this->db_version
=== null) {
78 'Upgrade version can not be empty!',
81 throw new \
RuntimeException('Upgrade version can not be empty!');
86 * Does upgrade have a SQL script to run
90 private function hasSql()
92 return !($this->sql_scripts
=== null);
97 * Update will take the following order:
103 * If one function fails, an Exception will be thrown
104 * and next function will not be called.
106 * @param Db $zdb Database instance
107 * @param Install $installer Installer instance
109 * @return Boolean|Exception
111 final public function run($zdb, $installer)
114 $this->installer
= $installer;
116 $res = $this->preUpdate();
118 throw new \
RuntimeException(
119 'Fail executing pre-update instructions'
123 $res = $this->update();
125 throw new \
RuntimeException(
126 'Fail executing update instructions'
130 if ($this->hasSql()) {
131 $res = $this->sql($zdb, $installer);
133 throw new \
RuntimeException(
134 'Fail executing SQL instructions'
139 $res = $this->postUpdate();
141 throw new \
RuntimeException(
142 'Fail executing post-update instructions'
146 $this->updateDbVersion();
150 * Update instructions
154 abstract protected function update();
158 * Will be extecuted first.
162 protected function preUpdate()
168 * Executes SQL instructions, if any.
170 * @param Db $zdb Database instance
171 * @param Install $installer Installer instance
175 private function sql($zdb, $installer)
177 $script = $this->sql_scripts
[TYPE_DB
];
180 @fopen
($script, 'r'),
184 if ($sql_query !== '') {
185 return $installer->executeSql($zdb, $sql_query);
190 * Post stuff, if any.
191 * Will be executed at the end.
195 protected function postUpdate()
201 * Set SQL files instructions for all supported databases
203 * @param string $version Version for scripts
207 protected function setSqlScripts($version)
209 $scripts = $this->getSqlScripts($version);
210 if (is_array($scripts)
211 && count($scripts) === count($this->engines
)
212 && count(array_diff(array_keys($scripts), $this->engines
)) == 0
215 foreach ($scripts as $file) {
216 if (file_exists($file)) {
224 if ($checked === true) {
225 $this->sql_scripts
= $scripts;
230 'Unable to see SQL scripts. Please check that scripts exists ' .
231 'in scripts/sql directory, for all supported SQL engines.',
239 * Get SQL scripts for specified version
241 * @param string $version Scripts version
245 private function getSqlScripts($version)
247 $dh = opendir(GALETTE_ROOT
. '/install/scripts/sql');
251 while (($file = readdir($dh)) !== false) {
252 if (preg_match('/upgrade-to-(.*)-(.+)\.sql/', $file, $ver)) {
253 if ($ver[1] == $version) {
254 $scripts[$ver[2]] = GALETTE_ROOT
. '/install/scripts/sql/' . $file;
265 * Add report entry in array
267 * @param string $msg Report message
268 * @param int $type Entry type
272 public function addReportEntry($msg, $type)
275 if ($type === self
::REPORT_ERROR
) {
278 $this->report
[] = array(
286 * Add an error in array
288 * @param string $msg Error message
292 public function addError($msg)
294 $this->addReportEntry($msg, self
::REPORT_ERROR
);
298 * Has current update errors?
302 public function hasErrors()
304 foreach ($this->report
as $report) {
305 if ($report['type'] === self
::REPORT_ERROR
) {
316 public function getReport()
318 return $this->report
;
322 * Update database version
326 private function updateDbVersion()
328 $update = $this->zdb
->update('database');
330 array('version' => $this->db_version
)
332 $this->zdb
->execute($update);