source: trunk/docs/examples/example.cli.php @ 612

Last change on this file since 612 was 612, checked in by anonymous, 7 years ago

Change hashbang to use env php

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1#!/usr/bin/env php
2<?php
3/**
4 * The Strangecode Codebase - a general application development framework for PHP
5 * For details visit the project site: <http://trac.strangecode.com/codebase/>
6 * Copyright 2001-2012 Strangecode, LLC
7 *
8 * This file is part of The Strangecode Codebase.
9 *
10 * The Strangecode Codebase is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by the
12 * Free Software Foundation, either version 3 of the License, or (at your option)
13 * any later version.
14 *
15 * The Strangecode Codebase is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25* example.cli.php - An example of a command-line interface script using the Strangecode Codebase.
26*
27*
28* @author   Quinn Comendant <quinn@strangecode.com>
29* @version  1.0
30* @since    28 Oct 2008 16:44:24
31*/
32
33
34/********************************************************************
35* STARTUP
36********************************************************************/
37
38$this_script = basename($_SERVER['argv'][0]);
39
40// First things first. Define the globally used directory paths.
41// The parent directory of all application DocRoots.
42define('COMMON_BASE', realpath(dirname(__FILE__) . '/../'));
43
44// Set include path for all templates and libraries.
45ini_set('include_path', get_include_path()
46    . PATH_SEPARATOR . COMMON_BASE
47);
48
49
50/********************************************************************
51* CONFIG
52********************************************************************/
53
54// This will disable sessions and things not needed by a cli script.
55define('_CLI', true);
56
57// Include core libraries.
58require_once 'codebase/lib/App.inc.php';
59require_once 'codebase/lib/Utilities.inc.php';
60
61// Primary application class.
62$app =& App::getInstance('cli');
63$app->setParam(array(
64    'site_name' => 'CLI Script',
65    'site_email' => 'codebase@strangecode.com',
66    'enable_session' => false,
67    'enable_db' => false,
68    'db_always_debug' => false,
69    'db_debug' => true,
70    'db_die_on_failure' => true,
71    'display_errors' => true,
72    'error_reporting' => E_ALL,
73    'log_file_priority' => LOG_INFO,
74    'log_screen_priority' => LOG_DEBUG,
75    'log_directory' => COMMON_BASE . '/log',
76    'log_filename' => 'cli_log',
77));
78
79// DB credentials for command line scripts stored in a file with read rights
80// given only to the user who will be executing the scripts: -rw-------
81// This file includes $app-> method calls so this must be included after App::getInstance().
82// require_once 'global/db_auth.inc.php';
83
84// Start application-based functionality: database, session, environment, ini setup, etc.
85// Most configuration parameters must be set before starting the App.
86$app->start();
87
88// Global DB object. Automatically pre-configured by $app->start().
89// $db =& DB::getInstance();
90
91
92/********************************************************************
93* MAIN
94********************************************************************/
95
96$options = getopt("habc");
97
98// Give them a fighting chance. Show the help message. ;P
99if (!$options) {
100    printf("%s: Syntax or usage error. Try '-h' if you are lost.\n", $this_script);
101    die(1);
102}
103
104if (isset($options['h'])) {
105    help();
106    die(1);
107}
108
109if (isset($options['a'])) {
110    // Run code associated with option -a
111    echo "Option a\n";
112}
113
114if (isset($options['b'])) {
115    // Run code associated with option -b
116    echo "Option b\n";
117}
118
119if (isset($options['c'])) {
120    // Run code associated with option -c
121    echo "Option c\n";
122}
123
124
125/********************************************************************
126* FUNCTIONS
127********************************************************************/
128
129function help()
130{
131    global $this_script;
132
133    ?>
134Description of this script goes here...
135
136Usage: <?php echo $this_script; ?> [OPTIONS]
137
138OPTIONS:
139
140    -a  Blah blah blah
141    -b  Blah blah blah
142    -c  Blah blah blah
143
144Strangecode :: www.strangecode.com
145<?php
146}
147
148
149
150
Note: See TracBrowser for help on using the repository browser.