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

Source for file trax.php

Documentation is available at trax.php

  1. <?php
  2. /**
  3.  *  Create Trax application work area
  4.  *
  5.  *  (PHP 5)
  6.  *
  7.  *  @package PHPonTrax
  8.  *  @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9.  *  @copyright (c) Walter O. Haas 2006
  10.  *  @version $Id: trax.php 227 2006-07-09 20:53:36Z john $
  11.  *  @author Walt Haas <haas@xmission.com>
  12.  */
  13.  
  14. /**
  15.  *  Define where to find files to copy to the work area
  16.  *
  17.  *  Set automatically by the Pear installer when you install Trax with
  18.  *  the <b>pear install</b> command.  If you are prevented from using
  19.  *  <b>pear install</b>, change "@DATA-DIR@/PHPonTrax" by hand to the
  20.  *  full filesystem path of the location where you installed the Trax
  21.  *  distribution
  22.  */
  23. define("SOURCE_DIR""@DATA-DIR@/PHPonTrax/data/");
  24.  
  25. /**
  26.  *  Symbol substitution tables
  27.  *
  28.  *  $search and $replace below are used to perform substitutions of
  29.  *  symbols in a file being copied.  $search is an array of
  30.  *  Perl-compatible regular expressions, and $replace is a congruent
  31.  *  array of replacements for RE matches.  So everywhere that the RE
  32.  *  in, for example, $search[3] is matched in a file, the matching
  33.  *  string is replaced by the contents of $replace[3].
  34.  */
  35. $search array(
  36.                 '/@TRAX-CONFIG@/' // symbol for the full filesystem path
  37.                                 // to the Trax config/ directory in
  38.                                 // the user's work area
  39.                 );
  40.  
  41. $replace array(
  42.                  ''             // actual value of the full filesystem
  43.                                 // path to the Trax config/ directory
  44.                                 // in the user's work area
  45.                  );
  46.  
  47.  
  48. function trax({
  49.  
  50.     global $search$replace;
  51.  
  52.     //  Get command line argument, if any
  53.     if (!array_key_exists('argc',$GLOBALS)
  54.         || ($GLOBALS['argc'2)) {
  55.         usage();                // print Usage message and exit
  56.     }
  57.  
  58.     //  Check for excess arguments
  59.     if ($GLOBALS['argc'2{
  60.         echo "unrecognized command argument ".$GLOBALS['argv'][2]."\n";
  61.         usage();
  62.     }
  63.  
  64.     //  Destination directory on command line
  65.     $dstdir $GLOBALS['argv'][1];
  66.  
  67.     //  Guarantee it ends with DIRECTORY_SEPARATOR
  68.     if (substr($dstdir,-1,1!= DIRECTORY_SEPARATOR{
  69.         $dstdir .= DIRECTORY_SEPARATOR;
  70.     }
  71.     if (!create_dir($dstdir)) {
  72.         return;
  73.     }
  74.  
  75.     //  Assign real values for symbol substitution
  76.     $replace[0realpath($dstdir).'/config'// actual value of
  77.                                 // the full filesystem path to the
  78.                                 // Trax config/ directory in the
  79.                                 // user's work area
  80.  
  81.     $srcdir SOURCE_DIR;
  82.     //  copy source directory to destination directory
  83.     copy_dir($srcdir,$dstdir);
  84. }
  85.  
  86. /**
  87.  *  Copy a directory with all its contents
  88.  *
  89.  *  When a file whose filename ends '.log' is created, its permissions
  90.  *  are set to be world writable.
  91.  *  @param string $src_path  Path to source directory
  92.  *  @param string $dst_path  Path to destination directory
  93.  *  @return boolean true=>success, false=>failure.
  94.  */
  95. function copy_dir($src_path,$dst_path{
  96.  
  97.     //  Make sure we have directories as arguments
  98.     if (!is_dir($src_path)) {
  99.         echo $src_path." is not a directory\n";
  100.         return false;
  101.     }
  102.     if (!is_dir($dst_path)) {
  103.         echo $dst_path." is not a directory\n";
  104.         return false;
  105.     }
  106.  
  107.     //  Open the source directory
  108.     $src_handle opendir($src_path);
  109.     if (!$src_handle{
  110.         echo "unable to open $src_path\n";
  111.         return false;
  112.     }
  113.  
  114.     //  Copy contents of source directory
  115.     while (false !== ($src_file readdir($src_handle))) {
  116.         if (!is_dir($src_path $src_file)) {
  117.  
  118.             //  If this file exists only to make the directory
  119.             //  non-empty so that PackageFileManager will add it to
  120.             //  the installable package, don't bother to copy it.
  121.             if ($src_file == '.delete_this_file'{
  122.                 continue;
  123.             }
  124.  
  125.             //  This is a regular file, need to copy it
  126.             if (file_exists$dst_path $src_file )) {
  127.  
  128.                 //  A destination file or directory with this name exists
  129.                 if (is_file$dst_path $src_file )) {
  130.  
  131.                     //  A regular destination file with this name exists.
  132.                     //  Check whether it's different from source.
  133.                     $src_content file_get_contents($src_path $src_file);
  134.                     $dst_content file_get_contents($dst_path $src_file);
  135.                     if ($src_content == $dst_content{
  136.                         //  Source and destination are identical
  137.                         echo "$dst_path$src_file exists\n";
  138.                         continue;
  139.                     }
  140.                 }
  141.  
  142.                 //  New and old files differ.  Save the old file.
  143.                 $stat stat($dst_path.$src_file);
  144.                 $new_name $dst_path.$src_file.'.'.$stat[9];
  145.                 if (!rename($dst_path.$src_file,$new_name)) {
  146.                     echo "unable to rename $dst_path$src_file to $new_name\n";
  147.                     return false;
  148.                 }
  149.                 echo "renamed $dst_path$src_file to $new_name\n";
  150.             }
  151.  
  152.             //  Destination file does not exist.  Create it
  153.             if (!copy_file($src_path $src_file$dst_path $src_file)) {
  154.                 return false;
  155.             }
  156.  
  157.             //  Log files need to be world writeable
  158.             if (substr($src_file,-4,4== '.log'{
  159.                 chmod($dst_path $src_file0666);
  160.             }
  161.  
  162.             // Generator & Console needs to be executable 
  163.             if ($src_file == 'generate.php' || $src_file == 'console.php'{
  164.                 chmod($dst_path $src_file0754);
  165.             }           
  166.  
  167.             echo  "$dst_path$src_file created\n";
  168.         else {
  169.  
  170.             //  This is a directory.  Ignore '.' and '..'
  171.             if ( ($src_file == '.'|| ($src_file == '..') ) {
  172.                 continue;
  173.             }
  174.             //  This directory needs to be copied.
  175.             if (!create_dir$dst_path $src_file )) {
  176.                 return false;
  177.             }
  178.  
  179.             //  Recursive call to copy directory
  180.             if (!copy_dir($src_path $src_file DIRECTORY_SEPARATOR,
  181.                           $dst_path $src_file DIRECTORY_SEPARATOR)) {
  182.                 return false;
  183.             }
  184.         }
  185.     }
  186.     closedir($src_handle);
  187.     return true;
  188. }                               // function copy_dir()
  189.  
  190. /**
  191.  *  Create a directory if it doesn't exist
  192.  *  @param string $dst_dir  Path of directory to create
  193.  *  @return boolean  true=>success, false=>failed
  194.  */
  195. function create_dir($dst_dir{
  196.  
  197.     //  Does a directory of this name exist?
  198.     if (file_exists$dst_dir )) {
  199.  
  200.         //  A destination file or directory with this name exists
  201.         if (is_dir$dst_dir )) {
  202.  
  203.             //  A destination directory with this name exists.
  204.             echo "$dst_dir".DIRECTORY_SEPARATOR." exists\n";
  205.             return true;
  206.         }
  207.  
  208.         //  There is an old destination file with the same
  209.         //  name as the new destination directory. 
  210.         //  Save the old file.
  211.         $stat stat($dst_dir);
  212.         $new_name $dst_dir.'.'.$stat[9];
  213.         if (!rename($dst_dir,$new_name)) {
  214.             echo "unable to rename $dst_dir to $new_name\n";
  215.             return false;
  216.         }
  217.         echo "renamed $dst_dir to $new_name\n";
  218.     }
  219.  
  220.     //  Destination directory does not exist.  Create it
  221.     if (!mkdir($dst_dir,0775,true)) {
  222.         return false;
  223.     }
  224.     echo "$dst_dir".DIRECTORY_SEPARATOR." created\n";
  225.     return true;
  226. }
  227.  
  228. /**
  229.  *  Copy a Trax file into user's work area, substituting @TRAX-...@
  230.  *
  231.  *  @param string $src_path  Path to source file
  232.  *  @param string $dst_path  Path to destination file
  233.  *  @return boolean true=>success, false=>failure.
  234.  */
  235. function copy_file($src_path$dst_path{
  236.  
  237.     global $search$replace;
  238.  
  239.     //  Read source file into a string
  240.     if (!$file file_get_contents($src_path)) {
  241.         return false;
  242.     }
  243.  
  244.     //  Substitute @TRAX-...@ symbols for appropriate values
  245.     $file preg_replace($search$replace$file);
  246.  
  247.     //  Write out file contents
  248.     @file_put_contents($dst_path$file);
  249.     return true;
  250. }
  251.  
  252. /**
  253.  *  Output a Usage message and exit
  254.  */
  255. function usage({
  256.     echo "Usage: @BIN-DIR@".DIRECTORY_SEPARATOR."trax"
  257.         ." ".DIRECTORY_SEPARATOR."path".DIRECTORY_SEPARATOR."to"
  258.         .DIRECTORY_SEPARATOR."your".DIRECTORY_SEPARATOR."app
  259.  
  260. Description:
  261.     The 'trax' command creates a new Trax application with a default
  262.     directory structure and configuration at the path you specify.
  263.  
  264. Example:
  265.     trax ".DIRECTORY_SEPARATOR."var".DIRECTORY_SEPARATOR."www"
  266.         .DIRECTORY_SEPARATOR."html
  267.  
  268.     This generates a skeletal Trax installation in "
  269.     .DIRECTORY_SEPARATOR."var".DIRECTORY_SEPARATOR."www"
  270.     .DIRECTORY_SEPARATOR."html.
  271.     See the README in the newly created application to get going.
  272. \n";
  273.     exit;
  274. }
  275.  
  276. /**
  277.  *  Main program
  278.  */
  279. trax();
  280.  
  281. // -- set Emacs parameters --
  282. // Local variables:
  283. // mode: php
  284. // tab-width: 4
  285. // c-basic-offset: 4
  286. // c-hanging-comment-ender-p: nil
  287. // indent-tabs-mode: nil
  288. // End:
  289.  
  290. ?>

Documentation generated on Mon, 21 May 2007 22:29:19 -0600 by phpDocumentor 1.3.2