Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / example.php
Created February 8, 2014 08:51
A Pattern for Reusable Repository Design in Laravel - http://www.slashnode.com/reusable-repository-design-in-laravel/
<?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'));
@coreymcmahon
coreymcmahon / Repository.php
Created February 8, 2014 08:50
A Pattern for Reusable Repository Design in Laravel - http://www.slashnode.com/reusable-repository-design-in-laravel/
<?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.
*/
<?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
<?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.)
*/
<?php namespace Acme\Repositories;
/**
* RepositoryInterface provides the standard functions to be expected of ANY
* repository.
*/
interface RepositoryInterface {
public function create(array $attributes);
@coreymcmahon
coreymcmahon / PostControllerTest.php
Last active August 31, 2017 00:51
Building Testable Applications using the Repository Pattern - http://www.slashnode.com/the-repository-pattern/
<?php
use Acme\Storage\PostRepositoryInterface;
use Mockery as m;
class PostControllerTest extends TestCase {
private $postRepository;
public function setUp()
@coreymcmahon
coreymcmahon / LiteratureControllerTest.php
Created January 14, 2014 06:50
Building Testable Applications using the Repository Pattern - http://www.slashnode.com/the-repository-pattern/
<?php
use Mockery as m;
class PostControllerTest extends PHPUnit_Framework_Testcase {
private $postRepository;
private $postController;
public function setUp()
@coreymcmahon
coreymcmahon / app.php
Last active January 3, 2016 05:09
Building Testable Applications using the Repository Pattern - http://www.slashnode.com/the-repository-pattern/
<?php
// etc...
'providers' => array(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
// etc...
// add custom service providers below:
'Acme\Storage\Eloquent\StorageServiceProvider',
@coreymcmahon
coreymcmahon / StorageServiceProvider.php
Last active January 3, 2016 05:09
Building Testable Applications using the Repository Pattern - http://www.slashnode.com/the-repository-pattern/
<?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
@coreymcmahon
coreymcmahon / PostController.php
Last active January 3, 2016 05:09
Building Testable Applications using the Repository Pattern - http://www.slashnode.com/the-repository-pattern/
<?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