DI doesn't work in FooCalculator service?

Hello, any idea why this isn’t working?

<?php

namespace AppBundle\Service;

use Symfony\Component\Routing\RouterInterface;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\CalculatedValue;
use Pimcore\Model\DataObject\ClassDefinition\CalculatorClassInterface;
use AppBundle\Model\LinkableInterface;

class LinkCalculator implements CalculatorClassInterface
{
	/**
	 * @var RouterInterface
	 */
	protected $router;

	public function __construct(RouterInterface $router)
	{
		$this->router = $router;
	}

	/**
	 * @inheritDoc
	 */
	public function compute(Concrete $object, CalculatedValue $context): string
	{
		$class = $object->getClassName();
		if (!$object instanceof LinkableInterface) {
			throw new \InvalidArgumentException(sprintf('Class %s must implement LinkableInterface!', $class));
		}
		$route = strtolower($class);
		return $this->router->generate($route, [
			'id' => $object->getId(),
			'slug' => $object->getKey(),
		]);
	}

	/**
	 * @inheritDoc
	 */
	public function getCalculatedValueForEditMode(Concrete $object, CalculatedValue $context): string
	{
		return $this->compute($object, $context);
	}
}

I get

Too few arguments to function AppBundle\Service\LinkCalculator::__construct(), 0 passed in /path/to/my/project/vendor/pimcore/pimcore/models/DataObject/ClassDefinition/Helper/ClassResolver.php on line 28 and exactly 1 expected

no matter what I do. I’ve got my services autoregistered and not facing this issue anywhere else. In my other service, DI just works. Same in my “LinkGenerator” class implementation, but not here.

No service is passed to the constructor (I’ve tried multiple).

What am I doing wrong, is there some Pimcore magic going on in the background that I am not aware of?

By the way, I’m doing this so that I can have a “link” (calculated) field accessible from my objects. The documentation covers only view helpers which doesn’t work for me, I need to access the object link (well, URI) in multiple places outside of view and can’t figure out a better way. If you know how to do this better, please let me know.

Thanks!

Hey mate,

did you make sure, that you configured the link generator as service and not as a class?
Example:

services.yml

    AppBundle\Service\LinkCalculator:~

In pimcore objects you would need to declare the service as:

    @AppBundle\Service\LinkCalculator

If you miss that @, Pimcore will create a new instance of your generator instead of consulting the di container.