Add possibility to add migrations from plugins via event dispatcher (#3168)
* Add possibility to add migrations from plugins via event dispatcher * Add possibility to add Doctrine mapping classes paths from plugins via event dispatcher
This commit is contained in:
parent
bfc937b5f6
commit
adec893a93
|
@ -6,7 +6,7 @@ use App\Middleware;
|
|||
use App\Settings;
|
||||
|
||||
return function (App\EventDispatcher $dispatcher) {
|
||||
$dispatcher->addListener(Event\BuildConsoleCommands::class, function (Event\BuildConsoleCommands $event) {
|
||||
$dispatcher->addListener(Event\BuildConsoleCommands::class, function (Event\BuildConsoleCommands $event) use ($dispatcher) {
|
||||
$console = $event->getConsole();
|
||||
$di = $console->getContainer();
|
||||
|
||||
|
@ -31,7 +31,7 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
$helper_set->set($doctrine_helpers->get('db'), 'db');
|
||||
$helper_set->set($doctrine_helpers->get('em'), 'em');
|
||||
|
||||
$migrateConfig = new Doctrine\Migrations\Configuration\Migration\ConfigurationArray([
|
||||
$migrationConfigurations = [
|
||||
'migrations_paths' => [
|
||||
'App\Entity\Migration' => $settings[Settings::BASE_DIR] . '/src/Entity/Migration',
|
||||
],
|
||||
|
@ -39,7 +39,17 @@ return function (App\EventDispatcher $dispatcher) {
|
|||
'table_name' => 'app_migrations',
|
||||
'version_column_length' => 191,
|
||||
],
|
||||
]);
|
||||
];
|
||||
|
||||
$buildMigrationConfigurationsEvent = new Event\BuildMigrationConfigurationArray(
|
||||
$migrationConfigurations,
|
||||
$settings[Settings::BASE_DIR]
|
||||
);
|
||||
$dispatcher->dispatch($buildMigrationConfigurationsEvent);
|
||||
|
||||
$migrationConfigurations = $buildMigrationConfigurationsEvent->getMigrationConfigurations();
|
||||
|
||||
$migrateConfig = new Doctrine\Migrations\Configuration\Migration\ConfigurationArray($migrationConfigurations);
|
||||
|
||||
$migrateFactory = Doctrine\Migrations\DependencyFactory::fromEntityManager(
|
||||
$migrateConfig,
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
* PHP-DI Services
|
||||
*/
|
||||
|
||||
use App\Event;
|
||||
use App\Settings;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
|
@ -58,7 +59,8 @@ return [
|
|||
Doctrine\Common\Annotations\Reader $reader,
|
||||
App\Settings $settings,
|
||||
App\Doctrine\Event\StationRequiresRestart $eventRequiresRestart,
|
||||
App\Doctrine\Event\AuditLog $eventAuditLog
|
||||
App\Doctrine\Event\AuditLog $eventAuditLog,
|
||||
App\EventDispatcher $dispatcher
|
||||
) {
|
||||
$connectionOptions = [
|
||||
'host' => $_ENV['MYSQL_HOST'] ?? 'mariadb',
|
||||
|
@ -95,9 +97,19 @@ return [
|
|||
$doctrineCache
|
||||
);
|
||||
|
||||
$mappingClassesPaths = [$settings->getBaseDirectory() . '/src/Entity'];
|
||||
|
||||
$buildDoctrineMappingPathsEvent = new Event\BuildDoctrineMappingPaths(
|
||||
$mappingClassesPaths,
|
||||
$settings->getBaseDirectory()
|
||||
);
|
||||
$dispatcher->dispatch($buildDoctrineMappingPathsEvent);
|
||||
|
||||
$mappingClassesPaths = $buildDoctrineMappingPathsEvent->getMappingClassesPaths();
|
||||
|
||||
$annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
|
||||
$reader,
|
||||
[$settings->getBaseDirectory() . '/src/Entity']
|
||||
$mappingClassesPaths
|
||||
);
|
||||
$config->setMetadataDriverImpl($annotationDriver);
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
namespace App\Event;
|
||||
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
class BuildDoctrineMappingPaths extends Event
|
||||
{
|
||||
protected array $mappingClassesPaths;
|
||||
protected string $baseDir;
|
||||
|
||||
public function __construct(array $mappingClassesPaths, string $baseDir)
|
||||
{
|
||||
$this->mappingClassesPaths = $mappingClassesPaths;
|
||||
$this->baseDir = $baseDir;
|
||||
}
|
||||
|
||||
public function getMappingClassesPaths(): array
|
||||
{
|
||||
return $this->mappingClassesPaths;
|
||||
}
|
||||
|
||||
public function setMappingClassesPaths(array $mappingClassesPaths): void
|
||||
{
|
||||
$this->mappingClassesPaths = $mappingClassesPaths;
|
||||
}
|
||||
|
||||
public function getBaseDir(): string
|
||||
{
|
||||
return $this->baseDir;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
namespace App\Event;
|
||||
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
class BuildMigrationConfigurationArray extends Event
|
||||
{
|
||||
protected array $migrationConfigurations;
|
||||
protected string $baseDir;
|
||||
|
||||
public function __construct(array $migrationConfigurations, string $baseDir)
|
||||
{
|
||||
$this->migrationConfigurations = $migrationConfigurations;
|
||||
$this->baseDir = $baseDir;
|
||||
}
|
||||
|
||||
public function getMigrationConfigurations(): array
|
||||
{
|
||||
return $this->migrationConfigurations;
|
||||
}
|
||||
|
||||
public function setMigrationConfigurations(array $migrationConfigurations): void
|
||||
{
|
||||
$this->migrationConfigurations = $migrationConfigurations;
|
||||
}
|
||||
|
||||
public function getBaseDir(): string
|
||||
{
|
||||
return $this->baseDir;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue