Search code examples
phpphpstormslim

PHPStorm and Slim 3.X DIC


I recently moved from Slim 2.X to Slim 3.X and I've found a problem that may be a little stupid but annoys me in some ways.
The newly Slim 3.X, as a difference between the older 2.X version, it implements a new container system using Dependency Injection Containers (DIC) build on Pimple.

As I've been reading, I find this a very great enhancement because it lets you manage your PHP app in a lot of different ways.
When I started playing around with the new things it has, I found something confusing that maybe is something I'm missing.

I use PHPStorm; one of the things that I like about this IDE is its code-completion and the facility for writing code and understanding classes (I'm actually a student, so this helps me a lot).
When I write a Slim route, if I want to access, for example, to my view object stored inside the container, I have to refer to it with the $this->view variable. The thing is that, normally, PHPStorm lists me the methods and properties inside an object when I mention it, but that didn't happen with the $this object.

I suppose that, inside a route, Slim stores all route functionalities and all the container objects into the $this assigner.

$container = $app->getContainer();
$container['view'] = new \Slim\Views\PhpRenderer('protected/views/');

$app->get('/products', function(Request $request, Response $response) {
    $response = $this->view->render($response, 'products.php');
    return $response;
})->setName('products');

When I access my /products route, it successfully renders my products template and shows what it's expected to show, so no problems with that.
The matter with it is that I want PHPStorm to know that $this variable inside a route it stores all the containers that are set previously before the route is called.

I thought about /* @var */ and /* @global */ or something like this but after searching for a while and trying different things, I haven't found anything that could work.

In conclusion, what I'm trying to say is if it's possible that PHPStorm could list me the properties and methods of the container objects like this: PHPStorm auto-completion feature for objects

but with the $this object inside a route: Missing auto-completion feature in this object

Thanks!


Solution

  • The easiest way to do this is to have separate Action classes rather than use closures. This also has the benefit of being easier to test.

    Firstly create your action, inject its dependencies into its constructor and write an `__invoke`` method that will be called by Slim:

    class ProductsListAction {
        protected $view;
    
        public function __construct(\Slim\Views\PhpRenderer $view) {
            $this->view = $view;
        }
    
        public function __invoke($request, $response, $args) {
            $response = $this->view->render($response, 'products.php');
            return $response;
        }
    }
    

    For this to work, you now need a DIC factory for it:

    $container['ProductsListAction'] = function ($c) {
        return new ProductsListAction($c['view']);
    };
    

    You can now register your new action as a route callable:

    $app->get('/products', 'ProductListAction');
    

    Now, PhpStorm will correctly autocomplete within your ProductsListAction class.