Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removing deprecated call to Configuration::setSchemaAssetsFilter()
... and making the schema filter stuff a "plugin" system, which is the intention of the new way it's handled in doctrine/dbal
- Loading branch information
1 parent
00b14f4
commit f724b13
Showing
11 changed files
with
267 additions
and
4 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,24 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Dbal; | ||
|
||
use Doctrine\DBAL\Schema\AbstractAsset; | ||
|
||
class RegexSchemaAssetFilter implements SchemaAssetFilterInterface | ||
{ | ||
private $filterExpression; | ||
|
||
public function __construct(string $filterExpression) | ||
{ | ||
$this->filterExpression = $filterExpression; | ||
} | ||
|
||
public function shouldIncludeAsset($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,17 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Dbal; | ||
|
||
/** | ||
* Classes consumed by SchemaAssetsFilterManager to filter DBAL assets. | ||
*/ | ||
interface SchemaAssetFilterInterface | ||
{ | ||
/** | ||
* Returning false will filter the asset out. | ||
* | ||
* @param $assetName | ||
* @return bool | ||
*/ | ||
public function shouldIncludeAsset($assetName): bool; | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Dbal; | ||
|
||
/** | ||
* Manages schema filters passed to Connection::setSchemaAssetsFilter() | ||
*/ | ||
class SchemaAssetsFilterManager | ||
{ | ||
private $schemaAssetFilters; | ||
|
||
/** | ||
* @param SchemaAssetFilterInterface[] $schemaAssetFilters | ||
*/ | ||
public function __construct(array $schemaAssetFilters) | ||
{ | ||
$this->schemaAssetFilters = $schemaAssetFilters; | ||
} | ||
|
||
public function __invoke($assetName): bool | ||
{ | ||
foreach ($this->schemaAssetFilters as $schemaAssetFilter) { | ||
if (false === $schemaAssetFilter->shouldIncludeAsset($assetName)) { | ||
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,56 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; | ||
|
||
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) | ||
{ | ||
// feature require doctrine/dbal 2.9 or higher | ||
if (!method_exists(\Doctrine\DBAL\Configuration::class, 'setSchemaAssetsFilter')) { | ||
return; | ||
} | ||
|
||
$filters = $container->findTaggedServiceIds('doctrine.dbal.schema_filter'); | ||
|
||
$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->shouldIncludeAsset('do_not_t_ignore_me')); | ||
$this->assertFalse($filter->shouldIncludeAsset('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: [] |