-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathunit.php
More file actions
127 lines (118 loc) · 3.46 KB
/
unit.php
File metadata and controls
127 lines (118 loc) · 3.46 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace KVSun\Unit
{
const ERROR_LOG = './errors.log';
if (PHP_SAPI !== 'cli') {
http_response_code(503);
exit();
} elseif (version_compare(\PHP_VERSION, '7.0.0', '<')) {
echo 'PHP version 7 or greater is required.' . PHP_EOL;
exit(1);
}
/**
* Function to register in `set_error_handler`, throws an `\ErrorException`
* @param integeer $errno Error level
* @param String $errstr Error message
* @param String $errfile File the error occured in
* @param integer $errline Line in $errfile
* @param array $errcontext Variables defined in the scope of error
* @return Bool True prevent default error handler
*/
function error_handler(
Int $errno,
String $errstr,
String $errfile = null,
Int $errline = null,
Array $errcontext = array()
) : Bool
{
exception_handler(new \ErrorException($errstr, 0, $errno, $errfile, $errline));
return true;
}
/**
* Function to register in `set_exception_handler`. Logs and echoes exception, then exits
* @param Throwable $exc The error or exception
* @return void
*/
function exception_handler(\Throwable $exc)
{
error_log($exc . PHP_EOL, 3, ERROR_LOG);
echo $exc;
// exit(1);
}
/**
* Recursively lint a directory
* @param String $dir Directory to lint
* @param Array $exts Array of extensions to lint in directory
* @param Array $ignore_dirs Ignore directories in this array
* @param Callable $error_callback Callback to call when linting fails
* @return Bool Whether or not all files linted without errors
* @see https://secure.php.net/manual/en/class.recursiveiteratoriterator.php
*/
function lint_dir(
String $dir = __DIR__,
Array $exts = ['php', 'phtml'],
Array $ignore_dirs = ['.git', 'node_modules', 'vendor'],
Callable $error_callback = null
): Bool
{
$path = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
ob_start();
while ($path->valid()) {
if ($path->isFile() and in_array($path->getExtension(), $exts)) {
$output = [];
$msg = @exec(
sprintf("php -l %s", escapeshellarg($path->getPathName())),
$output,
$return_var
);
if ($return_var !== 0) {
if (isset($error_callback)) {
$error_callback($msg);
return false;
} else {
throw new \ParseError($msg);
}
}
} elseif ($path->isDir() and ! in_array($path, $ignore_dirs)) {
// So long as $dir is the first argument of the function, this will
// always work, even if the name of the function changes.
$args = array_slice(func_get_args(), 1);
if (! call_user_func(__FUNCTION__, $path->getPathName(), ...$args)) {
return false;
}
}
$path->next();
}
ob_get_clean();
return true;
}
// Setup autoloading
set_include_path(realpath('./classes') . PATH_SEPARATOR . get_include_path());
spl_autoload_register('spl_autoload');
// Set error and exception handlers
set_error_handler(__NAMESPACE__ . '\error_handler', E_ALL);
set_exception_handler(__NAMESPACE__ . '\exception_handler');
// Set asser options
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 1);
assert_options(ASSERT_BAIL, 1);
assert(lint_dir(
__DIR__,
['php'], [
'.git',
'node_modules',
'vendor',
'scripts',
'stylesheets',
'fonts',
'custom-fonts',
'config',
'images',
'keys',
'rss',
]),
'Lint PHP scripts'
);
echo 'All test completed.';
}