Skip to content

Instantly share code, notes, and snippets.

// Binds a method to its owner, optionally adding more arguments
function bindMethod(_this, method) {
method = _this[method];
if (arguments.length <= 2) {
return method.bind(_this);
}
var args = Array.prototype.slice.call(arguments, 2);
return args.unshift(_this), method.bind.apply(method, args);
}
// Based on idea of http://millermedeiros.github.com/js-signals/
function Signal(context, remember) {
this._bindings = [];
this._sorted = true;
this._erased = false;
this._remember = !!remember;
this._params = null;
this._propagate = false;
this._context = context || this;
@xphere
xphere / robot.js
Created December 21, 2012 17:57
js[CODE].is('Fun');
// SimpleRand as part of C implementation
// Usage:
// s = new SimpleRand(seed);
// v = s.rand(5, 10);
function SimpleRand(randState) {
var u;
function next() {
if (randState === u) {
randState = 54819;
}
@xphere
xphere / Twig_Node_MergeContext.php
Last active December 16, 2015 09:09
Enable inline objects in Twig {% with %} tags
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
* (c) 2013 Berny Cantos
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\DemoBundle\Entity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class DemoController extends Controller
@xphere
xphere / security.yml
Created July 8, 2013 14:01
Demonstrates how order matters in firewall setup #Symfony #Security
# With this security.yml, browsing to /api resolves to root firewall
security:
# ...
firewalls:
root:
pattern: ^/
anonymous: true
api:
pattern: ^/api
@xphere
xphere / Diff.php
Last active December 20, 2015 12:18
Returns integer difference between calls to a callback. Mainly for usage with memory_get_usage and memory_get_peak_usage
<?php
/**
* Returns integer difference between calls to a callback
* Mainly for usage with memory_get_usage and memory_get_peak_usage
*
* Usage:
* $memDiff = new Diff(function() { return memory_get_usage(true); });
* do_somethin_memory_intensive();
* echo $memDiff;
@xphere
xphere / search.html
Created September 27, 2013 08:46
Fiddling with FRP and BaconJS
<input id="search" type="text" />
<h1>Results for search <span id="query">--</span></h1>
<ul id="results" />
@xphere
xphere / OwnershipVoter.php
Last active December 25, 2015 04:28
Usage of Ofertix\SecurityExtraBundle annotations
<?php
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class OwnershipVoter implements VoterInterface
{
public function supportsAttribute($attribute)
{
return $attribute === 'ROLE_OWNER';
@xphere
xphere / ExampleController.php
Created October 16, 2013 08:29
Makes data in `$request->request` accessible for other ParamConverters like `DoctrineParamConverter`. Just use the `from` option in your annotations, like in the example.
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class ExampleController
{
/**
* @ParamConverter("user", {"from" = "id"})
*/
public function exampleAction(User $user)
{
// …