PHP on T R A X
Rapid Application Development Made Easy

root/trunk/trax/trax.php

Revision 305, 9.7 kB (checked in by john, 4 months ago)

updating package make

  • Property svn:keywords set to Id
Line 
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$
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, $quiet;
51
52     //  Get command line argument, if any
53     if (!array_key_exists('argc',$GLOBALS) || ($GLOBALS['argc'] < 2)) {
54         usage(); // print Usage message and exit
55     }
56
57     $first_param_is_option = (substr($GLOBALS['argv'][1], 0, 1) == "-") ? true : false;
58
59     //  Check for excess arguments
60     if ($GLOBALS['argc'] > 3 && !$first_param_is_option) {
61         echo "unrecognized command argument ".$GLOBALS['argv'][2]."\n";
62         usage();
63     }
64
65     if($first_param_is_option) {
66         foreach($GLOBALS['argv'] as $arg) {
67             if($arg == '-v' || $arg == '--version') {
68                 include(dirname(__FILE__)."/vendor/trax/trax.php");
69                 echo "Trax ".Trax::version()."\n";
70                 exit;
71             } elseif($arg == '-h' || $arg == '--help') {
72                 usage();
73             }
74         }       
75     } else {
76         //  Destination directory on command line
77         $dstdir = $GLOBALS['argv'][1];   
78         if($GLOBALS['argv'][2] == '-q' || $GLOBALS['argv'][2] == '--quiet') {
79             $quiet = true;           
80         }
81     }
82
83     //  Guarantee it ends with DIRECTORY_SEPARATOR
84     if (substr($dstdir,-1,1) != DIRECTORY_SEPARATOR) {
85         $dstdir .= DIRECTORY_SEPARATOR;
86     }
87     if (!create_dir($dstdir)) {
88         return;
89     }
90
91     //  Assign real values for symbol substitution
92     $replace[0] = realpath($dstdir).'/config'; // actual value of
93                                 // the full filesystem path to the
94                                 // Trax config/ directory in the
95                                 // user's work area
96
97     $srcdir = SOURCE_DIR;
98     //  copy source directory to destination directory
99     copy_dir($srcdir, $dstdir);
100 }
101
102 /**
103  *  Copy a directory with all its contents
104  *
105  *  When a file whose filename ends '.log' is created, its permissions
106  *  are set to be world writable.
107  *  @param string $src_path  Path to source directory
108  *  @param string $dst_path  Path to destination directory
109  *  @return boolean true=>success, false=>failure.
110  */
111 function copy_dir($src_path, $dst_path) {
112     
113     global $quiet;
114
115     //  Make sure we have directories as arguments
116     if (!is_dir($src_path)) {
117         echo $src_path." is not a directory\n";
118         return false;
119     }
120     if (!is_dir($dst_path)) {
121         echo $dst_path." is not a directory\n";
122         return false;
123     }
124
125     //  Open the source directory
126     $src_handle = opendir($src_path);
127     if (!$src_handle) {
128         echo "unable to open $src_path\n";
129         return false;
130     }
131
132     //  Copy contents of source directory
133     while (false !== ($src_file = readdir($src_handle))) {
134         if (!is_dir($src_path . $src_file)) {
135
136             //  If this file exists only to make the directory
137             //  non-empty so that PackageFileManager will add it to
138             //  the installable package, don't bother to copy it.
139             if ($src_file == '.delete_this_file') {
140                 continue;
141             }
142
143             //  This is a regular file, need to copy it
144             if (file_exists( $dst_path . $src_file )) {
145
146                 //  A destination file or directory with this name exists
147                 if (is_file( $dst_path . $src_file )) {
148
149                     //  A regular destination file with this name exists.
150                     //  Check whether it's different from source.
151                     $src_content = file_get_contents($src_path . $src_file);
152                     $dst_content = file_get_contents($dst_path . $src_file);
153                     if ($src_content == $dst_content) {
154                         //  Source and destination are identical
155                         if(!$quiet) echo "\texists $dst_path$src_file\n";
156                         continue;
157                     }
158                 }
159
160                 //  New and old files differ.  Save the old file.
161                 $stat = stat($dst_path.$src_file);
162                 $new_name = $dst_path.$src_file.'.'.$stat[9];
163                 if (!rename($dst_path.$src_file,$new_name)) {
164                     echo "unable to rename $dst_path$src_file to $new_name\n";
165                     return false;
166                 }
167                 if(!$quiet) echo "\trenamed $src_file to ".($src_file.'.'.$stat[9])."\n";
168             }
169
170             //  Destination file does not exist.  Create it
171             if (!copy_file($src_path . $src_file, $dst_path . $src_file)) {
172                 return false;
173             }
174
175             //  Log files need to be world writeable
176             if (substr($src_file,-4,4) == '.log') {
177                 chmod($dst_path . $src_file, 0666);
178             }
179
180             // Generator & Console needs to be executable
181             if ($src_file == 'generate.php' || $src_file == 'console.php') {
182                 chmod($dst_path . $src_file, 0754);
183             }           
184
185             if(!$quiet) echo "\tcreate $dst_path$src_file\n";
186         } else {
187
188             //  This is a directory.  Ignore '.' and '..'
189             if ( ($src_file == '.') || ($src_file == '..') ) {
190                 continue;
191             }
192
193             //  This directory needs to be copied.
194             if (!create_dir( $dst_path . $src_file )) {
195                 return false;
196             }
197
198             //  Recursive call to copy directory
199             if (!copy_dir($src_path . $src_file . DIRECTORY_SEPARATOR,
200                           $dst_path . $src_file . DIRECTORY_SEPARATOR)) {
201                 return false;
202             }
203         }
204     }
205     closedir($src_handle);
206     return true;
207 }
208
209 /**
210  *  Create a directory if it doesn't exist
211  *  @param string $dst_dir  Path of directory to create
212  *  @param string $perms Chmod permissions (0775)
213  *  @return boolean  true=>success, false=>failed
214  */
215 function create_dir($dst_dir) {
216     
217     global $quiet;
218
219     //  Does a directory of this name exist?
220     if (file_exists( $dst_dir )) {
221
222         //  A destination file or directory with this name exists
223         if (is_dir( $dst_dir )) {
224
225             //  A destination directory with this name exists.
226             if(!$quiet) echo "\texists $dst_dir\n";
227             return true;
228         }
229
230         //  There is an old destination file with the same
231         //  name as the new destination directory.
232         //  Save the old file.
233         $stat = stat($dst_dir);
234         $new_name = $dst_dir.'.'.$stat[9];
235         if (!rename($dst_dir, $new_name)) {
236             echo "unable to rename $dst_dir to $new_name\n";
237             return false;
238         }
239         if(!$quiet) echo "\trenamed $dst_dir to $new_name\n";
240     }
241
242     //  Destination directory does not exist.  Create it
243     if (!mkdir($dst_dir, 0775, true)) {
244         return false;
245     }
246     if(!$quiet) echo "\tcreate ".$dst_dir."\n";
247     return true;
248 }
249
250 /**
251  *  Copy a Trax file into user's work area, substituting @TRAX-...@
252  *
253  *  @param string $src_path  Path to source file
254  *  @param string $dst_path  Path to destination file
255  *  @return boolean true=>success, false=>failure.
256  */
257 function copy_file($src_path, $dst_path) {
258
259     global $search, $replace;
260
261     //  Read source file into a string
262     if (!$file = file_get_contents($src_path)) {
263         return false;
264     }
265
266     //  Substitute @TRAX-...@ symbols for appropriate values
267     $file = preg_replace($search, $replace, $file);
268
269     //  Write out file contents
270     @file_put_contents($dst_path, $file);
271     return true;
272 }
273
274 /**
275  *  Output a Usage message and exit
276  */
277 function usage() {
278     echo "Usage: @BIN-DIR@".DIRECTORY_SEPARATOR."trax"
279         ." ".DIRECTORY_SEPARATOR."path".DIRECTORY_SEPARATOR."to"
280         .DIRECTORY_SEPARATOR."your".DIRECTORY_SEPARATOR."app [options]
281
282 Options:
283     -q, --quiet            Suppress normal output.
284     -v, --version            Show the Trax version number and quit.
285     -h, --help            Show this help message and quit.
286
287 Description:
288     The 'trax' command creates a new Trax application with a default
289     directory structure and configuration at the path you specify.
290
291 Example:
292     trax ".DIRECTORY_SEPARATOR."var".DIRECTORY_SEPARATOR."www"
293         .DIRECTORY_SEPARATOR."html
294
295     This generates a skeletal Trax installation in "
296     .DIRECTORY_SEPARATOR."var".DIRECTORY_SEPARATOR."www"
297     .DIRECTORY_SEPARATOR."html.
298     See the README in the newly created application to get going.
299 \n";
300     exit;
301 }
302
303 /**
304  *  Main program
305  */
306 trax();
307
308 // -- set Emacs parameters --
309 // Local variables:
310 // mode: php
311 // tab-width: 4
312 // c-basic-offset: 4
313 // c-hanging-comment-ender-p: nil
314 // indent-tabs-mode: nil
315 // End:
316
317 ?>
Note: See TracBrowser for help on using the browser.