JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

Use Fully Qualified Class Name by guessing on @Inject and @InjectParams instead of Parameter or Propertyname

Open purnama opened this issue 12 years ago • 2 comments
trafficstars

Hi,

I don't really understand why by guessing on @Inject and @InjectParams the Parameter or Propertyname used instead of the fully qualified class name like in @Service?

With this, i can not really automatically injecting an object. i need to always define the "service name" on the place where i want to inject it.

this example throws ServiceNotFoundException with message: You have requested a non-existent service "base_link".

DefaultController.php

<?php

namespace Kateglo\DefaultBundle\Controller;

use Kateglo\DefaultBundle\Service\BaseLink;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use JMS\DiExtraBundle\Annotation\InjectParams;

class DefaultController extends Controller
{
    /**
     * @var BaseLink
     */
    private $baseLink;

    /**
     * @param BaseLink $baseLink
     * @InjectParams
     */
    public function __construct(BaseLink $baseLink){
        $this->baseLink = $baseLink;
    }

}

BaseLink.php

<?php

namespace Kateglo\DefaultBundle\Service;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use JMS\DiExtraBundle\Annotation\Service;
/**
 *
 * @author  Arthur Purnama <[email protected]>
 * @Service
 */ 
class BaseLink {

    public function get(Controller $controller){
        return array(
            'user' => array(
                'register' => array(
                    'link' => $controller->generateUrl('fos_user_registration_register')
                ),
                'login' => array(
                    'link' => $controller->generateUrl('fos_user_security_login')
                ),
            )
        );
    }
}

To make this code fully functioning, i need to define the service name that automatically generated. in this case kateglo.defaultbundle.service.baselink because i want to name my property and my parameter the way i want.

<?php

namespace Kateglo\DefaultBundle\Controller;

use Kateglo\DefaultBundle\Service\BaseLink;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Inject;

class DefaultController extends Controller
{
    /**
     * @var BaseLink
     */
    private $baseLink;

    /**
     * @param BaseLink $baseLink
     * @InjectParams({
     *  "baseLink" = @Inject("kateglo.defaultbundle.service.baselink")
     * })
     */
    public function __construct(BaseLink $baseLink){
        $this->baseLink = $baseLink;
    }
}

purnama avatar Mar 25 '13 10:03 purnama

Well, on the other hand, guessing based on the FQCN would force to be explicit for all core services (for which the current guessing can work fine). And changing the guessing would be a BC break.

stof avatar Mar 25 '13 23:03 stof

I would like to also have the auto guess use the full name, or if it's backwards breaking, maybe have an option for it?

tarioch avatar Dec 13 '13 20:12 tarioch