Come to the 2010 CMS Expo

os x

OS X: Installing MongoDB and the PHP Mongo Driver

MongoDB is a full-featured object database. Since it is fast, versatile, and schema-less, you can develop a very complex data storage layer without an ORM, and without any tedious coding. For this reason, I have been investigating MongoDB as a storage layer for PHP. Here's how to set up an environment on OS X Snow Leopard.

In this blog we'll do the following:

  • Install MongoDB
  • Add some initial data to MongoDB
  • Install the PHP PECL driver for MongoDB
  • Write a short PHP Script that uses MongoDB
  • Shut down the MongoDB server

OS X: Using curl instead of wget

OS X does not come with wget, a command-line tool for retrieving websites. For a while, I grumbled about this. I knew that curl was installed, but I hadn't ever used curl from the command line. But once I tried it out, I realized that for my needs, curl is just as good as wget... and I don't have to install anything extra to get it.

Here's how to use curl to fetch a remote URL:

$ curl -OL h ttp://spine-health.com/index.php
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 34646    0 34646    0     0  15314      0 --:--:--  0:00:02 --:--:-- 17767

This will download the remote file and store it locally in index.php. If you leave off the -O, it will write the file to standard output (your terminal, usually).

curl: Remote file name has no length!

Fetching from a URL's root can behave differently. If you perform the same command as above, but pointing to the base URL, you will get an error:

$ curl -OL h ttp://spine-health.com/
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information

What's going on here? The error is not terribly informative on this point. The problem is that curl doesn't know where to write the output file. A better command here is something like this:

$ curl -L h ttp://spine-health.com/ > out.html

This time, the retrieved data will be written to the out.html file.

Like wget, curl has many options. You can read the man page for details, or you can get a quick summary with the usual curl --help command.

Phing: Fixing Output on OS X

Using Phing on a Mac OS X console sometimes has a strange result: While the Phing script runs correctly, the console displays nothing. The reason for this is that the ANSI color codes used by some versions of Phing are not supported by OS X.

The solution is simple. Change the output logger.

$ phing -logger phing.listener.DefaultLogger ftest

This will use a non-colorized logger.

Installing XDebug 2 on Mac OS 10.6 Snow Leopard (with PHP 5.3)

A while back, I wrote an extensive article on installing XDebug on OS X with MAMP. Now that I have Snow Leopard, I am making a new attempt at working with XDebug on OS X. This article discusses working with XDebug 2.0 or greater on Mac OS 10.6 with PHP 5.3, as packaged with OS X. Continue on for step-by-step instructions. (For more notes on upgrading a PHP dev environment for Snow Leopard, read this article.)

PHP Developer's Snow Leopard Upgrade Notes

I'm upgrading to Snow Leopard, and I intend to switch from MAMP to the built-in PHP/Apache 2 configuration. As a PHP developer, there are several notable things that I wanted to track as I performed my upgrades. This article tracks those changes

PHP 5.3PHP 5.3
My current OS 10.5 toolchain for PHP was this:

  • PHP 5.2.6 (MAMP)
  • Apache 2 (MAMP)
  • MySQL 5 (MAMP)
  • TextMate
  • Git
  • XDebug
  • Several PEAR packages installed into MAMP's PHP 5, including PHPUnit, PhpDocumentor, Phing, and XDebug

One of the desired outcomes was to switch to the OS X version of PHP and Apache, which is tenable now that PHP is more robust (and now that I know how to use PEAR with the OS X version). It's also desirable because Snow Leopard is now running PHP 5.3. Here are my notes on the upgrade.

Update: Problems with PHPUnit and Phing.

OS X Security Warning on Email Attachments: Getting rid of them

A colleague of mine recently pointed out that when you unzip an archive of scripts received as an email attachment, every time you open a file, you get a warning like this:

“csvimport.inc.php” is a script application which was attached to a mail message. Are you sure you want to open it?

When you have dozens of scripts to look at, clicking through this warning each time is irritating, to say the least. A little examination of the xattr flags on these files, though, revealed the problem:

$ python 
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xattr
>>> xattr.listxattr('config.inc.php')
(u'com.apple.quarantine',)
>>> 

There is a flag on such files called 'com.apple.quarantine'. This flag needs to be cleared.

Originally, I was going to write a script to do this, but a quick google obviated that need. Mark Liyanage has done this already. He shows two different ways to clear the flag.

Using the UNIX find Command

Today I needed to run a simple script against thousands of identically named files nested in a huge directory structure. What I needed was a quick way to recurse through all of the directories, ignore all of the files I didn't care about, but run a specified command on any files with a particular name.

While this sounds like the sort of thing that will require a couple dozen lines of shell scripting, it can actually be accomplished on a single line with the command find. find has been around for decades, and can be found on almost any UNIX-like file system. It is a simple to used for searching for files within the UNIX directory hierarchy. Honestly, there's not much more to it. Like all good UNIX tools, it does one thing well.

Vim Colorschemes

Here's a small tip for burgeoning Vim (VI Improved) users. Vim supports various colorschemes in its colorized editing mode. If you don't like the default syntax highlighting colorscheme that Vim uses, you can change it easily by running a command like this (within Vim):

:colorscheme koehler

The :colorscheme command takes one argument: the name of the colorscheme. Above, I am setting the syntax highlighting colorscheme to the koehler scheme. At any point, I can change it back to the default by running:

:colorscheme default

Mac: Using the Visor terminal utility

A week ago, I was introduced to Visor, a utility for turning the Mac OS X console into a "Quake-style" console. What does that mean? In a nutshell, it means that the terminal runs not as a normal window, but as a boderless window attached to the top of the screen. Hit a button and the console slides down. Hit the button again (or unfocus from the window) and up it slides. It's a convenient way of keeping a terminal accessible at all times. (And, yes, it supports multiple tabs.)
Visor: "Official" screenshotVisor: "Official" screenshot

Visor is written in Ruby, and is easy to install (though the installation process is a little more, ahem, manual than your typical Mac OS application.

Tip: By default, Visor slides away when the window loses focus. Sometimes, though, it is desirable to keep the visor down for short (or perhaps long) periods while working in another window. To do this, expand visor and then click on the visor terminal icon on the right side of your menu bar. Select "pin" and the visor will stay open. To set it back to its regular behavior, select "unpin".

Thanks to sdboyer for turning me on to Visor.

Mac: Tabbing through all form fields

By default, the Mac OS X behavior for tabbing through form fields is different than that of Windows and Linux. Instead of tabbing through all fields in a form, the default Mac behavior is to skip only between lists, text fields and text areas. Buttons and checkboxes are skipped. However, this behavior is configurable.

To configure OS X to tab through all fields, go to System Settings and navigate to Keyboard and Mouse. From there, click on the Keyboard Shortcuts at the top.

Syndicate content