Skip to content

Commit

Permalink
Merge pull request #534 from doctrine/3.3.x
Browse files Browse the repository at this point in the history
Merge 3.3.x up into 3.4.x
  • Loading branch information
greg0ire committed Mar 26, 2024
2 parents 241682a + 6223aab commit 62db2f2
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 43 deletions.
44 changes: 22 additions & 22 deletions Collector/MigrationsCollector.php
Expand Up @@ -10,6 +10,11 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\VarDumper\Cloner\Data;
use Throwable;

use function count;
use function get_class;

class MigrationsCollector extends DataCollector
{
Expand All @@ -21,20 +26,18 @@ class MigrationsCollector extends DataCollector
public function __construct(DependencyFactory $dependencyFactory, MigrationsFlattener $migrationsFlattener)
{
$this->dependencyFactory = $dependencyFactory;
$this->flattener = $migrationsFlattener;
$this->flattener = $migrationsFlattener;
}

/**
* @return void
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
/** @return void */
public function collect(Request $request, Response $response, ?Throwable $exception = null)
{
if (!empty($this->data)) {
if (! empty($this->data)) {
return;
}

$metadataStorage = $this->dependencyFactory->getMetadataStorage();
$planCalculator = $this->dependencyFactory->getMigrationPlanCalculator();
$planCalculator = $this->dependencyFactory->getMigrationPlanCalculator();

try {
$executedMigrations = $metadataStorage->getExecutedMigrations();
Expand All @@ -49,45 +52,42 @@ public function collect(Request $request, Response $response, \Throwable $except

$availableMigrations = $planCalculator->getMigrations();

$this->data['available_migrations_count'] = count($availableMigrations);
$unavailableMigrations = $executedMigrations->unavailableSubset($availableMigrations);
$this->data['available_migrations_count'] = count($availableMigrations);
$unavailableMigrations = $executedMigrations->unavailableSubset($availableMigrations);
$this->data['unavailable_migrations_count'] = count($unavailableMigrations);

$newMigrations = $availableMigrations->newSubset($executedMigrations);
$this->data['new_migrations'] = $this->flattener->flattenAvailableMigrations($newMigrations);
$newMigrations = $availableMigrations->newSubset($executedMigrations);
$this->data['new_migrations'] = $this->flattener->flattenAvailableMigrations($newMigrations);
$this->data['executed_migrations'] = $this->flattener->flattenExecutedMigrations($executedMigrations, $availableMigrations);

$this->data['storage'] = get_class($metadataStorage);
$configuration = $this->dependencyFactory->getConfiguration();
$storage = $configuration->getMetadataStorageConfiguration();
$configuration = $this->dependencyFactory->getConfiguration();
$storage = $configuration->getMetadataStorageConfiguration();
if ($storage instanceof TableMetadataStorageConfiguration) {
$this->data['table'] = $storage->getTableName();
$this->data['table'] = $storage->getTableName();
$this->data['column'] = $storage->getVersionColumnName();
}

$connection = $this->dependencyFactory->getConnection();
$connection = $this->dependencyFactory->getConnection();
$this->data['driver'] = get_class($connection->getDriver());
$this->data['name'] = $connection->getDatabase();
$this->data['name'] = $connection->getDatabase();

$this->data['namespaces'] = $configuration->getMigrationDirectories();
}

/**
* @return string
*/
/** @return string */
public function getName()
{
return 'doctrine_migrations';
}

/** @return array<string, mixed>|Data */
public function getData()
{
return $this->data;
}

/**
* @return void
*/
/** @return void */
public function reset()
{
$this->data = [];
Expand Down
37 changes: 31 additions & 6 deletions Collector/MigrationsFlattener.php
Expand Up @@ -4,44 +4,69 @@

namespace Doctrine\Bundle\MigrationsBundle\Collector;

use DateTimeImmutable;
use Doctrine\Migrations\Metadata\AvailableMigration;
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
use Doctrine\Migrations\Metadata\ExecutedMigration;
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
use ReflectionClass;

use function array_map;

class MigrationsFlattener
{
/**
* @return array{
* version: string,
* is_new: true,
* is_unavailable: bool,
* description: string,
* executed_at: null,
* execution_time: null,
* file: string|false,
* }[]
*/
public function flattenAvailableMigrations(AvailableMigrationsList $migrationsList): array
{
return array_map(static function (AvailableMigration $migration) {
return [
'version' => (string)$migration->getVersion(),
'version' => (string) $migration->getVersion(),
'is_new' => true,
'is_unavailable' => false,
'description' => $migration->getMigration()->getDescription(),
'executed_at' => null,
'execution_time' => null,
'file' => (new \ReflectionClass($migration->getMigration()))->getFileName(),
'file' => (new ReflectionClass($migration->getMigration()))->getFileName(),
];
}, $migrationsList->getItems());
}

/**
* @return array{
* version: string,
* is_new: false,
* is_unavailable: bool,
* description: string|null,
* executed_at: DateTimeImmutable|null,
* execution_time: float|null,
* file: string|false|null,
* }[]
*/
public function flattenExecutedMigrations(ExecutedMigrationsList $migrationsList, AvailableMigrationsList $availableMigrations): array
{
return array_map(static function (ExecutedMigration $migration) use ($availableMigrations) {

$availableMigration = $availableMigrations->hasMigration($migration->getVersion())
? $availableMigrations->getMigration($migration->getVersion())->getMigration()
: null;

return [
'version' => (string)$migration->getVersion(),
'version' => (string) $migration->getVersion(),
'is_new' => false,
'is_unavailable' => !$availableMigration,
'is_unavailable' => ! $availableMigration,
'description' => $availableMigration ? $availableMigration->getDescription() : null,
'executed_at' => $migration->getExecutedAt(),
'execution_time' => $migration->getExecutionTime(),
'file' => $availableMigration ? (new \ReflectionClass($availableMigration))->getFileName() : null,
'file' => $availableMigration ? (new ReflectionClass($availableMigration))->getFileName() : null,
];
}, $migrationsList->getItems());
}
Expand Down
7 changes: 3 additions & 4 deletions DoctrineMigrationsBundle.php
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle;

use Doctrine\Bundle\MigrationsBundle\DependencyInjection\CompilerPass\ConfigureDependencyFactoryPass;
Expand All @@ -8,15 +10,12 @@

/**
* Bundle.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class DoctrineMigrationsBundle extends Bundle
{
/** @return void */
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new ConfigureDependencyFactoryPass());
$container->addCompilerPass(new ConfigureDependencyFactoryPass());
}
}
16 changes: 6 additions & 10 deletions MigrationsFactory/ContainerAwareMigrationFactory.php
Expand Up @@ -9,24 +9,20 @@
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* @deprecated This class is not compatible with Symfony >= 7
*/
use function trigger_deprecation;

/** @deprecated This class is not compatible with Symfony >= 7 */
class ContainerAwareMigrationFactory implements MigrationFactory
{
/**
* @var ContainerInterface
*/
/** @var ContainerInterface */
private $container;

/**
* @var MigrationFactory
*/
/** @var MigrationFactory */
private $migrationFactory;

public function __construct(MigrationFactory $migrationFactory, ContainerInterface $container)
{
$this->container = $container;
$this->container = $container;
$this->migrationFactory = $migrationFactory;
}

Expand Down
9 changes: 8 additions & 1 deletion phpcs.xml.dist
Expand Up @@ -11,12 +11,19 @@

<config name="php_version" value="70200"/>

<file>Collector</file>
<file>MigrationsFactory</file>
<file>DoctrineMigrationsBundle.php</file>
<file>DependencyInjection</file>
<file>Tests</file>

<exclude-pattern>Tests/Fixtures/CustomEntityManager.php</exclude-pattern>

<rule ref="Doctrine"/>
<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint" />
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint" />
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint" />
</rule>

<!-- Disable the rules that will require PHP 7.4 -->
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
Expand Down
26 changes: 26 additions & 0 deletions phpstan-baseline.neon
@@ -0,0 +1,26 @@
parameters:
ignoreErrors:
-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 1
path: Collector/MigrationsCollector.php

-
message: "#^Only booleans are allowed in a negated boolean, Doctrine\\\\Migrations\\\\AbstractMigration\\|null given\\.$#"
count: 1
path: Collector/MigrationsFlattener.php

-
message: "#^Only booleans are allowed in a ternary operator condition, Doctrine\\\\Migrations\\\\AbstractMigration\\|null given\\.$#"
count: 2
path: Collector/MigrationsFlattener.php

-
message: "#^Call to method setContainer\\(\\) on an unknown class Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareInterface\\.$#"
count: 1
path: MigrationsFactory/ContainerAwareMigrationFactory.php

-
message: "#^Class Symfony\\\\Component\\\\DependencyInjection\\\\ContainerAwareInterface not found\\.$#"
count: 2
path: MigrationsFactory/ContainerAwareMigrationFactory.php
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Expand Up @@ -2,7 +2,10 @@ parameters:
level: 7
phpVersion: 80200
paths:
- Collector
- DependencyInjection
- DoctrineMigrationsBundle.php
- MigrationsFactory
- Tests

excludePaths:
Expand All @@ -13,6 +16,7 @@ parameters:
- '~Parameter \#1 \$configs.*DoctrineMigrationsExtension::load.*~'

includes:
- phpstan-baseline.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
Expand Down
8 changes: 8 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -10,6 +10,14 @@
<code><![CDATA[$configs]]></code>
</MoreSpecificImplementedParamType>
</file>
<file src="MigrationsFactory/ContainerAwareMigrationFactory.php">
<ContainerDependency>
<code><![CDATA[ContainerInterface $container]]></code>
</ContainerDependency>
<UndefinedClass>
<code><![CDATA[ContainerAwareInterface]]></code>
</UndefinedClass>
</file>
<file src="Tests/Fixtures/Migrations/ContainerAwareMigration.php">
<UndefinedClass>
<code><![CDATA[ContainerAwareInterface]]></code>
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Expand Up @@ -10,7 +10,9 @@
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="Collector" />
<directory name="DependencyInjection" />
<directory name="MigrationsFactory" />
<directory name="Tests" />
<file name="DoctrineMigrationsBundle.php" />
<ignoreFiles>
Expand Down

0 comments on commit 62db2f2

Please sign in to comment.