121 lines
3.7 KiB
PHP
121 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace ctiso\Controller;
|
||
use ctiso\Settings,
|
||
ctiso\Path,
|
||
ctiso\Database\JsonInstall;
|
||
|
||
class 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->get($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 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 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->get($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->set($name, [
|
||
'version' => $version_new,
|
||
'time' => filemtime($setup)
|
||
]);
|
||
// $registry->writeKey([$name], $settings->export());
|
||
|
||
$registry->write();
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
function install($dbinit_path, $dbfill_path = null) {
|
||
$json_installer = new JsonInstall($this->db_manager);
|
||
$json_installer->install($dbinit_path, $dbfill_path);
|
||
}
|
||
}
|