0

I have just started using PHP-DI and it is awesome, however I cannot manage to solve one issue.

Here is my services definitions

<?php

return [
    'services.report'=> DI\autowire( '\CRM\Report\Service\ReportService' ),
    '\CRM\Report\Service\ReportServiceContract' => DI\get ( 'services.report' )
];

And my class defined as follows

class ReportService implements ReportServiceContract {
    private $repository;

    /**
     * ReportService constructor.
     * @Inject("repositories.report_tasks")
     *
     * @param $repository
     */
    public function __construct( $repository ) {
        $this->repository = $repository;
        var_dump( $this->repository->getReportTaskById( 1 )->getDateSubmitted() );
    }

}

Having that configuration I am getting the following error

Entry "services.report" cannot be resolved: Parameter $repository of __construct() has no value defined or guessable Full definition: Object ( class = \CRM\Report\Service\ReportService lazy = false __construct( $repository = #UNDEFINED# ) )

I have tried to debug sources, and noticed that even an AnnotationReader instance is not created.

But when using the following definition

 <?php

    return [
'services.report' => DI\autowire( '\CRM\Report\Service\ReportService' )->constructorParameter('repository',DI\get('repositories.report_tasks')),
        '\CRM\Report\Service\ReportServiceContract' => DI\get ( 'services.report' )
    ];

Everything works fine.

What is wrong with my configuration ?

Virb
  • 1,639
  • 1
  • 16
  • 25

1 Answers1

0

To use annotations they need to be enabled (they are disabled by default): http://php-di.org/doc/annotations.html#installation

Annotations are disabled by default. To be able to use them, you first need to install the Doctrine Annotations library using Composer:

composer require doctrine/annotations

Then you need to configure the ContainerBuilder to use them:

$containerBuilder->useAnnotations(true);
Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261