I am having an issue trying to debug my code, as far as I can see looking at the various bits of code, it all links up correctly, but there must be something obvious I am missing here.
I am sorry about the amount of code I've pasted in to the question here, but unfortunately there are a lot of files in play for a single entity of the application, and I thought it better to provide more rather 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 which is where this error is being generated:
<?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 up is the interface:
<?php namespace Raid\Repo\Preference;
interface PreferenceInterface {
public function getActive();
}
And the 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 the 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 the 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 the service provider that binds the interface and the 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)
);
});
}
}
In the code above, you can see in the Composer, that I ask for an instance of the Interface, and in the composer's service provider, I resolve an instance of it out of the IoC.
In the repo service provider I bind the interface by making a instance of the repo (passing in its dependencies), and then return the repo wrapped with a cache decorator. All of the repo, the decorator, the abstract decorator implement the interface, so I'm not sure how it is being resolved as not being an instance of the interface?
Any help you are able to give with debugging would be me wonderful.
If you need further code, please shout.