You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.3 KiB

<?php
/**
* Created by Error202
* Date: 20.08.2018
*/
namespace core\components\modules;
use core\entities\ModuleRecord;
use core\services\ModuleService;
use Yii;
use yii\helpers\FileHelper;
class ModuleManager
{
public $moduleNames = [];
public $modules = [];
private $service;
public function __construct(ModuleService $service)
{
$this->service = $service;
}
public function appendToMigrationTable($name) {
$time = time();
$connection = Yii::$app->getDb();
$command = $connection->createCommand("INSERT INTO migration (version, apply_time) VALUE ('$name', '$time')");
$command->execute();
}
public function removeFromMigrationTable($name) {
$connection = Yii::$app->getDb();
$command = $connection->createCommand("DELETE FROM migration WHERE version = '$name'");
$command->execute();
}
public function getModules()
{
$modules = [];
$localModules = $this->getLocalModules();
foreach ($localModules as $local_module) {
if ($this->isTableExist('modules') && !$db_module = ModuleRecord::find()->andWhere(['name' => $local_module['name']])->one()) {
$db_module = $this->service->create($local_module['name'], "common\\modules\\".$local_module['name']."\\" . $local_module['module']);
$db_module->description = $local_module['description'];
$modules[] = $db_module;
}
else {
$db_module->description = $local_module['description'];
$modules[] = $db_module;
}
}
return $modules;
}
public function getLocalModules()
{
$this->_getLocalModulesNames();
if (empty($this->modules)) {
foreach ( $this->moduleNames as $module_name ) {
$manifest = Yii::getAlias( '@common/modules/' . $module_name . '/manifest.php' );
if ( file_exists( $manifest ) ) {
$this->modules[] = require $manifest;
}
}
}
return $this->modules;
}
private function _getLocalModulesNames(): void
{
if (!empty($this->moduleNames)) {
return;
}
$names = [];
$modulePath = Yii::getAlias('@common/modules');
$modules = file_exists($modulePath) ? FileHelper::findDirectories($modulePath, [
'recursive' => false,
]) : [];
foreach ($modules as $module) {
$module = basename($module);
$names[] = $module;
}
$this->moduleNames = $names;
}
public function isTableExist($name): bool
{
return Yii::$app->db->schema->getTableSchema($name) !== null;
}
}