Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
jhartikainen / gist:1050932
Created June 28, 2011 11:21
PHP error to exception converter with special invalid argument handling
<?php
//This converts errors into exceptions, but if the error is caused
//by an argument being invalid (for example failing a typehint), it gets converted into an InvalidArgumentException
function handle($code, $message, $file, $line) {
//This test might be naive but it worked in my very very simple test code :)
if(strpos($message, 'Argument ') === 0) {
throw new InvalidArgumentException($message);
}
else {
throw new ErrorException($message, 0, $code, $file, $line);
@jhartikainen
jhartikainen / gist:1053718
Created June 29, 2011 12:12
ZF controller plugin which fakes dojo.io.iframe requests into XMLHttpRequests
<?php
/**
* This plugin automatically converts requests made with GET parameter "dojo_io_iframe" into XMLHttpRequests
*
* This allows transparent handling of iframe transport requests with code that handles XHR requests, since they're
* both essentially the same anyway - only the transport is different.
*/
class Wantlet_Controller_Plugin_IframeRequestHandler extends Zend_Controller_Plugin_Abstract {
private $isIframe = false;
@jhartikainen
jhartikainen / DynLoad.hs
Created August 20, 2011 11:31
Module for loading modules dynamically in Haskell
{-# LANGUAGE ScopedTypeVariables #-}
module DynLoad (
loadSourceGhc,
execFnGhc
) where
import Control.Exception (throw)
import GHC hiding (loadModule)
import GHC.Paths (libdir)
import HscTypes (SourceError, srcErrorMessages)
@jhartikainen
jhartikainen / scrape.py
Created October 6, 2011 20:43
Very quick Battlelog soldier statistics scraper
#
# This script scrapes your soldier statistics from Battlelog
# NOTE: This is just a very quick demonstration, it doesn't really do much as-is, besides
# authenticate to Battlelog and get you a dict of your soldier's statistics.
# There is no error handling, no anything. It will only work if everything goes well.
#
#
# Input your Origin username and password into the params dict below and it should work.
#
# In the very bottom, the stats dict will contain the data from the Battlelog JSON response.
@jhartikainen
jhartikainen / gist:1326104
Created October 30, 2011 16:47
Example Doctrine 2 uniqueness validator for Zend_Form or such
<?php
use Doctrine\ORM\EntityRepository;
class Wantlet_Validate_EmailAvailable extends Zend_Validate_Abstract {
const NOT_AVAILABLE = 'notAvailable';
protected $_messageTemplates = array(
self::NOT_AVAILABLE => "Email address '%value%' is already in use"
);
@jhartikainen
jhartikainen / gist:1576557
Created January 8, 2012 00:11
Function for executing things in parallel, because I couldn't find any I liked =)
parallel = (ops) ->
results = []
numOps = ops.length
errHandler = null
hadError = false
okHandler = null
for op, num in ops
do (num) ->
@jhartikainen
jhartikainen / gist:1596108
Created January 11, 2012 18:42
List of weapon names used by Battlefield 3 server admin protocol. How did it get this inconsistent, I have no clue.
870MCS
Death
M16A4
DamageArea
USAS-12
Model98B
Weapons/SCAR-H/SCAR-H
Weapons/AK74M/AK74
Weapons/M416/M416
M240
@jhartikainen
jhartikainen / gist:1635885
Created January 18, 2012 21:39
Example of using Array.prototype.some in a sequential flow control fashion
//following functions would perform some operations, which would either succeed or fail,
//which would be indicated by the function returning true or false
function a() {
//do something which returns true or false
}
function b() {
//do something which returns true or false
}
@jhartikainen
jhartikainen / QueryPaginator.php
Created February 1, 2012 08:35
Doctrine 2 pagination adapter for Zend_Paginator
<?php
namespace Wantlet\ORM;
use Zend_Paginator_Adapter_Interface;
use Doctrine\ORM\Query;
/**
* Zend_Paginator adapter for Doctrine 2 queries
*/
class QueryPaginator implements Zend_Paginator_Adapter_Interface {
@jhartikainen
jhartikainen / gist:2522353
Created April 28, 2012 21:56
Multi-checkbox select field for Yesod
multiCheckBoxList :: (Eq a, RenderMessage master FormMessage, RenderMessage master msg) => [(msg, a)] -> Field sub master [a]
multiCheckBoxList = multiCheckBox . optionsPairs
multiCheckBox :: (Eq a, RenderMessage master FormMessage) => GHandler sub master (OptionList a) -> Field sub master [a]
multiCheckBox opts' = Field
{ fieldParse = \rawVals -> do
opts <- opts'
let (lefts, rights) = partitionEithers $ map (fieldParser opts) rawVals
if (length lefts) > 0