Skip to content

Instantly share code, notes, and snippets.

View jonathaningram's full-sized avatar

Jonathan Ingram jonathaningram

View GitHub Profile
@jonathaningram
jonathaningram / contact.html
Created May 4, 2015 23:39
One way in Go to build a template engine/renderer.
{{template "layout" .}}
{{define `htmlAttrs`}} ng-app="myApp"{{end}}
{{define "headTitle"}}Contact us<!-- can't include "parent" like in Jinja/Twig so these "blocks" are not 100% flexible, but still usable -->{{end}}
{{define "headExtraScripts"}}
<!-- assuming this is not in an asset pipeline where it normally would be -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
{{end}}
@jonathaningram
jonathaningram / api_test.go
Created June 23, 2014 23:09
Example integration test for API endpoints - tested using Go
// +build integration
package integration
import (
"bytes"
"encoding/json"
"net/http"
"testing"
)
@jonathaningram
jonathaningram / layout.html
Created February 10, 2014 02:06
Illustrates the Golang error "template: redefinition of template %q"
{{ define "layout" }}
<html>
<body>
{{ template "navbar" . }}
<div class="content">
{{ template "content" . }}
</div>
</body>
</html>
{{ end }}
@jonathaningram
jonathaningram / FeatureXController.php
Created October 13, 2012 09:32
Test that ensures that a controller action is only accessible by beta testers
<?php
namespace Acme\Bundle\WebBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
@jonathaningram
jonathaningram / NewMasterSlaveConnection.php
Created August 31, 2012 02:08
Doctrine MasterSlaveConnection beginTransaction connects to master but does NOT call parent method
<?php
<?php
class NewMasterSlaveConnection
{
public function beginTransaction()
{
$this->connect('master');
@jonathaningram
jonathaningram / Connection.php
Created August 31, 2012 02:03
Doctrine Connection beginTransaction method also calls connect, but implicitly on the slave connection which overwrites master
<?php
class Connection
{
public function beginTransaction()
{
$this->connect();
// **** Note: transaction nesting level is NOW incremented, so connect method does not consider it ***
++$this->_transactionNestingLevel;
@jonathaningram
jonathaningram / MasterSlaveConnection.php
Created August 31, 2012 01:58
Doctrine MasterSlaveConnection connect sets $this->_conn to the master one
<?php
class MasterSlaveConnection
{
public function connect($connectionName = 'slave')
{
if ( $connectionName !== 'slave' && $connectionName !== 'master' ) {
throw new \InvalidArgumentException("Invalid option to connect(), only master or slave allowed.");
}
@jonathaningram
jonathaningram / MasterSlaveConnection.php
Created August 31, 2012 01:55
Doctrine MasterSlaveConnection beginTransaction connects to master and then calls parent method which overwrites connection
<?php
class MasterSlaveConnection
{
public function beginTransaction()
{
$this->connect('master');
return parent::beginTransaction();
}
}
@jonathaningram
jonathaningram / Service.php
Created August 31, 2012 01:52
Doctrine service method to create a user that inserts a user so should use master connection
<?php
class Service
{
public function createUser(UserInterface $user)
{
$em = $this->getEntityManager();
$conn = $em->getConnection();
$conn->beginTransaction();
@jonathaningram
jonathaningram / gist:3485084
Created August 27, 2012 02:21
Symfony2 error when using Symfony's HTTP cache
Error 2: /var/www/beta.example.com/shared/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpCache/Store.php line 92: unlink(/var/www/beta.example.com/releases/20120827020525/app/cache/beta/http_cache/md/c2/88/66a911b5266a57bdd55131a47895b8861dfd.lck): No such file or directory