Skip to content

Commit

Permalink
enable XSD validation for xml mapping driver
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Mar 14, 2023
1 parent cceda91 commit 208310a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
53 changes: 53 additions & 0 deletions DependencyInjection/Compiler/XmlMappingDriverPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;

use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

use function array_keys;
use function count;
use function sprintf;

final class XmlMappingDriverPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (! $container->hasParameter('doctrine.entity_managers')) {
return;
}

foreach (array_keys($container->getParameter('doctrine.entity_managers')) as $managerName) {
$xmlDriverId = sprintf('doctrine.orm.%s_xml_metadata_driver', $managerName);

if (! $container->hasDefinition($xmlDriverId)) {
continue;
}

$xmlDriverDef = $container->getDefinition($xmlDriverId);
if ($xmlDriverDef->getClass() === null) {
continue;
}

if ($container->getParameterBag()->resolveValue($xmlDriverDef->getClass()) !== SimplifiedXmlDriver::class) {
continue;
}

$args = $xmlDriverDef->getArguments();
$numberOfArgs = count($args);
if ($numberOfArgs === 0 || $numberOfArgs === 3) {
continue;
}

if (count($args) < 2) {
$args[] = SimplifiedXmlDriver::DEFAULT_FILE_EXTENSION;
}

// enable validation
$args[] = true;

$xmlDriverDef->setArguments($args);
}
}
}
2 changes: 2 additions & 0 deletions DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveProfilerControllerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\XmlMappingDriverPass;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\Autoloader;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function process(ContainerBuilder $container): void
$container->addCompilerPass(new RemoveProfilerControllerPass());
$container->addCompilerPass(new RemoveLoggingMiddlewarePass());
$container->addCompilerPass(new MiddlewaresPass());
$container->addCompilerPass(new XmlMappingDriverPass());

if (! class_exists(RegisterUidTypePass::class)) {
return;
Expand Down
4 changes: 4 additions & 0 deletions Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function testContainer(): void
$this->assertInstanceOf(InfoCommand::class, $container->get('doctrine.mapping_info_command'));
$this->assertInstanceOf(UpdateCommand::class, $container->get('doctrine.schema_update_command'));

$xmlDriverDef = $container->getDefinition('doctrine.orm.default_xml_metadata_driver');
$this->assertCount(3, $xmlDriverDef->getArguments());
$this->assertTrue($xmlDriverDef->getArguments()[2]);

$this->assertTrue(Type::hasType('test'));

$this->assertFalse($container->has('doctrine.dbal.default_connection.events.mysqlsessioninit'));
Expand Down
2 changes: 2 additions & 0 deletions Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Bundle\DoctrineBundle\Tests;

use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\XmlMappingDriverPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestType;
use Doctrine\Common\Annotations\AnnotationReader;
Expand Down Expand Up @@ -85,6 +86,7 @@ public function createXmlBundleTestContainer(): ContainerBuilder
$compilerPassConfig->addPass(new CacheCompatibilityPass());
// make all Doctrine services public, so we can fetch them in the test
$compilerPassConfig->addPass(new TestCaseAllPublicCompilerPass());
$compilerPassConfig->addPass(new XmlMappingDriverPass());
$container->compile();

return $container;
Expand Down

0 comments on commit 208310a

Please sign in to comment.