Archive for October 2008
PHP atomic write function
Motivation:
An atomic write function for those who appreciate the convenience of file_put_contents(), but need an atomic operation. To be clear, this function does not perform all its actions in a single operation, but the replacement of the target file’s contents is atomic. Please leave a comment if you have a more convenient and/or efficient solution.
Source code:
function atomic_put_contents($filename, $data)
{
//define quasi-unique name of file to write tmp data to
$tmp_path = dirname($filename).'/'.microtime(true);
//file_put_contents fails if file doesn't exist
if(!is_file($tmp_path))
{
touch($tmp_path);
}
//file put contents is non-atomic, so write to tmp
file_put_contents($tmp_path, $data);
//mv is an atomic operation
exec("mv -f $tmp_path $filename");
}
Example usage:
$content = 'some content';
atomic_put_contents('path/file.ext', $content);
//creates file called path/file.ext containing 'some content'
PHP class to manage read-only vars and arrays
Source code:
class Constants
{
private $var_cache;
public function set($key, $val)
{
if(isset($this->var_cache[$key]))
{
throw(new Exception("Constants->get() error: var $key has already been defined"));
}
else
{
$this->var_cache[$key] = $val;
}
}
public function get($key)
{
return $this->var_cache[$key];
}
}
Example usage:
$constants = new Constants();
$constants->set('base_url', 'www.domain.com/path');
...
$img_src_url = $constants->get('base_url').'/img/file.jpg';
echo "<img src='$img_src_url'/>";
Everything
Must everything be important?
My favorite color is
the golden green produced by
sunlight shining through a leaf,
particularly those of fig trees and grape vines.
Sitting in the shade of a grape arbor,
I am awash in my favorite tone.
It’s calming;
my eyes are soothed.
I feel supported by the clay and dried grass beneath me.
I can imagine the water flowing
through the roots of this plant,
to transpire from its leaves.
I am home.
My favorite color is important to me.
sap of life
the sap of life
flows
from fingers to toes
and yet
is also still
like the air
we breath it in
and move in it
it is made of us
and we of it
a rock ledge
a rock ledge
far from the sounds of leaf blowers, garbage trucks, and trains
close to the nature of things
insignificant
below the snow
above a river wrapped in green
rust-colored
ancient
exposed to air and unabated starlight
Mother & baby by Os Gemeos
Amazing art by Os Gemeos
Beautiful graffiti by Daim
Test-driven JavaScript development with YUI
Notes from “Test Driven Development with YUI Test” presentation given by Nicholas Zakas
- Flow:
- Define requirements
- Write tests to verify requirements are met
- Write code to test
- Run tests
- If test succeed, repeat from step 1 until project is finished. If tests fail, repeat from step 3 until tests pass, and then repeat from step 1 until project is finished
- Motivations:
- Even small changes can break a complex app. Use unit tests and suites of tests to ensure app integrity after each change to the code base.
- Tasks become well-defined using this methodology
- We are best positioned to perform QA because we know our code best
- Tips & tricks:
- Use a test template to speed up the process
- Use setUp and tearDown functions to prepare and clean environment, respectively, after each test is run
- Develop the habit of creating files for code and tests for the code in pairs, e.g. code.js & test_code.js
- In general, each method gets a test and each object gets a test case
- In your build process, define a “test” state that inserts YUI test code into the app code base
- References:
- Presentation slides by Zakas
- YUI docs
building an RSS consolidator w/ Yahoo! Pipes
Goal:
Consolidate the feeds from my Flickr, WordPress, Del.icio.us, etc accounts
Solution:
Use Yahoo! Pipes
Intended Audience:
Pipes newbies
Steps:
Create a pipe
- Go to http://pipes.yahoo.com and log in, or create an account if you don’t already have one
- Click the “Create a pipe” button
- In the editor, click and drag a “Fetch Feed” object onto the stage
- Open a new browser tab or window, head over to http://flickr.com/, and log in, or create an account
- Click on the “Your photostream” link to view your photostream
- Scroll to the bottom of the page and copy the “Latest” RSS feed URL
- Switch back to the Pipes editor and paste the Flickr URL into the text field in the “Fetch Feed” object on the stage
- On the bottom on the “Fetch Feed” object, there is a circular attachment point. Click on it an drag to start forming a new pipe
- Drag the pipe to the attachment point on top of the “Pipe Output” object at the bottom of the stage.
- Release the pipe when the “Pipe Output” attachment point glows.
- Click the “Save” button to name and save your new pipe
- Click the “Back to my Pipes” link next to the save button
- Click on the name of your pipe in the list of your pipes. This will display the output of your pipe.
- Click the “More Options” link and then “Get as RSS” in the drop-down. This will open the RSS feed in your browser if you’re using Safari or Firefox.
- Following an example from Rasmus Lerdorf’s Open Hack talk (http://talks.php.net/show/hack08/8), use simplexml to parse the RSS feed:
<?php
$url = '<URL for Pipes output>';
//e.g. http://pipes.yahoo.com/pipes/pipe.run?_id=gIBkHK_V3RGpmv5oBRNMsA&_render=rss
$xml = simplexml_load_file($url);
$num_items = count($xml->channel->item);
for($i = 0; $i < $num_items; $i++)
{
$title = $xml->channel->item[$i]->title;
$date = $xml->channel->item[$i]->pubDate;
$desc = $xml->channel->item[$i]->description;
$link = $xml->channel->item[$i]->link;
echo "
<div>";
echo "
<h3><a href='$link'>$title</a></h3>
";
echo "$date";
echo "$desc";
echo "</div>
";
}
- Go back to the browser tab displaying the output from your pipe and copy the URL
- Paste this URL into your index.php file as the value for the variable “$url”
- Change the beginning of the URL from ‘feed://…’ to ‘http://…’
- Load your page and you should see the output defined above. Note: var_dump the ‘$xml’ object to see the other available fields.
- Back in the Pipes editor, drag another “Fetch Feed” object onto the stage
- Copy and paste another RSS feed into it
- From under the “Operators” heading in the list on the left, drag out a “Union” object
- Run the pipes from the two “Fetch Feed” objects into the union and pipe the output to the “Pipe Output” object
- To add more feeds, drag out additional “Fetch Feed” objects and connect them to the union






