Aerendir Aerendir - 1 year ago 124
PHP Question

Symfony Bundle: how to avoid using __DIR__ to build the path to Doctrine mappings

I'm finding myself dealing with the same problem in multiple bundles I've wrote.

The problem is that in my

BundleNameBundle
class I have to create the path to then load the mappings of Doctrine.

To do this I do something like:

/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);

$modelDir = realpath(__DIR__ . '/Resources/config/doctrine/mappings');
$mappings = [
$modelDir => 'SerendipityHQ\Bundle\QueuesBundle\Model',
];

$ormCompilerClass = DoctrineOrmMappingsPass::class;
if (class_exists($ormCompilerClass)) {
$container->addCompilerPass(
$this->getYamlMappingDriver($mappings)
);
}

$container->addCompilerPass(new DaemonDependenciesPass());
}


Complete code here.

As you can see I use
__DIR__
to get the path to the folder where the mappings are.

Now, Sensio Insights is alerting me that "Absolute path constants DIR and FILE should not be used".

Ok, but how can I solve this problem? Is there some alternative way to build the path to the mappings?

Answer Source

You can use $this->path. It returns the same result as __DIR__

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download