We're growing into something new. Read More
Sitebase

Wim Mostmans


alias gc="git commit -m"
alias ga="git add"
alias gs="git status" 
alias gd="git diff"
alias gl='git log --graph --full-history --all --color --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"'

Boost Git productivity

Here's a little trick to boost your git command line productivity. Add the following lines to your .bash_profile. This will make it possible to use the short commands like "gc" or "gs" instead of typing the full command every time.

<?php
while (true) {
    echo 'Hello world at ' . time() . PHP_EOL;
    sleep(1);
}

Heroku PHP worker

With the following code you can have a PHP worker on Heroku that auto restarts after a crash: worker.php file:

<?php

class AjaxResponse
{

	const STATUS_OK 		= "ok";
	const STATUS_REDIRECT 	= "redirect";
	const STATUS_ERROR		= "error";

	protected $status		= "ok";			// The status of the response
	protected $redirectUrl	= null;			// Redirect user to url x
	protected $redirectDelay= null;			// Redirect user after x seconds
	protected $errors		= array();		// List of errors (validation, permission, other problems)
	protected $html			= null;			// Html text to display
	protected $message		= null;			// A message to the can add to the response describin the result
	protected $data			= null;			// Data object or array that contains result data. For example a list of users
	protected $raw			= null;			// Raw data like pure HTML as a response
	
	public function __construct($data=null) {
		if(isset($data) && is_scalar($data)) {
			$this->setRawData($data);
		}
		if(isset($data)) {
			$this->setData($data);
		}
	}
	
	public function setStatus($status) 
	{
		if(!in_array($status, array(self::STATUS_OK, self::STATUS_REDIRECT, self::STATUS_ERROR))) {
			// @todo throw invalid property exception
		}
	
		$this->status = $status;
	}
	
	public function getStatus()
	{
		return $this->status;
	}
	
	/**
	 * Redirect the user to
	 *
	 * @param string $url	Redirect the user to where?
	 * @param int $delay	Wait x seconds before redirecting the user
	 * @return void
	 */
	public function setRedirect($url, $delay=0) 
	{
		if(!is_numeric($delay)) {
			//@todo throw invalid property exception
		}
		$this->status			= self::STATUS_REDIRECT;
		$this->redirectDelay 	= $delay;
		$this->redirectUrl 		= $url;
	}
	
	public function getRedirectUrl() 
	{
		if(!isset($this->redirectUrl) || empty($this->redirectUrl)) {
			return false;
		}
		return $this->redirectUrl;
	}
	
	public function getRedirectDelay() 
	{
		return $this->redirectDelay;
	}
	
	public function setHtml($html) 
	{
		$this->html = $html;
	}
	
	public function getHtml() 
	{
		if(!isset($this->html) || empty($this->html)) {
			return false;
		}
		return $this->html;
	}
	
	public function setMessage($text) 
	{
		$this->message = $text;
	}
	
	public function getMessage() 
	{
		if(!isset($this->message) || empty($this->message)) {
			return false;
		}
		return $this->message;
	}
	
	/**
	 * Add an error to the response
	 * 
	 * @param string $text	The error message
	 * @param string $about	The name of the element or var this error is about. This can for example be the name of a POST var
	 * @return void
	 */
	public function addError($text, $about=null)
	{
		// Adding an error will automaticly set the response status to error
		// If you want to force a status ok you can use setStatus(ok) after you've added all errors
		if($this->getStatus() != self::STATUS_ERROR) {
			$this->setStatus(self::STATUS_ERROR);
		}
		
		$this->errors[] = array(
			'about' => $about, 
			'text' => $text
		);
	}
	
	public function getErrors() 
	{
		if(count($this->errors) === 0) {
			return false;
		}
		return $this->errors;
	}
	
	public function setData($data) 
	{
		$this->data = $data;
	}
	
	public function getData() 
	{
		if(!isset($this->data) || empty($this->data)) {
			return false;
		}
		return $this->data;
	}
	
	public function setRawData($data) 
	{
		$this->raw = $data;
	}
	
	public function getRawData() 
	{
		if(!isset($this->raw) || empty($this->raw)) {
			return false;
		}
		return $this->raw;
	}
	
	/**
	 * Output the current response to the client
	 *
	 * @return void
	 */
	public function __toString() {
		
		header('Cache-Control: no-cache, must-revalidate');
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
		
		if($this->getRawData()) {
			header('Content-type: text/html');
			exit($this->getRawData());
		}
		
		
		$response = new stdClass();
		
		// Set the response status
		$response->status = $this->getStatus();
		
		// Set redirect to if needed
		if($this->getStatus() == self::STATUS_REDIRECT) {
			if(!$this->getRedirectUrl()) {
				// @todo throw exception propery missing
			}
			$response->redirect = array(
				'url' 	=> $this->getRedirectUrl(),
				'delay'	=> $this->getRedirectDelay()
			);
		}
		
		// Set html
		// @todo check if htmlentities is enough to get html working in json
		if($this->getHtml()) {
			$response->html = htmlentities($this->getHtml());
		}
		
		// Set message
		if($this->getMessage()) {
			$response->message = $this->getMessage();
		}
		
		// Set errors
		if($this->getErrors()) {
			$response->errors = $this->getErrors();
		}
		
		// Set data
		if($this->getData()) {
			$response->data = $this->getData();
		}
		
		$json = json_encode($response);
		
		header('Content-type: application/json');
		exit($json);
	}

}

Ajax JSON response standardization PHP helper

It's still work in progress but the reason behind this class was: when working with multiple developers on a project everyone has his own idea about how a Ajax JSON response should be structured. But by using this class everyone will use the same structure for their responses which makes debugging and the code itself much more simple. Some example outputs that the class generates: { status: "ok" data: {"data object here"}, html: "html string here", errors: { {"about":null,"text":"Name field is required"} } } That about field in the error object can for example be the ID of the element that specific errors is about. Or a response that should redirect the client to a specific page: { status: "redirect" "redirect": {"url":"sitebase.be","delay"} } Let me know what you think of the class and share your thoughts on what a standardized JSON response should look like. Standards FTW!

tail -f php_error.log | perl -pe 's/.*\bNotice\b.*/\e[0;32;40m$&\e[0m/g; s/.*\bFatal\b.*/\e[0;31;40m$&\e[0m/g; s/.*\bParse\b.*/\e[0;36;40m$&\e[0m/g; s/.*\bWarning\b.*/\e[0;33;40m$&\e[0m/g; s/.*\bCatchable\b.*/\e[0;35;40m$&\e[0m/g'

Colored tail command

Just want to share this command I use to monitor my php log files. The cool thing is that it adds different colors to the different log types so this will make it easier to analyze your log files. I hope you like it. For more information and some screenshots of how this looks like in your terminal, check out this (dutch) article: computerfaq.be/live-logbestanden-bekijken-in-kleur/

// Alert in IE
if(typeof console === 'undefined') { console = { log:function(){alert(arguments[0])} } };

// IE shut up mode (I like this one most)
if(typeof console === 'undefined') { console = { log:function(){} } };

Console log on IE

Do you also hate it that when you still have some console.log debug lines in your code, that it breaks your script in IE? This little code will solve that problem.