Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot decorate translation.reader #28843

Closed
quazardous opened this issue Oct 12, 2018 · 8 comments
Closed

Cannot decorate translation.reader #28843

quazardous opened this issue Oct 12, 2018 · 8 comments

Comments

@quazardous
Copy link

quazardous commented Oct 12, 2018

Symfony version(s) affected: 4.1.6

Description
Cannot decorate translation.reader (I want to change the default i18n file loading process)

How to reproduce

copy/adapt Symfony\Component\Translation\Reader\TranslationReader to App\Translation\Reader\TranslationReader

Follow https://symfony.com/doc/current/service_container/service_decoration.html

Modify services.yaml

    Symfony\Component\Translation\Reader\TranslationReader: ~
    
    App\Translation\Reader\TranslationReader:
        decorates: Symfony\Component\Translation\Reader\TranslationReader

    #translation.reader: '@App\Translation\Reader\TranslationReader'

Without the alias : the new service is ignored
With the alias : read() is trigger but not addLoder()

Possible Solution

Additional context

$ php bin/console debug:container | grep -i reader without alias

  App\Translation\Reader\TranslationReader                                   App\Translation\Reader\TranslationReader                                                
  App\Translation\Reader\TranslationReader.inner                             Symfony\Component\Translation\Reader\TranslationReader                                  
  Doctrine\Common\Annotations\Reader                                         alias for "annotations.cached_reader"                                                   
  Symfony\Component\Translation\Reader\TranslationReader                     alias for "App\Translation\Reader\TranslationReader"                                    
  Symfony\Component\Translation\Reader\TranslationReaderInterface            alias for "translation.reader"                                                          
  annotation_reader                                                          alias for "annotations.cached_reader"                                                   
  annotations.cached_reader                                                  Doctrine\Common\Annotations\CachedReader                                                
  annotations.reader                                                         Doctrine\Common\Annotations\AnnotationReader                                            
  doctrine.orm.metadata.annotation_reader                                    alias for "annotations.cached_reader"                                                   
  translation.reader                                                         Symfony\Component\Translation\Reader\TranslationReader   

$ php bin/console debug:container | grep -i reader with alias

  App\Translation\Reader\TranslationReader                                   App\Translation\Reader\TranslationReader                                                
  App\Translation\Reader\TranslationReader.inner                             Symfony\Component\Translation\Reader\TranslationReader                                  
  Doctrine\Common\Annotations\Reader                                         alias for "annotations.cached_reader"                                                   
  Symfony\Component\Translation\Reader\TranslationReader                     alias for "App\Translation\Reader\TranslationReader"                                    
  Symfony\Component\Translation\Reader\TranslationReaderInterface            alias for "App\Translation\Reader\TranslationReader"                                    
  annotation_reader                                                          alias for "annotations.cached_reader"                                                   
  annotations.cached_reader                                                  Doctrine\Common\Annotations\CachedReader                                                
  annotations.reader                                                         Doctrine\Common\Annotations\AnnotationReader                                            
  doctrine.orm.metadata.annotation_reader                                    alias for "annotations.cached_reader"                                                   
  translation.reader                                                         alias for "App\Translation\Reader\TranslationReader"
@xabbuh
Copy link
Member

xabbuh commented Oct 14, 2018

I am not sure I understand what you are trying to achieve. Why are you registering the Symfony\Component\Translation\Reader\TranslationReader on your own?

@quazardous
Copy link
Author

quazardous commented Oct 14, 2018

@xabbuh I may say why not 😁 ? I'm coming from Silex and love to tweak things. I should be able to decorate/override whatever is in the framework.

in this case I'm planning to port a Silex app to sf4 and the language files are split in subfolder by domaine with multiple files within (on for each controller/module).

So I want tout keep the files as it (little files are easier tout maintain Imo)

@xabbuh
Copy link
Member

xabbuh commented Oct 14, 2018

The addLoader() calls are added to the built-in translation.reader service. By defining a custom service you are not decorating the built-in service. From how I understand your issue you probably rather need something like this:

App\Translation\Reader\TranslationReader:
    decorates: translation.reader

@quazardous
Copy link
Author

#    Symfony\Component\Translation\Reader\TranslationReader: ~
    
    App\Translation\Reader\TranslationReader:
        decorates: translation.reader
#        decorates: Symfony\Component\Translation\Reader\TranslationReader

addLoader() is not triggered either


Seams to have the same effect on injection (and it's a good thing imo)

php bin/console debug:container | grep -i reader

  App\Translation\Reader\TranslationReader                                   App\Translation\Reader\TranslationReader                                                
  App\Translation\Reader\TranslationReader.inner                             Symfony\Component\Translation\Reader\TranslationReader                                  
  Doctrine\Common\Annotations\Reader                                         alias for "annotations.cached_reader"                                                   
  Symfony\Component\Translation\Reader\TranslationReader                     Symfony\Component\Translation\Reader\TranslationReader                                  
  Symfony\Component\Translation\Reader\TranslationReaderInterface            alias for "App\Translation\Reader\TranslationReader"                                    
  annotation_reader                                                          alias for "annotations.cached_reader"                                                   
  annotations.cached_reader                                                  Doctrine\Common\Annotations\CachedReader                                                
  annotations.reader                                                         Doctrine\Common\Annotations\AnnotationReader                                            
  doctrine.orm.metadata.annotation_reader                                    alias for "annotations.cached_reader"                                                   
  translation.reader                                                         alias for "App\Translation\Reader\TranslationReader"   

@xabbuh
Copy link
Member

xabbuh commented Oct 15, 2018

Wait, are we talking about addLoader() calls missing on the translation.reader service or that they are not defined for your App\Translation\Reader\TranslationReader service?

@quazardous
Copy link
Author

quazardous commented Oct 15, 2018

Basically I just made a copy of the native reader. So all functions are present.
read() is called but addLoader() is not and the internal loaders array is empty, so it can't handle the translation base folder.

@xabbuh
Copy link
Member

xabbuh commented Oct 15, 2018

If you decorate a service, the method calls configured for that service are not applied to your decorating service. If you need some methods of your class to be called, your service definition needs to reflect that.

However, I do not know exactly what you are trying to achieve and maybe service decoration is not the right solution to your problem. But since we use GitHub only to handle bug reports and discuss new features, I need to ask you to use one of the support channels to seek for further help. Since there is nothing to fix in Symfony I am closing here. Thank you for understanding.

@xabbuh xabbuh closed this as completed Oct 15, 2018
@quazardous
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants