1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Console; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Exception\NativeException; |
6
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
7
|
|
|
use Ffcms\Core\Helper\Type\Str; |
8
|
|
|
use Ffcms\Core\Properties; |
9
|
|
|
use Ffcms\Console\Transfer\Input; |
10
|
|
|
use Ffcms\Console\Transfer\Output; |
11
|
|
|
use \Illuminate\Database\Capsule\Manager as Capsule; |
12
|
|
|
|
13
|
|
|
class App |
14
|
|
|
{ |
15
|
|
|
/** @var \Ffcms\Core\Properties */ |
16
|
|
|
public static $Properties; |
17
|
|
|
/** @var \Ffcms\Console\Transfer\Input */ |
18
|
|
|
public static $Input; |
19
|
|
|
/** @var \Ffcms\Console\Transfer\Output */ |
20
|
|
|
public static $Output; |
21
|
|
|
/** @var \Illuminate\Database\Capsule\Manager */ |
22
|
|
|
public static $Database; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Build console entry point |
26
|
|
|
* @param array|null $services |
27
|
|
|
*/ |
28
|
|
|
public static function init(array $services = null) |
29
|
|
|
{ |
30
|
|
|
self::$Properties = new Properties(); |
31
|
|
|
self::$Input = new Input(); |
32
|
|
|
self::$Output = new Output(); |
33
|
|
|
|
34
|
|
|
// establish database link |
35
|
|
|
if (Obj::isArray(self::$Properties->get('database')) && (isset($services['Database']) && $services['Database'] === true || $services === null)) { |
36
|
|
|
self::$Database = new Capsule; |
37
|
|
|
self::$Database->addConnection(self::$Properties->get('database')); |
38
|
|
|
|
39
|
|
|
// Make this Capsule instance available globally via static methods... (optional) |
40
|
|
|
self::$Database->setAsGlobal(); |
41
|
|
|
|
42
|
|
|
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) |
43
|
|
|
self::$Database->bootEloquent(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Build console controllers. |
49
|
|
|
* php console.php Controller/Action index |
50
|
|
|
*/ |
51
|
|
|
public static function run() |
52
|
|
|
{ |
53
|
|
|
global $argv; |
|
|
|
|
54
|
|
|
$output = null; |
55
|
|
|
if (!Obj::isArray($argv) || Str::likeEmpty($argv[1])) { |
56
|
|
|
$output = 'Console command is unknown! Type "console main/help" to get help guide'; |
57
|
|
|
} else { |
58
|
|
|
$controller_action = $argv[1]; |
59
|
|
|
$arrInput = explode('/', $controller_action); |
60
|
|
|
$controller = ucfirst(strtolower($arrInput[0])); |
61
|
|
|
$action = ucfirst(strtolower($arrInput[1])); |
62
|
|
|
if($action == null) { |
63
|
|
|
$action = 'Index'; |
64
|
|
|
} |
65
|
|
|
// set action and id |
66
|
|
|
$action = 'action' . $action; |
67
|
|
|
$id = null; |
68
|
|
|
if (isset($argv[2])) { |
69
|
|
|
$id = $argv[2]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$controller_path = '/Apps/Controller/' . env_name . '/' . $controller . '.php'; |
74
|
|
|
if(file_exists(root . $controller_path) && is_readable(root . $controller_path)) { |
75
|
|
|
include_once(root . $controller_path); |
76
|
|
|
$cname = 'Apps\\Controller\\' . env_name . '\\' . $controller; |
77
|
|
|
if(class_exists($cname)) { |
78
|
|
|
$load = new $cname; |
79
|
|
|
if(method_exists($cname, $action)) { |
80
|
|
|
if($id !== null) { |
81
|
|
|
$output = @$load->$action($id); |
82
|
|
|
} else { |
83
|
|
|
$output = @$load->$action(); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
throw new NativeException('Method ' . $action . '() not founded in ' . $cname . ' in file {root}' . $controller_path); |
87
|
|
|
} |
88
|
|
|
unset($load); |
89
|
|
|
} else { |
90
|
|
|
throw new NativeException('Namespace\\Class - ' . $cname . ' not founded in {root}' . $controller_path); |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
throw new NativeException('Controller not founded: {root}' . $controller_path); |
94
|
|
|
} |
95
|
|
|
} catch(NativeException $e) { |
96
|
|
|
$e->display($e->getMessage()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return App::$Output->write($output); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state