Skip to content

Instantly share code, notes, and snippets.

@simshaun
simshaun / gist:9198574
Last active August 29, 2015 13:56
Symfony2 change form data in event
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) {
$data = $event->getData();
if (isset($data['url'])) {
$url = (string) $data['url'];
if (empty($url)) {
$url = isset($data['title']) ? (string) $data['title'] : null;
@simshaun
simshaun / example.php
Created February 26, 2014 00:11
Sample stand-alone Assetic usage
$name = 'js';
$outputPath = 'assets/js/cache';
$assets = array(
'file1.js',
'file2.js',
);
$filterManager = null;
// $filterManager = new \Assetic\FilterManager();
// $filterManager->set('cssrewrite', new \Assetic\Filter\CssRewriteFilter());
@simshaun
simshaun / AdminBarVoter.php
Created March 31, 2014 18:37
Example Symfony2 security voter
<?php
namespace S2\AppBundle\Security\Voter;
use FOS\UserBundle\Model\UserInterface;
use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
/**
@simshaun
simshaun / gist:81216edee0854f84615e
Last active August 29, 2015 14:01
Pingdom IPs / up to date as of 5/12-2014
95.141.32.46
95.211.217.68
91.109.115.41
83.170.113.210
188.138.118.144
174.34.224.167
72.46.140.106
76.72.172.208
184.75.210.226
78.40.124.16
@simshaun
simshaun / config.yml
Created July 23, 2015 19:34
Symfony2 form help text
twig:
form:
resources:
- "Form/form_theme.html.twig"
@simshaun
simshaun / AppBundle\File\AbstractFile.php
Last active August 4, 2016 21:07
Makes use of Flysystem for fs abstraction and is setup in a manner that lets me easily swap out local files (single-server apps) with remote files (e.g. S3, for multi-server apps) by tweaking the service definitions. The Avatar file is just an example of how I might handle an image. Validation is enforced before this handler is called.
<?php
namespace AppBundle\File;
use AppBundle\File\Namer\NamerInterface;
use AppBundle\File\UrlGenerator\FileUrlGeneratorInterface;
use League\Flysystem\Filesystem;
abstract class AbstractFile
{
@simshaun
simshaun / config.yml
Created August 4, 2016 21:06
Repository as a service
services:
repository.bucket:
class: AppBundle\Entity\BucketRepository
factory: ['@doctrine.orm.default_entity_manager', getRepository]
arguments: [AppBundle\Entity\Bucket]
@simshaun
simshaun / ArrayCollectionProvider.php
Created September 1, 2016 23:16
Alice Doctrine ArrayCollection data provider
<?php
namespace AppBundle\DataFixtures\Faker;
use Doctrine\Common\Collections\ArrayCollection;
class ArrayCollectionProvider
{
public function ArrayCollection($elements)
{
@simshaun
simshaun / fixtures.yml
Created September 7, 2016 20:03
Alice fixture
Model\Address (local):
address_{1..20}:
street: <streetName()>
city: <city()>
state: 'NC'
zip: <postcode()>
Entity\Foo:
foo_{1..20}:
name (unique): <company()>
@simshaun
simshaun / sample.php
Last active October 7, 2016 19:14
Symfony2 get form errors as array. May not work properly with nested forms & duplicate keys
$errors = [];
foreach ($form->getErrors(true, true) as $error) {
$errors[$error->getOrigin()->getName()] = $error->getMessage();
}