phplibrary/core/controller/installer.php
2016-07-14 16:29:26 +03:00

89 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once 'core/settings.php';
class Installer
{
protected $db;
protected $installPath;
public $_registry;
public function __construct(Settings $_registry)
{
$this->_registry = $_registry;
}
public function setUp($db, $installPath)
{
$this->db = $db;
$this->installPath = $installPath;
}
function getSetupFile($name)
{
return Path::join(call_user_func($this->installPath, $name), "setup.php");
}
// Проверка версии обновления
function isChanged($name) // Информация о модулях
{
$item = $this->_registry->readKey(array($name));
if ($item) {
$setup = $this->getSetupFile($name);
if (file_exists($setup) && (filemtime($setup) > $item['time'])) {
return true;
}
return false;
}
return true;
}
function installSQL(array $sql, $version_new, $version_old, $name)
{
require_once "core/setup.php";
foreach ($sql as $version => $install) {
if (version_compare($version, $version_new, "<=") && version_compare($version, $version_old, ">")) {
// this->installPath this->db
$file = Path::join(call_user_func($this->installPath, $name), "sql", $install);
Setup::batchSQL($this->db, $file);
}
}
}
// Устанавливает обновления если есть
function doUpdates($name, $force = false) // Установка модуля
{
$setup = $this->getSetupFile($name);
if (file_exists($setup) && ($this->isChanged($name) || $force)) {
$registry = $this->_registry;
$settings = new Settings($setup);
$settings->read();
$item = $registry->readKey(array($name));
$version_new = $settings->get('version');
if ($item) {
$version_old = $item['version'];
} else {
$version_old = "0.0";
$registry->writeKey(array($name), array());
}
if (version_compare($version_old, $settings->get('version'), "!=")) {
$sql = $settings->get('sql');
if (is_array($sql)) {
$this->installSQL($sql, $version_new, $version_old, $name);
}
}
// Обновление версии меню
$registry->writeKey(array($name), $settings->get('settings'));
$registry->writeKey(array($name),
array('version' => $version_new,
'time' => filemtime($setup)));
$registry->write();
}
}
}