Skip to content

Instantly share code, notes, and snippets.

@notomato
notomato / environment.php
Created March 21, 2013 04:15
Configuring environment in a legacy app with Lithium.
use lithium\core\Environment;
$request = new lithium\action\Request();
Environment::is(function($request) {
switch (true) {
case (in_array($request->env('SERVER_ADDR'), array('::1', '127.0.0.1'))):
return 'development';
case ($request->env('HTTP_HOST') == 'foobar.com.au'):
return 'staging';
@notomato
notomato / gist:4981259
Created February 18, 2013 22:14
Configure logger to save to date based files - 'info-01-03-2012.log'
Logger::config(array(
'default' => array(
'adapter' => 'File',
'path' => dirname(__DIR__) . '/logs/',
'file' => function($details, $config) {
return $details['priority'] . '-' . date('d-m-Y') '.log';
}
)
));
@notomato
notomato / gist:3463043
Created August 25, 2012 09:45
Example of a custom helper outputting cached assets using Assetic, Yui Compressor and LessPHP.
<?php
namespace app\extensions\helper;
use Assetic\AssetWriter;
use Assetic\AssetManager;
use Assetic\FilterManager;
use Assetic\Asset\AssetCache;
use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
@notomato
notomato / gist:3377689
Last active October 8, 2015 19:18
Working with geolocation
<?php
$coords = Geocoder::find('google', "Sydney, Australia");
// Within a box
$files = Files::find('within', [
'conditions' => [
'location' => [
'$within' => [
'$box' => [
@notomato
notomato / gist:3377621
Created August 17, 2012 10:01
Example filter on model find
<?php
Posts::applyFilter('find', function($self, $params, $chain) {
$key = // Make a cache key from $params['options']
if ($result = Cache::read('default', $key)) {
return $result;
}
$result = $chain->next($self, $params, $chain);
Cache::write('default', $key, $result);
return $result;
@notomato
notomato / gist:3377619
Created August 17, 2012 10:01
Query api
<?php
$query = new Query(array(
'type' => 'read',
'model' => 'app\models\Post',
'fields' => array('Post.title', 'Post.body'),
'conditions' => array('Post.id' => new Query(array(
'type' => 'read',
'fields' => array('post_id'),
'model' => 'app\models\Tagging',
@notomato
notomato / gist:3377609
Created August 17, 2012 09:58
Working with collections
<?php
$posts = Posts::findAllBySomeCondition();
$posts->first(function($post) {
return $post->published == true;
});
$posts->each(function($post) {
return $post->counter++;
@notomato
notomato / gist:3377573
Created August 17, 2012 09:55
Setting custom post source/connection
<?php
namespace app\models;
class Posts extends \lithium\data\Model {
protected $_meta = array(
'key' => 'custom_id',
'source' => 'custom_posts_table',
'connection' => 'legacy_mysql_db'
@notomato
notomato / gist:3377567
Created August 17, 2012 09:54
Router configuration examples
<?php
Router::connect('/{:user}/{:controller}/{:action}', array(), function($request) {
if (!Users::count(array('conditions' => array('user' => $request->user)))) {
return false;
}
return $request;
});
Router::connect('/', array(), function($request) {
@notomato
notomato / gist:3377510
Created August 17, 2012 09:47
Custom detector/detect iphone requests
<?php
$request->detect('iPhone', array('HTTP_USER_AGENT', '/iPhone/'));
$isiPhone = $request->is('iPhone');
$request->detect('custom', function($request) {
if ($value = $request->env("HTTP_WHATEVER")) {
// Do something with $value
}
return false;
});