I am trying to use php-di
in a project for creating a renderer that renders my default views.
This is how my index.php
looks like :
<?php
require "../vendor/autoload.php";
use DIContainerBuilder;
use FrameworkRendererRendererInterface;
use FrameworkRendererTwigRendererFactory;
use function DIfactory;
$builder= new ContainerBuilder();
$builder->addDefinitions([
RendererInterface::class => factory(TwigRendererFactory::class),
'views.path'=> dirname(__DIR__) . '/views'
]);
$container= $builder->build();
$renderer= $container->get(RendererInterface::class);//<-- Line causing the error
var_dump($renderer);
die();
My TwigRenderFactory looks like this :
<?php
namespace FrameworkRenderer;
use PsrContainerContainerInterface;
class TwigRendererFactory
{
public function __invoke(ContainerInterface $container)
{
return new TwigRenderer($container->get('views.path'));
}
}
My directory hierarchy is like this :
+---public
| | index.php
+---src
| +---Blog
| | | BlogModule.php
| | ---views
| | index.php
| | index.twig
| | show.php
| | show.twig
| ---Framework
| | App.php
| | Router.php
| | TwigRendererFactory.php
| +---Renderer
| | PHPRenderer.php
| | RendererInterface.php
| | TwigRenderer.php
| ---Router
| Route.php
+---vendor
---views
footer.php
header.php
layout.twig
And I have my autoload set in my composer.json
like this :
"autoload": {
"psr-4": {
"Framework\": "src/Framework",
"App\": "src",
"Tests\": "tests"
}
},
With this setup I get this exception from php-di
:
Fatal error: Uncaught DIDefinitionExceptionInvalidDefinition:
Entry "FrameworkRendererRendererInterface" cannot be resolved:
factory 'Framework\Renderer\TwigRendererFactory' is neither a callable nor a valid container entry in E:____MQBAKAPOO-pratiquevendorphp-diphp-disrcDefinitionResolverFactoryResolver.php:91
Stack trace:
#0 E:____MQBAKAPOO-pratiquevendorphp-diphp-disrcDefinitionResolverResolverDispatcher.php(71): DIDefinitionResolverFactoryResolver->resolve(Object(DIDefinitionFactoryDefinition), Array)
#1 E:____MQBAKAPOO-pratiquevendorphp-diphp-disrcContainer.php(390): DIDefinitionResolverResolverDispatcher->resolve(Object(DIDefinitionFactoryDefinition), Array)
#2 E:____MQBAKAPOO-pratiquevendorphp-diphp-disrcContainer.php(139): DIContainer->resolveDefinition(Object(DIDefinitionFactoryDefinition))
#3 E:____MQBAKAPOO-pratiquepublicindex.php(23): DIContainer->get('Framework\Rende...')
#4 {main} thrown in E:____MQBAKAPOO-pratiquevendorphp-diphp-disrcDefinitionResolverFactoryResolver.php on line 91
I’ve double-checked everything that I could, I tried to put an echo
inside the __invoke()
method of the TwigRendererFactory
class but it seems the __invoke()
method is never called for some reason so I don’t know what to do.