The start of something beautiful
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Bundle\MigrationsBundle\Collector;
|
||||
|
||||
use Doctrine\DBAL\Exception;
|
||||
use Doctrine\Migrations\DependencyFactory;
|
||||
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
|
||||
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
|
||||
{
|
||||
/** @var DependencyFactory */
|
||||
private $dependencyFactory;
|
||||
/** @var MigrationsFlattener */
|
||||
private $flattener;
|
||||
|
||||
public function __construct(DependencyFactory $dependencyFactory, MigrationsFlattener $migrationsFlattener)
|
||||
{
|
||||
$this->dependencyFactory = $dependencyFactory;
|
||||
$this->flattener = $migrationsFlattener;
|
||||
}
|
||||
|
||||
/** @return void */
|
||||
public function collect(Request $request, Response $response, ?Throwable $exception = null)
|
||||
{
|
||||
if (! empty($this->data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$metadataStorage = $this->dependencyFactory->getMetadataStorage();
|
||||
$planCalculator = $this->dependencyFactory->getMigrationPlanCalculator();
|
||||
|
||||
try {
|
||||
$executedMigrations = $metadataStorage->getExecutedMigrations();
|
||||
} catch (Exception $dbalException) {
|
||||
$this->dependencyFactory->getLogger()->error(
|
||||
'error while trying to collect executed migrations',
|
||||
['exception' => $dbalException]
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$availableMigrations = $planCalculator->getMigrations();
|
||||
|
||||
$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);
|
||||
$this->data['executed_migrations'] = $this->flattener->flattenExecutedMigrations($executedMigrations, $availableMigrations);
|
||||
|
||||
$this->data['storage'] = get_class($metadataStorage);
|
||||
$configuration = $this->dependencyFactory->getConfiguration();
|
||||
$storage = $configuration->getMetadataStorageConfiguration();
|
||||
if ($storage instanceof TableMetadataStorageConfiguration) {
|
||||
$this->data['table'] = $storage->getTableName();
|
||||
$this->data['column'] = $storage->getVersionColumnName();
|
||||
}
|
||||
|
||||
$connection = $this->dependencyFactory->getConnection();
|
||||
$this->data['driver'] = get_class($connection->getDriver());
|
||||
$this->data['name'] = $connection->getDatabase();
|
||||
|
||||
$this->data['namespaces'] = $configuration->getMigrationDirectories();
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function getName()
|
||||
{
|
||||
return 'doctrine_migrations';
|
||||
}
|
||||
|
||||
/** @return array<string, mixed>|Data */
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/** @return void */
|
||||
public function reset()
|
||||
{
|
||||
$this->data = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
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(),
|
||||
'is_new' => true,
|
||||
'is_unavailable' => false,
|
||||
'description' => $migration->getMigration()->getDescription(),
|
||||
'executed_at' => null,
|
||||
'execution_time' => null,
|
||||
'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(),
|
||||
'is_new' => false,
|
||||
'is_unavailable' => ! $availableMigration,
|
||||
'description' => $availableMigration ? $availableMigration->getDescription() : null,
|
||||
'executed_at' => $migration->getExecutedAt(),
|
||||
'execution_time' => $migration->getExecutionTime(),
|
||||
'file' => $availableMigration ? (new ReflectionClass($availableMigration))->getFileName() : null,
|
||||
];
|
||||
}, $migrationsList->getItems());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user