Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #935 from weaverryan/dbal_filter_plugin
Removing deprecated call to Configuration::setSchemaAssetsFilter() + extendable
- Loading branch information
Showing
10 changed files
with
252 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Dbal; | ||
|
||
use Doctrine\DBAL\Schema\AbstractAsset; | ||
|
||
class RegexSchemaAssetFilter | ||
{ | ||
/** @var string */ | ||
private $filterExpression; | ||
|
||
public function __construct(string $filterExpression) | ||
{ | ||
$this->filterExpression = $filterExpression; | ||
} | ||
|
||
public function __invoke($assetName) : bool | ||
{ | ||
if ($assetName instanceof AbstractAsset) { | ||
$assetName = $assetName->getName(); | ||
} | ||
|
||
return preg_match($this->filterExpression, $assetName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Dbal; | ||
|
||
/** | ||
* Manages schema filters passed to Connection::setSchemaAssetsFilter() | ||
*/ | ||
class SchemaAssetsFilterManager | ||
{ | ||
/** @var callable[] */ | ||
private $schemaAssetFilters; | ||
|
||
/** | ||
* @param callable[] $schemaAssetFilters | ||
*/ | ||
public function __construct(array $schemaAssetFilters) | ||
{ | ||
$this->schemaAssetFilters = $schemaAssetFilters; | ||
} | ||
|
||
public function __invoke($assetName) : bool | ||
{ | ||
foreach ($this->schemaAssetFilters as $schemaAssetFilter) { | ||
if ($schemaAssetFilter($assetName) === false) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; | ||
|
||
use Doctrine\DBAL\Configuration; | ||
use LogicException; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Processes the doctrine.dbal.schema_filter | ||
*/ | ||
class DbalSchemaFilterPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$filters = $container->findTaggedServiceIds('doctrine.dbal.schema_filter'); | ||
|
||
if (count($filters) > 0 && ! method_exists(Configuration::class, 'setSchemaAssetsFilter')) { | ||
throw new LogicException('The doctrine.dbal.schema_filter tag is only supported when using doctrine/dbal 2.9 or higher.'); | ||
} | ||
|
||
$connectionFilters = []; | ||
foreach ($filters as $id => $tagAttributes) { | ||
foreach ($tagAttributes as $attributes) { | ||
$name = isset($attributes['connection']) ? $attributes['connection'] : $container->getParameter('doctrine.default_connection'); | ||
|
||
if (! isset($connectionFilters[$name])) { | ||
$connectionFilters[$name] = []; | ||
} | ||
|
||
$connectionFilters[$name][] = new Reference($id); | ||
} | ||
} | ||
|
||
foreach ($connectionFilters as $name => $references) { | ||
$configurationId = sprintf('doctrine.dbal.%s_connection.configuration', $name); | ||
|
||
if (! $container->hasDefinition($configurationId)) { | ||
continue; | ||
} | ||
|
||
$definition = new ChildDefinition('doctrine.dbal.schema_asset_filter_manager'); | ||
$definition->setArgument(0, $references); | ||
|
||
$id = sprintf('doctrine.dbal.%s_schema_asset_filter_manager', $name); | ||
$container->setDefinition($id, $definition); | ||
$container->findDefinition($configurationId) | ||
->addMethodCall('setSchemaAssetsFilter', [new Reference($id)]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Tests\Dbal; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Dbal\RegexSchemaAssetFilter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RegexSchemaAssetFilterTest extends TestCase | ||
{ | ||
public function testShouldIncludeAsset() | ||
{ | ||
$filter = new RegexSchemaAssetFilter('~^(?!t_)~'); | ||
|
||
$this->assertTrue($filter('do_not_t_ignore_me')); | ||
$this->assertFalse($filter('t_ignore_me')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Tests\Dbal; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Dbal\RegexSchemaAssetFilter; | ||
use Doctrine\Bundle\DoctrineBundle\Dbal\SchemaAssetsFilterManager; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SchemaAssetsFilterManagerTest extends TestCase | ||
{ | ||
public function testInvoke() | ||
{ | ||
$filterA = new RegexSchemaAssetFilter('~^(?!t_)~'); | ||
$filterB = new RegexSchemaAssetFilter('~^(?!s_)~'); | ||
|
||
$manager = new SchemaAssetsFilterManager([$filterA, $filterB]); | ||
$tables = ['do_not_filter', 't_filter_me', 's_filter_me_too']; | ||
$this->assertSame( | ||
['do_not_filter'], | ||
array_values(array_filter($tables, $manager)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
Tests/DependencyInjection/Fixtures/config/yml/dbal_schema_filter.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
doctrine: | ||
dbal: | ||
schema_filter: ^sf2_ | ||
default_connection: connection1 | ||
connections: | ||
connection1: | ||
schema_filter: ~^(?!t_)~ | ||
connection2: [] | ||
connection3: [] |