I can define anonymous inline factory (Symfony 6+) like this:
GuzzleClient1:
class: GuzzleHttp\ClientInterface
factory: [ !service { class: 'App\GuzzleClientFactory', calls: [ { 'withBaseUri': [ 'http://example.com' ] } ] }, 'build' ]
or like this:
GuzzleClient1:
class: GuzzleHttp\ClientInterface
factory:
- !service
class: 'App\GuzzleClientFactory'
calls:
- withBaseUri: ['http://example.com' ]
- 'build'
but I can't figure out the correct syntax for anonymous inline invokable factory. How do I omit method name here?
Related
I had already know we could create global interceptors from this code below:
import { Module } from '#nestjs/common';
import { APP_INTERCEPTOR } from '#nestjs/core';
#Module({
providers: [
{
provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor,
},
],
})
export class AppModule {}
Source: documentation
However, what if I want to have let say, UserInterceptor.
UserInterceptor will get user from database and transform the request.
UserInterceptor need to inject let say UserService.
And I want to use UserInterceptor globally.
#Injectable()
export class UserInterceptor {
constructor(private readonly service: UserService) {}
}
From documentation, we can't do app.useGlobalInterceptors(new UserInterceptor()) because UserInterceptor need 1 argument in the constructor (UserService).
And since we had use APP_INTERCEPTOR for LoggingInterceptor, I didn't found another way to assign another value to APP_INTERCEPTOR to use the interceptor globally.
For example I think the problem will solved if we could do:
providers: [
{
provide: APP_INTERCEPTOR,
useClass: [LoggingInterceptor, UserInterceptor]
}
]
providers: [
{
provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor
},
{
provide: APP_INTERCEPTOR,
useClass: UserInterceptor
}
]
Just like this
app.imagine.cache.resolver.proxy:
class: Liip\ImagineBundle\Imagine\Cache\Resolver\ProxyResolver
arguments:
- "#app.imagine.cache.resolver.s3"
- [ '%env(S3_CDN_URL)%' ]
tags:
- { name: "liip_imagine.cache.resolver", resolver: "proxy" }
app.s3:
class: Aws\S3\S3Client
factory: [Aws\S3\S3Client, factory]
arguments:
-
version: 'latest'
region: 'fra1'
endpoint: '%env(S3_BASE_URL)%'
credentials:
key: '%env(S3_KEY)%'
secret: '%env(S3_SECRET_KEY)%'
app.imagine.cache.resolver.s3:
class: Liip\ImagineBundle\Imagine\Cache\Resolver\AwsS3Resolver
arguments:
- "#app.s3"
- '%env(S3_BUCKET_NAME)%'
tags:
- { name: "liip_imagine.cache.resolver", resolver: "s3" }
The problem is I'd like to use DigitalOcean spaces CDN and its CDN is per bucket, so there is no bucket prefix.
In the end I get a resolved url like:
S3_CDN_URL/bucketName/filePath
which is non existent.
I need to get:
S3_CDN_URL/filePath
I can't find any setting for that.
Basically, I need to access S3 in 'Virtual Hosted Style': https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html#access-bucket-intro
In Symfony I try to merge a block of YAML from parameters.yml to config.yml. My question is how I can store some configurations in Symfony 3.4 and then insert into my config file. For now I got an error:
In Parser.php line 290:
Reference "'%insurance%'" does not exist at line 188 (near "<<:
*'%insurance%'").
parameters.yml
parameters:
Insurance: &insurance
list:
title: '<strong>Ubezpieczenia</strong>'
sort: ['sequence', 'ASC']
fields:
- { property: 'sequence', label: 'Kolejność'}
- { property: 'title', label: 'Tytuł'}
- { property: 'description', label: 'Opis'}
form:
fields:
- { property: 'title', type: 'text', label: 'Tytuł'}
- { property: 'description', type: 'ckeditor', label: 'Opis',
type_options: { config_name: 'simple_config' }}
- { property: 'sequence', type: 'integer', label: 'Kolejność'}
config.yml
imports:
- { resource: parameters.yml }
easy_admin:
[...]
entities:
[...]
MotorInsurance:
class: AppBundle\Entity\MotorInsurance
label: menu.motorInsurance
<<: *'%insurance%'
[...] there are non-related configurations
Am I did something wrong when I am calling Inusrance block?
So I try again and #xabbuh was right. I forgot that I have also block for parameters in my config.yml. My file now look like this:
parameters:
locale: pl
Insurance: &insurance
list:
title: '<strong>Ubezpieczenia</strong>'
sort: ['sequence', 'ASC']
fields:
- { property: 'sequence', label: 'Kolejność'}
- { property: 'title', label: 'Tytuł'}
- { property: 'description', label: 'Opis'}
form:
fields:
- { property: 'title', type: 'text', label: 'Tytuł'}
- { property: 'description', type: 'ckeditor', label: 'Opis',
type_options: { config_name: 'simple_config' }}
- { property: 'sequence', type: 'integer', label: 'Kolejność'}
easy_admin:
[...]
entities:
[...]
MotorInsurance:
class: AppBundle\Entity\MotorInsurance
label: menu.motorInsurance
<<: *insurance
And this works fine :) also everything can be overriden for your minds.
This does not work. Different files are parsed independently so you cannot reuse a reference defined in one YAML file in another YAML file.
The solution would be to place all the config that is required in the same file.
I tring to get an argument in my services.yaml for my ImageManager.php but it's not working and I can not solve this error.
here is the mistake :
Type error: Too few arguments to function App\Manager\ImageManager::__construct(), 0 passed in C:\wamp64\www\SymfonyAPI\var\cache\dev\ContainerZxFSS5S\getImageManagerService.php on line 14 and exactly 1 expected
services.yaml
parameters:
images_directory: '%kernel.project_dir%/public/uploads/images/'
...
services:
_defaults:
autowire: false
autoconfigure: false
public: true
App\Manager\ImageManager:
arguments:
$targetDir: '%images_directory%'
if for autowire & autoconfigure I put true I have this error :
RuntimeException
Cannot autowire service "App\Manager\ImageManager": argument "$targetDir" of method "__construct()" has type "App\Manager\targetDir" but this class was not found.
ImageManager.php
private $targetDir;
public function __construct(targetDir $targetDir)
{
$this->targetDir = $targetDir;
}
Full services.yml
parameters:
liip_imagine.mozjpeg.binary: /mozjpeg/cjpeg.exe
images_directory: '%kernel.project_dir%/public/uploads/images/'
mozjpg_directory: '%kernel.project_dir%/mozjpg'
locale: 'en'
services:
_defaults:
autowire: false
autoconfigure: false
public: true
App\Manager\ImageManager:
arguments:
$targetDir: '%images_directory%'
App\EventListener\ImageUploadListener:
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: preUpdate }
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
app.post_processor.my_custom_post_processor:
class: '%kernel.project_dir%/src/Controller/ImageController.php'
tags:
- { name: 'liip_imagine.filter.post_processor', post_processor: 'mozjpeg' }
You are adding a type of targetDir and your Application thinks that is some kind of class and you can see that in your error has type "App\Manager\targetDir" but this class was not found., just replace targetDir with string if you are on php 7 or don't put anything as a type and it will work even if you have autowire true/false because of:
App\Manager\ImageManager:
arguments:
$targetDir: '%images_directory%'
To clarify the answer by #kunicmarko20
Your service constructor requires an object of App\Manager\targetDir as the $targetDir argument, but you are supplying your service with a string as the $targetDir argument.
You need to change your service constructor to look like one of the following.
PHP 7.x
public function __construct(string $targetDir)
PHP 5.x
public function __construct($targetDir)
Update with configuration changes
The second issue you have is that you have prototyping enabled on your service class directory. This causes Symfony to override your manual service definition with the prototype service definition.
So what happens is your manual service definition is created, then overridden by the auto configured definition.
The prototype definition is
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
Since it is below your manual service definition, Symfony uses it instead of your manual definition.
For example if I write
services:
AppBundle\MyDirectory\Object:
parameters: ['a']
AppBundle\MyDirectory\Object:
parameters: ['b']
The end result of what symfony uses as the service definition would be.
new AppBundle\MyDirectory\Object('b');
You should change your services.yml to the following:
parameters:
liip_imagine.mozjpeg.binary: /mozjpeg/cjpeg.exe
images_directory: '%kernel.project_dir%/public/uploads/images/'
mozjpg_directory: '%kernel.project_dir%/mozjpg'
locale: 'en'
services:
_defaults:
autowire: false
autoconfigure: false
public: true
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
#... Your manual service definitions below here.
App\Manager\ImageManager:
arguments:
$targetDir: '%images_directory%'
App\EventListener\ImageUploadListener:
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: preUpdate }
app.post_processor.my_custom_post_processor:
class: '%kernel.project_dir%/src/Controller/ImageController.php'
tags:
- { name: 'liip_imagine.filter.post_processor', post_processor: 'mozjpeg' }
I use Symfony 2.8 and Behat 3.3. I have standard FeatureContext class in project_root/features/bootstrap directory. Before scenario execution I want purge DB like that:
/**
* #BeforeScenario
*/
public function beforeScenario()
{
// use Doctrine\Common\DataFixtures\Purger\ORMPurger
$purger = new ORMPurger($this->em);
$purger->purge();
}
But when I execute test I receive error:
The annotation "#Doctrine\ORM\Mapping\Entity" in class XXX does not
exist, or could not be auto-loaded.
My behat.yml is:
default:
suites:
default:
contexts:
- FeatureContext:
em: '#doctrine.orm.entity_manager'
extensions:
Behat\Symfony2Extension:
kernel:
bootstrap: "vendor/autoload.php"
env: "test"
debug: "true"
composer.json autoload section:
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
}
If remove line bootstrap: "vendor/autoload.php" from behat.yml, everything will work as needed.