I had a problem debugging my code, as far as I can see the various bits of code, it all gets connected correctly, but there should be something obvious that I'm missing here.
I apologize for the amount of code I put in the question here, but unfortunately there are a lot of files in one part of the application, and I thought it was better to provide more than less.
First of all, the error message:
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
So, first of all, my view composer in which this error is created:
<?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());
}
}
and then the service provider for the 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');
}
}
Next is the interface:
<?php namespace Raid\Repo\Preference;
interface PreferenceInterface {
public function getActive();
}
And 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;
}
}
and abstract decorator:
<?php namespace Raid\Repo\Preference;
abstract class AbstractPreferenceDecorator implements PreferenceInterface {
protected $preference;
public function __construct(PreferenceInterface $preference)
{
$this->preference = $preference;
}
}
then we have a repository:
<?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();
}
}
and then the eloquent model itself:
<?php
class Preference extends Eloquent {
protected $table = 'preferences';
protected $guarded = array('id');
protected $softDelete = true;
public function scopeWhereActive($query)
{
return $query->where('active', '=', '1');
}
}
and then a service provider that binds the interface and 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, , IoC.
, ( ), , . , , , , ?
, , .
, , .