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. 42
      core/components/LanguageDynamicModel.php
  5. 33
      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\web\NotFoundHttpException;
/**
* Modules Management
* Class ModuleController
* @package backend\controllers
*/
class ModuleController extends Controller
{
/**
* @var ModuleService Modules management service
*/
private $_service;
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()
{
$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
* @param $name
*/

32
core/behaviors/LanguageBehavior.php

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

42
core/components/LanguageDynamicModel.php

@ -6,26 +6,25 @@
namespace core\components;
use yii\base\DynamicModel;
use Yii;
use yii\db\ActiveRecord;
class LanguageDynamicModel extends DynamicModel
{
private $new_labels = [];
private $new_hints = [];
private $new_rules = [];
private $_new_labels = [];
private $_new_hints = [];
private $_new_rules = [];
public function __construct( ActiveRecord $entity = null, array $attributes = [], array $config = [] )
public function __construct(ActiveRecord $entity = null, array $attributes = [], array $config = [])
{
$used_attributes = $this->getStringAttributes();
parent::__construct( array_merge($this->getPublicAttributes(), $this->prepareLanguageAttributes()), $config );
parent::__construct(array_merge($this->getPublicAttributes(), $this->prepareLanguageAttributes()), $config);
if ($entity) {
foreach ($used_attributes as $used_attribute) {
foreach ( $entity->translations as $translate ) {
$defaultLanguage = basename( \Yii::$app->getBasePath() ) === 'backend'
foreach ($entity->translations as $translate) {
$defaultLanguage = basename(\Yii::$app->getBasePath()) === 'backend'
?
Yii::$app->params['backendDefaultLanguage']
:
@ -33,10 +32,9 @@ class LanguageDynamicModel extends DynamicModel
$languageAttribute = $used_attribute . '_' . $translate->language;
if ( $translate->language === $defaultLanguage && isset($translate->{$used_attribute})) {
if ($translate->language === $defaultLanguage && isset($translate->{$used_attribute})) {
$this->{$used_attribute} = $translate->{$used_attribute};
}
elseif (isset($translate->{$used_attribute})) {
} elseif (isset($translate->{$used_attribute})) {
$this->{$languageAttribute} = $translate->{$used_attribute};
}
}
@ -56,6 +54,7 @@ class LanguageDynamicModel extends DynamicModel
}
}
}
return $string_attributes;
}
@ -73,30 +72,33 @@ class LanguageDynamicModel extends DynamicModel
foreach ($attributes as $attribute) {
// add attribute
$language_attributes[] = $attribute . '_' . $language;
$this->new_labels[$attribute . '_' . $language] = isset($labels[$attribute]) ? $labels[$attribute] : null;
$this->new_hints[$attribute . '_' . $language] = isset($hints[$attribute]) ? $hints[$attribute] : null;
$this->_new_labels[$attribute . '_' . $language] = isset($labels[$attribute]) ? $labels[$attribute] : null;
$this->_new_hints[$attribute . '_' . $language] = isset($hints[$attribute]) ? $hints[$attribute] : null;
$rule_attributes[] = $attribute . '_' . $language;
}
// add rule
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 $this->new_labels;
public function attributeLabels()
{
return $this->_new_labels;
}
public function rules() {
return $this->new_rules;
public function rules()
{
return $this->_new_rules;
}
public function getPublicAttributes () {
public function getPublicAttributes()
{
return call_user_func('get_object_vars', $this);
}
}

33
core/components/modules/ModuleManager.php

@ -16,60 +16,64 @@ class ModuleManager
public $moduleNames = [];
public $modules = [];
private $service;
private $_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();
$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'");
$command->execute();
}
public function getModules()
public function getModules() : array
{
$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 = ModuleRecord::find()->andWhere(['name' => $local_module['name']])->one();
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'];
$modules[] = $db_module;
}
else {
} else {
$db_module->description = $local_module['description'];
$modules[] = $db_module;
}
}
return $modules;
}
public function getLocalModules()
{
$this->_getLocalModulesNames();
$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 ) ) {
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
private function getLocalModulesNames(): void
{
if (!empty($this->moduleNames)) {
return;
@ -91,5 +95,4 @@ class ModuleManager
{
return Yii::$app->db->schema->getTableSchema($name) !== null;
}
}

13
setup.php

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

Loading…
Cancel
Save