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

Documented injector configuration shorthand doesn't work, but it should #11595

Open
1 task done
beerbohmdo opened this issue Jan 31, 2025 · 3 comments
Open
1 task done

Comments

@beerbohmdo
Copy link
Contributor

Pages affected

https://docs.silverstripe.org/en/5/developer_guides/extending/injector/#service-inheritance

Description

I am not sure if this is a issue of the documentation or a bug:

The documentation says that you can use App\GZIPJSONProvider: '%$App\JSONServiceDefinition' to define an alias for another class definition. This does not work.

If I read the code of the injector class correctly, using a string is just an shortcut for the class property. So omitting the '%$' prefix works but does not copy the definition.

And the second example does not contain the namespace...

Validations

  • Check that there isn't already an issue that reports the same problem
@GuySartorelli
Copy link
Member

Can you please provide more information? What about this doesn't work? What happens, vs what is the expected behaviour in-so-far as your understanding of the documentation?

@beerbohmdo
Copy link
Contributor Author

Sorry I found the issue:

I had this configuration:

SilverStripe\Core\Injector\Injector:
  MyNamespace\HTTPGateway:
    constructor:
      0: '%$Psr\Log\LoggerInterface'

  MyNamespace\Gateway: '%$MyNamespace\NullGateway'

This throws the error Class MyNamespace\Gateway does not exist.

The issue is that I have not defined the none paramethized class MyNamespace\NullGateway in the yml file.

This works:

SilverStripe\Core\Injector\Injector:
  MyNamespace\HTTPGateway:
    constructor:
      0: '%$Psr\Log\LoggerInterface'

  MyNamespace\NullGateway:
    class: MyNamespace\NullGateway

  MyNamespace\Gateway: '%$MyNamespace\NullGateway'

And now even my later replacement with the other class works WITH copying all configurations:

---
Name: custom_my_module
After:
  - 'my_module'
---
SilverStripe\Core\Injector\Injector:
  MyNamespace\Gateway: '%$MyNamespace\HTTPGateway'

So the only issue is that you have to explizitly define a service for a class if you wan't to use the this syntax. For example inside a private static array $dependencies this just works without explizit definition.

Maybe one additional sentence in the documentation would help for this special case.

Regardless of this, the second example misses the namespace in the reference and cannot not work.

@GuySartorelli
Copy link
Member

So the only issue is that you have to explizitly define a service for a class if you wan't to use the this syntax. For example inside a private static array $dependencies this just works without explizit definition.

Maybe one additional sentence in the documentation would help for this special case.

If it works without an explicit definition in the dependencies configuration (when an actual class with that name exists), it should probably also work without an explicitly definition in the injector configuration. This sounds like a bug in framework.

@GuySartorelli GuySartorelli transferred this issue from silverstripe/developer-docs Feb 3, 2025
@GuySartorelli GuySartorelli changed the title Service Inheritence Documentation is wrong Documented injector configuration shorthand doesn't work, but it should Feb 3, 2025
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

2 participants