This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$repo = new Acme\Repositories\UserRepository; | |
// echo the name of the user with id 1 | |
echo $repo->find(1)->name; | |
// echo the number of users with username = johnsmith | |
echo count($repo->findByUsername('johnsmith')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Acme\Abstracts; | |
use Acme\Repositories\RepositoryInterface; | |
/** | |
* The Abstract Repository provides default implementations of the methods defined | |
* in the base repository interface. These simply delegate static function calls | |
* to the right eloquent model based on the $modelClassName. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Acme\Repositories; | |
use Acme\Abstracts\Repository as AbstractRepository; | |
class UserRepository extends AbstractRepository implements UserRepositoryInterface | |
{ | |
// This is where the "magic" comes from: | |
protected $modelClassName = 'User'; | |
// This class only implements methods specific to the UserRepository |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Acme\Repositories; | |
/** | |
* The UserRepositoryInterface contains ONLY method signatures for methods | |
* related to the User object. | |
* | |
* Note that we extend from RepositoryInterface, so any class that implements | |
* this interface must also provide all the standard eloquent methods (find, all, etc.) | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Acme\Repositories; | |
/** | |
* RepositoryInterface provides the standard functions to be expected of ANY | |
* repository. | |
*/ | |
interface RepositoryInterface { | |
public function create(array $attributes); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Acme\Storage\PostRepositoryInterface; | |
use Mockery as m; | |
class PostControllerTest extends TestCase { | |
private $postRepository; | |
public function setUp() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Mockery as m; | |
class PostControllerTest extends PHPUnit_Framework_Testcase { | |
private $postRepository; | |
private $postController; | |
public function setUp() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// etc... | |
'providers' => array( | |
'Illuminate\Foundation\Providers\ArtisanServiceProvider', | |
'Illuminate\Auth\AuthServiceProvider', | |
// etc... | |
// add custom service providers below: | |
'Acme\Storage\Eloquent\StorageServiceProvider', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Acme\Storage\Eloquent; | |
use Illuminate\Support\ServiceProvider; | |
class StorageServiceProvider extends ServiceProvider { | |
public function register() | |
{ | |
// tells the IoC container that when we request an instance of PostRepositoryInterface, | |
// it should instantiate and return Acme\Storage\Eloquent\PostRepository |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Acme\Controllers; | |
use Acme\Storage\PostRepositoryInterface as PostRepository; | |
use Acme\Models\Post; | |
class PostController extends BaseController { | |
private $postRepository; | |
// notice that our controller now expects PostRepositoryInterface to be injected |