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. 674
      core/behaviors/LanguageBehavior.php
  4. 156
      core/components/LanguageDynamicModel.php
  5. 141
      core/components/modules/ModuleManager.php
  6. 13
      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
*/ */

674
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;
@ -13,235 +14,234 @@ use yii\db\ActiveQuery;
class LanguageBehavior extends Behavior class LanguageBehavior extends Behavior
{ {
/** /**
* Attributes for translate, ex.: ['name', 'content'] * Attributes for translate, ex.: ['name', 'content']
* @var array * @var array
*/ */
public $attributes; public $attributes;
/** /**
* Class name for language active record entity * Class name for language active record entity
* @var string * @var string
*/ */
public $virtualClassName = 'VirtualTranslate'; public $virtualClassName = 'VirtualTranslate';
/** /**
* Field name for language in translate table * Field name for language in translate table
* @var string * @var string
*/ */
public $languageField = 'language'; public $languageField = 'language';
/** /**
* Field name for tables relative, ex.: 'post_id' * Field name for tables relative, ex.: 'post_id'
* @var string * @var string
*/ */
public $relativeField; public $relativeField;
/** /**
* Available languages, ex.: ['en', 'ru'] * Available languages, ex.: ['en', 'ru']
* @var array * @var array
*/ */
public $translatedLanguages; public $translatedLanguages;
/** /**
* Default language, ex.: 'en' * Default language, ex.: 'en'
* @var string * @var string
*/ */
public $defaultLanguage; public $defaultLanguage;
/** /**
* Translate table name, ex.: 'post_lng' * Translate table name, ex.: 'post_lng'
* @var string * @var string
*/ */
public $tableName; public $tableName;
/** /**
* Abridge the language ID. * Abridge the language ID.
* @var boolean whether to abridge the language ID. * @var boolean whether to abridge the language ID.
*/ */
public $abridge = true; public $abridge = true;
/** /**
* Delete relative if foreign key not set on delete: cascade * Delete relative if foreign key not set on delete: cascade
* @var bool * @var bool
*/ */
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
* @return array * @return array
*/ */
public function events() public function events()
{ {
return [ return [
ActiveRecord::EVENT_AFTER_FIND => 'afterFind', ActiveRecord::EVENT_AFTER_FIND => 'afterFind',
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 $this->translatedLanguages = array_keys($this->translatedLanguages);
$this->translatedLanguages = array_keys($this->translatedLanguages); }
}
if (!$this->defaultLanguage) {
if (!$this->defaultLanguage) { throw new InvalidConfigException('Please specify default language for the ' . get_class($this));
throw new InvalidConfigException('Please specify default language for the ' . get_class($this)); }
}
if (empty($this->attributes) || !is_array($this->attributes)) {
if (empty($this->attributes) || !is_array($this->attributes)) { throw new InvalidConfigException('Please specify translated attributes for the ' . get_class($this) . ' in the '
throw new InvalidConfigException('Please specify translated attributes for the ' . get_class($this) . ' in the ' . 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 ' . get_class($this->owner), 105);
. get_class($this->owner), 105); }
}
//$rules = $owner->rules();
//$rules = $owner->rules(); //$validators = $owner->getValidators();
//$validators = $owner->getValidators(); /*foreach ($rules as $rule) {
/*foreach ($rules as $rule) { if (in_array($rule[1], $this->excludedValidators))
if (in_array($rule[1], $this->excludedValidators)) continue;
continue; $rule_attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]];
$rule_attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; $attributes = array_intersect($this->attributes, $rule_attributes);
$attributes = array_intersect($this->attributes, $rule_attributes); if (empty($attributes))
if (empty($attributes)) continue;
continue; $rule_attributes = [];
$rule_attributes = []; foreach ($attributes as $key => $attribute) {
foreach ($attributes as $key => $attribute) { foreach ($this->languages as $language)
foreach ($this->languages as $language) if ($language != $this->defaultLanguage)
if ($language != $this->defaultLanguage) $rule_attributes[] = $this->getAttributeName($attribute, $language);
$rule_attributes[] = $this->getAttributeName($attribute, $language); }
} if (isset($rule['skipOnEmpty']) && !$rule['skipOnEmpty'])
if (isset($rule['skipOnEmpty']) && !$rule['skipOnEmpty']) $rule['skipOnEmpty'] = !$this->requireTranslations;
$rule['skipOnEmpty'] = !$this->requireTranslations; $params = array_slice($rule, 2);
$params = array_slice($rule, 2); if ($rule[1] !== 'required' || $this->requireTranslations) {
if ($rule[1] !== 'required' || $this->requireTranslations) { $validators[] = Validator::createValidator($rule[1], $owner, $rule_attributes, $params);
$validators[] = Validator::createValidator($rule[1], $owner, $rule_attributes, $params); } elseif ($rule[1] === 'required') {
} elseif ($rule[1] === 'required') { $validators[] = Validator::createValidator('safe', $owner, $rule_attributes, $params);
$validators[] = Validator::createValidator('safe', $owner, $rule_attributes, $params); }
} }*/
}*/
$this->createLanguageClass();
$this->createLanguageClass(); $translation = new $this->virtualClassName;
$translation = new $this->virtualClassName;
foreach ($this->translatedLanguages as $language) {
foreach ($this->translatedLanguages as $language) { foreach ($this->attributes as $attribute) {
foreach ($this->attributes as $attribute) { $attributeName = $attribute;
$attributeName = $attribute; $this->setLanguageAttribute($attribute . '_' . $language, $translation->{$attributeName});
$this->setLanguageAttribute($attribute . '_' . $language, $translation->{$attributeName});
//$this->owner->__set($attribute . '_' . $language, $translation->{$attributeName});
//$this->owner->__set($attribute . '_' . $language, $translation->{$attributeName}); //$this->owner->{$attribute . '_' . $language} = $translation->{$attributeName};
//$this->owner->{$attribute . '_' . $language} = $translation->{$attributeName}; //$this->owner->createProperty($attribute . '_' . $language, $translation->{$attributeName});
//$this->owner->createProperty($attribute . '_' . $language, $translation->{$attributeName});
if ($language == $this->defaultLanguage) {
if ($language == $this->defaultLanguage) { $this->setLanguageAttribute($attribute, $translation->{$attributeName});
$this->setLanguageAttribute($attribute, $translation->{$attributeName}); //$this->owner->__set($attribute, $translation->{$attributeName});
//$this->owner->__set($attribute, $translation->{$attributeName}); }
} }
} }
} }
}
/**
/** * Insert event
* Insert event */
*/ public function afterInsert()
public function afterInsert() {
{ $this->saveTranslations();
$this->saveTranslations(); }
}
/**
/** * Update event
* Update event */
*/ public function afterUpdate()
public function afterUpdate() {
{ /** @var ActiveRecord $owner */
/** @var ActiveRecord $owner */ $owner = $this->owner;
$owner = $this->owner; //if ($owner->isRelationPopulated('translations')) {
//if ($owner->isRelationPopulated('translations')) { if ($translationRecords = $owner->translations) {
if ($translationRecords = $owner->translations) { $translations = $this->indexByLanguage($owner->getRelatedRecords()['translations']);
$translations = $this->indexByLanguage($owner->getRelatedRecords()['translations']); //$translations = $this->indexByLanguage($translationRecords);
//$translations = $this->indexByLanguage($translationRecords); $this->saveTranslations($translations);
$this->saveTranslations($translations); }
} }
}
/**
/** * Find event
* Find event */
*/ public function afterFind()
public function afterFind() {
{ /** @var ActiveRecord $owner */
/** @var ActiveRecord $owner */ $owner = $this->owner;
$owner = $this->owner; if ($owner->isRelationPopulated('translations') && $related = $owner->getRelatedRecords()['translations']) {
if ($owner->isRelationPopulated('translations') && $related = $owner->getRelatedRecords()['translations']) { $translations = $this->indexByLanguage($related);
$translations = $this->indexByLanguage($related); foreach ($this->translatedLanguages as $language) {
foreach ($this->translatedLanguages as $language) { foreach ($this->attributes as $attribute) {
foreach ($this->attributes as $attribute) { foreach ($translations as $translation) {
foreach ($translations as $translation) { if ($translation->{$this->languageField} == $language) {
if ($translation->{$this->languageField} == $language) { $attributeName = $attribute;
$attributeName = $attribute; $this->setLanguageAttribute($attribute . '_' . $language, $translation->{$attributeName});
$this->setLanguageAttribute($attribute . '_' . $language, $translation->{$attributeName}); if ($language == $this->defaultLanguage) {
if ($language == $this->defaultLanguage) { $this->setLanguageAttribute($attribute, $translation->{$attributeName});
$this->setLanguageAttribute($attribute, $translation->{$attributeName}); }
} }
} }
} }
} }
} } else {
} else { if (!$owner->isRelationPopulated('translation')) {
if (!$owner->isRelationPopulated('translation')) { $owner->translation;
$owner->translation; }
} $translation = $owner->getRelatedRecords()['translation'];
$translation = $owner->getRelatedRecords()['translation']; if ($translation) {
if ($translation) { foreach ($this->attributes as $attribute) {
foreach ($this->attributes as $attribute) { $attribute_name = $attribute;
$attribute_name = $attribute; $owner->setLanguageAttribute($attribute, $translation->$attribute_name);
$owner->setLanguageAttribute($attribute, $translation->$attribute_name); }
} }
} }
} foreach ($this->attributes as $attribute) {
foreach ($this->attributes as $attribute) { if ($owner->hasAttribute($attribute) && $this->getLangAttribute($attribute)) {
if ($owner->hasAttribute($attribute) && $this->getLangAttribute($attribute)) { $owner->setAttribute($attribute, $this->getLangAttribute($attribute));
$owner->setAttribute($attribute, $this->getLangAttribute($attribute)); }
} }
} }
}
public function afterDelete()
public function afterDelete() {
{ if ($this->forceDelete) {
if ($this->forceDelete) { /** @var ActiveRecord $owner */
/** @var ActiveRecord $owner */ $owner = $this->owner;
$owner = $this->owner; $owner->unlinkAll('translations', true);
$owner->unlinkAll('translations', true); }
} }
}
public function createLanguageClass()
public function createLanguageClass() {
{ if (!class_exists($this->virtualClassName, false)) {
if (!class_exists($this->virtualClassName, false)) { eval('
eval('
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
class ' . $this->virtualClassName . ' extends ActiveRecord class ' . $this->virtualClassName . ' extends ActiveRecord
{ {
@ -250,112 +250,114 @@ class LanguageBehavior extends Behavior
return \'' . $this->tableName . '\'; return \'' . $this->tableName . '\';
} }
}'); }');
} }
} }
private function saveTranslations($translations = []) private function saveTranslations($translations = [])
{ {
/** @var ActiveRecord $owner */ /** @var ActiveRecord $owner */
$owner = $this->owner; $owner = $this->owner;
if (!isset($owner->_form) || !$owner->_form) { if (!isset($owner->_form) || !$owner->_form) {
return; return;
} }
foreach ($this->translatedLanguages as $language) { foreach ($this->translatedLanguages as $language) {
$isDefaultLanguage = $language == $this->defaultLanguage; $isDefaultLanguage = $language == $this->defaultLanguage;
if (!isset($translations[$language])) { if (!isset($translations[$language])) {
/** @var ActiveRecord $translation */ /** @var ActiveRecord $translation */
$translation = new $this->virtualClassName; $translation = new $this->virtualClassName;
$translation->{$this->languageField} = $language; $translation->{$this->languageField} = $language;
$translation->{$this->relativeField} = $owner->getPrimaryKey(); $translation->{$this->relativeField} = $owner->getPrimaryKey();
} else { } else {
$translation = $translations[$language]; $translation = $translations[$language];
} }
$save = false; $save = false;
foreach ($this->attributes as $attribute) { foreach ($this->attributes as $attribute) {
//$value = $isDefaultLanguage ? $owner->$attribute : $owner->{$attribute . '_' . $language}; //$value = $isDefaultLanguage ? $owner->$attribute : $owner->{$attribute . '_' . $language};
$value = $isDefaultLanguage ? $owner->_form->$attribute : $owner->_form->{$attribute . '_' . $language}; $value = $isDefaultLanguage ? $owner->_form->$attribute : $owner->_form->{$attribute . '_' . $language};
if ($value !== null) { if ($value !== null) {
//$field = $isDefaultLanguage ? $attribute : $attribute . '_' . $language; //$field = $isDefaultLanguage ? $attribute : $attribute . '_' . $language;
$field = $attribute; $field = $attribute;
$translation->$field = $value; $translation->$field = $value;
$save = true; $save = true;
} }
} }
if ($translation->isNewRecord && !$save) { if ($translation->isNewRecord && !$save) {
continue; continue;
} }
$translation->save(); $translation->save();
} }
} }
private function getShortClassName($className) private function getShortClassName($className)
{ {
return substr($className, strrpos($className, '\\') + 1); return substr($className, strrpos($className, '\\') + 1);
} }
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)
{ {
$sorted = []; $sorted = [];
foreach ($records as $record) { foreach ($records as $record) {
$sorted[$record->{$this->languageField}] = $record; $sorted[$record->{$this->languageField}] = $record;
} }
unset($records); unset($records);
return $sorted;
} return $sorted;
}
/**
* Relation to model translations /**
* @return ActiveQuery * Relation to model translations
*/ * @return ActiveQuery
public function getTranslations() */
{ public function getTranslations()
/** @var ActiveRecord */ {
return $this->owner->hasMany($this->virtualClassName, [$this->relativeField => $this->ownerPrimaryKey]); /** @var ActiveRecord */
} return $this->owner->hasMany($this->virtualClassName, [$this->relativeField => $this->ownerPrimaryKey]);
}
public function getTranslation($language = null)
{ public function getTranslation($language = null)
//if (basename(\Yii::$app->getBasePath()) === 'backend') { {
// $language = $language ?: $this->defaultLanguage; //if (basename(\Yii::$app->getBasePath()) === 'backend') {
//} // $language = $language ?: $this->defaultLanguage;
//else { //}
$language = $language ?: \Yii::$app->language; //else {
//} $language = $language ?: \Yii::$app->language;
// if translate exists //}
$translate = $this->virtualClassName::find() // if translate exists
->andWhere([$this->relativeField => $this->owner->id]) $translate = $this->virtualClassName::find()
->andWhere([$this->languageField => $language]) ->andWhere([$this->relativeField => $this->owner->id])
->one(); ->andWhere([$this->languageField => $language])
->one();
$language = $translate ? $language : $this->defaultLanguage;
$language = $translate ? $language : $this->defaultLanguage;
return $this->owner->hasOne($this->virtualClassName, [$this->relativeField => $this->ownerPrimaryKey])
->where([$this->languageField => $language]); return $this->owner->hasOne($this->virtualClassName, [$this->relativeField => $this->ownerPrimaryKey])
} ->where([$this->languageField => $language]);
}
public function findTranslation($language = null)
{ public function findTranslation($language = null)
$language = $language ?: $this->defaultLanguage; {
//$class = call_user_func(array($this->virtualClassName, 'getInstance')); $language = $language ?: $this->defaultLanguage;
return $this->virtualClassName::find()
->andWhere([$this->relativeField => $this->owner->id]) //$class = call_user_func(array($this->virtualClassName, 'getInstance'));
->andWhere([$this->languageField => $language]) return $this->virtualClassName::find()
->one(); ->andWhere([$this->relativeField => $this->owner->id])
} ->andWhere([$this->languageField => $language])
->one();
public function hasLangAttribute($name) }
{
return array_key_exists($name, $this->languageAttributes); public function hasLangAttribute($name)
} {
return array_key_exists($name, $this->_languageAttributes);
public function getLangAttribute($name) }
{
return $this->hasLangAttribute($name) ? $this->languageAttributes[$name] : null; public function getLangAttribute($name)
} {
return $this->hasLangAttribute($name) ? $this->_languageAttributes[$name] : null;
}
} }

156
core/components/LanguageDynamicModel.php

@ -6,97 +6,99 @@
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 = [])
{
$used_attributes = $this->getStringAttributes();
parent::__construct(array_merge($this->getPublicAttributes(), $this->prepareLanguageAttributes()), $config);
public function __construct( ActiveRecord $entity = null, array $attributes = [], array $config = [] ) if ($entity) {
{ foreach ($used_attributes as $used_attribute) {
$used_attributes = $this->getStringAttributes(); foreach ($entity->translations as $translate) {
parent::__construct( array_merge($this->getPublicAttributes(), $this->prepareLanguageAttributes()), $config ); $defaultLanguage = basename(\Yii::$app->getBasePath()) === 'backend'
?
Yii::$app->params['backendDefaultLanguage']
:
Yii::$app->params['defaultLanguage'];
if ($entity) { $languageAttribute = $used_attribute . '_' . $translate->language;
foreach ($used_attributes as $used_attribute) {
foreach ( $entity->translations as $translate ) {
$defaultLanguage = basename( \Yii::$app->getBasePath() ) === 'backend'
?
Yii::$app->params['backendDefaultLanguage']
:
Yii::$app->params['defaultLanguage'];
$languageAttribute = $used_attribute . '_' . $translate->language; if ($translate->language === $defaultLanguage && isset($translate->{$used_attribute})) {
$this->{$used_attribute} = $translate->{$used_attribute};
} elseif (isset($translate->{$used_attribute})) {
$this->{$languageAttribute} = $translate->{$used_attribute};
}
}
}
}
}
if ( $translate->language === $defaultLanguage && isset($translate->{$used_attribute})) { private function getStringAttributes()
$this->{$used_attribute} = $translate->{$used_attribute}; {
} $string_attributes = [];
elseif (isset($translate->{$used_attribute})) { foreach ($this->rules() as $rule) {
$this->{$languageAttribute} = $translate->{$used_attribute}; $attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]];
} $type = $rule[1];
} if ($type == 'string') {
} foreach ($attributes as $attribute) {
} $string_attributes[] = $attribute;
} }
}
}
private function getStringAttributes() return $string_attributes;
{ }
$string_attributes = [];
foreach ($this->rules() as $rule) {
$attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]];
$type = $rule[1];
if ($type == 'string') {
foreach ($attributes as $attribute) {
$string_attributes[] = $attribute;
}
}
}
return $string_attributes;
}
private function prepareLanguageAttributes() private function prepareLanguageAttributes()
{ {
$language_attributes = []; $language_attributes = [];
$labels = $this->attributeLabels(); $labels = $this->attributeLabels();
$hints = $this->attributeHints(); $hints = $this->attributeHints();
foreach ($this->rules() as $rule) { foreach ($this->rules() as $rule) {
$attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]]; $attributes = is_array($rule[0]) ? $rule[0] : [$rule[0]];
$type = $rule[1]; $type = $rule[1];
if ($type == 'string') { if ($type == 'string') {
foreach (Yii::$app->params['translatedLanguages'] as $language => $language_name) { foreach (Yii::$app->params['translatedLanguages'] as $language => $language_name) {
$rule_attributes = []; $rule_attributes = [];
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;
}
public function attributeLabels(){ return $language_attributes;
return $this->new_labels; }
}
public function rules() { public function attributeLabels()
return $this->new_rules; {
} return $this->_new_labels;
}
public function getPublicAttributes () { public function rules()
return call_user_func('get_object_vars', $this); {
} return $this->_new_rules;
}
public function getPublicAttributes()
{
return call_user_func('get_object_vars', $this);
}
} }

141
core/components/modules/ModuleManager.php

@ -13,83 +13,86 @@ use yii\helpers\FileHelper;
class ModuleManager 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(); {
$connection = Yii::$app->getDb(); $time = time();
$command = $connection->createCommand("INSERT INTO migration (version, apply_time) VALUE ('$name', '$time')"); $connection = Yii::$app->getDb();
$command->execute(); $command = $connection->createCommand("INSERT INTO migration (version, apply_time) VALUE ('$name', '$time')");
} $command->execute();
}
public function removeFromMigrationTable($name) { public function removeFromMigrationTable($name)
$connection = Yii::$app->getDb(); {
$command = $connection->createCommand("DELETE FROM migration WHERE version = '$name'"); $connection = Yii::$app->getDb();
$command->execute(); $command = $connection->createCommand("DELETE FROM migration WHERE version = '$name'");
} $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->description = $local_module['description']; $db_module = $this->_service->create($local_module['name'], 'common\\modules\\' . $local_module['name'] . '\\' . $local_module['module']);
$modules[] = $db_module; $db_module->description = $local_module['description'];
} $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;
}
public function getLocalModules() return $modules;
{ }
$this->_getLocalModulesNames();
if (empty($this->modules)) { public function getLocalModules()
foreach ( $this->moduleNames as $module_name ) { {
$manifest = Yii::getAlias( '@common/modules/' . $module_name . '/manifest.php' ); $this->getLocalModulesNames();
if ( file_exists( $manifest ) ) {
$this->modules[] = require $manifest;
}
}
}
return $this->modules;
}
private function _getLocalModulesNames(): void if (empty($this->modules)) {
{ foreach ($this->moduleNames as $module_name) {
if (!empty($this->moduleNames)) { $manifest = Yii::getAlias('@common/modules/' . $module_name . '/manifest.php');
return; if (file_exists($manifest)) {
} $this->modules[] = require $manifest;
}
}
}
$names = []; return $this->modules;
$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 private function getLocalModulesNames(): void
{ {
return Yii::$app->db->schema->getTableSchema($name) !== null; 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;
}
}

13
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();
@ -374,13 +379,11 @@ SH;
echo Console::log($this->l('Complete'), 'green') . PHP_EOL; echo Console::log($this->l('Complete'), 'green') . PHP_EOL;
} }
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