gpt4 book ai didi

php - 传递给构造函数的参数必须是接口(interface)的实例,给定的装饰器实例

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:38:33 26 4
gpt4 key购买 nike

我在尝试调试我的代码时遇到问题,据我所见,查看代码的各个部分,它们都正确链接,但一定有一些明显的东西是我在这里遗漏的。

对于我在此处粘贴到问题中的代码量,我感到抱歉,但不幸的是,应用程序的单个实体有很多文件在起作用,我认为提供更多而不是更少更好.

首先是错误信息:

Argument 1 passed to Raid\Composers\PreferenceDataComposer::__construct() must be an instance of Raid\Repo\User\PreferenceInterface, instance of Raid\Repo\Preference\CacheDecorator given, called in C:\wamp\www\raid\app\Raid\Composers\ComposerServiceProvider.php on line 34 and defined

所以首先,我的 View Composer 是生成此错误的地方:

<?php namespace Raid\Composers;

use Raid\Repo\User\PreferenceInterface;

class PreferenceDataComposer {

protected $preference;

public function __construct(PreferenceInterface $preference)
{
$this->preference = $preference;
}

public function compose($view)
{
$view->with('preferences', $this->preference->getActive());
}

}

然后是 Composer 的服务提供商:

<?php namespace Raid\Composers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider {

public function register()
{
$this->app->bind('Raid\Composers\PreferenceDataComposer', function($app)
{
return new PreferenceDataComposer(
$this->app->make('Raid\Repo\Preference\PreferenceInterface')
);
});
}

public function boot()
{
$this->app->view->composer('account.preferences', 'Raid\Composers\PreferenceDataComposer');
}

}

接下来是界面:

<?php namespace Raid\Repo\Preference;

interface PreferenceInterface {

public function getActive();

}

还有 CacheDecorator:

<?php namespace Raid\Repo\Preference;

use Raid\Service\Cache\CacheInterface;

class CacheDecorator extends AbstractPreferenceDecorator implements PreferenceInterface {

protected $cache;

public function __construct(PreferenceInterface $preference, CacheInterface $cache)
{
parent::__construct($preference);
$this->cache = $cache;
}

public function getActive()
{
$key = md5('active');

if ($this->cache->has($key)) {
return $this->cache->get($key);
}

$preferences = $this->preference->getActive();

$this->cache->put($key, $preferences);

return $preferences;
}
}

和抽象装饰器:

<?php namespace Raid\Repo\Preference;

abstract class AbstractPreferenceDecorator implements PreferenceInterface {

protected $preference;

public function __construct(PreferenceInterface $preference)
{
$this->preference = $preference;
}

}

然后我们有存储库:

<?php namespace Raid\Repo\Preference;

use Raid\Repo\AbstractRepo;
use Illuminate\Database\Eloquent\Model;
use Raid\Service\Log\LogInterface;
use Raid\Service\Email\EmailInterface;

class EloquentPreference extends AbstractRepo implements PreferenceInterface {

protected $preference;
protected $preferenceType;
protected $log;
protected $email;

public function __construct(Model $preference, Model $preferenceType, LogInterface $log, EmailInterface $email)
{
$this->preference = $preference;
$this->preferenceType = $preferenceType;
$this->log = $log;
$this->email = $email;
}

public function getActive()
{
return $this->preference->whereActive()->get();
}

}

然后是 Eloquent 模型本身:

<?php

class Preference extends Eloquent {

protected $table = 'preferences';
protected $guarded = array('id');
protected $softDelete = true;

public function scopeWhereActive($query)
{
return $query->where('active', '=', '1');
}

}

然后是绑定(bind)接口(interface)和repo的服务提供者:

<?php namespace Raid\Repo;

use Preference;
use PreferenceType;
use Raid\Service\Cache\FileCache;
use Raid\Repo\Preference\CacheDecorator as PreferenceCacheDecorator;
use Raid\Repo\Preference\EloquentPreference;
use Illuminate\Support\ServiceProvider;

class RepoServiceProvider extends ServiceProvider {

public function register()
{
$app = $this->app;

$app->bind('Raid\Repo\Preference\PreferenceInterface', function($app)
{
$preference = new EloquentPreference(
new Preference,
new PreferenceType,
$app->make('Raid\Service\Log\LogInterface'),
$app->make('Raid\Service\Email\EmailInterface')
);

return new PreferenceCacheDecorator(
$preference,
new FileCache($app['cache'], 'preferences', 1440)
);
});
}

}

在上面的代码中,您可以在 Composer 中看到我请求接口(interface)的一个实例,而在 Composer 的服务提供者中,我从 IoC 中解析了它的一个实例。

在 repo 服务提供者中,我通过创建 repo 实例(传入其依赖项)来绑定(bind)接口(interface),然后返回用缓存装饰器包装的 repo 。所有的 repo、装饰器、抽象装饰器都实现了接口(interface),所以我不确定它是如何被解析为不是接口(interface)的实例的?

如果您能在调试方面提供任何帮助,我将非常感激。

如果您需要更多代码,请喊。

最佳答案

你的问题可能出在Raid\Repo\User\PreferenceInterface,应该是Raid\Repo\Preference\PreferenceInterface。

除此之外,我在您的代码中看不到任何问题,为了测试,我创建了一些命名空间、接口(interface)和类,但 PreferenceDataComposer 实例化对我来说没有任何问题。

在我的 routes.php 中,我创建了一个路由来实例化它,创建基于 Eloquent 的类,一些需要的绑定(bind),手动注册服务提供者并加载命名空间文件:

<?php

class Preference extends Eloquent {

protected $table = 'preferences';
protected $guarded = array('id');
protected $softDelete = true;

public function scopeWhereActive($query)
{
return $query->where('active', '=', '1');
}

}

class PreferenceType extends Eloquent {

protected $table = 'preferences';
protected $guarded = array('id');
protected $softDelete = true;

public function scopeWhereActive($query)
{
return $query->where('active', '=', '1');
}

}

App::bind('Raid\Service\Log\LogInterface', 'Raid\Service\Log\Log');
App::bind('Raid\Service\Email\EmailInterface', 'Raid\Service\Email\Email');
App::bind('Raid\Service\Cache\CacheInterface', 'Raid\Service\Cache\FileCache');

require app_path().'/Mortelugo.php';

Route::any('test', function() {

$repo = new Raid\Repo\RepoServiceProvider(App::make('app'));
$repo->register();

$composer = new Raid\Composers\ComposerServiceProvider(App::make('app'));
$composer->register();

$preference = App::make('Raid\Composers\PreferenceDataComposer');

// This is it instantiated

dd($preference);

});

我创建了一个包含所有命名空间的 Mortelugo.php 文件,但我不得不创建一些缺失的文件并更改其中一个命名不正确的文件:

<?php

namespace Raid\Composers {

use Raid\Repo\Preference\PreferenceInterface;

class PreferenceDataComposer {

protected $preference;

public function __construct(PreferenceInterface $preference)
{
$this->preference = $preference;
}

public function compose($view)
{
$view->with('preferences', $this->preference->getActive());
}

}

}

namespace Raid\Composers {

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider {

public function register()
{
$this->app->bind('Raid\Composers\PreferenceDataComposer', function($app)
{
return new PreferenceDataComposer(
$this->app->make('Raid\Repo\Preference\PreferenceInterface')
);
});
}

public function boot()
{
$this->app->view->composer('account.preferences', 'Raid\Composers\PreferenceDataComposer');
}

}

}

namespace Raid\Repo {

use Preference;
use PreferenceType;
use Raid\Service\Cache\FileCache;
use Raid\Repo\Preference\CacheDecorator as PreferenceCacheDecorator;
use Raid\Repo\Preference\EloquentPreference;
use Illuminate\Support\ServiceProvider;

abstract class AbstractRepo {

}

class RepoServiceProvider extends ServiceProvider {

public function register()
{
$app = $this->app;

$app->bind('Raid\Repo\Preference\PreferenceInterface', function($app)
{
$preference = new EloquentPreference(
new Preference,
new PreferenceType,
$app->make('Raid\Service\Log\LogInterface'),
$app->make('Raid\Service\Email\EmailInterface')
);

return new PreferenceCacheDecorator(
$preference,
new FileCache($app['cache'], 'preferences', 1440)
);
});
}

}

}

namespace Raid\Repo\Preference {

interface PreferenceInterface {

public function getActive();

}

use Raid\Service\Cache\CacheInterface;

abstract class AbstractPreferenceDecorator implements PreferenceInterface {

protected $preference;

public function __construct(PreferenceInterface $preference)
{
$this->preference = $preference;
}

}


class CacheDecorator extends AbstractPreferenceDecorator implements PreferenceInterface {

protected $cache;

public function __construct(PreferenceInterface $preference, CacheInterface $cache)
{
parent::__construct($preference);
$this->cache = $cache;
}

public function getActive()
{
$key = md5('active');

if ($this->cache->has($key)) {
return $this->cache->get($key);
}

$preferences = $this->preference->getActive();

$this->cache->put($key, $preferences);

return $preferences;
}
}

use Raid\Repo\AbstractRepo;
use Illuminate\Database\Eloquent\Model;
use Raid\Service\Log\LogInterface;
use Raid\Service\Email\EmailInterface;

class EloquentPreference extends AbstractRepo implements PreferenceInterface {

protected $preference;
protected $preferenceType;
protected $log;
protected $email;

public function __construct(Model $preference, Model $preferenceType, LogInterface $log, EmailInterface $email)
{
$this->preference = $preference;
$this->preferenceType = $preferenceType;
$this->log = $log;
$this->email = $email;
}

public function getActive()
{
return $this->preference->whereActive()->get();
}

}

}

namespace Raid\Service\Log {

interface LogInterface {

}

class Log implements LogInterface {

}

}

namespace Raid\Service\Email {

interface EmailInterface {

}

class Email implements EmailInterface {

}

}

namespace Raid\Service\Cache {

interface CacheInterface {

}

class FileCache implements CacheInterface {

}

}

关于php - 传递给构造函数的参数必须是接口(interface)的实例,给定的装饰器实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23383267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com