How can I pass nested parameter value to service in Symfony2 - symfony

For example, in my services.yml file next code:
parameters:
social:
facebook:
app_id: 123456789
secret: dkl41a5dw1daw11d1wa135451awwlflaw
google:
id: 12548411654
services:
bw.user.social:
class: BW\UserBundle\Service\SocialService
arguments: [%social.facebook%]
This method does not work. I can use this:
services:
bw.user.social:
class: BW\UserBundle\Service\SocialService
arguments: [%social%]
But it's not exactly what I need. How can I pass only facebook value without google and , it's very important, not change parameters structure?

You have to write your parameters like this :
parameters:
social.facebook:
app_id: 123456789
secret: dkl41a5dw1daw11d1wa135451awwlflaw
social.google:
id: 12548411654
Then you can use :
services:
bw.user.social:
class: BW\UserBundle\Service\SocialService
arguments: [%social.facebook%]

Related

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.

How to override namespace argument in RedisAdapter for a Symfony4 app to cache

I want to use custom namespace in our RedisAdapter to cache in our Symfony4 app. However, when i want to set arguments like this in our services.yaml;
cache.adapter.redis:
class: Symfony\Component\Cache\Adapter\RedisAdapter
arguments:
- '#Redis'
- 'app'
I see this error message:
Symfony\Component\Cache\Traits\RedisTrait::init() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\ClientInterface, string given.
By the way, our cache config(config/packages/cache.yaml) is simple like below. So, how can I set namespace directly from any config?
cache:
app: cache.adapter.redis
default_redis_provider: 'redis://%env(REDIS_HOST)%:%env(REDIS_PORT)%'
I solved it with this config:
app.cache.adapter.redis:
parent: 'cache.adapter.redis'
public: true
autowire: true
autoconfigure: false
tags:
- {name: 'cache.pool', namespace: 'app'}
If you need the namespace to be interoperable with a third-party app,
you can take control over auto-generation by setting the namespace attribute of the cache.pool service tag.
For example, you can override the service definition of the adapter:
# config/services.yaml
services:
app.cache.adapter.redis:
parent: 'cache.adapter.redis'
tags:
- { name: 'cache.pool', namespace: 'my_custom_namespace' }
more details here: https://github.com/symfony/symfony-docs/pull/12527

How can one use a defined channel, in monolog handler from a tagged service?

I have the following monolog handler definitions:
# config_prod.yml
app_generic:
type: rotating_file
max_files: 15
path: "%param.app_logging_config.log_generic_file%"
level: info
channels: [app]
app_api:
max_files: 15
path: "%param.app_logging_config.log_api_file%"
level: info
channels: [app]
level: info
app_response:
max_files: 15
path: "%param.app_logging_config.log_response_file%"
channels: [app]
level: info
And in service.yml, my intention is to inject monolog (#logger) with an array of the above defined handlers.
#service.yml
app.app_logger:
class: AppBundle\Classes\AppLogger
arguments: ['#logger': ['#app_generic', '#app_api', '#app_response']]
calls:
- [init, ['%app_logging_config%']
tags:
- { name: monolog.logger, channel: app }
How does one pass arguments to an injected argument?
Update:
Re-reading the description, I was going for this approach, by just tagging on the service definition:
app.logger:
arguments: ['#logger']
tags:
- { name: monolog.logger, channel: app }
channels: ['app']
Or even ( if I understood correctly), adding a channels: ['app'] key and just having this in service argument:
app.logger:
arguments: ['#monolog.logger.app']
I have not been able to use ( or see via dump ) the handlers defined in config_prod.yml. I have placed these at top because of other "fingers_crossed" handlers I thought may interfere.
I woud like to know, why neither of above (documented) approaches seem to work?
Handlers in
monolog:
handlers:
handler1: ...
handler2: ...
are automatically injected in #logger service.
It looks like you need new custom logger. Please read about The DependencyInjection Component
Create dependencies
services:
app_generic:
....
app_api:
....
app_response:
....
Create custom_logger service
custom_logger:
class: Monolog\Logger
arguments: ["my logger", ["#app_generic", "#app_api", "#app_response"]
Inject you custom logger in you service
app.app_logger:
class: AppBundle\Classes\AppLogger
arguments: ['#custom_logger']
calls:
- [init, ['%app_logging_config%']

Service not recognized by Symfony

I have a concern with the following service in my project:
app.security.guardAuthenticatorLoginPassword:
class: AppBundle\Security\LoginPasswordAuthenticator
arguments: ["#router"]
app.jwt_token_authenticator:
class: AppBundle\Security\JwtAuthenticator
arguments: ['#doctrine.orm.entity_manager', '#lexik_jwt_authentication.encoder']
I feel that whatever I do it is not recognized by my program ..
I have been looking for a few hours already but I do not understand why it does not work. While I have both my LoginPasswordAuthenticator and JwtAuthenticator classes in the specified path ..
Thank you in advance for your help
Here is some potentials issues:
_ Clear the cache
_ Check if you didn't forget a 'use' for your differents classes
_ Give to your services a name (and why not an alias) and use it when you call your service:
app.security.guardAuthenticatorLoginPassword:
class: AppBundle\Security\LoginPasswordAuthenticator
arguments: ["#router"]
tags:
- { name: login.password.authenticator, alias: login.auth }
app.jwt_token_authenticator:
class: AppBundle\Security\JwtAuthenticator
arguments:['#doctrine.orm.entity_manager','#lexik_jwt_authentication.encoder']
tags:
- { name: security.authenticator, alias: security.auth }

Having problems setting up AWS S3/Cloudfront with Symfony and LiipImagineBundle

I'm trying to set up AWS S3/Cloudfront to work with liipimaginebundle in Symfony, but I really have no idea what I'm doing.
So far I have tried the following documented here http://symfony.com/doc/current/bundles/LiipImagineBundle/cache-resolver/aws_s3.html:
Installed aws-sdk-php:
"require": {
"aws/aws-sdk-php": "^3.28",
}
Set up my parameters (with the correct values not this dummy data):
amazon.s3.key: "your-aws-key"
amazon.s3.secret: "your-aws-secret"
amazon.s3.bucket: "your-bucket.example.com"
amazon.s3.region: "your-bucket-region"
Set up a resolver (although I'm not sure what that even means).
"%amazon.s3.cache_bucket%" is in the documentation but the parameter doesn't exist so I used "%amazon.s3.bucket%" instead:
liip_imagine:
cache: profile_photos
resolvers:
profile_photos:
aws_s3:
client_config:
credentials:
key: "%amazon.s3.key%"
secret: "%amazon.s3.secret%"
region: "%amazon.s3.region%"
bucket: "%amazon.s3.bucket%"
get_options:
Scheme: https
put_options:
CacheControl: "max-age=86400"
Added these lines to create the services:
services:
acme.amazon_s3:
class: Aws\S3\S3Client
factory: Aws\S3\S3Client
arguments:
-
credentials: { key: "%amazon.s3.key%", secret: "%amazon.s3.secret%" }
region: "%amazon.s3.region%"
acme.imagine.cache.resolver.amazon_s3:
class: Liip\ImagineBundle\Imagine\Cache\Resolver\AwsS3Resolver
arguments:
- "#acme.amazon_s3"
- "%amazon.s3.bucket%"
tags:
- { name: "liip_imagine.cache.resolver", resolver: "amazon_s3" }
I'm currently getting this error when I run php bin/console server:run:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\UndefinedFunctionException: Attempted to call function "S3Client" from namespace "Aws\S3". in /var/www/swing-polls/var/cache/dev/appDevDebugProjectContainer.php:360
I've tried half a dozen other configs/tutorials to no avail. If someone can point me in the right direction I'd be incredibly grateful.
Using the code provided at Simple S3 Symfony Service with a few tweaks, I've been able to get my images to upload to my s3 bucket, but I just don't know how to get liipimaginebundle work with them.
In vendor/liip/imagine-bundle/DependencyInjection/Compiler/ResolversCompilerPass.php you can see the CompilerPass is getting the value from "resolver" attribute of the tag and is using it to create a Reference object. This means the resolver should contain the id of a service.
Try replacing
tags:
- { name: "liip_imagine.cache.resolver", resolver: "amazon_s3" }
with
tags:
- { name: "liip_imagine.cache.resolver", resolver: "acme.amazon_s3" }

Resources