1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pimf |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf; |
10
|
|
|
|
11
|
|
|
use Pimf\Util\Character as Str; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A full featured package for managing command-line options and arguments, |
15
|
|
|
* it allows the developer to easily build complex command line interfaces. |
16
|
|
|
* |
17
|
|
|
* @package Pimf |
18
|
|
|
* @author Gjero Krsteski <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class Cli |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Prints out a list of CLI commands from the system, |
24
|
|
|
* which is defined at the controllers with the "CliAction()" suffix at the method-name. |
25
|
|
|
* |
26
|
|
|
* @param string $appClr Path to application controller repository |
27
|
|
|
* @param string $coreClr Path to core controller repository |
28
|
|
|
* @param string $root Path to home directory |
29
|
|
|
*/ |
30
|
|
|
public static function absorb($appClr = null, $coreClr = null, $root = null) |
31
|
|
|
{ |
32
|
|
|
echo PHP_EOL . 'PIMF v' . \Pimf\Application::VERSION . ' PHP Command Line Interface by Gjero Krsteski' . PHP_EOL; |
33
|
|
|
|
34
|
|
|
echo '+------------------------------------------------------+' . PHP_EOL; |
35
|
|
|
|
36
|
|
|
self::reflect(self::collect($appClr, $coreClr, $root)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $classes |
41
|
|
|
*/ |
42
|
|
|
public static function reflect(array $classes) |
43
|
|
|
{ |
44
|
|
|
array_map( |
45
|
|
|
function ($class) { |
46
|
|
|
|
47
|
|
|
$reflection = new \ReflectionClass($class); |
48
|
|
|
|
49
|
|
|
if ($reflection->isSubclassOf('\Pimf\Controller\Base')) { |
50
|
|
|
|
51
|
|
|
$methods = $reflection->getMethods(); |
52
|
|
|
$controller = explode('_', $class); |
53
|
|
|
|
54
|
|
|
echo 'controller: ' . strtolower(end($controller)) . '' . PHP_EOL; |
55
|
|
|
|
56
|
|
|
array_map( |
57
|
|
|
function (\ReflectionMethod $method) { |
58
|
|
|
if (false !== $command = strstr($method->name, 'CliAction', true)) { |
59
|
|
|
echo PHP_EOL . ' action: ' . $command . ' ' . PHP_EOL; |
60
|
|
|
} |
61
|
|
|
}, $methods |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
echo PHP_EOL . '+------------------------------------------------------+' . PHP_EOL; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
}, $classes |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $appClr |
73
|
|
|
* @param string $coreClr |
74
|
|
|
* @param string $root |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public static function collect($appClr = null, $coreClr = null, $root = null) |
79
|
|
|
{ |
80
|
|
|
$classes = array(); |
81
|
|
|
|
82
|
|
|
if (!$root && !$coreClr && !$appClr) { |
|
|
|
|
83
|
|
|
$coreClr = str_replace('/', DS, BASE_PATH . '/pimf-framework/core/Pimf/Controller/'); |
84
|
|
|
$appClr = str_replace('/', DS, BASE_PATH . '/app/' . Config::get('app.name') . '/Controller/'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
foreach (array($appClr, $coreClr) as $dir) { |
88
|
|
|
|
89
|
|
|
$iterator |
90
|
|
|
= new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)), |
91
|
|
|
'/^.+\.php$/i', |
92
|
|
|
\RecursiveRegexIterator::GET_MATCH); |
93
|
|
|
|
94
|
|
|
foreach (iterator_to_array($iterator, false) as $file) { |
95
|
|
|
$file = str_replace("\\", '/', current($file)); |
96
|
|
|
$file = str_replace('/', DS, $file); |
97
|
|
|
$name = str_replace( |
98
|
|
|
array(BASE_PATH . DS . 'pimf-framework' . DS . 'core' . DS, BASE_PATH . DS . 'app' . DS), '', $file |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$name = str_replace(DS, '\\', $name); |
102
|
|
|
$name = str_replace('.php', '', $name); |
103
|
|
|
$classes[] = '\\' . $name; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $classes; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $commands |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public static function parse(array $commands) |
116
|
|
|
{ |
117
|
|
|
$cli = array(); |
118
|
|
|
|
119
|
|
|
parse_str(implode('&', array_slice($commands, 1)), $cli); |
120
|
|
|
|
121
|
|
|
$command = current(array_keys((array)$cli, '')); |
122
|
|
|
|
123
|
|
|
if (Str::contains($command, ':')) { |
124
|
|
|
|
125
|
|
|
list($controller, $action) = explode(':', $command); |
126
|
|
|
|
127
|
|
|
$cli['controller'] = $controller; |
128
|
|
|
$cli['action'] = $action; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $cli; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: