1 | #!/usr/bin/php |
---|
2 | <?php |
---|
3 | /* |
---|
4 | * example.cli.php - An example of a command-line interface script using the Strangecode Codebase. |
---|
5 | * |
---|
6 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information. |
---|
7 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
8 | * @version 1.0 |
---|
9 | * @since 28 Oct 2008 16:44:24 |
---|
10 | */ |
---|
11 | |
---|
12 | |
---|
13 | /******************************************************************** |
---|
14 | * STARTUP |
---|
15 | ********************************************************************/ |
---|
16 | |
---|
17 | $this_script = basename($_SERVER['argv'][0]); |
---|
18 | |
---|
19 | // First things first. Define the globally used directory paths. |
---|
20 | // The parent directory of all application DocRoots, such as /home/user/www.example.com/ |
---|
21 | define('COMMON_BASE', realpath('../..')); |
---|
22 | |
---|
23 | // Set include path for all templates and libraries. |
---|
24 | ini_set('include_path', get_include_path() |
---|
25 | . PATH_SEPARATOR . COMMON_BASE |
---|
26 | ); |
---|
27 | |
---|
28 | |
---|
29 | /******************************************************************** |
---|
30 | * CONFIG |
---|
31 | ********************************************************************/ |
---|
32 | |
---|
33 | // Include core libraries. |
---|
34 | require_once 'codebase/lib/App.inc.php'; |
---|
35 | require_once 'codebase/lib/Utilities.inc.php'; |
---|
36 | |
---|
37 | // Primary application class. |
---|
38 | $app =& App::getInstance('cli'); |
---|
39 | $app->setParam(array( |
---|
40 | 'site_name' => 'CLI Script', |
---|
41 | 'site_email' => 'codebase@strangecode.com', |
---|
42 | 'enable_session' => false, |
---|
43 | 'enable_db' => true, |
---|
44 | 'db_always_debug' => false, |
---|
45 | 'db_debug' => true, |
---|
46 | 'db_die_on_failure' => true, |
---|
47 | 'display_errors' => true, |
---|
48 | 'error_reporting' => E_ALL, |
---|
49 | 'log_file_priority' => LOG_INFO, |
---|
50 | 'log_screen_priority' => LOG_DEBUG, |
---|
51 | 'log_directory' => COMMON_BASE . '/log', |
---|
52 | 'log_filename' => 'cli_log', |
---|
53 | )); |
---|
54 | |
---|
55 | // DB credentials for command line scripts stored in a file with read rights |
---|
56 | // given only to the user who will be executing the scripts: -rw------- |
---|
57 | // This file includes $app-> method calls so this must be included after App::getInstance(). |
---|
58 | require_once 'global/db_auth.inc.php'; |
---|
59 | |
---|
60 | // Start application-based functionality: database, session, environment, ini setup, etc. |
---|
61 | // Most configuration parameters must be set before starting the App. |
---|
62 | $app->start(); |
---|
63 | |
---|
64 | // Global DB object. Automatically pre-configured by $app->start(). |
---|
65 | $db =& DB::getInstance(); |
---|
66 | |
---|
67 | |
---|
68 | /******************************************************************** |
---|
69 | * MAIN |
---|
70 | ********************************************************************/ |
---|
71 | |
---|
72 | $options = getopt("habc"); |
---|
73 | |
---|
74 | // Give them a fighting chance. Show the help message. ;P |
---|
75 | if (!$options) { |
---|
76 | printf("%s: Syntax or usage error. Try '-h' if you are lost.\n", $this_script); |
---|
77 | die(1); |
---|
78 | } |
---|
79 | |
---|
80 | if (isset($options['h'])) { |
---|
81 | help(); |
---|
82 | die(1); |
---|
83 | } |
---|
84 | |
---|
85 | if (isset($options['a'])) { |
---|
86 | // Run code associated with option -a |
---|
87 | echo "Option a\n"; |
---|
88 | } |
---|
89 | |
---|
90 | if (isset($options['b'])) { |
---|
91 | // Run code associated with option -b |
---|
92 | echo "Option b\n"; |
---|
93 | } |
---|
94 | |
---|
95 | if (isset($options['c'])) { |
---|
96 | // Run code associated with option -c |
---|
97 | echo "Option c\n"; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /******************************************************************** |
---|
102 | * FUNCTIONS |
---|
103 | ********************************************************************/ |
---|
104 | |
---|
105 | function help() |
---|
106 | { |
---|
107 | global $this_script; |
---|
108 | |
---|
109 | ?> |
---|
110 | Description of this script goes here... |
---|
111 | |
---|
112 | Usage: <?php echo $this_script; ?> [OPTIONS] |
---|
113 | |
---|
114 | OPTIONS: |
---|
115 | |
---|
116 | -a Blah blah blah |
---|
117 | -b Blah blah blah |
---|
118 | -c Blah blah blah |
---|
119 | |
---|
120 | Strangecode :: www.strangecode.com |
---|
121 | <?php |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | ?> |
---|