-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a simple token migration for dashes
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Terminal42\NotificationCenterBundle\Migration; | ||
|
||
use Contao\CoreBundle\Migration\AbstractMigration; | ||
use Contao\CoreBundle\Migration\MigrationResult; | ||
use Doctrine\DBAL\Connection; | ||
|
||
class SimpleTokenMigration extends AbstractMigration | ||
{ | ||
private const REGEX = '/##([a-zA-Z0-9_]+-[a-zA-Z0-9_-]*)##/'; | ||
|
||
public function __construct(private readonly Connection $connection) | ||
{ | ||
} | ||
|
||
public function shouldRun(): bool | ||
{ | ||
$schemaManager = $this->connection->createSchemaManager(); | ||
|
||
if (!$schemaManager->tablesExist(['tl_nc_language'])) { | ||
return false; | ||
} | ||
|
||
return [] !== $this->getRowsToUpdate(); | ||
} | ||
|
||
public function run(): MigrationResult | ||
{ | ||
foreach ($this->getRowsToUpdate() as $rowId => $columns) { | ||
$rows = $this->connection->fetchAssociative('SELECT '.implode(',', $columns).' FROM tl_nc_language WHERE id=?', [$rowId]); | ||
$set = []; | ||
|
||
foreach ($rows as $column => $value) { | ||
$set[$column] = preg_replace_callback( | ||
self::REGEX, | ||
static fn ($matches) => '##'.str_replace('-', '_', $matches[1]).'##', | ||
$value, | ||
); | ||
} | ||
|
||
$this->connection->update('tl_nc_language', $set, ['id' => $rowId]); | ||
} | ||
|
||
return $this->createResult(true); | ||
} | ||
|
||
private function getRowsToUpdate(): array | ||
{ | ||
$rowsToUpdate = []; | ||
|
||
foreach ($this->connection->fetchAllAssociative('SELECT * FROM tl_nc_language') as $row) { | ||
foreach ($row as $column => $value) { | ||
if (!\is_string($value)) { | ||
continue; | ||
} | ||
|
||
if (preg_match(self::REGEX, $value)) { | ||
$rowsToUpdate[$row['id']][] = $this->connection->quoteIdentifier($column); | ||
} | ||
} | ||
} | ||
|
||
return $rowsToUpdate; | ||
} | ||
} |