Enabling Symfony's AppCache replaces Doctrine's ApcCache - symfony

When enabling AppCache in my Symfony application, suddenly any injected ApcCache gets replaced with AppCache instances, yielding:
Argument passed to MyClass::__construct() must be an instance of Doctrine\Common\Cache\ApcCache, instance of AppCache given
The injection happens through:
services:
my_class:
class: MyClass
arguments:
- #cache
cache:
class: Doctrine\Common\Cache\ApcCache
Which seems very weird. Any suggestions on how to fix this would be much appreciated.

According to the previous response and my analyze, it seems that when using the http cache ($kernel = new AppCache($kernel); in app.php), Symfony create a #cache variable which is an instance of AppCache. It will override the #cache variable you created. Thus name differently your variable like :
services:
my_class:
class: MyClass
arguments:
- #apc_cache
apc_cache:
class: Doctrine\Common\Cache\ApcCache
It seems to be a bug of Symfony3.0.3

Related

Unused binding error when trying to bind logger to autowired controller constructor in Symfony 3.4

After upgrading to Symfony 3.4 from 2.8, I am attempting to get rid of warnings about using services from the container. One hang up is my controller all extend from an abstract controller which needs access to the monolog logger. I've decided to use autwiring for my controllers and have added a constructor in the base controller which has a LoggerInterface $logger as the only argument. In attempt to configure this once, I've added the $logger variable with a reference to the logger service under the bind section of services.yml.
However, I keep getting the error:
Unused binding "$logger" in service "security.authentication.failure_handler.secured_area.form_login"
I believe this error is supposed to appear only if no services have a constructor argument with that variable name. Now I know that my controllers all have this in the abstract class, as well as being part of some of my other services, so this seems wrong. How can I get rid of this error?
Here is what my services.yml looks like:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$logger: "#logger"
$env: "%sys_env%"
_instanceof:
\Twig_Extension:
tags: ['twig.extension']
AppBundle\:
resource: '../../../../../../src/AppBundle/{Controller,Service,Twig}/*'
exclude: '../../../../../../src/AppBundle/Service/Exception/*'
# SECURITY ########################################################################
security.authentication.failure_handler:
class: AppBundle\Security\AuthenticationFailureHandler
autowire: false
arguments: ["#http_kernel", "#security.http_utils", {}, "#app.service.security", "#doctrine.orm.entity_manager", "#logger"]
tags:
- { name: 'monolog.logger', channel: 'security' }
UPDATE 1:
I noticed that in security.authentication.failure_handler I have a reference to one of my services: app.service.security. I forgot to declare that below, so I added the following to services.yml:
app.service.security:
class: AppBundle\Service\SecurityService
That got rid of the logger error, however now I'm seeing an error about the $env string variable:
Unused binding "$env" in service "security.authentication.failure_handler.secured_area.form_login".
I'm concerned that the error message is not the real error, and this is a red herring. The bind options seem a little flaky. Any advice appreciated...
UPDATE 2:
I've decided to get rid of the bind and instanceof config and am setting up the values manually, but now this is the error: Cannot autowire service "app.service.security": argument "$sysInfoService" of method "AppBundle\Service\SecurityService::__construct()" references class "AppBundle\Service\SystemInfoService" but no such service exists. You should maybe alias this class to the existing "app.service.system_info" service.
What's weird is that I believe I'm doing exactly what the error is suggesting to do; I've added aliases for the supposedly autowired service:
app.service.system_info:
class: AppBundle\Service\SystemInfoService
app.service.security:
class: AppBundle\Service\SecurityService
I do have some services which I manually declare with autowired: false in order to manually set the arguments. That should be ok, I think; you should be able to have autowired and manual wiring coexisting in the service container, right?

symfony4 migrate autowire to true - get error message

I am migrating from symofony 2.7 to symfony 4.0. With success I migrated one bundle. Now I am migrating the second bundle and the error message is coming up. I don't get at all what symfony 4.0 wants from me.
If I turn on autowire: true this error message is coming up.
Cannot autowire service "App\Kernel": argument "$environment" of method "Symfony\Component\HttpKernel\Kernel::__construct()" must have a type-hint or be given a value explicitly.
Can somebody help me?
If I turn it off, no message is coming up.
Update
I registered my bundle only in bundles.php
App\Backend\AccountBundle\BackendAccountBundle::class => ['all' => true],
Usually the Kernel is added to the Service Container as a so called synthetic service, meaning it's not generated by the DI-container from configuration. Rather the id is set and then the previously configured service is just added to the container. It seems rather odd that your bundle's container wants to create a new kernel here. So I would check where and how you want to access the kernel in any of the bundle's services and whether you actually want to pass in the kernel and not something else. If you do you might want to check the Service Container-documentation on synthetic services.
As to the error itself. Symfony's autowiring often falls flat when you have services that require parameters like with the Kernel:
public function __construct(string $environment, bool $debug) {...}
In these cases you have to either have a parameter defined in your services.yaml that matches the name of the parameter:
# config/services.yaml
parameters:
environment: prod
debug: false
or you have to tell the configuration which parameters you want to have in those places.
App\Kernel:
$environment: prod
$debug: false
This will tell the autowiring that only the 2 arguments named environment and debug should be overwritten with the values you provide, but the rest is done via autowiring. This way you can skip the arguments: part of the definition and you can also skip all arguments you know are correctly set via autowiring.
For example if you have a service like this:
class MyService {
public function __construct(OtherServce $service, string $someParameter) {}
}
# config/services.yaml
services:
_defaults:
autowiring: true
MyService:
$someParameter: 'someValue'
This is the same as explicitly writing:
services:
MyService:
class: MyService
arguments:
- '#OtherServce'
- 'someValue'

Symfony Dependency Injection: Set a repository as a service using php

I have the set the following respository as a service on my Symfony 3.2 project:
person_in_need_repository:
class: AppBundle\Repository\PersonInNeedRepository
factory: ["#doctrine","getRepository"]
arguments: ["AppBundle:PersonInNeed"]
How can I change its annotation in php format?
It can be done by like this:
use AppBundle\Repository\PersonInNeedRepository;
$container->register('app.person_in_need_repository',PersonInNeedRepository::class)
->setFactory([new Reference("doctrine"),"getRepository"])
->addArgument('AppBundle:PersonInNeed');
Also ensure that you have replaced the services.yml with services.php on config.yml when using it as a global way to set the services. Also rename properly the services.yml.

Symfony2: overriding Sensio FrameworkExtraBundle template guesser

I am trying to override the standard template guesser ( located in Sensio\Bundle\FrameworkExtraBundle\Templating; ) becouse I want to use annotations to set the view but need to change the logic of how the actual view file is chosen.
I have seen this: https://github.com/elnur/ElnurTemplateGuesserBundle
but I was wondering if there is a way to just override the service in configuration.
I tried setting:
services:
sensio_framework_extra.view.guesser:
class: myCompany\myBundle\Templating\TemplateGuesser
but I get:
ContextErrorException: Catchable Fatal Error:
Argument 1 passed to Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser::__construct()
must implement interface Symfony\Component\HttpKernel\KernelInterface, none given
Am I supposed to set an argument in the service config setting? But how do I reference the HttpKernel?
Or am I missing something?
TIA.
You can inject the kernel the same way the as the original TemplateGuesser. The name of the Kernel service is just simply kernel.
services:
sensio_framework_extra.view.guesser:
class: myCompany\myBundle\Templating\TemplateGuesser
arguments: [ "#kernel" ]
To see a full list of services in the container, run
$ php app/console container:debug
Of which you'll see the kernel listed as one of them.

After tagging a service in services.yml symfony starts throwing ReflectionException

I have the following configuration in my SF2 Bundle
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
services:
catalogue.title:
class: %catalogue.title.class%
Which works like a charm.
Now I want to enhance the configured service and tag it so it gets the knp_paginator injected (I've also tried with other tags). My config becomes:
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
services:
catalogue.title:
class: %catalogue.title.class%
tags:
- { name: knp_paginator.injectable, paginator: knp_paginator }
For the mentioned tag to work the service implements the Knp\Bundle\PaginatorBundle\Definition\PaginatorAware. As mentioned on the KnpPaginatorBundle docs.
Running my app will show the following:
ReflectionException: Class %catalogue.title.class% does not exist.
Since it works without adding the tags part, I know the class does exist.
Replacing %catalogue.title.class% with the value of the parameter (the fully qualified class name) solves the issue, but we are using this way of configuring our service classes in all our projects and really don't want to deviate from that just to resolve this issue.
[EDIT] Using a different parameter like catalogue_title_class or my_abc has the same result.
I'm running Symfony version 2.3.5.
What am I doing wrong?
Probably this exceptions throws because of same names of class of parameter and provided service name.
Try to rename
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
to some other name
parameters:
catalogue_title.class: My\SomeBundle\Services\TitleService
and use this
services:
catalogue.title:
class: %catalogue_title.class%
tags:
- { name: knp_paginator.injectable, paginator: knp_paginator }

Resources