phplibrary/src/Controller/Installer.php

113 lines
3.7 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
class Controller_Installer
{
protected $db_manager;
protected $installPath;
public $_registry;
public function __construct(Settings $_registry)
{
$this->_registry = $_registry;
}
public function setUp($db_manager, $installPath)
{
$this->db_manager = $db_manager;
$this->installPath = $installPath;
}
function getSetupFile($name)
{
$setup = Path::join(call_user_func($this->installPath, $name), "install.json");
return $setup;
}
function getUninstallFile($name){
return Path::join(call_user_func($this->installPath, $name), "sql", "uninstall.json");
}
// Проверка версии обновления
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)
{
$result = [];
$json_installer = new Database_JsonInstall($this->db_manager);
foreach ($sql as $version => $install) {
if (version_compare($version, $version_new, "<=") && version_compare($version, $version_old, ">")) {
$file = Path::join(call_user_func($this->installPath, $name), "sql", $install);
$json_installer->install($file,null);
$result[] = $version;
}
}
return $result;
}
function uninstall($name){
$uninstall = $this->getUninstallFile($name);
if (file_exists($uninstall)) {
$json_installer = new Database_JsonInstall($this->db_manager);
$json_installer->install($uninstall,null);
}
$this->_registry->removeKey($name);
$this->_registry->write();
}
// Устанавливает обновления если есть
function doUpdates($name, $force = false) // Установка модуля
{
$result = array();
$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)) {
$res = $this->installSQL($sql, $version_new, $version_old, $name);
if($res){
$result[]=$res;
}
}
// Обновление версии меню
$registry->removeKey($name);
$registry->writeKey(array($name), $settings->get('settings'));
$registry->writeKey(array($name),
array('version' => $version_new,
'time' => filemtime($setup)));
}
$registry->write();
}
return $result;
}
function install($dbinit_path, $dbfill_path = null) {
$json_installer = new Database_JsonInstall($this->db_manager);
$json_installer->install($dbinit_path,$dbfill_path);
}
}