Skip to content

Interface injection#42

Closed
avalanche123 wants to merge 1 commit intofabpot:masterfrom
avalanche123:interface_injection
Closed

Interface injection#42
avalanche123 wants to merge 1 commit intofabpot:masterfrom
avalanche123:interface_injection

Conversation

@avalanche123
Copy link
Copy Markdown

Here is the case for the feature - http://avalanche123.com/post/1221451286/interface-injection-and-symfony2-dic
The actual implementation is very simple right now, but I wasn't familiar with DIC component enough to implement otherwise. Please provide feedback:)

@fabpot
Copy link
Copy Markdown
Owner

fabpot commented Oct 1, 2010

Very interesting. I was aware of interface injection, but I always thought that this was not really possible in PHP (don't ask me why). So, I really like the idea and the way you configure it. As for the implementation, I will need a bit more time to read and understand it before merging it.

Thanks for putting so much effort into that.

Can you send an email to the dev mailing-list to open the discussion?

@avalanche123
Copy link
Copy Markdown
Author

Sure, will do. Will be in the office in 30 minutes, and will send out a nice email then. Thanks for the feedback, can't wait for it to be implemented.

@avalanche123
Copy link
Copy Markdown
Author

Closing as is not very usable right now. The plan is to move Interface Injection logic into the instantiation time instead of configuration time. This will affect Dumper::dump() and ContainerBuilder::createService().
Thanks for pointing that out, Fabien

@Seldaek
Copy link
Copy Markdown

Seldaek commented Oct 1, 2010

Isn't that going to be slow if you have to do tons of instanceof checks + assign the dependencies at runtime? I mean, it may be fast still, but it won't be as fast as hardcoded dependencies I guess. Not sure the little convenience it brings is worth the performance hit.

@avalanche123
Copy link
Copy Markdown
Author

Not really. The instance checks would take place during the dumping stage, since at that stage the full class name is already known. Then, depending on whether that class implements the required interface, the correct method calls would be added to the generated PHP container. So when you look at cached container all you see is service instantiation and appropriate method calls, just like now.
The only place it would make performance much slower is in ContainerBuilder::get(), but you're not supposed to use that anyway as performance there is bad as is...

fabpot added a commit that referenced this pull request Sep 30, 2020
…rrabus)

This PR was merged into the 5.2-dev branch.

Discussion
----------

[VarDumper] Support for ReflectionAttribute

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | N/A
| License       | MIT
| Doc PR        | not needed

VarDumper currently does not understand that certain reflection objects might have attributes attached to it. Dumping a `ReflectionAttribute` just yields `ReflectionAttribute {symfony#4711}` which is not really helpful. This PR attempts to fix this.

```
ReflectionAttribute {symfony#4711
  name: "App\MyAttribute"
  arguments: array:2 [
    0 => "one"
    "extra" => "hello"
  ]
}
```

While working on this, I noticed that class constants (which can be reflected on since PHP 7.1) are just dumped as plain values, so I've also added a caster for `ReflectionClasConstant` as bonus.

The full output for the `LotsOfAttributes` fixture class that is included with is PR looks like this:

<details>

```
^ ReflectionClass {#7
  +name: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
  modifiers: "final"
  implements: []
  constants: array:1 [
    0 => ReflectionClassConstant {#20
      +name: "SOME_CONSTANT"
      +class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
      modifiers: "public"
      value: "some value"
      attributes: array:2 [
        0 => ReflectionAttribute {#33
          name: "Symfony\Component\VarDumper\Tests\Fixtures\RepeatableAttribute"
          arguments: array:1 [
            0 => "one"
          ]
        }
        1 => ReflectionAttribute {#34
          name: "Symfony\Component\VarDumper\Tests\Fixtures\RepeatableAttribute"
          arguments: array:1 [
            0 => "two"
          ]
        }
      ]
    }
  ]
  properties: array:1 [
    "someProperty" => ReflectionProperty {#19
      +name: "someProperty"
      +class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
      modifiers: "private"
      attributes: array:1 [
        0 => ReflectionAttribute {#30
          name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
          arguments: array:2 [
            0 => "one"
            "extra" => "hello"
          ]
        }
      ]
    }
  ]
  methods: array:1 [
    "someMethod" => ReflectionMethod {#21
      +name: "someMethod"
      +class: "Symfony\Component\VarDumper\Tests\Fixtures\LotsOfAttributes"
      returnType: "void"
      parameters: {
        $someParameter: ReflectionParameter {#28
          +name: "someParameter"
          position: 0
          attributes: array:1 [
            0 => ReflectionAttribute {#42
              name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
              arguments: array:1 [
                0 => "three"
              ]
            }
          ]
          typeHint: "string"
        }
      }
      attributes: array:1 [
        0 => ReflectionAttribute {#27
          name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
          arguments: array:1 [
            0 => "two"
          ]
        }
      ]
      modifiers: "public"
    }
  ]
  attributes: array:1 [
    0 => ReflectionAttribute {#22
      name: "Symfony\Component\VarDumper\Tests\Fixtures\MyAttribute"
      arguments: []
    }
  ]
  extra: {
    file: "./src/Symfony/Component/VarDumper/Tests/Fixtures/LotsOfAttributes.php"
    line: "15 to 28"
    isUserDefined: true
  }
}
```

</details>

Commits
-------

34dbf01 [VarDumper] Support for ReflectionAttribute.
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants