ukautz / wart

Extends the Pimple dependency injection container and provides auto class resolving, instantiation and (constructor) injection
14 stars 1 forks source link

How to replace interfaces with concrete implementations? #2

Closed hassankhan closed 5 years ago

hassankhan commented 10 years ago

For example, when using Twig, the Twig_Environment object requires another object of type Twig_LoaderInterface. Can I somehow "map" an object onto it? Using createArgs() doesn't seem to be working for me.

dellsala commented 10 years ago

Can you not do something like this?

$c['Twig_LoaderInterface'] = function ($c) {
    return $c['MyApp_TwigLoaderImplementation'];
};
hassankhan commented 10 years ago

I could, I guess I was hoping for an auto-resolving solution. That said, I hadn't even thought of doing what you did, thanks for that!

meetmatt commented 6 years ago

It could be a useful feature, already actively using it in my own home project, saves time and allows to auto-wire interfaces, e.g.:

$container = new Wart([], [
    'createArgs' => [
        Twig_Loader_Filesystem::class => [__DIR__ . '/templates'],
        Twig_Environment::class => [['cache' => __DIR__ . '/cache']],
    ],
    'aliases' => [
        Twig_LoaderInterface::class => Twig_Loader_Filesystem::class
    ],
]);

$twig = $container[Twig_Environment::class]; // works correctly
meetmatt commented 6 years ago

Added here: https://github.com/ukautz/wart/pull/4

meetmatt commented 5 years ago

@ukautz you can close this issue I guess