2
votes

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' }
2
I don't understand how, but I just clone my project that was on github and everythings works. I'm sorry for making you waste your time, thanks anyway.Morgan Millet

2 Answers

3
votes

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%'
2
votes

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' }