|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* CLI file |
|
4
|
|
|
* |
|
5
|
|
|
* @author C.O. |
|
6
|
|
|
* @created 14.11.12 13:20 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @namespace |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Application; |
|
13
|
|
|
|
|
14
|
|
|
// Check CLI |
|
15
|
|
|
if (PHP_SAPI !== 'cli') { |
|
16
|
|
|
exit; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
// Get CLI arguments |
|
20
|
|
|
$arguments = getopt( |
|
21
|
|
|
"u:e::dlh", |
|
22
|
|
|
[ |
|
23
|
|
|
"uri:", // required |
|
24
|
|
|
"env::", // optional |
|
25
|
|
|
"debug", // just flag |
|
26
|
|
|
"help" // display help |
|
27
|
|
|
] |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
// Check help |
|
31
|
|
|
if (array_key_exists('h', $arguments) || array_key_exists('help', $arguments)) { |
|
32
|
|
|
echo "Option `--uri` is required, it's similar to browser query\n"; |
|
33
|
|
|
echo "Use `--env` option for setup application environment\n"; |
|
34
|
|
|
echo "Use `--debug` flag for receive more information\n"; |
|
35
|
|
|
echo "Example:\n"; |
|
36
|
|
|
echo "\tphp ./bin/cli.php --uri '/index/index/?foo=bar'\n"; |
|
37
|
|
|
echo "\tphp ./bin/cli.php --uri '/index/index/?foo=bar' --env='dev' --debug\n"; |
|
38
|
|
|
echo "Example of short syntax:\n"; |
|
39
|
|
|
echo "\tphp ./bin/cli.php -u '/index/index/?foo=bar'\n"; |
|
40
|
|
|
echo "\tphp ./bin/cli.php -u '/index/index/?foo=bar' -e='dev' -d\n"; |
|
41
|
|
|
exit(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Check URI option |
|
45
|
|
View Code Duplication |
if (!array_key_exists('u', $arguments) && !array_key_exists('uri', $arguments)) { |
|
|
|
|
|
|
46
|
|
|
echo "Option `--uri` is required\n"; |
|
47
|
|
|
echo "Use `--help` flag for show help notices\n"; |
|
48
|
|
|
exit(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Check and setup environment |
|
52
|
|
|
if (array_key_exists('e', $arguments) || array_key_exists('env', $arguments)) { |
|
53
|
|
|
putenv('BLUZ_ENV='. (isset($arguments['e'])?$arguments['e']:$arguments['env'])); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Check and setup log save |
|
57
|
|
|
if (array_key_exists('l', $arguments) || array_key_exists('log', $arguments)) { |
|
58
|
|
|
putenv('BLUZ_LOG=1'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Debug mode for development environment only |
|
62
|
|
|
if (array_key_exists('d', $arguments) || array_key_exists('debug', $arguments)) { |
|
63
|
|
|
putenv('BLUZ_DEBUG=1'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Display error |
|
67
|
|
|
function errorDisplay() { |
|
68
|
|
|
$e = error_get_last(); |
|
69
|
|
View Code Duplication |
if (!is_array($e) |
|
|
|
|
|
|
70
|
|
|
|| !in_array($e['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR))) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
echo "Application Error\n"; |
|
74
|
|
|
if (getenv('BLUZ_DEBUG')) { |
|
75
|
|
|
echo $e['message']."\n"; |
|
76
|
|
|
echo $e['file'] ."#". $e['line'] ."\n"; |
|
77
|
|
|
} |
|
78
|
|
|
// try to write log |
|
79
|
|
|
errorLog($e['type'], $e['message'], $e['file'] ."#". $e['line']); |
|
80
|
|
|
exit(1); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Shutdown function for handle critical errors |
|
84
|
|
|
register_shutdown_function('\\Application\\errorDisplay'); |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
// Try to run application |
|
88
|
|
|
try { |
|
89
|
|
|
/** |
|
90
|
|
|
* @var \Composer\Autoload\ClassLoader $loader |
|
91
|
|
|
* @see http://getcomposer.org/apidoc/master/Composer/Autoload/ClassLoader.html |
|
92
|
|
|
*/ |
|
93
|
|
|
require_once dirname(__DIR__) . '/vendor/autoload.php'; |
|
94
|
|
|
|
|
95
|
|
|
// Error handler for log other errors |
|
96
|
|
|
set_error_handler('\\Application\\errorLog', E_ALL); |
|
97
|
|
|
|
|
98
|
|
|
// Environment |
|
99
|
|
|
$env = getenv('BLUZ_ENV')?:'production'; |
|
100
|
|
|
|
|
101
|
|
|
$app = CliBootstrap::getInstance(); |
|
102
|
|
|
$app->init($env); |
|
103
|
|
|
$app->run(); |
|
104
|
|
|
} catch (Exception $e) { |
|
105
|
|
|
echo "Application Exception\n"; |
|
106
|
|
|
if (getenv('BLUZ_DEBUG')) { |
|
107
|
|
|
echo strip_tags($e->getMessage())."\n\n"; |
|
108
|
|
|
echo "# --- \n"; |
|
109
|
|
|
echo $e->getTraceAsString()."\n"; |
|
110
|
|
|
echo "# --- \n"; |
|
111
|
|
|
} else { |
|
112
|
|
|
echo "Use `--help` flag for show help notices\n"; |
|
113
|
|
|
echo "Use `--debug` flag for receive more information\n"; |
|
114
|
|
|
} |
|
115
|
|
|
// try to write log |
|
116
|
|
|
errorLog(E_USER_ERROR, $e->getMessage()); |
|
117
|
|
|
exit(1); |
|
118
|
|
|
} |
|
119
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.