File_Fortune
[ class tree: File_Fortune ] [ index: File_Fortune ] [ all elements ]

Introduction

File_Fortune provides an OOP interface for reading *nix fortune cookie files. File_Fortune_Writer provides methods for writing fortune files.

Description

The fortune program is a small but important part of *nix culture, and this package aims to provide support for its "fortune cookie" databases to PHP programmers.

For efficiency, all versions of fortune rely on a binary header consisting mainly of offsets into the fortune file proper. Modern versions of fortune keep this header in a separate file, and this is the style adopted by this package; the older style of munging the header and data into one large "compiled" file is not currently supported, and may never be.

Using File_Fortune makes it trivial to write a simplified version of the fortune program:

  1. <?php
  2.     // Trivial 'fortune' program
  3.     $fortuneFilename $_SERVER['argv'][1];
  4.     $fortuneFile     = new File_Fortune($fortuneFilename);
  5.     $fortune         $fortuneFile->getRandom();
  6.     echo $fortune;
  7. ?>

(A more robust example of this may be found in the File/Fortune/examples/phpFortune script provided with this package.)

The original fortune program provides additional features, such as parallel databases of offensive fortunes, selection of long or short fortunes, dealing with multiple fortune files, etc. These features are not currently available with this package, but may be included in future releases.

Additionally, File_Fortune includes the package File_Fortune_Writer, which makes it trivial to maintain fortune databases. It provides methods for writing new files, as well as for adding, updating, and deleting fortunes from files.

Usage Example 1: Retrieve a random fortune

  1. <?php
  2.     require_once 'File/Fortune.php';
  3.     try {
  4.         $ffile   = new File_Fortune('/path/to/fortune/file');
  5.         $fortune $ffile->getRandom();
  6.         echo $fortune;
  7.     catch (File_Fortune_Exception $e{
  8.         echo "An error occurred: " $e->getMessage;
  9.     }
  10. ?>

Usage Example 1a: Retrieve a random fortune from a list of files

  1. <?php
  2.     require_once 'File/Fortune.php';
  3.     try {
  4.         $fortune   File_Fortune::getRandomFromSet(
  5.             '/path/to/fortune/file',
  6.             '/path/to/fortune/file2',
  7.             '/path/to/fortune/file3',
  8.         );
  9.         echo $fortune;
  10.     catch (File_Fortune_Exception $e{
  11.         echo "An error occurred: " $e->getMessage;
  12.     }
  13. ?>

Usage Example 2: Retrieve a specific fortune

  1. <?php
  2.     require_once 'File/Fortune.php';
  3.     try {
  4.         $ffile   = new File_Fortune('/path/to/fortune/file');
  5.         $fortune $ffile->getOne(3);
  6.         echo $fortune;
  7.     catch (File_Fortune_Exception $e{
  8.         echo "An error occurred: " $e->getMessage;
  9.     }
  10. ?>

Usage Example 3: Write a fortune file from an array of fortunes

  1. <?php
  2.     require_once 'File/Fortune/Writer.php';
  3.     $fortunes = array(
  4.         'A thousand mile journey begins with a single step.',
  5.         'cd /pub ; more beer',
  6.         'We apologize for the inconvenience'
  7.     );
  8.     $ffile   = new File_Fortune_Writer();
  9.     $ffile->filename = '/path/to/fortune/file';
  10.     try {
  11.         $ffile->write($fortunes);
  12.     catch (File_Fortune_Exception $e{
  13.         echo "An error occurred: " $e->getMessage;
  14.     }
  15. ?>

Usage Example 4: Add a fortune to an existing file

  1. <?php
  2.     require_once 'File/Fortune/Writer.php';
  3.     try {
  4.         $ffile   = new File_Fortune_Writer('/path/to/fortune/file');
  5.         $ffile->add("There's no place like 127.0.0.1");
  6.     catch (File_Fortune_Exception $e{
  7.         echo "An error occurred: " $e->getMessage;
  8.     }
  9. ?>

Usage Example 5: Update a fortune in an existing file

  1. <?php
  2.     require_once 'File/Fortune/Writer.php';
  3.     try {
  4.         $ffile   = new File_Fortune_Writer('/path/to/fortune/file');
  5.         $ffile->update(2'mv /can/spam /dev/null');
  6.     catch (File_Fortune_Exception $e{
  7.         echo "An error occurred: " $e->getMessage;
  8.     }
  9. ?>

Usage Example 6: Delete a fortune from a file

  1. <?php
  2.     require_once 'File/Fortune/Writer.php';
  3.     try {
  4.         $ffile   = new File_Fortune_Writer('/path/to/fortune/file');
  5.         $ffile->delete(3);
  6.     catch (File_Fortune_Exception $e{
  7.         echo "An error occurred: " $e->getMessage;
  8.     }
  9. ?>

Documentation generated on Mon, 11 Mar 2019 14:18:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.