dev-master
9999999-devExtending Pimple to be a singleton
MIT
The Requires
by Steve
dependency injection container pimple
3.0.0
3.0.0.0Extending Pimple to be a singleton
MIT
The Requires
by Steve
dependency injection container pimple
Wallogit.com
2017 © Pedro Peláez
Extending Pimple to be a singleton
The task is simple: I'd like to easily get my Pimple Container from anywhere in the code., (*1)
Whether it's a module, an external class, independent library that has no idea which framework my particular project is using, or, I'd like not to worry about how I am passing it around., (*2)
So a singleton seems the easy option., (*3)
The singleton is created automatically when the Container is invoked. You can do this:, (*4)
new \PimpleSingleton\Container(); $di = \PimpleSingleton\Container::getContainer();
Have you already defined your Pimple instance, for instance through a framework initialization, or maybe in a unit test?, (*5)
$di = new Pimple\Container(); \PimpleSingleton\Container::setContainer($di); $di = \PimpleSingleton\Container::getContainer();
Using PhpStorm (the greatest IDE of them all... ;-), (*6)
namespace PHPSTORM_META {
/** @noinspection PhpUnusedLocalVariableInspection */
/** @noinspection PhpIllegalArrayKeyTypeInspection */
$STATIC_METHOD_TYPES = [
\PimpleSingleton\Container::get('') => array(
'db' instanceof \Aura\Sql\ExtendedPdo,
'request' instanceof \Slim\Http\Request,
'response' instanceof \Slim\Http\Response
)
];
}
PhpStorm will now perform autocompletion for code like this:, (*7)
$db = \PimpleSingleton\Container::get('db');
Enjoy!, (*8)
Extending Pimple to be a singleton
MIT
dependency injection container pimple
Extending Pimple to be a singleton
MIT
dependency injection container pimple