phpdependency-injectionphp-dino-frameworkauryn

Sharing the same instance of an object: auryn vs. PHP-DI


I am trying to build my first no-framework PHP application and I am following this tutorial. I am relatively new to some concepts described in the tutorial. Despite this, I decided to use, as Dependency Injector, PHP-DI instead of the suggested one (rdlowrey/auryn).

I have created everything according to the tutorial except for the file Bootstrap.php (and the file Dependencies.php:

<?php declare(strict_types = 1);

require(__DIR__ . '/../vendor/autoload.php');

...

$container = include('Dependencies.php');
$request = $container->make('Http\HttpRequest');
$response = $container->make('Http\HttpResponse');

...

switch ($routeInfo[0]) {
    ...
    case \FastRoute\Dispatcher::FOUND:
        $className = $routeInfo[1][0];
        $method = $routeInfo[1][1];
        $vars = $routeInfo[2];

        $class = $container->make($className);
        $class->$method($vars); // (**)
        break;
}

echo $response->getContent(); // (*)

$class can be only an instance of a Homepage class which has only one method (show()), called in (**):

class Homepage
{
    private $request;
    private $response;
    private $renderer;

    public function __construct(
        Request $request, 
        Response $response,
        Renderer $renderer
    ) {
        $this->request = $request;
        $this->response = $response;
        $this->renderer = $renderer;
    }

    public function show() {
        $data = [
            'name' => $this->request->getParameter('name', 'stranger'),
        ];
        $html = $this->renderer->render('Homepage', $data);
        $this->response->setContent($html); // (***)
    }
}

With all that said, the application returns a 200 HTTP response with an empty body [here (*)] but if I try to print the content of the HTTP response after (***) I get the correct response. This could mean that there are two different instances of an HttpResponse class. (Is that right?)

By using rdlowrey/auryn, the author of the tutorial, used the method share() to share the same HttpReponse instance among classes, as shown in the "original" Dependencies.php file:

<?php declare(strict_types = 1);
use \Auryn\Injector;
...
$injector = new Injector;
$injector->alias('Http\Response', 'Http\HttpResponse');
$injector->share('Http\HttpResponse');
...
return $injector;

Is there a way to get the same behavior using PHP-DI (with PHP definitions)?

Here's my version of Dependencies.php:

<?php declare(strict_types = 1);

$definitions = [
    'Http\Request' => DI\create('Http\HttpRequest')->constructor(
        $_GET, $_POST, $_COOKIE, $_FILES, $_SERVER),
    'Http\HttpRequest' => function () {
        $r = new Http\HttpRequest($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
        return $r;
    },
    'Http\Response' => DI\create('Http\HttpResponse'),

    'Twig\Environment' => function () {
        $loader = new Twig\Loader\FilesystemLoader(
            dirname(__DIR__) . '/templates');
        $twig = new Twig\Environment($loader);
        return $twig;
    },

    'Example\Template\TwigRenderer' => function (Twig\Environment $renderer) {
        return new Example\Template\TwigRenderer($renderer);
    },
    'Example\Template\Renderer' => DI\create(
        'Example\Template\TwigRenderer')->constructor(
            DI\get('Twig\Environment')),
];

$containerBuilder = new DI\ContainerBuilder;
$containerBuilder->addDefinitions($definitions);
$container = $containerBuilder->build();
return $container;

Solution

  • In Bootstrap.php, getting (get()) HttpRequest/HttpResponse instances, instead of making (make()) them, solved the problem.

    ...
    $container = include('Dependencies.php');
    $request = $container->get('Http\HttpRequest');
    $response = $container->get('Http\HttpResponse');
    ...
    

    As clearly stated in the documentation:

    The make() method works like get() except it will resolve the entry every time it is called. [..] if the entry is an object, an new instance will be created every time [..]