Skip to content

Commit

Permalink
[TASK] adds TYPO3 v13 compatibility
Browse files Browse the repository at this point in the history
[CLEANUP] updates phpstan baseline and updates classes accordingly
  • Loading branch information
EvilBMP committed Sep 17, 2024
1 parent 32cb7e0 commit 9491988
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 55 deletions.
11 changes: 10 additions & 1 deletion Build/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
includes:
- %currentWorkingDirectory%/.Build/vendor/phpstan/phpstan-strict-rules/rules.neon
- %currentWorkingDirectory%/.Build/vendor/phpstan/phpstan-deprecation-rules/rules.neon
- %currentWorkingDirectory%/.Build/vendor/friendsoftypo3/phpstan-typo3/extension.neon

parameters:
level: 5
level: 8

paths:
- %currentWorkingDirectory%/Classes

ignoreErrors:
-
identifier: missingType.generics
86 changes: 58 additions & 28 deletions Classes/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Portrino\PxDbmigrator\Command;

use Portrino\PxDbmigrator\DirectoryIterator\SortableDirectoryIterator;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -18,7 +17,7 @@
class MigrateCommand extends Command
{
/**
* @var array
* @var array<string, mixed>
*/
protected $extConf;

Expand All @@ -34,7 +33,7 @@ class MigrateCommand extends Command

protected function configure()
{
$this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('px_dbmigrator');
$this->extConf = (GeneralUtility::makeInstance(ExtensionConfiguration::class))->get('px_dbmigrator');
$this->registry = GeneralUtility::makeInstance(Registry::class);

$this->setDescription(
Expand All @@ -48,7 +47,7 @@ protected function configure()
* @return int
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

Expand All @@ -57,10 +56,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$pathFromConfig = Environment::getPublicPath() . DIRECTORY_SEPARATOR . $this->extConf['migrationFolderPath'];
$migrationFolderPath = realpath($pathFromConfig);

if (!$migrationFolderPath) {
if ($migrationFolderPath === false) {
GeneralUtility::mkdir_deep($pathFromConfig);
$migrationFolderPath = realpath($pathFromConfig);
if (!$migrationFolderPath) {
if ($migrationFolderPath === false) {
$io->writeln(
sprintf(
'<fg=red>Migration folder not found. Please make sure "%s" exists!</>',
Expand All @@ -80,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$errors = [];
$executedFiles = 0;

/** @var SplFileInfo $fileInfo */
/** @var \SplFileInfo $fileInfo */
foreach ($iterator as $fileInfo) {
$fileVersion = (int)$fileInfo->getBasename('.' . $fileInfo->getExtension());

Expand All @@ -94,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
['tstamp' => null, 'success' => false]
);

if ($migrationStatus['success']) {
if ($migrationStatus['success'] === true) {
// already successfully executed
continue;
}
Expand Down Expand Up @@ -142,16 +141,16 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->outputMessages($executedFiles, $errors, $io);

return empty($errors) ? Command::SUCCESS : Command::FAILURE;
return count($errors) === 0 ? Command::SUCCESS : Command::FAILURE;
}

/**
* @param SplFileInfo $fileInfo
* @param array $errors
* @param \SplFileInfo $fileInfo
* @param string[] $errors
* @param string $output
* @return bool
*/
protected function migrateSqlFile(SplFileInfo $fileInfo, array &$errors, string &$output): bool
protected function migrateSqlFile(\SplFileInfo $fileInfo, array &$errors, string &$output): bool
{
$filePath = $fileInfo->getPathname();

Expand All @@ -165,11 +164,16 @@ protected function migrateSqlFile(SplFileInfo $fileInfo, array &$errors, string
$filePath
);

if ($this->isExecutableWithinPath($this->extConf['mysqlBinaryPath']) === false) {
$errors[] = 'MySQL binary not found or not executable. Please check your configuration.';
return false;
}

$output .= shell_exec($shellCommand);

$outputMessages = explode("\n", $output);
$outputMessages = explode(PHP_EOL, $output);
foreach ($outputMessages as $outputMessage) {
if (trim($outputMessage) && strpos($outputMessage, 'ERROR') !== false) {
if (trim($outputMessage) !== '' && str_contains($outputMessage, 'ERROR')) {
$errors[] = $outputMessage;
}
}
Expand All @@ -178,21 +182,25 @@ protected function migrateSqlFile(SplFileInfo $fileInfo, array &$errors, string
}

/**
* @param SplFileInfo $fileInfo
* @param array $errors
* @param \SplFileInfo $fileInfo
* @param string[] $errors
* @param string $output
* @return bool
*/
protected function migrateTypo3CmsFile(SplFileInfo $fileInfo, array &$errors, string &$output): bool
protected function migrateTypo3CmsFile(\SplFileInfo $fileInfo, array &$errors, string &$output): bool
{
$migrationContent = file_get_contents($fileInfo->getPathname());
if ($migrationContent === false) {
$errors[] = 'Could not read file ' . $fileInfo->getFilename();
return false;
}
foreach (explode(PHP_EOL, $migrationContent) as $line) {
$line = trim($line);
if (!empty($line) && strpos($line, '#') !== 0 && strpos($line, '//') !== 0) {
if ($line !== '' && !str_starts_with($line, '#') && !str_starts_with($line, '//')) {
$outputLines = [];
$status = null;
$shellCommand =
($this->extConf['typo3cmsBinaryPath'] ? : './vendor/bin/typo3cms')
($this->extConf['typo3cmsBinaryPath'] !== '' ? $this->extConf['typo3cmsBinaryPath'] : './vendor/bin/typo3cms')
. ' '
. $line
. ' 2>&1';
Expand All @@ -211,12 +219,12 @@ protected function migrateTypo3CmsFile(SplFileInfo $fileInfo, array &$errors, st
}

/**
* @param SplFileInfo $fileInfo
* @param array $errors
* @param \SplFileInfo $fileInfo
* @param string[] $errors
* @param string $output
* @return bool
*/
protected function migrateShellFile(SplFileInfo $fileInfo, array &$errors, string &$output): bool
protected function migrateShellFile(\SplFileInfo $fileInfo, array &$errors, string &$output): bool
{
$command = '/usr/bin/env sh ' . $fileInfo->getPathname() . ' 2>&1';
$outputLines = [];
Expand All @@ -234,15 +242,15 @@ protected function migrateShellFile(SplFileInfo $fileInfo, array &$errors, strin

/**
* @param int $executedFiles
* @param array $errors
* @param array<string, string[]> $errors
* @param SymfonyStyle $io
*/
protected function outputMessages(int $executedFiles, array $errors, SymfonyStyle $io): void
{
if ($executedFiles === 0 && count($errors) === 0) {
$io->writeln('Everything up to date. No migrations needed.');
} else {
if ($executedFiles) {
if ($executedFiles > 0) {
$io->writeln(
sprintf(
'<fg=green>Migration of %d file%s completed.</>',
Expand All @@ -253,7 +261,7 @@ protected function outputMessages(int $executedFiles, array $errors, SymfonyStyl
} else {
$io->writeln('<fg=red>Migration failed</>');
}
if (count($errors)) {
if (count($errors) > 0) {
$io->writeln(sprintf('<fg=red>The following error%s occured:</>', (count($errors) > 1 ? 's' : '')));
foreach ($errors as $filename => $error) {
$io->writeln(sprintf('File %s: ', $filename));
Expand All @@ -263,17 +271,17 @@ protected function outputMessages(int $executedFiles, array $errors, SymfonyStyl
}
}

protected function updateObsoleteRegistryNamespace()
protected function updateObsoleteRegistryNamespace(): void
{
$result = GeneralUtility::makeInstance(ConnectionPool::class)
$result = (GeneralUtility::makeInstance(ConnectionPool::class))
->getConnectionForTable('sys_registry')
->count(
'uid',
'sys_registry',
['entry_namespace' => 'Appzap\\Migrator']
);
if ($result > 0) {
GeneralUtility::makeInstance(ConnectionPool::class)
(GeneralUtility::makeInstance(ConnectionPool::class))
->getConnectionForTable('sys_registry')
->update(
'sys_registry',
Expand All @@ -282,4 +290,26 @@ protected function updateObsoleteRegistryNamespace()
);
}
}

protected function isExecutableWithinPath(string $filename): bool
{
if (is_executable($filename)) {
return true;
}

if ($filename !== basename($filename)) {
return false;
}

$env = getenv('PATH');
if ($env !== false) {
$paths = explode(PATH_SEPARATOR, $env);
foreach ($paths as $path) {
if (is_executable($path . DIRECTORY_SEPARATOR . $filename)) {
return true;
}
}
}
return false;
}
}
17 changes: 6 additions & 11 deletions Classes/DirectoryIterator/SortableDirectoryIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@

namespace Portrino\PxDbmigrator\DirectoryIterator;

use ArrayObject;
use DirectoryIterator;
use IteratorAggregate;
use Traversable;

class SortableDirectoryIterator implements IteratorAggregate
class SortableDirectoryIterator implements \IteratorAggregate
{
/**
* @var ArrayObject
* @var \ArrayObject
*/
private $_storage;

public function __construct(string $path)
{
$this->_storage = new ArrayObject();
$this->_storage = new \ArrayObject();

$files = new DirectoryIterator($path);
/** @var DirectoryIterator $file */
$files = new \DirectoryIterator($path);
/** @var \DirectoryIterator $file */
foreach ($files as $file) {
if ($file->isDot()) {
continue;
Expand All @@ -33,7 +28,7 @@ function ($a, $b) {
);
}

public function getIterator(): Traversable
public function getIterator(): \Traversable
{
return $this->_storage->getIterator();
}
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# TYPO3 extension `px_dbmigrator`

[![Latest Stable Version](https://poser.pugx.org/portrino/px_dbmigrator/v/stable)](https://packagist.org/packages/portrino/px_dbmigrator)
[![TYPO3 10](https://img.shields.io/badge/TYPO3-10-orange.svg)](https://get.typo3.org/version/10)
[![TYPO3 11](https://img.shields.io/badge/TYPO3-11-orange.svg)](https://get.typo3.org/version/11)
[![TYPO3 12](https://img.shields.io/badge/TYPO3-12-orange.svg)](https://get.typo3.org/version/12)
[![TYPO3 13](https://img.shields.io/badge/TYPO3-13-orange.svg)](https://get.typo3.org/version/13)
[![Total Downloads](https://poser.pugx.org/portrino/px_dbmigrator/downloads)](https://packagist.org/packages/portrino/px_dbmigrator)
[![Monthly Downloads](https://poser.pugx.org/portrino/px_dbmigrator/d/monthly)](https://packagist.org/packages/portrino/px_dbmigrator)
[![CI](https://github.com/portrino/px_dbmigrator/actions/workflows/ci.yml/badge.svg)](https://github.com/portrino/px_dbmigrator/actions/workflows/ci.yml)

> Database Migrator for TYPO3
Expand Down Expand Up @@ -113,3 +112,8 @@ No, the migrator only knows one direction. You’ll need to do it manually.
## 5 Authors

* See the list of [contributors](https://github.com/portrino/px_dbmigrator/graphs/contributors) who participated in this project.

#### Based on other great extensions:

* [etobi/typo3-migrator](https://github.com/etobi/typo3-migrator) by Tobias Liebig
* [smichaelsen/typo3-migrator](https://github.com/smichaelsen/typo3-migrator) by Sebastian Michaelsen
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
"license": "GPL-2.0",
"type": "typo3-cms-extension",
"authors": [
{
"name": "Sebastian Michaelsen",
"email": "[email protected]",
"role": "developer"
},
{
"name": "portrino GmbH",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"typo3/cms-core": "^10.4 || ^11.5 || ^12.4"
"typo3/cms-core": "^13.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.28",
"friendsofphp/php-cs-fixer": "^3.14",
"friendsoftypo3/phpstan-typo3": "^0.9",
"helmich/typo3-typoscript-lint": "^3.1",
"php-coveralls/php-coveralls": "^2.5",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-strict-rules": "^1.5",
"seld/jsonlint": "^1.9",
"typo3/coding-standards": "^0.6"
"typo3/coding-standards": "dev-main"
},
"suggest": {
"portrino/px_dbsequencer": ""
Expand Down
10 changes: 5 additions & 5 deletions ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
'description' => 'TYPO3 DB Migrator',
'category' => 'be',
'state' => 'beta',
'author' => 'Sebastian Michaelsen, portrino GmbH',
'author_email' => '[email protected], [email protected]',
'author_company' => 'app zap, portrino GmbH',
'version' => '2.0.2',
'author' => 'portrino GmbH',
'author_email' => '[email protected]',
'author_company' => 'portrino GmbH',
'version' => '3.0.0',
'constraints' => [
'depends' => [
'typo3' => '10.4.0-12.4.99',
'typo3' => '13.0.0-13.4.99',
],
],
];

0 comments on commit 9491988

Please sign in to comment.