Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Транзакции, конструкторы (#11)
Browse files Browse the repository at this point in the history
* Конструкторы миграций

* Конструктор свойств

* Конструктор списков

* Конструктор HL

* Update README.md

* Update Migrator.php
  • Loading branch information
MsNatali authored and arrilot committed Oct 18, 2018
1 parent 95f5a38 commit 8501892
Show file tree
Hide file tree
Showing 17 changed files with 2,027 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,4 @@ Arrilot\BitrixMigrations\Autocreate\Manager::init($_SERVER["DOCUMENT_ROOT"].'/mi
2) Реализовать свой аналог ` Arrilot\BitrixMigrations\Repositories\BitrixDatabaseRepository;` и использовать его.

3) По желанию отключить существующие шаблоны миграций, сделав свои.

12 changes: 11 additions & 1 deletion migrator
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Arrilot\BitrixMigrations\Commands\MigrateCommand;
use Arrilot\BitrixMigrations\Commands\RollbackCommand;
use Arrilot\BitrixMigrations\Commands\TemplatesCommand;
use Arrilot\BitrixMigrations\Commands\StatusCommand;
use Arrilot\BitrixMigrations\Constructors\IBlock;
use Arrilot\BitrixMigrations\Migrator;
use Arrilot\BitrixMigrations\Storages\BitrixDatabaseStorage;
use Arrilot\BitrixMigrations\TemplatesCollection;
Expand All @@ -19,11 +20,20 @@ $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];
require $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php";

CModule::IncludeModule("iblock");

$config = [
'table' => 'migrations',
'dir' => './migrations',
// 'dir_archive' => 'archive', // not required. default = "archive"
'use_transaction' => true, // not required. default = false
'default_fields' => [
IBlock::class => [
'INDEX_ELEMENT' => 'N',
'INDEX_SECTION' => 'N',
'VERSION' => 2,
'SITE_ID' => 's1',
]
]
];

$database = new BitrixDatabaseStorage($config['table']);
Expand Down
19 changes: 19 additions & 0 deletions src/BaseMigrations/BitrixMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class BitrixMigration implements MigrationInterface
*/
protected $db;

/**
* @var bool
*/
public $use_transaction = null;

/**
* Constructor.
*/
Expand Down Expand Up @@ -47,6 +52,20 @@ public function down()
//
}

/**
* Does migration use transaction
* @param bool $default
* @return bool
*/
public function useTransaction($default = false)
{
if (!is_null($this->use_transaction)) {
return $this->use_transaction;
}

return $default;
}

/**
* Find iblock id by its code.
*
Expand Down
49 changes: 49 additions & 0 deletions src/Constructors/Constructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


namespace Arrilot\BitrixMigrations\Constructors;


class Constructor
{
const OBJ_USER = 'USER'; // для пользователя
const OBJ_BLOG_BLOG = 'BLOG_BLOG'; // для блога
const OBJ_BLOG_POST = 'BLOG_POST'; // для сообщения в блоге
const OBJ_BLOG_COMMENT = 'BLOG_COMMENT'; // для комментария сообщения
const OBJ_TASKS_TASK = 'TASKS_TASK'; // для задач
const OBJ_CALENDAR_EVENT = 'CALENDAR_EVENT'; // для событий календаря
const OBJ_LEARN_ATTEMPT = 'LEARN_ATTEMPT'; // для попыток теста
const OBJ_SONET_GROUP = 'SONET_GROUP'; // для групп соцсети
const OBJ_WEBDAV = 'WEBDAV'; // для библиотек документов
const OBJ_FORUM_MESSAGE = 'FORUM_MESSAGE'; // для сообщений форума

/**
* для highload-блока с ID=N
* @param $id
* @return string
*/
public static function objHLBlock($id)
{
return "HLBLOCK_{$id}";
}

/**
* для секций инфоблока с ID = N
* @param $id
* @return string
*/
public static function objIBlockSection($id)
{
return "IBLOCK_{$id}_SECTION";
}

/**
* Для инфоблока с ID = N
* @param $id
* @return string
*/
public static function objIBlock($id)
{
return "IBLOCK_{$id}";
}
}
21 changes: 21 additions & 0 deletions src/Constructors/FieldConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php


namespace Arrilot\BitrixMigrations\Constructors;


trait FieldConstructor
{
/** @var array */
public $fields = [];

public static $defaultFields = [];

/**
* Получить итоговые настройки полей
*/
public function getFieldsWithDefault()
{
return array_merge((array)static::$defaultFields[get_called_class()], $this->fields);
}
}
124 changes: 124 additions & 0 deletions src/Constructors/HighloadBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php


namespace Arrilot\BitrixMigrations\Constructors;


use Arrilot\BitrixMigrations\Helpers;
use Arrilot\BitrixMigrations\Logger;
use Bitrix\Highloadblock\HighloadBlockLangTable;
use Bitrix\Highloadblock\HighloadBlockTable;

class HighloadBlock
{
use FieldConstructor;

public $lang;

/**
* Добавить HL
* @throws \Exception
*/
public function add()
{
$result = HighloadBlockTable::add($this->getFieldsWithDefault());

if (!$result->isSuccess()) {
throw new \Exception(join(', ', $result->getErrorMessages()));
}

foreach ($this->lang as $lid => $name) {
HighloadBlockLangTable::add([
"ID" => $result->getId(),
"LID" => $lid,
"NAME" => $name
]);
}

Logger::log("Добавлен HL {$this->fields['NAME']}", Logger::COLOR_GREEN);

return $result->getId();
}

/**
* Обновить HL
* @param $table_name
* @throws \Exception
*/
public function update($table_name)
{
$id = Helpers::getHlId($table_name);
$result = HighloadBlockTable::update($id, $this->fields);

if (!$result->isSuccess()) {
throw new \Exception(join(', ', $result->getErrorMessages()));
}

Logger::log("Обновлен HL {$table_name}", Logger::COLOR_GREEN);
}

/**
* Удалить HL
* @param $table_name
* @throws \Exception
*/
public static function delete($table_name)
{
$id = Helpers::getHlId($table_name);
$result = HighloadBlockTable::delete($id);

if (!$result->isSuccess()) {
throw new \Exception(join(', ', $result->getErrorMessages()));
}

Logger::log("Удален HL {$table_name}", Logger::COLOR_GREEN);
}

/**
* Установить настройки для добавления HL по умолчанию
* @param string $name Название highload-блока
* @param string $table_name Название таблицы с элементами highload-блока.
* @return $this
*/
public function constructDefault($name, $table_name)
{
return $this->setName($name)->setTableName($table_name);
}

/**
* Название highload-блока.
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->fields['NAME'] = $name;

return $this;
}

/**
* Название таблицы с элементами highload-блока.
* @param string $table_name
* @return $this
*/
public function setTableName($table_name)
{
$this->fields['TABLE_NAME'] = $table_name;

return $this;
}

/**
* Установить локализацию
* @param $lang
* @param $text
* @return HighloadBlock
*/
public function setLang($lang, $text)
{
$this->lang[$lang] = $text;

return $this;
}
}
Loading

0 comments on commit 8501892

Please sign in to comment.