Browse Source

Installer fix modules activating

master
Egorka 6 years ago
parent
commit
3392964fc1
  1. 12
      backend/controllers/ModuleController.php
  2. 8
      console/controllers/ModuleController.php
  3. 32
      core/behaviors/LanguageBehavior.php
  4. 32
      core/components/LanguageDynamicModel.php
  5. 27
      core/components/modules/ModuleManager.php
  6. 11
      setup.php

12
backend/controllers/ModuleController.php

@ -13,8 +13,16 @@ use yii\filters\VerbFilter;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
/**
* Modules Management
* Class ModuleController
* @package backend\controllers
*/
class ModuleController extends Controller class ModuleController extends Controller
{ {
/**
* @var ModuleService Modules management service
*/
private $_service; private $_service;
public function __construct(string $id, $module, ModuleService $service, array $config = []) public function __construct(string $id, $module, ModuleService $service, array $config = [])
@ -51,6 +59,10 @@ class ModuleController extends Controller
]; ];
} }
/**
* List of modules
* @return string
*/
public function actionList() public function actionList()
{ {
$modules = \Yii::$app->moduleManager->getModules(); $modules = \Yii::$app->moduleManager->getModules();

8
console/controllers/ModuleController.php

@ -29,6 +29,14 @@ class ModuleController extends Controller
} }
/** /**
* First modules initialization
*/
public function actionInit() : void
{
\Yii::$app->moduleManager->getModules();
}
/**
* Activate module and apply it migration if needed * Activate module and apply it migration if needed
* @param $name * @param $name
*/ */

32
core/behaviors/LanguageBehavior.php

@ -6,6 +6,7 @@
namespace core\behaviors; namespace core\behaviors;
use core\components\language\LanguageVirtualAbstract;
use yii\base\Behavior; use yii\base\Behavior;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
@ -67,10 +68,10 @@ class LanguageBehavior extends Behavior
*/ */
public $forceDelete = false; public $forceDelete = false;
private $ownerClassName; private $_ownerClassName;
private $ownerClassShortName; private $_ownerClassShortName;
private $ownerPrimaryKey; private $_ownerPrimaryKey;
private $languageAttributes = []; private $_languageAttributes = [];
/** /**
* Control events firing * Control events firing
@ -83,16 +84,15 @@ class LanguageBehavior extends Behavior
ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert', ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
//ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
]; ];
} }
public function attach( $owner ) { public function attach($owner)
{
/** @var ActiveRecord $owner */ /** @var ActiveRecord $owner */
parent::attach($owner); parent::attach($owner);
if (empty($this->translatedLanguages) || !is_array($this->translatedLanguages)) { if (empty($this->translatedLanguages) || !is_array($this->translatedLanguages)) {
throw new InvalidConfigException('Please specify array of available languages for the ' . get_class($this) . ' in the ' throw new InvalidConfigException('Please specify array of available languages for the ' . get_class($this) . ' in the ' . get_class($this->owner) . ' or in the application parameters', 101);
. get_class($this->owner) . ' or in the application parameters', 101);
} }
if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array
@ -108,12 +108,12 @@ class LanguageBehavior extends Behavior
. get_class($this->owner), 103); . get_class($this->owner), 103);
} }
$this->ownerClassName = get_class($this->owner); $this->_ownerClassName = get_class($this->owner);
$this->ownerClassShortName = $this->getShortClassName($this->ownerClassName); $this->_ownerClassShortName = $this->getShortClassName($this->_ownerClassName);
/** @var ActiveRecord $className */ /** @var ActiveRecord $className */
$className = $this->ownerClassName; $className = $this->_ownerClassName;
$this->ownerPrimaryKey = $className::primaryKey()[0]; $this->_ownerPrimaryKey = $className::primaryKey()[0];
if (!isset($this->relativeField)) { if (!isset($this->relativeField)) {
throw new InvalidConfigException('Please specify relativeField for the ' . get_class($this) . ' in the ' throw new InvalidConfigException('Please specify relativeField for the ' . get_class($this) . ' in the '
@ -295,7 +295,7 @@ class LanguageBehavior extends Behavior
public function setLanguageAttribute($name, $value) public function setLanguageAttribute($name, $value)
{ {
$this->languageAttributes[$name] = $value; $this->_languageAttributes[$name] = $value;
} }
protected function indexByLanguage(array $records) protected function indexByLanguage(array $records)
@ -305,6 +305,7 @@ class LanguageBehavior extends Behavior
$sorted[$record->{$this->languageField}] = $record; $sorted[$record->{$this->languageField}] = $record;
} }
unset($records); unset($records);
return $sorted; return $sorted;
} }
@ -341,6 +342,7 @@ class LanguageBehavior extends Behavior
public function findTranslation($language = null) public function findTranslation($language = null)
{ {
$language = $language ?: $this->defaultLanguage; $language = $language ?: $this->defaultLanguage;
//$class = call_user_func(array($this->virtualClassName, 'getInstance')); //$class = call_user_func(array($this->virtualClassName, 'getInstance'));
return $this->virtualClassName::find() return $this->virtualClassName::find()
->andWhere([$this->relativeField => $this->owner->id]) ->andWhere([$this->relativeField => $this->owner->id])
@ -350,12 +352,12 @@ class LanguageBehavior extends Behavior
public function hasLangAttribute($name) public function hasLangAttribute($name)
{ {
return array_key_exists($name, $this->languageAttributes); return array_key_exists($name, $this->_languageAttributes);
} }
public function getLangAttribute($name) public function getLangAttribute($name)
{ {
return $this->hasLangAttribute($name) ? $this->languageAttributes[$name] : null; return $this->hasLangAttribute($name) ? $this->_languageAttributes[$name] : null;
} }
} }

32
core/components/LanguageDynamicModel.php

@ -6,16 +6,15 @@
namespace core\components; namespace core\components;
use yii\base\DynamicModel; use yii\base\DynamicModel;
use Yii; use Yii;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
class LanguageDynamicModel extends DynamicModel class LanguageDynamicModel extends DynamicModel
{ {
private $new_labels = []; private $_new_labels = [];
private $new_hints = []; private $_new_hints = [];
private $new_rules = []; private $_new_rules = [];
public function __construct(ActiveRecord $entity = null, array $attributes = [], array $config = []) public function __construct(ActiveRecord $entity = null, array $attributes = [], array $config = [])
{ {
@ -35,8 +34,7 @@ class LanguageDynamicModel extends DynamicModel
if ($translate->language === $defaultLanguage && isset($translate->{$used_attribute})) { if ($translate->language === $defaultLanguage && isset($translate->{$used_attribute})) {
$this->{$used_attribute} = $translate->{$used_attribute}; $this->{$used_attribute} = $translate->{$used_attribute};
} } elseif (isset($translate->{$used_attribute})) {
elseif (isset($translate->{$used_attribute})) {
$this->{$languageAttribute} = $translate->{$used_attribute}; $this->{$languageAttribute} = $translate->{$used_attribute};
} }
} }
@ -56,6 +54,7 @@ class LanguageDynamicModel extends DynamicModel
} }
} }
} }
return $string_attributes; return $string_attributes;
} }
@ -73,30 +72,33 @@ class LanguageDynamicModel extends DynamicModel
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
// add attribute // add attribute
$language_attributes[] = $attribute . '_' . $language; $language_attributes[] = $attribute . '_' . $language;
$this->new_labels[$attribute . '_' . $language] = isset($labels[$attribute]) ? $labels[$attribute] : null; $this->_new_labels[$attribute . '_' . $language] = isset($labels[$attribute]) ? $labels[$attribute] : null;
$this->new_hints[$attribute . '_' . $language] = isset($hints[$attribute]) ? $hints[$attribute] : null; $this->_new_hints[$attribute . '_' . $language] = isset($hints[$attribute]) ? $hints[$attribute] : null;
$rule_attributes[] = $attribute . '_' . $language; $rule_attributes[] = $attribute . '_' . $language;
} }
// add rule // add rule
if (!empty($rule_attributes)) { if (!empty($rule_attributes)) {
$this->new_rules[] = [ $rule_attributes, $rule[1] ]; $this->_new_rules[] = [$rule_attributes, $rule[1]];
} }
} }
} }
} }
return $language_attributes; return $language_attributes;
} }
public function attributeLabels(){ public function attributeLabels()
return $this->new_labels; {
return $this->_new_labels;
} }
public function rules() { public function rules()
return $this->new_rules; {
return $this->_new_rules;
} }
public function getPublicAttributes () { public function getPublicAttributes()
{
return call_user_func('get_object_vars', $this); return call_user_func('get_object_vars', $this);
} }
} }

27
core/components/modules/ModuleManager.php

@ -16,47 +16,50 @@ class ModuleManager
public $moduleNames = []; public $moduleNames = [];
public $modules = []; public $modules = [];
private $service; private $_service;
public function __construct(ModuleService $service) public function __construct(ModuleService $service)
{ {
$this->service = $service; $this->_service = $service;
} }
public function appendToMigrationTable($name) { public function appendToMigrationTable($name)
{
$time = time(); $time = time();
$connection = Yii::$app->getDb(); $connection = Yii::$app->getDb();
$command = $connection->createCommand("INSERT INTO migration (version, apply_time) VALUE ('$name', '$time')"); $command = $connection->createCommand("INSERT INTO migration (version, apply_time) VALUE ('$name', '$time')");
$command->execute(); $command->execute();
} }
public function removeFromMigrationTable($name) { public function removeFromMigrationTable($name)
{
$connection = Yii::$app->getDb(); $connection = Yii::$app->getDb();
$command = $connection->createCommand("DELETE FROM migration WHERE version = '$name'"); $command = $connection->createCommand("DELETE FROM migration WHERE version = '$name'");
$command->execute(); $command->execute();
} }
public function getModules() public function getModules() : array
{ {
$modules = []; $modules = [];
$localModules = $this->getLocalModules(); $localModules = $this->getLocalModules();
foreach ($localModules as $local_module) { foreach ($localModules as $local_module) {
if ($this->isTableExist('modules') && !$db_module = ModuleRecord::find()->andWhere(['name' => $local_module['name']])->one()) { $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']); if ($this->isTableExist('modules') && !$db_module) {
$db_module = $this->_service->create($local_module['name'], 'common\\modules\\' . $local_module['name'] . '\\' . $local_module['module']);
$db_module->description = $local_module['description']; $db_module->description = $local_module['description'];
$modules[] = $db_module; $modules[] = $db_module;
} } else {
else {
$db_module->description = $local_module['description']; $db_module->description = $local_module['description'];
$modules[] = $db_module; $modules[] = $db_module;
} }
} }
return $modules; return $modules;
} }
public function getLocalModules() public function getLocalModules()
{ {
$this->_getLocalModulesNames(); $this->getLocalModulesNames();
if (empty($this->modules)) { if (empty($this->modules)) {
foreach ($this->moduleNames as $module_name) { foreach ($this->moduleNames as $module_name) {
@ -66,10 +69,11 @@ class ModuleManager
} }
} }
} }
return $this->modules; return $this->modules;
} }
private function _getLocalModulesNames(): void private function getLocalModulesNames(): void
{ {
if (!empty($this->moduleNames)) { if (!empty($this->moduleNames)) {
return; return;
@ -91,5 +95,4 @@ class ModuleManager
{ {
return Yii::$app->db->schema->getTableSchema($name) !== null; return Yii::$app->db->schema->getTableSchema($name) !== null;
} }
} }

11
setup.php

@ -24,6 +24,8 @@ class Setup
private $_http_protocol; private $_http_protocol;
private $_domain; private $_domain;
private $_systemModules = ['languages', 'pages', 'forms', 'links'];
private $_l = [ private $_l = [
'ru' => [ 'ru' => [
'Connection failed. Try again' => 'Ошибка соединения. Попробуйте снова', 'Connection failed. Try again' => 'Ошибка соединения. Попробуйте снова',
@ -102,6 +104,9 @@ class Setup
// install modules // install modules
$this->activateSystemModules(); $this->activateSystemModules();
// remove garbage modules
// todo
// install system permissions // install system permissions
$this->addPermissions(); $this->addPermissions();
@ -377,10 +382,8 @@ SH;
private function activateSystemModules() : void private function activateSystemModules() : void
{ {
Console::log($this->l('Activating modules: '), 'white'); Console::log($this->l('Activating modules: '), 'white');
$systemModules = [ shell_exec('php ' . __DIR__ . '/yii module/init');
'languages', 'pages', 'forms', 'links' foreach ($this->_systemModules as $name) {
];
foreach ($systemModules as $name) {
shell_exec('php ' . __DIR__ . '/yii module/activate "' . $name . '"'); shell_exec('php ' . __DIR__ . '/yii module/activate "' . $name . '"');
} }
echo Console::log($this->l('Complete'), 'green') . PHP_EOL; echo Console::log($this->l('Complete'), 'green') . PHP_EOL;

Loading…
Cancel
Save