Giter Club home page Giter Club logo

piping-bag's Introduction

Dependency Injection Container Plugin for CakePHP 4

This plugin adds the ability to configure object instances and their dependencies before they are used, and to store them into a container class to easy access.

It uses the clean and flexible Ray.Di Library which is a PHP dependency injection framework in the style of "Google Guice".

Ray.Di also allows you to program using AOP, that is, decorating the configured instances so some logic can be run before or after any of their methods.

Installation

You can install this plugin into your CakePHP application using composer.

composer require lorenzo/piping-bag=dev-master

Configuration

Upgrading from library version 1.x (Cake 3.x)

DEPRECATED:

  • ControllerFactoryFilter.php for injecting controllers
  • ShellDispatcher.php for injecting shell classes

Remove the following lines in your: config/bootstrap.php:

DispatcherFactory::add('PipingBag\Routing\Filter\ControllerFactoryFilter');
Plugin::load('PipingBag', ['bootstrap' => true]);

Controller Injection

For getting injection in your controllers to work you need to add the following line in the bootstrap method of your Application.php:

$this->addPlugin('PipingBag', ['bootstrap' => true]);
$this->controllerFactory = new \PipingBag\Controller\DIControllerFactory();

Shell Injection

For getting injection in your controllers to work you need to add the following line of your bin/cake.php:

$commandFactory = new \PipingBag\Console\DICommandFactory();
$runner = new CommandRunner(new Application(dirname(__DIR__) . '/config'), 'cake', $commandFactory);

Additionally, you can configure the modules to be used and caching options in your config/app.php file.

'PipingBag' => [
    'modules' => ['MyWebModule', 'AnotherModule', 'APlugin.SuperModule'],
    'cacheConfig' => 'default'
]

Modules can also be returned as instances in the configuration array:

'PipingBag' => [
    'modules' => [new App\Di\Module\MyWebModule()],
    'cacheConfig' => 'default'
]

Finally, if you wish to tune your modules before they are registered, you can use a callable function:

'PipingBag' => [
    'modules' => function () {
        return [new MyWebModule()];
    },
    'cacheConfig' => 'default'
]

What is a Module anyway?

Modules are classes that describe how instances and their dependencies should be constructed, they provide a natural way of grouping configurations. An example module looks like this:

// in app/src/Di/Module/MyModule.php

namespace App\Di\Module;

use Ray\Di\AbstractModule;

class MyModule extends AbstractModule
{
    public function configure()
    {
        $this->bind('MovieApp\FinderInterface')->to('MovieApp\Finder');
        $this->bind('MovieApp\HttpClientInterface')->to('Guzzle\HttpClient');
        $this->install(new OtherModule()); // Modules can install other modules
    }
}

Modules are, by convention, placed in your src/Di/Module folder. Read more about creating modules and how to bind instances to names in the Official Ray.Di Docs.

Usage

After creating and passing the modules in the configuration, you can get instance of any class and have their dependencies resolved following the rules created in the modules:

use PipingBag\Di\PipingBag;

$httpClient = PipingBag::get('MovieApp\HttpClientInterface');

Injecting Dependencies in Controllers

Ray.Di is able to inject instances to your controllers based on annotations:

// in src/Controller/ArticlesController.php

use App\Controller\AppController;
use MovieApp\HttpClientInterface;
use Ray\Di\Di\Inject; // This is important

class ArticlesController extends AppController
{
    /**
     * @Inject
     */
    public function setHttpClient(HttpClientInterface $connection)
    {
        $this->httpClient = $connection;
    }
}

As soon as the controller is created, all methods having the @Inject annotation will get instances of the hinted class passed. This works for constructors as well.

Injecting Dependencies in Controller Actions

It is also possible to inject dependencies directly in the controller actions. When doing this, add the type-hinted dependency to the end of the arguments and set the default value to null, it is also required to annotate the method using @Assisted

// in src/Controller/ArticlesController.php

use App\Controller\AppController;
use MovieApp\HttpClientInterface;
use PipingBag\Annotation\Assisted; // This is important

class ArticlesController extends AppController
{
    /**
     * @Assisted
     */
    public function edit($id, HttpClientInterface $connection = null)
    {
        $article = $this->Articles->get($id)
        $connection->post(...);
    }
}

Injecting Dependencies in Shells

Shells are also able to receive dependencies via the @Inject annotation. But first, you need to change the cake console executable to use PipingBag. Open your bin/cake.php file and make it look like this:

// bin/cake.php
...
exit(PipingBag\Console\ShellDispatcher::run($argv)); // Changed namespace of ShellDispatcher

Then you can apply annotations to your shells:

use Cake\Console\Shell;
use Ray\Di\Di\Inject; // This is important

class MyShell extends Shell
{
    /**
     * @Inject
     */
    public function setHttpClient(HttpClientInterface $connection)
    {
        $this->httpClient = $connection;
    }
}

piping-bag's People

Contributors

amayer5125 avatar antograssiot avatar aydot avatar koriym avatar lorenzo avatar vanoostrum avatar wyrihaximus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

piping-bag's Issues

3.1 viewPath is deprectared

Hey.
Upgraded to CakePHP 3.1, and a few things got deprecated:
Controller::$viewPath is deprecated. Use $this->viewBuilder()->viewPath() instead.
This is in file src/Routing/Filter/ControllerFactoryFilter.php.

Dependency error

Hi! I'm trying to use the plugin but i'm getting this error:

{ "success": false, "data": { "message": "dependency 'App\\Controller\\HttpClientInterface' with name '' used in /Users/brunoserra/Sites/EventsPlatform/src/Controller/EventsController.php:29 ($connection)", "url": "/events/set-http-client", "code": 500, "file": "/Users/brunoserra/Sites/EventsPlatform/vendor/ray/di/src/Arguments.php", "line": 63 } }

I did exactly what the docs suggests. How can I solve it?
Thanks in advence

PHP 8 Attributes support

Hi there,

Will the Code & Docs be updated for newer versions of PHP & Ray DI with native PHP Attributes Support and added functionality within Ray DI?

PipingBag ControllerFactoryFilter invalidates template paths

in my app.php, my paths is set up like:

'App' => [
    'paths' => [
        'plugins' => [ROOT . DS . 'plugins' . DS],
        'templates' => [
            APP . 'Template' . DS,
            APP . 'Template' . DS . 'UserMgt' . DS 
        ],
        'locales' => [APP . 'Locale' . DS],
    ]
],

My Users template is located at
template\UserMgt\Users\index
template\UserMgt\Users\view
when i added

DispatcherFactory::add('PipingBag\Routing\Filter\ControllerFactoryFilter');

or

use PipingBag\Di\PipingBag;
DispatcherFactory::add('PipingBag\Routing\Filter\ControllerFactoryFilter');

to my bootstrap, and try to navigate to localhost\users\index from my browser, I get

[Semantical Error] The annotation "@Inject" in method App\Controller\UsersController::index() was never imported. 
Did you maybe forget to add a "use" statement for this annotation? 
Doctrine\Common\Annotations\AnnotationException

I get this error even though I am not injecting anything inside my UsersController. my UsersController has just the basic bake crud code.

Also if i have another template located at template\UserMgt\UserRegistration\index.ctp,
cake cant locate the index page when i add your pipingbag controller factory filter to my bootsrap. it either shows a blank page or I get a MissingTemplateException.
when i comment out the line, the index page works fine.

Pipingbag\Routing\Filter\ControllerFactoryFilter::beforeDispatch missing?

It seems beforeDispatch is missing from the class mentioned in the title.

I'm not sure if I'm doing it wrong, and it's supposed to be this way, but because of this the controller's $request->params is never populated, so I can't find out its' _ext (extension) property, for example, if it was called as an html or json, or something.

Modules with Configure::readOrFail() in them

Hi,

I'm still investigating this, but maybe I missed something obvious and you can help.

In app.php I have this key:

'PipingBag' => [
        'modules' => [
            'MetricsModule'
            'SomeClientModule',
        ],
        'cacheConfig' => 'default'
    ],

MetricsModule:

class MetricsModule extends AbstractModule
{

    protected function configure()
    {

        $this->bind(SomeMetrics::class)
            ->toConstructor(
                SomeMetrics::class,
                [
                    'host' => 'dHost',
                    'port' => 'dPort'
                ]
            );

        $this->bind()
            ->annotatedWith('dHost')
            ->toInstance(Configure::readOrFail('Telegraf')['host']);

        $this->bind()
            ->annotatedWith('dPort')
            ->toInstance(Configure::readOrFail('Telegraf')['port']);
    }
}

SomeClientModule:

class SomeClientModule extends AbstractModule
{
    protected function configure()
    {
        $this->bind(RequestFactory::class)->to(GuzzleMessageFactory::class);
        $this->bind(ClientInterface::class)->toInstance(
            new Client(['base_uri' => Configure::readOrFail('someValue.url'), 'http_errors' => false])
        );
        $this->bind(HttpClient::class)->to(GuzzleAdapter::class);
        $this->bind(Some::class)->in(Scope::SINGLETON);
    }
}

With this config, when I do request to my application I get fatal error:

Fatal Error (1): Uncaught Error: Call to a member function getInstance() on null in /somepath/vendor/lorenzo/piping-bag/src/Di/PipingBag.php:8

I also see that there is no write to cache (Redis) with PippingBag cache key (I cleared the cache before this try), there's only "get" from cache (which returns empty).

When I change SomeClientModule and remove Configure:readOrFail() from it:

class SomeClientModule extends AbstractModule
{
    protected function configure()
    {
        $this->bind(RequestFactory::class)->to(GuzzleMessageFactory::class);
        $this->bind(ClientInterface::class)->toInstance(
            new Client(['base_uri' =>'http://url.com', 'http_errors' => false])
        );
        $this->bind(HttpClient::class)->to(GuzzleAdapter::class);
        $this->bind(Some::class)->in(Scope::SINGLETON);
    }
}

it works.

They key

'someValue.url'

exists, so it's not because of missing key. In bootstrap.php I first load config file with 'someValue.url' then app.php.

Is there anything more I should know about using Configure:readOrFail() in DI modules?

ControllerFactoryFilter::getController returns false

ControllerFactoryFilter::getController returns false when trying to use controller method injection.
I see it returns false from here :
[https://github.com/lorenzo/piping-bag/blob/master/src/Routing/Filter/ControllerFactoryFilter.php#L93]
but I am calling from a controller, and I don't understand why it's returning false when there is definitely a controller.

Bad type hinting on PipingBag::get()

Hello,
The phpdocs have an error on PipingBag::get().

/**
     * Return an instance of a class after resolving its dependencies.
     *
     * @param $class The class name or interface name to load.
     * @param $name The alias given to this class for namespacing the configuration.
     * @return mixed
     */
    public static function get($class, $name = Name::ANY)
    {
    }

@param $class and @param $name should both be @param string $class and @param string $name. My IDE is looking for a The class, which does not exist, obviously.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.