-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathCommand.php
138 lines (120 loc) · 2.89 KB
/
Command.php
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
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* This file is part of the CLIFramework package.
*
* (c) Yo-An Lin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CLIFramework;
use Exception;
use CLIFramework\CommandInterface;
use CLIFramework\Exception\CommandClassNotFoundException;
use CLIFramework\Application;
/**
* abstract command class
*
*/
abstract class Command extends CommandBase implements CommandInterface
{
/**
* @var CLIFramework\Application Application object.
*/
public $application;
public $name;
public function __construct(CommandBase $parent = null)
{
parent::__construct($parent);
}
public function setApplication(Application $application)
{
$this->application = $application;
}
/**
* Get the main application object from parents
*
* @return Application
*/
public function getApplication()
{
if ($this->application) {
return $this->application;
}
$p = $this->parent;
while (true) {
if (! $p) {
return null;
}
if ($p instanceof Application) {
return $p;
}
$p = $p->parent;
}
}
public function hasApplication()
{
return $this->getApplication() !== null;
}
public function setName($name)
{
$this->name = $name;
}
/**
* Translate current class name to command name.
*
* @return string command name
*/
public function getName()
{
if ($this->name) {
return $this->name;
}
// Extract command name from the class name.
$class = get_class($this);
// strip command suffix
$parts = explode('\\', $class);
$class = end($parts);
$class = preg_replace('/Command$/', '', $class);
return strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '-\1', $class));
}
/**
* Returns logger object.
*
* @return CLIFramework\Logger
*/
public function getLogger()
{
return $this->getApplication()->getLogger();
}
/**
* Returns text style formatter.
*
* @return CLIFramework\Formatter
*/
public function getFormatter()
{
return $this->getApplication()->getFormatter();
}
/**
* User may register their aliases
*/
public function aliases()
{
return array();
}
/**
* Provide a shorthand property for retrieving logger object.
*
* @param string $k property name
*/
public function __get($k)
{
if ($k === 'logger') {
return $this->getLogger();
} elseif ($k === 'formatter') {
return $this->getFormatter();
}
throw new Exception("$k is not defined.");
}
}