Skip to content

Instantly share code, notes, and snippets.

@xphere
xphere / QueueBuilder.php
Created November 13, 2013 10:24
Verbose method for AMQP queue declaration. Before: $queue = $channel->queue_declare('queue_name', false, true, false, false, true); Now: $queue = QueueBuilder::create($channel, 'queue_name')->setDurable()->setAutoDelete()->buildQueue();
<?php
namespace Amqp;
use PhpAmqpLib\Channel\AMQPChannel;
class QueueBuilder
{
/** @var AMQPChannel */
protected $channel;
@xphere
xphere / CSVType.php
Created November 7, 2013 13:15
Store arrays of strings into comma-separated values with Doctrine2
<?php
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class CSVType extends StringType
{
const NAME = 'csv';
const DELIMITER = ',';
@xphere
xphere / FileLocator.php
Created November 5, 2013 16:18
Adds globbing support to FileLocator in symfony/config
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
@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)
{
// …
@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 / 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 / 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 / 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
<?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 / 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.