Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bundle-relative paths in the migration path resolution #318

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions DependencyInjection/DoctrineMigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use function array_keys;
use function explode;
use function implode;
use function sprintf;
use function strlen;
use function substr;

/**
* DoctrineMigrationsExtension.
Expand All @@ -38,6 +45,7 @@ public function load(array $configs, ContainerBuilder $container) : void
$configurationDefinition = $container->getDefinition('doctrine.migrations.configuration');

foreach ($config['migrations_paths'] as $ns => $path) {
$path = $this->checkIfBundleRelativePath($path, $container);
$configurationDefinition->addMethodCall('addMigrationsDirectory', [$ns, $path]);
}

Expand Down Expand Up @@ -98,6 +106,34 @@ public function load(array $configs, ContainerBuilder $container) : void
$container->setParameter('doctrine.migrations.preferred_connection', $config['connection']);
}

private function checkIfBundleRelativePath(string $path, ContainerBuilder $container) : string
goetas marked this conversation as resolved.
Show resolved Hide resolved
{
if (isset($path[0]) && $path[0] === '@') {
$pathParts = explode('/', $path);
$bundleName = substr($pathParts[0], 1);

$bundlePath = $this->getBundlePath($bundleName, $container);
return $bundlePath . substr($path, strlen('@' . $bundleName));
}

return $path;
}

private function getBundlePath(string $bundleName, ContainerBuilder $container) : string
{
$bundleMetadata = $container->getParameter('kernel.bundles_metadata');

if (! isset($bundleMetadata[$bundleName])) {
throw new RuntimeException(sprintf(
'The bundle "%s" has not been registered, available bundles: %s',
$bundleName,
implode(', ', array_keys($bundleMetadata))
));
}

return $bundleMetadata[$bundleName]['path'];
}

/**
* Returns the base path for the XSD files.
*
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ application:
migrations_paths:
'App\Migrations': 'src/App'
'AnotherApp\Migrations': '/path/to/other/migrations'
'SomeBundle\Migrations': '@SomeBundle/Migrations'

# List of additional migration classes to be loaded, optional
migrations:
Expand Down
34 changes: 33 additions & 1 deletion Tests/DependencyInjection/DoctrineMigrationsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Bundle\MigrationsBundle\DependencyInjection\DoctrineMigrationsExtension;
use Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle;
use Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\CustomEntityManager;
use Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\TestBundle\TestBundle;
use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\DependencyFactory;
Expand Down Expand Up @@ -101,6 +102,30 @@ public function testNoConfigsAreNeeded() : void
self::assertSame([], $config->getMigrationDirectories());
}

public function testBundleRelativePathResolution() : void
{
$container = $this->getContainer([
'migrations_paths' => [
'DoctrineMigrationsTest' => '@TestBundle',
'DoctrineMigrationsTestAnother' => '@TestBundle/another-path',
],
]);

$conn = $this->createMock(Connection::class);
$container->set('doctrine.dbal.default_connection', $conn);
$container->compile();

$config = $container->get('doctrine.migrations.configuration');
$bundle = new TestBundle();

self::assertInstanceOf(Configuration::class, $config);
self::assertSame([
'DoctrineMigrationsTest' => $bundle->getPath(),
'DoctrineMigrationsTestAnother' => $bundle->getPath() . '/another-path',

], $config->getMigrationDirectories());
}

private function assertConfigs(?object $config) : void
{
self::assertInstanceOf(Configuration::class, $config);
Expand Down Expand Up @@ -288,9 +313,16 @@ private function getContainer(array $config) : ContainerBuilder

private function getContainerBuilder() : ContainerBuilder
{
$bundle = new TestBundle();
return new ContainerBuilder(new ParameterBag([
'kernel.debug' => false,
'kernel.bundles' => [],
'kernel.bundles' => [$bundle->getName() => TestBundle::class],
'kernel.bundles_metadata' => [
$bundle->getName() => [
'path' => $bundle->getPath(),
'namespace' => $bundle->getNamespace(),
],
],
'kernel.cache_dir' => sys_get_temp_dir(),
'kernel.environment' => 'test',
'kernel.project_dir' => __DIR__ . '/../',
Expand Down
11 changes: 11 additions & 0 deletions Tests/Fixtures/TestBundle/TestBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\Tests\Fixtures\TestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class TestBundle extends Bundle
{
}