I am trying to inject a service from Akeneo to my selfwritten Bundle.
To be exact, i want to use the service defined here in my bundle instead of an Processor.
services:
_defaults:
autowire: true
autoconfigure: true
Acme\CategoryBuilder:
class: Acme\CategoryBuilder\CategoryBuilder
arguments:
- '#pim_catalog.repository.category'
tags:
- { name: kernel.event_listener, event: akeneo.storage.post_save, method: onPostSave }
and this constructor:
public function __construct(CategoryRepositoryInterface
$categoryRepositoryInterface){
$this->categoryRepositoryInterface = $categoryRepositoryInterface;
}
in a Class called CategoryBuilder.
This setup does not work and if i try to clear & warmup the cache, i get the Error Response that 0 arguments are passed but 1 was expected.
How can i pass this service to my Bundle?
Edit:
if i run: php bin/console debug:container pim_catalog.repository.category i get the following output:
Information for Service "pim_catalog.repository.category"
=========================================================
Category repository
----------------- ------------------------------------------------------------------------------------
Option Value
----------------- ------------------------------------------------------------------------------------
Service ID pim_catalog.repository.category
Class Akeneo\Tool\Bundle\ClassificationBundle\Doctrine\ORM\Repository\CategoryRepository
Tags pim_repository
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
Factory Service doctrine.orm.default_entity_manager
Factory Method getRepository
----------------- ------------------------------------------------------------------------------------
! [NOTE] The "pim_catalog.repository.category" service or alias has been removed or inlined when the container was
! compiled.
Does the Note mean i just cant get this Service? If so, how can i restore it?
Related
I have a registered service where I inject the content of a json as argument (see followed configuration, App\Service\Authenticator service)
parameters:
env(LDAP_CONFIG_FILE): '../auth.json'
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
App\Service\Authenticator:
arguments:
$config: '%env(json:file:resolve:LDAP_CONFIG_FILE)%'
$environment: '%env(APP_ENV)%'
When I try to inject this service in a controller, this works fine as expected. But when I inject the service into a command I get this error :
> php bin/console app:execute
[WARNING] Some commands could not be registered:
In EnvVarProcessor.php line 136:
File "../auth.json" not found (resolved from "resolve:LDAP_CONFIG_FILE").
Command "app:execute" is not defined.
And if I replace env(LDAP_CONFIG_FILE) by auth.json it work in a command but not in a controller anymore.
How can I configure to make it work in both commands and controllers ?
Edit: I found a temporary fix with the default processor but I really want a clean solution.
I have a problem with registering a class as a service in services.yaml.
I have created a class MenuBuilder.php in Symfony 6 that looks somewhat like this.
src/Menu/MenuBuilder.php
namespace App\Menu;
class MenuBuilder
{
public function createMainMenu(array $options)
{
// method logic
}
}
Now, when I want to register it as a service: (and use it in twig with KnpMenuBundle)
config/services.yaml
parameters:
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
app.menu_builder:
class: App\Menu\MenuBuilder
arguments: [ "#knp_menu.factory" ]
tags:
- { name: knp_menu.menu_builder, method: createMainMenu, alias: main }
I get an error Class "App\Menu\MenuBuilder" not found.
I tried making some other classes (ex. Test/TestClass) and namespaces and it doesn't work with freshly created classes.
On the other hand it i fiddled around with the naming and it recognized my entities and factories.
I tried clearing the cache
I am in development mode
I am getting this message when calling {{ knp_menu_render('main') }} in twig.
My composer autoload:
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
symfony console debug:container app.menubuilder gives me this:
Information for Service "app.menu_builder"
==========================================
---------------- -------------------------------------------------------------
Option Value
---------------- -------------------------------------------------------------
Service ID app.menu_builder
Class App\Menu\MenuBuilder
Tags knp_menu.menu_builder (method: createMainMenu, alias: main)
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired yes
Autoconfigured yes
Usages knp_menu.menu_provider.lazy
---------------- -------------------------------------------------------------
! [NOTE] The "app.menu_builder" service or alias has been removed or inlined when the container was compiled.
How do i get Symfony to recognize my classses?
I want to configure the Doctrine bundle to have a DBAL connection. For some reason the configuration needs a bit of logic to retrieve. I tried to use a container extension and then a compiler pass to execute the logic while the container is compiled and store the configuration as container parameters.
During my attempts, I registered the extension and compiler pass like this in the Kernel class:
protected function build(ContainerBuilder $container)
{
// Those lines weren't there at the same time
$container->registerExtension(new MyCustomExtension());
$container->addCompilerPass(new MyCustomCompilerPass());
}
It seemed to work well as I could see my parameters in the console:
# ./bin/console debug:container --parameters
Symfony Container Parameters
============================
------------------------------------------------------------- ------------------------------------------------------------------------
Parameter Value
------------------------------------------------------------- ------------------------------------------------------------------------
...
some.prefix.host some-mariadb-host
some.prefix.dbname some-database-name
...
The problem is that when I try to use those parameters in my config/packages/doctrine.yaml I get an error on my next console command:
doctrine:
dbal:
driver: pdo_mysql
host: '%some.prefix.host%'
dbname: '%some.prefix.dbname%'
# ...
# ./bin/console debug:container --parameters
In ParameterBag.php line 98:
You have requested a non-existent parameter "some.prefix.host".
I am using Symfony 5.3 and Doctrine bundle 2.4.
Why do my parameters seem inaccessible for 3rd party bundle configuration ?
How can I make this work ?
Is there a better way to achieve this ?
I think the Doctrine bundle configuration gets processed before my compiler pass can declare the parameters. It probably can't be solved using the DependencyInjection component.
Solved it by importing a PHP configuration file in the services.yaml:
imports:
- { resource: my_custom_file.php }
With the following content:
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return function(ContainerConfigurator $configurator) {
// My specific logic
// Saving the configuration as parameters
$configurator->parameters()->set('some.prefix.host', $host);
$configurator->parameters()->set('some.prefix.dbname', $dbname);
// ...
};
I am using Symfony's Dependency Injection component version 3.4 in my custom PHP project. My project is running on PHP 5.6
"symfony/dependency-injection": "^3.4"
I have defined my services.yaml file to contain following service definitions
logger:
class: Monolog\Logger
arguments: ["application"]
autowire: true
public: true
Monolog\Logger: '#logger'
plugin_context:
class: MyProject\PluginContext
autowire: true
public: true
I can confirm that the autoloading is working and the instance of both classes are present in the definition, but the Logger class is not autowired in PluginContext constructor. the class is defined in the following code
use Monolog\Logger;
class PluginContext
{
private $logger;
function __construct(Logger $logger) {
$this->logger = $logger;
}
}
When the following code is run, PHP throws an exception
$container->get("plugin_context");
Catchable fatal error: Argument 1 passed to MyProject\PluginContext::__construct() must be an instance of Monolog\Logger, none given
Change your FQCN $logger and use this one use Psr\Log\LoggerInterface instead Monolog\Logger
Another thing, thanks to autowiring you don't need to specify anything in service.yaml except this (the default configuration):
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
The Doc said : « Aliases are used by the core bundles to allow services to be autowired. For example, MonologBundle creates a service whose id is logger. But it also adds an alias: Psr\Log\LoggerInterface that points to the logger service. This is why arguments type-hinted with Psr\Log\LoggerInterface can be autowired » so in your case the Psr\Log\LoggerInterface is an alias for Monolog https://symfony.com/doc/current/service_container/autowiring.html#using-aliases-to-enable-autowiring
It seems that the either the contents of services.yaml are not full.
Your services file should be like this
services:
logger:
class: Monolog\Logger
arguments: ["application"]
autowire: true
public: true
Monolog\Logger: '#logger'
plugin_context:
class: MyProject\PluginContext
autowire: true
public: true
I am trying to use different cache system on my environments. I would like to have, for example, Filesystem for dev and memcached for prod.
I am using symfony 3.3.10.
To achieve this, I would like to autowire the CacheInterface as follow:
use Psr\SimpleCache\CacheInterface;
class Api {
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
}
Here are my configuration files:
config_dev.yml:
framework:
cache:
app: cache.adapter.filesystem
config_prod.yml:
framework:
cache:
app: cache.adapter.memcached
...
Here is the error I get:
The error disappears when the FilesystemCache is declared as a service:
services:
Symfony\Component\Cache\Simple\FilesystemCache: ~
But now I cannot have another cache system for the test environment like NullCache. In fact, I have to declare only one service inheriting from CacheInterface. It is not possible as config_test is using config_dev too.
This is the beginning of services.yml if it can help:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
Any idea on how to autowire different cache system depending on the environment?
EDIT:
Here is the working configuration:
use Psr\Cache\CacheItemPoolInterface;
class MyApi
{
/**
* #var CacheItemPoolInterface
*/
private $cache;
public function __construct(CacheItemPoolInterface $cache)
{
$this->cache = $cache;
}
}
config.yml:
framework:
# ...
cache:
pools:
app.cache.api:
default_lifetime: 3600
services.yml:
# ...
Psr\Cache\CacheItemPoolInterface:
alias: 'app.cache.api'
Even though factory pattern is a good option to solve this kind of problem, normally you don't need to do that for Symfony cache system. Typehints CacheItemPoolInterface instead:
use Psr\Cache\CacheItemPoolInterface;
public function __construct(CacheItemPoolInterface $cache)
It automatically injects the current cache.app service depending on the active environment, so Symfony does the job for you!
Just make sure to configure the framework.cache.app for each environment config file:
# app/config/config_test.yml
imports:
- { resource: config_dev.yml }
framework:
#...
cache:
app: cache.adapter.null
services:
cache.adapter.null:
class: Symfony\Component\Cache\Adapter\NullAdapter
arguments: [~] # small trick to avoid arguments errors on compile-time.
As cache.adapter.null service isn't available by default, you might need to define it manually.
In Symfony 3.3+/4 and 2017/2019 you can omit any config dependency and keep full control of the behavior with factory pattern:
// AppBundle/Cache/CacheFactory.php
namespace AppBundle\Cache;
final class CacheFactory
{
public function create(string $environment): CacheInterface
{
if ($environment === 'prod') {
// do this
return new ...;
}
// default
return new ...;
}
}
And services.yml of course:
# app/config/services.yml
services:
Psr\SimpleCache\CacheInterface:
factory: 'AppBundle\Cache\CacheFactory:create'
arguments: ['%kernel.environment%']
See more about service factory in Symfony Documentation.
You can read more about this in my Why Config Coding Sucks post.