Symfony2 passing associative array as argument in service definition issue - symfony

I am trying to pass an associative array as argument to a service definition (Solarium, to be precise). However, I get the following error:
"Catchable Fatal Error: Argument 1 passed to Symfony\Component\DependencyInjection\Definition::setArguments() must be of the type array, string given, "
My services.yml reads as follows:
parameters:
mynamespace.api.solrclient.config:
endpoint:
solrserver:
host: "search.mysite.com"
port: "80"
path: "/solr/"
services:
mynamespace.api.solrclient:
class: Solarium\Client
arguments: "%mynamespace.api.solrclient.config%"
Is there anything obviously wrong with the way I have defined the parameter array?

arguments must be an array, try:
services:
mynamespace.api.solrclient:
class: Solarium\Client
arguments: [%mynamespace.api.solrclient.config%]

Related

Named service in service.yml gives me autowire error

In symfony5 (it happened with me in symfony4 too, I just forgot the solution) I have strange behivor.
Here is the service.yml related part:
app.service.media.mediaresolve:
class: Vaso123\Service\Media\MediaResolve
arguments:
[
'%incomingPdf%',
'%processedPdf%',
'%workDir%',
]
If I am using like this, I am getting error:
Cannot autowire service "Vaso123\Service\Media\MediaResolve": argument
"$incomingPdf" of method "__construct()" is type-hinted "array", you
should configure its value explicitly.
Here comes the magic. If I remove the name and give exactly the class, it works:
Vaso123\Service\Media\MediaResolve:
arguments:
[
'%incomingPdf%',
'%processedPdf%',
'%workDir%',
]
Why is it happens? The name does not matter, it could be a.b, xxxxx, anything.

Symfony serializer with GetSetMethodNormalizer returns an empty arrays instead of dates

I configured service for using GetSetMethodNormalizer
services:
Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer:
arguments: [ '#Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface', '#serializer.name_converter.camel_case_to_snake_case' ]
tags: [serializer.normalizer]
And when I call $serializer->serialize() I got an empty array as a value for serialized date variable.
Could you please advise how it can be fixed?
The problem was in the priority of normalizers. Here is a solution:
services:
Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer:
arguments: ['#Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface', '#serializer.name_converter.camel_case_to_snake_case']
tags:
- { name: 'serializer.normalizer', priority: -1100 }
The new priority will put this normalizer right after all standard ones and serialization will works as expected.

Symfony 3.4 - Autowire not working for bind values?

I am working on migrating an existing Symfony 2.8 project to Symfony 3.4 and would like to autowire / auto inject the value of the %kernel.environment% parameter into a controller action.
However binding the parameter to a argument name does not work:
// app/config/services.yml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$kernelEnvironment: '%kernel.environment%'
MyBundle\:
resource: '../../src/MyBundle/*'
exclude: '../../src/MyBundle/{Entity,Repository,Tests}'
MyBundle\Controller\:
resource: '../../src/MyBundle/Controller'
tags: ['controller.service_arguments']
// src/MyBundle/SomeController.php
public function showPostAction($kernelEnvironment, $post_id) {
...
}
Uncaught PHP Exception RuntimeException: "Controller "MyBundle\Controller\SomeController::showPostAction()" requires that you provide a value for the "$kernelEnvironment" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one."
Why does this not work?
I know that I could specify MyBundle\Controller\SomeController directly within in the services.yml and set the value of $kernelEnvironment in the arguments list. However this solution would mean that I would have to specify the argument for every controller that uses this parameter. This is is not what autorwire is good for. Binding the argument should work, shouldn't it?

Symfony service container injecting string not object

I'm trying to inject an object into one of my services, as well as another service. The second argument is just an object, however when declaring it in the services.yml file, I receive an exception staying that its expecting an object, but a string was given.
services.yml
parameters:
app.my_class.class: AppBundle\MyClass
app.object_to_inject.class: AppBundle\InjectObject
services:
app.my_service:
class: %app.my_class.class%
arguments: [#app.another_service, %app.object_to_inject.class%]
Which results in:
Catchable Fatal Error: Argument 2 passed to
AppBundle\MyClass::__construct() must be an instance
of AppBundle\InjectObject, string given
I've tried quoting, unquoting and using new lines instead of square brackets:
arguments
- #app.another_service
- %app.object_to_inject.class%
Parameters are just strings, so you you trying to inject the string of your class name, not the actual class.
You need to define AppBundle\InjectObject as a service, then inject that.
Example:
parameters:
app.my_class.class: AppBundle\MyClass
app.object_to_inject.class: AppBundle\InjectObject
services:
app.my_object:
class: "%app.object_to_inject.class%"
app.my_service:
class: %app.my_class.class%
arguments: [#app.another_service, #app.my_object]

How do I configure my Twig / Symfony2 services for this?

How do I configure my services for this?
https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Extension/RoutingExtension.php
I'm guessing it's something like this:
twig.extension.anyName:
class: Bundle\Twig\RoutingExtension
arguments:
- "twig.extension.routing"
tags:
- { name: twig.extension }
...but it errors with:
Catchable Fatal Error: Argument 1 passed to Bundle\Twig\RoutingExtension::__construct() must implement interface Symfony\Component\Routing\Generator\UrlGeneratorInterface, string given...
Valid syntax for referencing other services is # symbol followed by service name. Currently you are passing string "twig.extension.routing" to your service constructor.
If you want pass service instance to your service, it could look something like this
twig.extension.anyName:
class: Bundle\Twig\RoutingExtension
arguments:
- "#twig.extension.routing"
tags:
- { name: twig.extension }

Resources