简体   繁体   English

Laravel模型观察器存储库注入

[英]Laravel model observer repository injection

I'm trying to wrap my head around how to inject Laravel's model observers with repositories. 我试图将我的头转向如何向Laravel的模型观察者注入存储库。 Currently, I have this setup. 目前,我有此设置。

UserPetServiceProvider.php UserPetServiceProvider.php

<?php namespace Bunny\Providers;

use Illuminate\Support\ServiceProvider;
use Bunny\Observers\Pet\UserPetObserver;

class UserPetServiceProvider extends ServiceProvider {

    public function register()
    {
        // User pets
        $this->app->bind('Bunny\Repos\Pet\IUserPetRepo', 'Bunny\Repos\Pet\UserPetRepo');

        // User pet layers
        $this->app->bind('Bunny\Repos\Pet\IUserPetLayerRepo', 'Bunny\Repos\Pet\UserPetLayerRepo');

        // User pet markings
        $this->app->bind('Bunny\Repos\Pet\IUserPetMarkingRepo', 'Bunny\Repos\Pet\UserPetMarkingRepo');

        $this->app->events->subscribe(new UserPetObserver());
    }

}

It binds all the interfaces and repositories fine and would with the observer, but I need repository injection which I do in the constructor. 它可以很好地绑定所有接口和存储库,并且可以与观察者绑定,但是我需要在构造函数中进行存储库注入。 Nothing is being passed in the constructor so it would explain why it fails. 没有在构造函数中传递任何内容,因此可以解释为什么失败。

UserPetRepo.php UserPetRepo.php

<?php namespace Bunny\Repos\Pet;

use Bunny\Repos\BaseRepo;
use Bunny\Models\Pet\UserPet;
use Bunny\Repos\User\IUserRepo;
use Bunny\Repos\Breed\IStageRepo;
use Bunny\Repos\Breed\IBreedLayerRepo;

use Illuminate\Config\Repository as Config;
use Illuminate\Support\Str as String;
use Illuminate\Session\SessionManager as Session;
use Illuminate\Events\Dispatcher;

class UserPetRepo extends BaseRepo implements IUserPetRepo {

    public function __construct(UserPet $pet, IUserRepo $user, IStageRepo $stage, IBreedLayerRepo $layer, Config $config, String $string, Session $session, Dispatcher $events)
    {
        $this->model = $pet;
        $this->user = $user;
        $this->stage = $stage;
        $this->layer = $layer;
        $this->config = $config;
        $this->string = $string;
        $this->session = $session;
        $this->events = $events;

        $this->directory = $this->config->get('pathurl.userpets');
        $this->url       = $this->config->get('pathurl.userpets_url');
    }

    /**
     * Create new user pet
     * @param attributes     Attributes
     */
    public function createWithImage( array $attributes, array $colors, $domMarkings = null, $domMarkingColors = null, $recMarkings = null, $recMarkingColors = null )
    {
        $this->model->name = $attributes['name'];
        $this->model->breed_id = $attributes['breed_id'];
        $this->model->user_id = $this->user->getId();
        $this->model->stage_id = $this->stage->getBaby()->id;

        // Get image
        $image = $this->layer->compile(
            $attributes['breed_id'],
            'baby',
            $colors,
            $domMarkings,
            $domMarkingColors
        );

        // Write image and set
        $file = $this->writeImage( $image );
        if( ! $file )
        {
            return false;
        }

        $this->model->base_image = $file;
        $this->model->image = $file;

        if( ! $this->model->save() )
        {
            return false;
        }

        $this->events->fire('userpet.create', array($this->model));

        return $this->model;
    }

    /**
     * Write image
     * @param image      Imagick Object
     */
    protected function writeImage( $image )
    {
        $fileName = $this->string->random(50) . '.png';

        if( $image->writeImage( $this->directory . $fileName ) )
        {
            return $fileName;
        }

        $this->model->errors()->add('writeImage', 'There was an error writing your image. Please contact an administrator');
        return false;
    }

}

UserPetObserver.php UserPetObserver.php

use Bunny\Repos\Pet\IUserPetLayerRepo;
use Bunny\Repos\Pet\IUserPetMarkingRepo;
use Bunny\Observers\BaseObserver;

class UserPetObserver extends BaseObserver {

    public function __construct(IUserPetLayerRepo $layers, IUserPetMarkingRepo $markings)
    {
        $this->layers = $layers;
        $this->markings = $markings;
    }

    /**
     * After create
     */
    public function onCreate( $model )
    {
        $this->layers->user_pet_id = $model->id;
        dd(Input::all());
        $this->layers->breed_layer_id = $model->id;
    }

    public function subscribe( $event )
    {
        $event->listen('userpet.create', 'UserPetObserver@onCreate');
    }

}

It throws this as the error: 它将其视为错误:

Argument 1 passed to Bunny\\Observers\\Pet\\UserPetObserver::__construct() must be an instance of Bunny\\Repos\\Pet\\IUserPetLayerRepo, none given, called in H:\\WD SmartWare.swstor\\HALEY-HP\\Source2\\bunny-meadows\\app\\Bunny\\Providers\\UserPetServiceProvider.php on line 22 and defined 传递给Bunny \\ Observers \\ Pet \\ UserPetObserver :: __ construct()的参数1必须是Bunny \\ Repos \\ Pet \\ IUserPetLayerRepo的实例,没有给出,在H:\\ WD SmartWare.swstor \\ HALEY-HP \\ Source2 \\ bunny-中调用第22行上的Meadows \\ app \\ Bunny \\ Providers \\ UserPetServiceProvider.php已定义

Which makes sense since I'm not passing anything in the constructor. 这是有道理的,因为我没有在构造函数中传递任何东西。 So I try to pass my repository manually. 因此,我尝试手动传递我的存储库。

$this->app->events->subscribe(new UserPetObserver(new UserPetLayerRepo, new UserPetMarkingRepo));

But then it throws errors of UserPetLayerRepo needing injections... and it just chains on and on. 但是随后它抛出需要注入的UserPetLayerRepo错误...并且它只是不断地链接。 Is there anyway of doing this that I'm just overthinking? 无论如何,我只是在想什么吗?

Thanks. 谢谢。

EDIT::: This is the only thing I could think of doing. 编辑:::这是我唯一想到的事情。 This seems like a really ugly/bad way of doing it though: 不过,这似乎是一种非常难看/糟糕的方法:

$this->app->events->subscribe(new UserPetObserver($this->app->make('Bunny\Repos\Pet\UserPetLayerRepo'), $this->app->make('Bunny\Repos\Pet\UserPetMarkingRepo')));

Any other ideas? 还有其他想法吗?

Try just: 尝试一下:

$this->app->events->subscribe($this->app->make('UserPetObserver'));

When Laravel makes the UserPetObserver object, it will read the type-hinted dependencies in the constructor and automatically make them, as well. 当Laravel创建UserPetObserver对象时,它将读取构造函数中UserPetObserver类型提示的依赖关系,并自动使其成为对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM