Laravel 5中的合同

Interfaces I understand the reasons for. Single responsibility I understand the reasoning. Basically I do understand why the 'contract' path has been chosen, I'm just struggling with understanding the implementation.

For example the docs give us this example:

<?php namespace App\Orders;

use Illuminate\Contracts\Cache\Repository as Cache;

class Repository {

    /**
     * Create a new repository instance.
     *
     * @param  Cache  $cache
     * @return void
     */
    public function __construct(Cache $cache)
    {
        $this->cache = $cache;
    }

}

If i were to replace the laravel native cache driver with, let's say, a third party, framework agnostic cache driver, would I then have to write an adapter to adhere to Laravel 5.0 cache contract?

I think you are asking Binding Interfaces To Implementations.

How to bind an implementation to an interface?

$this->app->bind('App\Contracts\EventPusher', 'App\Services\PusherEventPusher');

And where does Laravel itself bind the default implementations?

In 'Illuminate/Foundation/Application.php', it told the container what are the aliases for an abstract keyword:

'cache' => ['Illuminate\Cache\CacheManager', 'Illuminate\Contracts\Cache\Factory'],

this way, when you write either

$this->app->make('Illuminate\Cache\CacheManager')

or

$this->app->make('Illuminate\Contracts\Cache\Factory')

it's basically the same as

$this->app->make('cache')

And in Illuminate\Cache\CacheServiceProvider.php, it told the container what are the real bindings to the abstract keyword:

$this->app->singleton('cache', function ($app) {
    return new CacheManager($app);
});

Let me know if you have other questions.