Skip to content

Commit

Permalink
Replace runonce with Contao migrations, fixes #76
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Jun 25, 2021
1 parent a9ad0b4 commit b6eac72
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 171 deletions.
27 changes: 27 additions & 0 deletions src/DependencyInjection/RockSolidSliderExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* 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');
}
}
91 changes: 91 additions & 0 deletions src/Migration/SliderPermissionsMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* 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);
}
}
8 changes: 8 additions & 0 deletions src/Resources/config/migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
autoconfigure: true

MadeYourDay\RockSolidSlider\Migration\SliderPermissionsMigration:
arguments:
- '@database_connection'
- '@contao.framework'
3 changes: 0 additions & 3 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
168 changes: 0 additions & 168 deletions src/SliderRunonce.php

This file was deleted.

0 comments on commit b6eac72

Please sign in to comment.