Completed
Push — master ( 503636...a81f54 )
by Anton
09:53 queued 06:00
created

cli.php ➔ errorDisplay()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 0
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 67 and the first side effect is on line 16.

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.

Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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
    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);
0 ignored issues
show
Coding Style Compatibility introduced by
The function errorDisplay() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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