G
G
grabbee2021-11-30 13:43:46
symfony
grabbee, 2021-11-30 13:43:46

Does an abstract class need an interface?

When I make an Interface in a bundle, then I make an abstract class with an implementation and apply this interface to it, and then I create a child inheriting this abstract class, Symphony swears that it cannot find a candidate and asks to explicitly declare an alias for this interface.

But after all, an instance from an abstract class should not be created in principle. And the implementation of the interface in this case is only one. There shouldn't have been any mistakes.

---

services.yaml

services:
  _defaults:
    autowire: true  
    autoconfigure: true  

  Vendor\SomeBundle\:
    resource: '../src/*'
    exclude: '../src/{DependencyInjection,Entity,Examples,Repository,Message}'

  Vendor\SomeBundle\Controller\:
    resource: '../src/Controller'
    tags: ['controller.service_arguments']

  Vendor\SomeBundle\Controller\TokenController:
    autowire: false

Here. I disabled autowire for it . But then I try to turn it on in the tests, as described above. And it does not work even in this form
services_test.yaml
services:
  _defaults:
    autowire: true 
    autoconfigure: true 
    public: true
 
  Vendor\SomeBundle\Controller\SomeController:
    autowire: true
    arguments:
      $authenticator: Vendor\SomeBundle\Examples\Authenticator
 
  Vendor\SomeBundle\Examples\Authenticator:
    calls:
      - setIdentityClassName: ...

Cannot autowire service "Vendor\SomeBundle\Controller\SomeController": argument "$authenticator" of method "__construct()" references interface "Vendor\SomeBundle\S
ervices\AuthenticatorInterface" but no such service exists. You should maybe alias this interface to the existing "Vendor\SomeBundle\Examples\Authenticator" service.


I delete the folder with the cache and then run the command to clear the cache.

Starts to work only after
services_test.yaml
Vendor\SomeBundle\Services\AuthenticatorInterface:
    '@Vendor\SomeBundle\Examples\Authenticator'

However, there is only one implementation of the interface
interface AuthenticatorInterface {}
abstract class AbstractAuthenticator implements AuthenticatorInterface {}
class Authenticator extends AbstractAuthenticator {}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gordinskiy, 2021-11-30
@DmitriyGordinskiy

It's not about the abstract class. Symfony requires explicit registration of interface implementations.
It will look something like this:

services:
  ...
  App/Interfaces/UserRepositoryInterface:
    alias: App/Repositories/UserRepository

doka

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question