First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.
- — 17 March, 2003 08:55
Using PEAR: Part II
In the previous page we looked at managing and installing PHP Extension and Application Repository (PEAR) packages, as well as how to use such a package from a PHP script. Now we will look at some of the other packages in the PEAR library.
The packages required for this column are available from www.pear.php.net.
To install them into your local PHP system, copy all packages from the PEAR site into a temporary directory, then type:
$ pear install
Where <package filename> is the filename of the package you are installing. The packages are stored in tar gzipped format, so you may get an error like this:
The extension 'zlib' couldn't be found.
Please make sure your version of PHP was built with 'zlib' support.
If so, you will need to rebuild PHP passing the '--with-zlib' argument to configure (see the INSTALL file in the PHP source archive on the cover CD for more information).
File system packages
It is often convenient to store information for your Web application on the file system. Using the File_Find PEAR package you can search and map directories used by your application. Consider the following simple script, which lists all files ending with .php in a specified directory:
06 $ff = new File_Find();
07 $files = $ff->glob("/.*\.php$/","scripts/","perl");
Lines 02, 03 and 04 include the relevant PEAR package files. Line 07 creates a new File_Find object. Line 08 calls the glob() method with three arguments: the first a search pattern, the second the directory to search - in this case, the directory scripts/ - and the third the search algorithm to use. The search pattern may look strange to readers unfamiliar with Perl regular expression syntax. It means: match strings ending in '.php'. For more information on Perl regular expressions see the Regular Expression Functions (Perl Compatible) section of the PHP manual.
This script can easily be expanded to search all sub-directories by calling the search method.
$files = $ff->search("/.*\.php$/","scripts/","perl");
Alternatively, it might be useful to build a map of a directory hierarchy and its files. This can be done by calling the maptree() method:
$files = $ff->maptree("scripts/");
This returns a two dimensional array. The first element, $files, is an array of sub-directories; the second element, $files, is an array of files in those sub-directories.
Search and replace
PEAR makes it easy to search multiple files and replace strings of text. Consider the following script, which changes a copyright string in each file.
07 $ff = new File_Find();
08 $files = $ff->search("/.*\.php$/","scripts/","perl");
10 $fsr = new File_SearchReplace("Copyright Abc, Inc. 2002","Copyright Abc, Inc. 2003", $files, "scripts/");
This script builds on the file system search functionality we looked at above by building a list of files that we want to manipulate (line 08). Line 10 creates a File_SearchReplace object. We pass four arguments to the object constructor: the first argument is the string to replace, the second argument is the replacement string, the third argument is an array of files to search and replace in, and the fourth argument specifies the directory in which to look.
Line 11 performances the actual search and replace.
Logging can be a very important part of application development, debugging and management. The PEAR Log package provides a convenient mechanism for doing this in a standardised manner.
04 $log = Log::singleton("file","log.txt");
05 $log->log("Log file test");
Line 04 creates an instance of the object we're interested in: namely, a file-based logging object (argument one) that logs to log.txt (argument two). Line 05 buffers a log message inside the object and line 06 writes the buffer to log.txt. This last point is very important: if you do not call the WriteOut() method the log message(s) will be lost.