Problem/Motivation

You can reference a service via an alias.

Usually the compiler pass takes care of that, but if you want to do it without compile() this will trigger a bug. Core itself is not affected.

Proposed resolution

- Fix it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Fabianx created an issue. See original summary.

dawehner’s picture

Note: The same problem exists in the symfony implementation as well. They call it by design.

Fabianx’s picture

Fabianx’s picture

#2: Symfony works fine, for the use case tested here.

public function testAliasReference()
    {
        $builder = new ContainerBuilder();
        $builder->register('service', '\stdClass')
          ->setPublic(FALSE);
        $builder->setAlias('service.alias', 'service');

       $builder->register('bar', '\Drupal\Tests\Component\DependencyInjection\MockService')
         ->addArgument(new Reference('service.alias'))
         ->addArgument('');

 // Do not compile and it will still work.
 //       $builder->compile();

        $this->assertEquals(
          new \stdClass(),
          $builder->get('bar')->getSomeOtherService()
        );
    }

dawehner’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.12 KB

I just ran fabian's example code against symfony master, see patch file, to prove that this kind of syntax works. Wow.

The last submitted patch, 3: optimizedphparraydumper-2749819-2--tests.patch, failed testing.

  • catch committed 389d663 on 8.2.x
    Issue #2749819 by Fabianx, dawehner: OptimizedPhpArrayDumper does not...

  • catch committed 5bf26fa on 8.1.x
    Issue #2749819 by Fabianx, dawehner: OptimizedPhpArrayDumper does not...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x and cherry-picked to 8.1.x. Thanks!

Wim Leers’s picture

I'd swear I opened an issue for this months ago…

Wim Leers’s picture

This seems very closely related to #2650812: Container::has() doesn't work for service aliases (consequently, decorating services doesn't work). Reopening this to assess its relation to that issue. Then we can mark this Fixed again.

Wim Leers’s picture

Fabianx’s picture

Status: Active » Fixed

Marking back as fixed as this is a very special case that should never happen in Core as ContainerBuilder is compile'd().

I will take a look at the other issue to assess it.

Wim Leers’s picture

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.