Injecting the mailer service, got "The service definition 'mailer' does not exist"?
Posted
by
Gremo
on Stack Overflow
See other posts from Stack Overflow
or by Gremo
Published on 2012-09-25T09:03:40Z
Indexed on
2012/09/25
9:37 UTC
Read the original article
Hit count: 315
This is the class where the service mailer
should be injected:
namespace Gremo\SkebbyBundle\Transport;
use Swift_Mailer;
use Gremo\SkebbyBundle\Message\AbstractSkebbyMessage;
class MailerTransport extends AbstractSkebbyTransport
{
/**
* @var \Swift_Mailer
*/
private $mailer;
public function __construct(Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
/**
* @param \Gremo\SkebbyBundle\Message\AbstractSkebbyMessage $message
* @return void
*/
public function executeTransport(AbstractSkebbyMessage $message)
{
/* ... */
}
}
Service id is gremo_skebby.transport.mailer
, placed inside mailer.xml
file:
<parameters>
<parameter key="gremo_skebby.converter.swift_message.class">
Gremo\SkebbyBundle\Converter\SwiftMessageConverter
</parameter>
<parameter key="gremo_skebby.transport.mailer.class">
Gremo\SkebbyBundle\Transport\MailerTransport
</parameter>
</parameters>
<services>
<service id="gremo_skebby.converter.swift_message"
class="%gremo_skebby.converter.swift_message.class%" public="false" />
<service id="gremo_skebby.transport.mailer"
class="%gremo_skebby.transport.mailer.class%" public="false"
parent="gremo_skebby.tranport.abstract_transport">
<argument type="service" id="mailer" />
</service>
</services>
When i try to inject the gremo_skebby.transport.mailer
into another service (an helper) i get:
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException : The service definition "mailer" does not exist.
That is strange, because command php app/console container:debug
shows that the mailer
service actually exists:
mailer container Swift_Mailer
I'm loading mailer.xml
file dynamically by the extension:
if(in_array($configs['transport'], array('http', 'rest', 'mailer'))) {
$loader->load('transport.xml');
$loader->load($configs['transport'] . '.xml');
$container->setAlias('gremo_skebby.transport',
'gremo_skebby.transport.' . $configs['transport']);
}
$loader->load('skebby.xml');
... and gremo_skebby.transport
service is injected into gremo_skebby.skebby
service (skebby.xml
):
<parameters>
<parameter key="gremo_skebby.skebby.class">
Gremo\SkebbyBundle\Skebby
</parameter>
</parameters>
<services>
<service id="gremo_skebby.skebby" class="%gremo_skebby.skebby.class%">
<argument type="service" id="gremo_skebby.transport" />
</service>
</services>
A quick test is giving me the same InvalidArgumentException
:
public function testSkebbyWithMailerTransport()
{
$loader = new GremoSkebbyExtension();
$container = new ContainerBuilder();
$config = $this->getEmptyConfiguration();
$loader->load(array($config), $container);
$this->assertTrue($container->hasDefinition('gremo_skebby.transport.mailer'));
$this->assertTrue($container->hasDefinition('gremo_skebby.skebby'));
$this->assertInstanceOf('Gremo\SkebbyBundle\Skebby',
$container->get('gremo_skebby.skebby'));
}
© Stack Overflow or respective owner