我试图思考如何为Laravel的模型观察者注入存储库。目前,我有这个设置。
用户宠物服务提供商.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());
}
}
它可以很好地绑定所有接口和存储库,并且可以与观察者绑定,但是我需要在构造函数中执行此操作的存储库注入。构造函数中没有传递任何内容,因此它将解释它失败的原因。
用户宠物回购.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;
}
}
用户宠物观察者.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');
}
}
它将其作为错误抛出:
参数 1 传递给 Bunny''Observers''Pet''UserPetObserver::__construct() 必须是实例 的 Bunny''Repos''Pet''IUserPetLayerRepo,没有给出,在 H:''WD 中调用 SmartWare.swstor''HALEY-HP''Source2''bunny-meadows''app''Bunny''Providers''UserPetServiceProvider.php 在第 22 行并定义
这是有道理的,因为我没有在构造函数中传递任何东西。所以我尝试手动传递我的存储库。
$this->app->events->subscribe(new UserPetObserver(new UserPetLayerRepo, new UserPetMarkingRepo));
但随后它会抛出需要注入的用户宠物层存储库的错误......它只是链条不停地链子。难道我只是想多了?
谢谢。
编辑:::这是我唯一能想到做的事情。不过,这似乎是一种非常丑陋/糟糕的方法:
$this->app->events->subscribe(new UserPetObserver($this->app->make('Bunny'Repos'Pet'UserPetLayerRepo'), $this->app->make('Bunny'Repos'Pet'UserPetMarkingRepo')));
还有其他想法吗?
试试:
$this->app->events->subscribe($this->app->make('UserPetObserver'));
当Laravel创建UserPetObserver
对象时,它将读取构造函数中的类型提示依赖项并自动生成它们。