Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / PostRepository.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 Acme\Storage\PostRepositoryInterface;
use Acme\Models\Post;
class PostRepository implements PostRepositoryInterface {
public function paginate($perPage = null, $columns = array('*'))
{
return Post::paginate($perPage, $columns);
@coreymcmahon
coreymcmahon / PostRepositoryInterface.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;
interface PostRepositoryInterface {
public function paginate($perPage = null, $columns = array('*'));
public function findOrFail($id, $columns = array('*'));
// ...etc
}
@coreymcmahon
coreymcmahon / PostsController.php
Created January 14, 2014 06:14
Building Testable Applications using the Repository Pattern - http://www.slashnode.com/the-repository-pattern/
<?php namespace Acme\Controllers;
use Acme\Storage\PostRepository;
class PostController extends BaseController {
private $postRepository;
public function __construct(PostRepository $postRepository = null)
{
@coreymcmahon
coreymcmahon / PostRepository.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;
use Acme\Models\Post;
class PostRepository {
public function paginate($perPage = null, $columns = array('*'))
{
return Post::paginate($perPage, $columns);
}
@coreymcmahon
coreymcmahon / PostsController.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\Models\Post;
class PostController extends BaseController {
public function index()
{
$posts = Post::paginate(20);
return View::make('post.index', compact('posts'));
@coreymcmahon
coreymcmahon / env-vars.php
Created February 19, 2013 07:29
Using environment variables. From the article: The 12 Factor PHP App - Part 1, http://www.modernphpbook.com/articles/the-12-factor-php-app-part-1 - Fig 3
<?php
// get the environment variable and parse it:
$url = parse_url(getenv('DATABASE_URL'));
// construct the data source name:
$scheme = $url['scheme'];
$host = $url['host'] . ':' . $url['port'];
$dbname = trim($url['path'], '/'); // '/application_db' -> 'application_db'
$dsn = "{$scheme}:host={$host};dbname={$path}";
@coreymcmahon
coreymcmahon / parse-url-eg.php
Created February 19, 2013 07:28
Using parse_url. From the article: The 12 Factor PHP App - Part 1, http://www.modernphpbook.com/articles/the-12-factor-php-app-part-1 - Fig 2
<?php
$url = parse_url('mysql://dbuser:dbpass@localhost:4567/application_db');
var_dump($url);
@coreymcmahon
coreymcmahon / db-env-var.php
Created February 19, 2013 07:26
Initialising a DB connection from environment variables. From the article: The 12 Factor PHP App - Part 1, http://www.modernphpbook.com/articles/the-12-factor-php-app-part-1 - Fig 1
<?php
// Initialising a DB connection from environment vars...
$dbHost = getenv('DB_HOST');
$dbName = getenv('DB_NAME');
$dbUser = getenv('DB_USER');
$dbPass = getenv('DB_PASS');
$pdo = new \PDO("mysql:host={$dbHost};dbname={$dbName}", $dbUser, $dbPass);
<?php
namespace Service;
class TwitterAPI
{
private $httpClient;
public function __construct($httpClient = null)
{
$this->httpClient = $httpClient;
<?php
namespace Model;
class Product { /* etc... */ }
class Order { /* etc... */ }
class Customer { /* etc... */ }
/* * * * * * * * * * * * * * * * */