|
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
|
|
|
// Try to run application |
|
67
|
|
|
try { |
|
68
|
|
|
/** |
|
69
|
|
|
* @var \Composer\Autoload\ClassLoader $loader |
|
70
|
|
|
* @see http://getcomposer.org/apidoc/master/Composer/Autoload/ClassLoader.html |
|
71
|
|
|
*/ |
|
72
|
|
|
require_once dirname(__DIR__) . '/vendor/autoload.php'; |
|
73
|
|
|
|
|
74
|
|
|
// Error handler for log all errors |
|
75
|
|
|
set_error_handler('\\Application\\errorHandler', E_ALL); |
|
76
|
|
|
|
|
77
|
|
|
// Environment |
|
78
|
|
|
$env = getenv('BLUZ_ENV')?:'production'; |
|
79
|
|
|
|
|
80
|
|
|
$app = CliBootstrap::getInstance(); |
|
81
|
|
|
$app->init($env); |
|
82
|
|
|
$app->run(); |
|
83
|
|
|
} catch (\Throwable $e) { |
|
84
|
|
|
echo "Application Exception\n"; |
|
85
|
|
|
if (getenv('BLUZ_DEBUG')) { |
|
86
|
|
|
echo strip_tags($e->getMessage())."\n\n"; |
|
87
|
|
|
echo "# --- \n"; |
|
88
|
|
|
echo $e->getTraceAsString()."\n"; |
|
89
|
|
|
echo "# --- \n"; |
|
90
|
|
|
} else { |
|
91
|
|
|
echo "Use `--help` flag for show help notices\n"; |
|
92
|
|
|
echo "Use `--debug` flag for receive more information\n"; |
|
93
|
|
|
} |
|
94
|
|
|
// try to write log |
|
95
|
|
|
errorLog($e); |
|
96
|
|
|
exit(1); |
|
97
|
|
|
} |
|
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.