PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 11, 2022

[FIXED] How can I use a class constant as the tag for bound parameter service?

 May 11, 2022     php, symfony, symfony-dependency-injection, symfony4     No comments   

Issue

I have tagged a group of services that implement the same interface like this in my configuration:

// services.yml
services:
  _instanceof:
     App\SomeInterface:
       tags: [ !php/const App\SomeInterface::TAG ]

(Where the value for App\SomeInterface::TAG is very_unique_identifier)

I would like to reuse this value to bind this parameter like this:

// services.yaml
 services:
  _defaults:
    autowire: true      
    autoconfigure: true 
    public: false
    bind:
      $fooStrategies: !tagged !php/const App\SomeInterface::TAG 

But when I do it like this, I get an empty RewindableGenerator.

Instead I can do it like this, using the literal value for the constant:

bind:
      $fooStrategies: !tagged 'very_unique_identifier'

... and it works as expected, and the RewindableGenerator has all the necessary services inside.

How can I use the PHP constant in both places, so I do not have to repeat the value in different places? Obviously, not a huge deal, but I'd like to avoid it if possible.


Solution

You should be able to configure a parameter with the value !php/const App\SomeInterface::TAG and then use the parameter name in service.yml.

parameters.yml

parameters:
    interfaceTag: !php/const App\SomeInterface::TAG

and then

services.yml

services:
  _defaults:
     autowire: true      
     autoconfigure: true 
     public: false
     bind:
       $fooStrategies: !tagged '%interfaceTag%'


Answered By - NorthernDev
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing