1

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?

Aerendir
  • 6,152
  • 9
  • 55
  • 108
  • @Gordon, this is not a duplicate as the solution provided in the question you linked is not applicable to my case. Using `$container->get('kernel')->locateResource('@QueuesBundle/Resources/config/doctrine/mappings')` is not possible as it triggers the error "You have requested a non-existent service "kernel".". The solution refers to the resource location in Controller, while I'm in the initialization file of the bundle: in this context the `kernel` is not already available. Please, remove the duplication flag. – Aerendir Feb 07 '17 at 10:11
  • 1
    Ok, I'll still link this here for reference though: http://stackoverflow.com/questions/34331370/why-absolute-path-constants-dir-and-file-should-not-be-used-in-symfony – Gordon Feb 07 '17 at 10:13

1 Answers1

1

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

Gordon
  • 312,688
  • 75
  • 539
  • 559
Barkati.med
  • 620
  • 5
  • 11