akrabat / slim3-skeleton

Simple Slim Framework 3 skeleton with Twig & Monolog
BSD 3-Clause "New" or "Revised" License
344 stars 99 forks source link

Common dependencies #10

Closed Swader closed 9 years ago

Swader commented 9 years ago

As per discussion in this Tweet stream.

Defining a new factory for every controller separately seems wasteful, especially when most might share some, if not all dependencies.

What would be good approaches to handling this? Originally, I thought about something like this:

screenshot 2015-08-05 00 08 56

And then extending the get and has methods as necessary. But this seems a bit clumsy and too much work for such a simple feature - especially considering a new offsetExists would need to be added into the mix, too, and some other things.

Suggestions?

akrabat commented 9 years ago

Loosely, Pimple is too simple for what you want. Personally, I suspect that you'd be better off with an auto-wiring DI container such as PHP.DI.

Another way would be use Zend\ServiceManager's initializers to add the common dependencies via setters. You still need to tell the DI container the name of every controller, though.

If you don't want the DI Container to know the names of the controller in advance, then I would use Zend\ServiceManager's abstract factories so that a single factory that can create all the controllers. If I get time, I'll see if I can knock up an example that does this at some point

Swader commented 9 years ago

Thanks, I'll look into all that.

The original idea was mapping class name patterns to callbacks, so that when a pattern like "Controller" is detected, unless there is a mapping that corresponds to the controller name exactly, the default takes over and constructs with the standard view and logger.

Extending Pimple and Slim's Container in such a way proved to require way more work than it should have, though, and just using a different DI container seemed more logical.

akrabat commented 9 years ago

OK, The Abstract Factory solution was easy to create an example for - https://github.com/akrabat/slim3-abstract-action-factory

The work is done here: https://github.com/akrabat/slim3-abstract-action-factory/blob/master/app/src/ActionAbstractFactory.php

Swader commented 9 years ago

That looks great, thanks. I'll take a more detailed look soon.

geniuscd commented 9 years ago

I tried to implement the following abstract factory, but its not working for me. i'm getting 'addAbstractFactory' methode is not defined. i installed 'zendframework/zend-servicemanager' and 'akrabat/rka-slim-zfsm-container' via composer after cloning 'akrabat/slim3-skeleton'. and made the required changes in two files as sited in slim3-abstract-action-factory github project. am i missing something else ? thanks

akrabat commented 9 years ago

Are you using Zend\ServiceManager. I usually set it up in index.php: https://github.com/akrabat/slim3-abstract-action-factory/blob/master/public/index.php#L18-L19

geniuscd commented 9 years ago

yep i modified two files, the index.php to instantiate the object and the dependencies.php :/