0

I'm currently developing an application with the FlightPHP framework and wondering how am I able to inject FlightPHP into my custom class so that I am able to use specific classes I have injected into it's dependency container.

use Flight;
use Logger;

class DB{

    public function __construct(...){

        $this->app = $app; // Flight:: instance

    }

    public function doStuff($stuff){

        return $this->app->log()->function($stuff);

    }

}

Flight::register('log', 'Logger', ['app'], function($log) {
    return $log->pushHandler(new StreamHandler('app.log'));
});

Flight::register('database', 'DB', array($data), function($db) {
    return $db;
});

I'm attempting to inject Flight into my database class constructor so that I am able to use the log function which was previously injected into the Flight dependency container.

The "Logger" works in the index.php when used under the Flight instance "Flight::log()->function("test");", however when I attempt to use it in another scope(within the Database class), it doesn't allow me to use it in the context of "Flight".

Update:

Flight::register('log', 'Monolog\Logger', ['app'], function($log) {
    return $log->pushHandler(new StreamHandler('app.log'));
});

class DB{
    function __construct(Monolog\Logger $engine){

        #var_dump($engine);
        $engine->addInfo("injected"); // works

    }
}

Flight::register('database', 'DB', array(Flight::log()), function($db) {
    return $db;
});
Flight::database();

Is correct usage?

tereško
  • 58,060
  • 25
  • 98
  • 150
mhvvzmak1
  • 307
  • 2
  • 12
  • So you are saying that you are not able to receive/inject instance of Flight in DB constructor? What hides under those ... ? – featherbits Mar 18 '16 at 19:12
  • I'm not sure how to pass Flight into the DB constructor so that my custom class(DB) is able to utilize other classes I have injected into the Flight dependency container. – mhvvzmak1 Mar 18 '16 at 19:42

1 Answers1

0

You could pass instance of \Flight\Engine in the array of third parameter at register method to pass framework instance in you DB controller. \Flight\Engine does not use interface sou you are coupling your code with framework implementation I guess. In this case you can use Flight::app() everywhere to obtain framework instance.

<?php error_reporting(E_ALL);
require 'vendor/autoload.php';


class DB
{
    function __construct(\Flight\Engine $engine)
    {
        var_dump($engine->get('connectionString'));
    }
}

Flight::set('connectionString', 'mssql');
Flight::register('database', 'DB', array(Flight::app()), function($db) {
    return $db;
});

Flight::database();

Looks like that Flight does not have such a concept as Dependency Injection Container. You have to specify your parameter values explicitly.

Update:

By doing this ['app'] you are injecting string into constructor of Monolog\Logger. This line return $log->pushHandler(new StreamHandler('app.log')); should raise error.

Read more carefully http://flightphp.com/learn

featherbits
  • 790
  • 6
  • 24
  • I guess this is on the right tracks I'm attempting to achieve, adjusted this a little and kind of works however not sure if I'm doing it correctly in the constructor and the register array, have added my version to the main post. – mhvvzmak1 Mar 19 '16 at 16:54
  • I updated my answer. Looks okay, but does Logger really need this string 'app' ? – featherbits Mar 19 '16 at 18:42
  • Yes Logger requires one argument in it's constructor which is the name of the current logging profile. – mhvvzmak1 Mar 19 '16 at 22:51