Autoconfiguration replace imported services - php

When I import services in the main configuration:
imports:
- { resource: services/attribute_loaders.yaml }
The services in the included file will be replaced with an autoconfigured version, so I've missed all my configured tags:
services:
App\Infrastructure\Bridge\Doctrine\EventListener\AttributeLoader\OrderAttributeLoader:
autowire: true
tags:
- name: 'doctrine_mongodb.odm.event_listener'
entity: 'App\Infrastructure\Bridge\Doctrine\EventListener\AttributeLoader\OrderAttributeLoader'
event: 'postLoad'
As a workaround, I've excluded my service from autoconfigure.
Is there any way to decompose services into several files?

That's exactly how you do it.
If you are going to use multiple files to configure your services, you simply can't define the same service twice.
Your definitions need to be specific enough so they do not overlap.
If you are going to use broad resource settings, you will need to add all the corresponding excludes so services defined in different files are not defined before it's time:
services:
App\:
resource: '../src/*'
exclude:
- '../src/Infrastructure/Symfony/DependencyInjection'
- '../src/Infrastructure/Symfony/Kernel.php'
- '../src/Tests'
- '../src/Messenger'
Services on src/Messenger, that belong to the App\Messenger namespace, can be defined independently on a different file:
E.g. something like this:
# messenger_services.yaml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false
App\Messenger\CommandHandler\:
resource: '../../src/Messenger/CommandHandler/*'
tags:
- {name: 'messenger.message_handler', bus: command.bus}
App\Messenger\EventHandler\:
resource: '../../src/Application/EventHandler/*'
tags:
- {name: 'messenger.message_handler', bus: event.bus}

Related

how to fix this error "App\Controller\CoreController" has no container set, did you forget to define it as a service subscriber?

i was deconnected any times at my application so i've want fix the problem but i've this error ans my folders / services was correctly configured. I would like to know if anyone has ever encountered this problem and if so how to fix it.
Services.yaml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# 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/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
CoreController
namespace App\Controller;
use App\Entity\Intervention;
use Doctrine\ORM\Mapping\Id;
use Doctrine\DBAL\Schema\Index;
use App\Repository\OperateurRepository;
use Doctrine\Persistence\ManagerRegistry;
use App\Repository\InterventionRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class CoreController extends AbstractController
{```
I've fix with
calls:
- [setContainer, ["#service_container"]]
in services.yaml

service error after upgraded from symfony 3.3 to symfony 3.4

I've upgraded my symfony projet version from 3.3 to 3.4 a few month ago and everything woked find but i recently tried to ad an event listener and then i've got that error
The configuration key "public" cannot be used to define a default value in "/home/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/services.yml". Allowed keys are "private", "tags", "autowire", "autoconfigure", "bind" in /home
/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/services.yml (which is being imported from "/home/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/config.yml").
obviuosly nothing seriuos i edited the default service.yml file from this
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
twig.extension:
class: AppBundle\Twig\Extension\MenuExtension
arguments:
- '#doctrine'
tags:
- {name : twig.extension }
# service.extension:
# class: AdminBundle\DependencyInjection\AdminExtension
# arguments: []
# tags: []
to this (because in symfony 3.4 or 4, all services are by default private)
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
private: true
twig.extension:
class: AppBundle\Twig\Extension\MenuExtension
arguments:
- '#doctrine'
tags:
- {name : twig.extension }
# service.extension:
# class: AdminBundle\DependencyInjection\AdminExtension
# arguments: []
# tags: []
but when i tried to run the server i've now got this error
The service "templating.loader.cache" has a dependency on a non-existent service "templating.loader.wrapped".
and i don't now how to solve it and where is that template.loder.wrapped service.

Symfony 4 autowiring exclude ignores subfolders

Assume following directory structure:
/src
/DTO
/Factory
/Collection
I want to exclude all classes, including classes from subdirectories of /DTO directory
In my services file I do:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*
exclude:
- '../src/DTO/*'
This leads to:
Symfony\Component\DependencyInjection\Exception\RuntimeException : Cannot autowire service App\DTO\Factory\Collection\MyCollection
If this service placed in DTO folder directly - then autowiring works.
Can I specify the exclude expression in any way to include subfolders?
To exclude everything from DTO folder use:
App\:
resource: '../src/*'
exclude:
- '../src/DTO'
i.e. w/o * symbol
I found a solution to exclude not only classes placed directly in folder, but also subfolders with two asterisk signs (**).
Here is the example of a valid configuration:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*
exclude:
- '../src/DTO/**'

Do steel need register Twig Extensions in the services.yml?

I learning Symfony3 on https://knpuniversity.com. Now on create their own filters in Twig (https://knpuniversity.com/screencast/symfony-services/create-twig-extension). In this video the author creates a class filter, but when using this filter in the Twig template:
<dd>{{ genus.funFact|markdownify }}</dd>
Symfony crashes with the error, and the author of the video says that the filter has used need to say Symfony about new filter Twig'. This is done using the register Twig extension as service in the services.yml. But, I have it all working, without errors, without any registrations in service.yml.
Do steel need register Twig Extensions in the services.yml?
Or is now not required and determines its own Twig extension?
I have a Symfony 3.3.6 (in video used 3.0.0).
Here listing my service.yml:
parameters:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
AppBundle\Service\MarkdownTransformer:
arguments:
- '#markdown.parser'
- '#doctrine_cache.providers.my_markdown_cache'
public: true
# AppBundle\Twig\MarkdownExtension:
# arguments: ['#AppBundle\Service\MarkdownTransformer']
# tags:
# - { name: twig.extension }
# public: true
In the bottom I'm added the settings block, which registers a new TWIG extension. Without this block, Symfony throws an exception (in the author of the lesson). But I have everything working without this block. So for example, I have it commented out.

Getting FileLoaderLoadException in symfony

I am new to Symfony.I am trying to learn File upload in Symfony.I am getting below error:
(3/3) FileLoaderLoadException
The file "D:\wamp\www\symfony_project\app/config\services.yml" does not contain valid YAML in D:\wamp\www\symfony_project\app/config\services.yml (which is being imported from "D:\wamp\www\symfony_project\app/config\config.yml").
config.yml
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'
services.yml
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
public: true
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
FileUploader.php
namespace AppBundle\Service;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $targetDir;
public function __construct($targetDir)
{
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->getTargetDir(), $fileName);
return $fileName;
}
public function getTargetDir()
{
return $this->targetDir;
}
}
As the error says, services.yml is not valid YAML. Remove $ sign before targetDir in line:
$targetDir: '%brochures_directory%'
So it will be:
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
I guess that this is your issue, but I cannot be sure as I don't know how looks the whole content of this file.
your services.yml does not contain valid YAML (Error message can't be more clear ;) )
Try this kind of code :
/***/
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
admin.file_uploader:
class: AppBundle\Service\FileUploader
arguments: [~, '%brochures_directory%', ~]
Change
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'
To
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'
Should work now:
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags:
- { name: "controller.service_arguments" }
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
public: true
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
You have space in front of AppBundle\Service\FileUploader:
→ ﹍AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
Hint: In future use Yaml linter:
bin/console lint:yaml app/config/services.yml

Categories