1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Scabbia2 Tasks Component |
4
|
|
|
* https://github.com/eserozvataf/scabbia2 |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/eserozvataf/scabbia2-tasks for the canonical source repository |
10
|
|
|
* @copyright 2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/) |
11
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Scabbia\Tasks; |
15
|
|
|
|
16
|
|
|
use Scabbia\Formatters\FormatterInterface; |
17
|
|
|
use Scabbia\Formatters\Formatters; |
18
|
|
|
use Scabbia\Tasks\TaskBase; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A small command line implementation which helps us during the development of |
22
|
|
|
* Scabbia2 PHP Framework's itself and related production code |
23
|
|
|
* |
24
|
|
|
* @package Scabbia\Tasks |
25
|
|
|
* @author Eser Ozvataf <[email protected]> |
26
|
|
|
* @since 2.0.0 |
27
|
|
|
*/ |
28
|
|
|
class Tasks |
29
|
|
|
{ |
30
|
|
|
/** @type string base class */ |
31
|
|
|
const BASE_CLASS = "\\Scabbia\\Tasks\\TaskBase"; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** @type array task namespaces */ |
35
|
|
|
public static $namespaces = [ |
36
|
|
|
"Scabbia" |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Resolves given task name into class name |
42
|
|
|
* |
43
|
|
|
* @param string $uTask task name |
44
|
|
|
* |
45
|
|
|
* @return string resolved class name |
46
|
|
|
*/ |
47
|
|
|
public static function taskClassName($uTask) |
48
|
|
|
{ |
49
|
|
|
$tOutput = "\\"; |
50
|
|
|
$tCapital = true; |
51
|
|
|
|
52
|
|
|
for ($tPos = 0, $tLen = strlen($uTask); $tPos < $tLen; $tPos++) { |
53
|
|
|
$tChar = $uTask[$tPos]; |
54
|
|
|
|
55
|
|
|
if ($tChar === ":") { |
56
|
|
|
$tOutput .= "\\"; |
57
|
|
|
$tCapital = true; |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($tCapital) { |
62
|
|
|
$tOutput .= strtoupper($tChar); |
63
|
|
|
$tCapital = false; |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$tOutput .= $tChar; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$tClassName = "{$tOutput}Task"; |
71
|
|
|
if (is_subclass_of($tClassName, self::BASE_CLASS)) { |
|
|
|
|
72
|
|
|
return $tClassName; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
foreach (self::$namespaces as $tNamespace) { |
76
|
|
|
$tClassName = "\\{$tNamespace}{$tOutput}Task"; |
77
|
|
|
|
78
|
|
|
if (is_subclass_of($tClassName, self::BASE_CLASS)) { |
|
|
|
|
79
|
|
|
return $tClassName; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Runs given task |
88
|
|
|
* |
89
|
|
|
* @param string $uTask name of the task |
90
|
|
|
* @param array $uParameters set of parameters |
91
|
|
|
* @param FormatterInterface $uFormatter formatter class |
92
|
|
|
* |
93
|
|
|
* @return int exit code |
94
|
|
|
*/ |
95
|
|
|
public static function run($uTask, array $uParameters, $uFormatter = null) |
96
|
|
|
{ |
97
|
|
|
if ($uFormatter === null) { |
98
|
|
|
$uFormatter = Formatters::getCurrent(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($tHelpCommand = ($uTask === "help")) { |
102
|
|
|
$uTask = array_shift($uParameters); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($uTask === null) { |
106
|
|
|
$uFormatter->write("Please specify a task"); |
107
|
|
|
return 1; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$tClassName = self::taskClassName($uTask); |
111
|
|
|
if ($tClassName === null) { |
112
|
|
|
$uFormatter->write(sprintf("Task not found - %s", $uTask)); |
113
|
|
|
return 1; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
$tInstance = new $tClassName (); |
118
|
|
|
if ($tHelpCommand) { |
119
|
|
|
$tInstance->help($uFormatter); |
120
|
|
|
return 0; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $tInstance->executeTask($uParameters, $uFormatter); |
124
|
|
|
} catch (\Exception $ex) { |
125
|
|
|
$uFormatter->write(sprintf("%s: %s", get_class($ex), $ex->getMessage())); |
126
|
|
|
return 1; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|