Skip to content

Instantly share code, notes, and snippets.

View adamwathan's full-sized avatar

Adam Wathan adamwathan

  • Ontario, Canada
View GitHub Profile
<?php namespace Acme\Presenters;
use Illuminate\Support\Collection;
abstract class Presenter {
protected $resource;
public function __construct($resource)
{
@adamwathan
adamwathan / laravel4basecontroller.php
Last active December 27, 2015 10:39
Base controller for Laravel 4.0 that lets you use the new instance filter functionality from 4.1
<?php
class BaseController extends Controller {
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
@adamwathan
adamwathan / laravel-cors.php
Last active September 16, 2022 00:16
Need to do something like this to serve APIs that are going to be consumed by JS clients.
<?php
App::before(function($request) {
if ($request->getMethod() == 'OPTIONS') {
$response = Response::make('', 204);
$response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS');
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
return $response;
}
@adamwathan
adamwathan / laravel-env.php
Created April 15, 2014 14:31
Better default for Laravel environment detection
<?php
// bootstrap/start.php
/*
|--------------------------------------------------------------------------
| Detect The Application Environment
|--------------------------------------------------------------------------
|
| Laravel takes a dead simple approach to your application environments
@adamwathan
adamwathan / mocks.php
Last active October 19, 2016 08:26
Mocks are dangerous
<?php
// Our test
$songs = M::mock('SongCollection');
$songs->shouldReceive('sum')
->with('length')
->andReturn(500);
$album->songs = $songs;
@adamwathan
adamwathan / database.php
Created May 2, 2014 20:07
Using an in-memory SQLite for functional testing in Laravel 4
<?php
// app/config/testing/database.php
return [
'default' => 'sqlite',
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
testtttt
@adamwathan
adamwathan / less-bem.less
Created November 2, 2014 14:53
BEM-ish nesting in Less
.episode {
margin-bottom: 5em;
&-header {
&-title {
margin-top: 0;
margin-bottom: (@line-height-computed / 4);
font-size: @font-size-h4;
}
&-meta {
margin-top: 0;
@adamwathan
adamwathan / scublish.sh
Created November 4, 2014 14:57
Sculpin GitHub publish script
function scublish() {
sculpin generate --env=prod
cd output_prod
git add --all
git commit -am "$1"
git push origin master
cd ..
}
@adamwathan
adamwathan / struct.php
Last active September 9, 2022 11:12
Structs in PHP
<?php
// Wow this whole thing is horrible
class Struct
{
public function __construct($properties)
{
foreach ($properties as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;