Skip to content

Instantly share code, notes, and snippets.

<?php
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
// Interface implementation for testing purposes
class MyParameterBag implements ParameterBagInterface
{
private $container = array();
public function add(array $parameters)
{
@xphere
xphere / gist:907539
Created April 7, 2011 10:48
Validation of Value Objects
<?php
// With static validation, no object is created if not valid.
if (Locale::isValid($localeName)) {
$locale = new Locale($localeName);
doFancyStuffWith($locale);
}
// With explicit validation
$locale = new Locale($localeName);
@xphere
xphere / TagBundle.php
Created July 11, 2012 18:55
Mark a service as a consumer of others via tags and extension
<?php
namespace xPheRe\Bundle\TagBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use xPheRe\Bundle\TagBundle\DependencyInjection\Compiler\TagCollectionPass;
class xPheReTagBundle extends Bundle
{
@xphere
xphere / gist:3907873
Created October 17, 2012 20:15
How to start a new Symfony2 project easily with Composer
# Download composer as explained in http://getcomposer.org/download
> curl -s https://getcomposer.org/installer | php
# Create a new Symfony2 project
> composer.phar create-project symfony/framework-standard-edition directory
@xphere
xphere / MenuPlannerAPI.php
Last active October 12, 2015 03:38
Menu Planner API
$planner = new Planner();
$planner
->menu('menu')
->avoid('ingredient:sugar')
->avoid('type:fish')
->day('monday')
->meal('breakfast')
->dish('snack')->end()
->end()
->meal('dinner')
@xphere
xphere / google-maps-geocode.php
Last active October 12, 2015 12:18
Usage of Google\Maps\Geocode
<?php
$geocode = new \Google\Maps\Geocode\GeocodeService();
$cachePath = $this->container->getParameter('kernel.root_dir') . '/../cache';
$cache = new \Doctrine\Common\Cache\FilesystemCache($cachePath);
$cacheAdapter = new \Guzzle\Cache\DoctrineCacheAdapter($cache);
$cachePlugin = new \Guzzle\Plugin\Cache\CachePlugin($cacheAdapter);
$geocode->getClient()->addSubscriber($cachePlugin);
$observableUniverse = new Location('Universo observable');
$virgoSuperCluster = new Location('Supercúmulo de Virgo', $observableUniverse);
$localGroup = new Location('Grupo Local', $virgoSuperCluster);
$milkyWay = new Location('Via Lactea', $localGroup);
$orionArm = new Location('Brazo de Orión', $milkyWay);
$solarSystem = new Location('Sistema Solar', $orionArm);
$earth = new Location('La Tierra', $solarSystem);
$europe = new Location('Europa', $earth);
$spain = new Location('España', $europe);
$catalonia = new Location('Catalunya', $spain);
@xphere
xphere / inherit.js
Created December 11, 2012 16:30
Inherit JS
// Makes fn inherit from parent class
// Allows "super" calls ONLY in the constructor via Class.parent(this, param1, param2, ...)
// Also allows to extend the prototype giving more arguments
function inherit(parent, fn) {
fn || (fn = function() { });
fn.prototype = Object.create(parent.prototype);
for (var i = 2; i < arguments.length; ++i) {
extend(fn.prototype, arguments[i]);
}
fn.prototype.constructor = fn;
@xphere
xphere / Event.js
Created December 11, 2012 16:35
EventDispatcher JS
function Event() {
}
extend(Event.prototype, {
propagationStopped: false,
stopPropagation: function() {
this.propagationStopped = true;
return this;
},
isPropagationStopped: function() {
@xphere
xphere / extend.js
Created December 12, 2012 02:05
Extend JS
function extend(object) {
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; ++i) {
for (j in arguments[i]) {
object[j] = arguments[i][j];
}
}
}
return object;
}