From b6eac725c5b9376a74dcbcc0e1b3c3a40a267462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Fri, 25 Jun 2021 09:12:44 +0200 Subject: [PATCH] Replace runonce with Contao migrations, fixes #76 --- .../RockSolidSliderExtension.php | 27 +++ src/Migration/SliderPermissionsMigration.php | 91 ++++++++++ src/Resources/config/migrations.yml | 8 + src/Resources/contao/config/config.php | 3 - src/SliderRunonce.php | 168 ------------------ 5 files changed, 126 insertions(+), 171 deletions(-) create mode 100644 src/DependencyInjection/RockSolidSliderExtension.php create mode 100644 src/Migration/SliderPermissionsMigration.php create mode 100644 src/Resources/config/migrations.yml delete mode 100644 src/SliderRunonce.php diff --git a/src/DependencyInjection/RockSolidSliderExtension.php b/src/DependencyInjection/RockSolidSliderExtension.php new file mode 100644 index 0000000..7f52389 --- /dev/null +++ b/src/DependencyInjection/RockSolidSliderExtension.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace MadeYourDay\RockSolidSlider\DependencyInjection; + +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; + +class RockSolidSliderExtension extends Extension +{ + public function load(array $configs, ContainerBuilder $container): void + { + $loader = new YamlFileLoader( + $container, + new FileLocator(__DIR__.'/../Resources/config') + ); + + $loader->load('migrations.yml'); + } +} diff --git a/src/Migration/SliderPermissionsMigration.php b/src/Migration/SliderPermissionsMigration.php new file mode 100644 index 0000000..8d41f49 --- /dev/null +++ b/src/Migration/SliderPermissionsMigration.php @@ -0,0 +1,91 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace MadeYourDay\RockSolidSlider\Migration; + +use Contao\CoreBundle\Framework\ContaoFramework; +use Contao\CoreBundle\Migration\AbstractMigration; +use Contao\CoreBundle\Migration\MigrationResult; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Types\BlobType; +use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Types\Types; +use MadeYourDay\RockSolidSlider\Slider; + +/** + * @internal + */ +class SliderPermissionsMigration extends AbstractMigration +{ + /** + * @var Connection + */ + private $connection; + + /** + * @var ContaoFramework + */ + private $framework; + + public function __construct(Connection $connection, ContaoFramework $framework) + { + $this->connection = $connection; + $this->framework = $framework; + } + + public function shouldRun(): bool + { + $schemaManager = $this->connection->getSchemaManager(); + + if ( + !$schemaManager->tablesExist('tl_rocksolid_slider') + || !$schemaManager->tablesExist('tl_user') + || !$schemaManager->tablesExist('tl_user_group') + ) { + return false; + } + + $columnsUser = $schemaManager->listTableColumns('tl_user'); + $columnsGroup = $schemaManager->listTableColumns('tl_user_group'); + + if ( + isset($columnsUser['rsts_sliders']) + || isset($columnsUser['rsts_permissions']) + || isset($columnsGroup['rsts_sliders']) + || isset($columnsGroup['rsts_permissions']) + ) { + return false; + } + + $this->framework->initialize(); + + return Slider::checkLicense(); + } + + public function run(): MigrationResult + { + $defaultPermissions = serialize(['create', 'delete']); + $defaultSliders = serialize(array_values($this->connection->fetchFirstColumn("SELECT id FROM tl_rocksolid_slider"))); + + foreach (['tl_user', 'tl_user_group'] as $table) { + foreach ([ + "ALTER TABLE $table ADD rsts_permissions BLOB DEFAULT NULL", + "ALTER TABLE $table ADD rsts_sliders BLOB DEFAULT NULL", + ] as $query) { + $this->connection->executeStatement($query); + } + $this->connection->executeStatement( + "UPDATE $table SET rsts_permissions = ?, rsts_sliders = ?", + [$defaultPermissions, $defaultSliders], + [Types::BLOB, Types::BLOB] + ); + } + + return $this->createResult(true); + } +} diff --git a/src/Resources/config/migrations.yml b/src/Resources/config/migrations.yml new file mode 100644 index 0000000..a0a23d4 --- /dev/null +++ b/src/Resources/config/migrations.yml @@ -0,0 +1,8 @@ +services: + _defaults: + autoconfigure: true + + MadeYourDay\RockSolidSlider\Migration\SliderPermissionsMigration: + arguments: + - '@database_connection' + - '@contao.framework' diff --git a/src/Resources/contao/config/config.php b/src/Resources/contao/config/config.php index da365e5..4ffe165 100644 --- a/src/Resources/contao/config/config.php +++ b/src/Resources/contao/config/config.php @@ -33,8 +33,5 @@ ) )); -// TODO: Replace with migration services -$GLOBALS['TL_HOOKS']['sqlCompileCommands'][] = array('MadeYourDay\\RockSolidSlider\\SliderRunonce', 'onSqlCompileCommands'); - $GLOBALS['TL_PERMISSIONS'][] = 'rsts_sliders'; $GLOBALS['TL_PERMISSIONS'][] = 'rsts_permissions'; diff --git a/src/SliderRunonce.php b/src/SliderRunonce.php deleted file mode 100644 index aed5e83..0000000 --- a/src/SliderRunonce.php +++ /dev/null @@ -1,168 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace MadeYourDay\RockSolidSlider; - -/** - * RockSolid Slider Runonce - * - * @author Martin Auswöger - */ -class SliderRunonce -{ - public function onSqlCompileCommands($sql) - { - return static::run($sql); - } - - /** - * Run database migrations - * - * @return array - */ - public static function run($sql = []) - { - $database = \Database::getInstance(); - - // Copy license key from extension repository - if ( - !\Config::get('rocksolid_slider_license') - && $database->tableExists('tl_repository_installs') - && $database->fieldExists('lickey', 'tl_repository_installs') - && $database->fieldExists('extension', 'tl_repository_installs') - ) { - $result = $database->prepare('SELECT lickey FROM tl_repository_installs WHERE extension = \'rocksolid-slider-pro\'')->execute(); - if ( - $result - && $result->lickey - && class_exists('MadeYourDay\\RockSolidSlider\\Slider') - && Slider::checkLicense((string)$result->lickey) - ) { - \Config::getInstance()->add( - '$GLOBALS[\'TL_CONFIG\'][\'rocksolid_slider_license\']', - (string)$result->lickey - ); - } - } - - // Update the multiSRC and orderSRC field from IDs to UUIDs - if ($database->tableExists('tl_rocksolid_slider') && class_exists('Database\\Updater')) { - - $needUpdate = true; - $result = $database->prepare('SELECT multiSRC FROM tl_rocksolid_slider WHERE multiSRC != \'\'')->execute(); - - if (!$result->count()) { - $needUpdate = false; - } - - while ($result->next()) { - foreach (deserialize($result->multiSRC, true) as $value) { - if (\Validator::isUuid($value)) { - $needUpdate = false; - break 2; - } - } - } - - if ($needUpdate) { - \Database\Updater::convertMultiField('tl_rocksolid_slider', 'multiSRC'); - \Database\Updater::convertOrderField('tl_rocksolid_slider', 'orderSRC'); - } - - } - - // Update the singleSRC field from IDs to UUIDs - if ($database->tableExists('tl_rocksolid_slide') && class_exists('Database\\Updater')) { - $fields = $database->listFields('tl_rocksolid_slide'); - foreach ($fields as $field) { - if ($field['name'] === 'singleSRC' && $field['type'] !== 'binary') { - \Database\Updater::convertSingleField('tl_rocksolid_slide', 'singleSRC'); - } - } - } - - // Initialize the slider type field - if ( - $database->tableExists('tl_rocksolid_slider') - && $database->tableExists('tl_rocksolid_slide') - && $database->fieldExists('pid', 'tl_rocksolid_slide') - ) { - if (!$database->fieldExists('type', 'tl_rocksolid_slider')) { - $database->query('ALTER TABLE tl_rocksolid_slider ADD type varchar(255) NOT NULL default \'\''); - } - if ($database->prepare('SELECT id FROM tl_rocksolid_slider WHERE type = \'\'')->execute()->count()) { - $database->query("UPDATE tl_rocksolid_slider - SET type = CASE - WHEN EXISTS (SELECT id FROM tl_rocksolid_slide - WHERE tl_rocksolid_slide.pid = tl_rocksolid_slider.id - ) - THEN 'content' - ELSE 'image' - END - WHERE type = '' - "); - } - } - - // Initialize the slide type field - if ( - $database->tableExists('tl_rocksolid_slide') - && $database->tableExists('tl_content') - && $database->fieldExists('pid', 'tl_content') - && $database->fieldExists('ptable', 'tl_content') - ) { - if (!$database->fieldExists('type', 'tl_rocksolid_slide')) { - $database->query('ALTER TABLE tl_rocksolid_slide ADD type varchar(255) NOT NULL default \'\''); - } - if ($database->prepare('SELECT id FROM tl_rocksolid_slide WHERE type = \'\'')->execute()->count()) { - $database->query("UPDATE tl_rocksolid_slide - SET type = CASE - WHEN EXISTS (SELECT id FROM tl_content - WHERE tl_content.ptable = 'tl_rocksolid_slide' - AND tl_content.pid = tl_rocksolid_slide.id - ) - THEN 'content' - WHEN videoURL != '' OR videos IS NOT NULL - THEN 'video' - ELSE 'image' - END - WHERE type = '' - "); - } - } - - // Initialize the permissions fields - if ( - Slider::checkLicense() - && $database->tableExists('tl_rocksolid_slider') - && $database->tableExists('tl_user') - && $database->tableExists('tl_user_group') - && !$database->fieldExists('rsts_sliders', 'tl_user') - && !$database->fieldExists('rsts_permissions', 'tl_user') - && !$database->fieldExists('rsts_sliders', 'tl_user_group') - && !$database->fieldExists('rsts_permissions', 'tl_user_group') - ) { - $defaultPermissions = serialize(['create', 'delete']); - $defaultSliders = serialize(array_values($database->query("SELECT id FROM tl_rocksolid_slider")->fetchEach('id'))); - foreach (['tl_user', 'tl_user_group'] as $table) { - foreach ([ - "ALTER TABLE $table ADD rsts_permissions BLOB DEFAULT NULL", - "ALTER TABLE $table ADD rsts_sliders BLOB DEFAULT NULL", - ] as $query) { - if (($key = array_search($query, $sql['ALTER_ADD'] ?? [], true)) !== false) { - unset($sql['ALTER_ADD'][$key]); - } - $database->query($query); - } - $database->prepare("UPDATE $table SET rsts_permissions = ?, rsts_sliders = ?")->execute($defaultPermissions, $defaultSliders); - } - } - - return $sql; - } -}