forked from BrownBook/ExperientialLearningInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.php
executable file
·106 lines (82 loc) · 2.8 KB
/
cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/php
<?php
// Make sure this can only be run from the command line
if(php_sapi_name() !== 'cli'){
die('This script can only be run from the command line.');
}
// Expected arguments
$args = array('phpwsConfigPath'=>'',
'module' => '',
'className'=>'');
$switches = array();
// Process arguments into $args
processArgs($argc, $argv, $args, $switches);
// Try to include the main phpws config file
includePhpwsConfigFile($args['phpwsConfigPath']);
// Change current dir to the root of phpws' install
chdir(PHPWS_SOURCE_DIR);
// Define these to avoid errors in sanity checking later
$_SERVER['REQUEST_URI'] = 'cli.php';
$_SERVER['HTTP_HOST'] = 'localhost';
//$_SERVER['SERVER_NAME'] = 'aa.ess'; // NB: Pass SERVER_NAME="blah.blah" on the command line *before* the php executable
// Include PHPWS bootstrapping code
require_once PHPWS_SOURCE_DIR . 'config/core/source.php';
require_once PHPWS_SOURCE_DIR . 'src/Bootstrap.php';
// For older versions of PHPWS, comment this out
//require_once PHPWS_SOURCE_DIR . 'src/Autoloader.php';
// For older versions of PHPWS, uncomment these
//require_once PHPWS_SOURCE_DIR . 'core/conf/defines.php';
//require_once PHPWS_SOURCE_DIR . 'Global/Functions.php';
//require_once PHPWS_SOURCE_DIR . 'Global/Implementations.php';
//require_once PHPWS_SOURCE_DIR . 'config/core/source.php';
require_once PHPWS_SOURCE_DIR . 'src/Autoloader.php';
// Set the 'module' request variable to help the autoloader find classes
$_REQUEST['module'] = $args['module'];
// Try to include and run the specified file/function
try {
$className = $args['className'];
$className::cliExec();
}catch (\Exception $e) {
echo "Error:\n";
echo $e->getMessage();
print_r($e);
echo "\n\n";
}
function processArgs($argc, $argv, &$args, &$switches)
{
if($argc < count(array_keys($args)) + 1) {
echo "USAGE: php {$argv[0]}";
foreach(array_keys($switches) as $switch) {
echo " [$switch]";
}
foreach(array_keys($args) as $arg) {
echo " <$arg>";
}
echo "\n";
exit();
}
$args_keys = array_keys($args);
foreach($argv as $arg) {
if($arg == $argv[0]) continue;
if(in_array($arg, array_keys($switches))) {
$switches[$arg] = true;
continue;
}
if(substr($arg,0,1) == '-') {
echo "Ignoring unknown switch: $arg\n";
continue;
}
$args[current($args_keys)] = $arg;
next($args_keys);
}
}
function includePhpwsConfigFile($filePath)
{
if (!is_file($filePath)) {
exit("Configuration file not found: $filePath\n");
}
require_once $filePath;
if (!defined('PHPWS_DSN')) {
exit("Configuration file loaded, but database connection string (DSN) not found\n");
}
}