]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Updater/AbstractUpdater.php
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
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7.6dev - 2013-07-21
37 namespace Galette\Updater
;
43 * Galette abstract updater script
46 * @name AbstractUpdater
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since Available since 0.7.6dev - 2013-07-21
54 abstract class AbstractUpdater
56 public const REPORT_SUCCESS
= 0;
57 public const REPORT_ERROR
= 1;
58 public const REPORT_WARNING
= 2;
60 protected $sql_scripts = null;
61 protected $db_version = null;
62 private $engines = array(
63 Db
::MYSQL
=> Db
::MYSQL
,
64 Db
::PGSQL
=> Db
::PGSQL
,
68 private $report = array();
73 public function __construct()
75 if ($this->db_version
=== null) {
77 'Upgrade version can not be empty!',
80 throw new \
RuntimeException('Upgrade version can not be empty!');
85 * Does upgrade have a SQL script to run
89 private function hasSql()
91 return !($this->sql_scripts
=== null);
96 * Update will take the following order:
102 * If one function fails, an Exception will be thrown
103 * and next function will not be called.
105 * @param Db $zdb Database instance
106 * @param Install $installer Installer instance
108 * @return Boolean|Exception
110 final public function run($zdb, $installer)
113 $this->installer
= $installer;
115 $res = $this->preUpdate();
117 throw new \
RuntimeException(
118 'Fail executing pre-update instructions'
122 $res = $this->update();
124 throw new \
RuntimeException(
125 'Fail executing update instructions'
129 if ($this->hasSql()) {
130 $res = $this->sql($zdb, $installer);
132 throw new \
RuntimeException(
133 'Fail executing SQL instructions'
138 $res = $this->postUpdate();
140 throw new \
RuntimeException(
141 'Fail executing post-update instructions'
145 $this->updateDbVersion();
149 * Update instructions
153 abstract protected function update();
157 * Will be extecuted first.
161 protected function preUpdate()
167 * Executes SQL instructions, if any.
169 * @param Db $zdb Database instance
170 * @param Install $installer Installer instance
174 private function sql($zdb, $installer)
176 $script = $this->sql_scripts
[TYPE_DB
];
179 @fopen
($script, 'r'),
183 if ($sql_query !== '') {
184 return $installer->executeSql($zdb, $sql_query);
189 * Post stuff, if any.
190 * Will be executed at the end.
194 protected function postUpdate()
200 * Set SQL files instructions for all supported databases
202 * @param string $version Version for scripts
206 protected function setSqlScripts($version)
208 $scripts = $this->getSqlScripts($version);
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);