mardi 20 décembre 2016

Laravel 5, passing a value into dependency

I want to implement Laravel 5 IoC technology into a simple task. 1) We need to get a content of a post; 2) We can get content by 2 ways: reading a file, getting it from web by url

1st step: created Post model (i have posts table in database, model was created automatically using artisan). Now we can create a new object by

$post = new Post;

or by selecting an object from DB

$post = DB::table('posts')->where('id', 1)->first();

In Post class I added a few methods for processing it's content

class Post extends Model {
    protected $postContentManager;

    public function setPostContentManager($postContentManager) {
        $this->postContentManager = $postContentManager;
    }

    public function processPost() {
        $content = $this->postContentManager->getContent();

        // process content
    }
}

2nd step: created an interface IPostContentManager

interface IPostContentManager {
    public function getContent();
    public function setReference($reference);
}

3rd step: created 2 classes for interface implementation

class PostContentFileManager implements IPostContentManager {
    var $reference;

    public function getContent() {
        // getting content from network by reference
    }

    public function setReference($reference) {
        $this->reference = $reference;
    }
}

class PostContentNetManager implements IPostContentManager {
    var $reference;

    public function getContent() {
        // getting content from file by reference
    }

    public function setReference($reference) {
        $this->reference = $reference;
    }
}

4th step: every post in my DB has 2 different parameters: reference for network resource and reference to local file. I wanted to bind creation an object of Post type for a callback with creation a new post object and filling it postContentManager parameter:

public function register()
{
    $this->app->bind('Post', function($app) {
        $fb_post = $app->make('Post');
        $app->rebinding('IPostContentManager', function($app, $postContentManager) {
            $app['Post']->setPostContentManager($postContentManager);
        });
        return $post;
    });
}

And after creating a post object by App::make('Post') i wanted to create a new implementation of IPostContentManager and bind it:

$post = App::make('Post');
$postContentManager = new PostContentNetManager;
$postContentManager->setReference($post->net_reference);

App::bind('IPostContentManager', $postContentManager);
$post->processPost();

But that code brokes in AppServiceProvider.php with method $app->rebinding();

  1. Can you explain me, how can I bind a dependency to an object without immediately creating of its implementation?
  2. Is it normal to create object dependency wich depends on object parameter itself, or there are any other ways?
  3. With explained code we can create a new post object with App::make(), but I cant get posts using Eloquent ORM and with accepting all bindings, how can I do this?

Thank you very much, sorry for my English and too long text



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire