1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: |
|
15: |
|
16: | namespace LucidFrame\Console;
|
17: |
|
18: | |
19: | |
20: |
|
21: | class Console
|
22: | {
|
23: |
|
24: | protected $command;
|
25: |
|
26: | private $commandName;
|
27: |
|
28: | protected $argc;
|
29: |
|
30: | protected $argv;
|
31: |
|
32: | protected static $commands = array();
|
33: |
|
34: | |
35: | |
36: |
|
37: | public function __construct()
|
38: | {
|
39: | global $argv;
|
40: |
|
41: | $this->argv = array_slice($argv, 1);
|
42: | $this->argc = count($this->argv) - 1;
|
43: | $this->commandName = array_shift($this->argv);
|
44: | $this->command = $this->getCommand($this->commandName);
|
45: |
|
46: | $this->execute();
|
47: | }
|
48: |
|
49: | |
50: | |
51: | |
52: | |
53: |
|
54: | public static function registerCommand(Command $command)
|
55: | {
|
56: | self::$commands[$command->getName()] = $command;
|
57: | }
|
58: |
|
59: | |
60: | |
61: | |
62: |
|
63: | public static function getCommands()
|
64: | {
|
65: | return self::$commands;
|
66: | }
|
67: |
|
68: | |
69: | |
70: | |
71: | |
72: |
|
73: | public function hasCommand($name)
|
74: | {
|
75: | return array_key_exists($name, self::$commands);
|
76: | }
|
77: |
|
78: | |
79: | |
80: | |
81: | |
82: |
|
83: | public function getCommand($name)
|
84: | {
|
85: | return $this->hasCommand($name) ? self::$commands[$name] : null;
|
86: | }
|
87: |
|
88: | |
89: | |
90: |
|
91: | private function execute()
|
92: | {
|
93: | _writeln('PHPLucidFrame %s by Sithu K.', _version());
|
94: | _writeln();
|
95: |
|
96: | if ($this->command instanceof Command) {
|
97: | $this->command->run($this->argv);
|
98: | } else {
|
99: | if (!$this->command && $this->commandName && !in_array($this->commandName, array('-V', '--version'))) {
|
100: | _writeln('Command "%s" not found.', $this->commandName);
|
101: | } else {
|
102: | if (empty($this->command) || in_array($this->command, array('-V', '--version'))) {
|
103: | _writeln(_version());
|
104: | _writeln('PHP Version: %s', phpversion());
|
105: | _writeln('The MIT License');
|
106: | _writeln('Simple, lightweight & yet powerful PHP Application Framework');
|
107: | _writeln('Copyright (c) 2014-%d, phplucidframe.com', date('Y'));
|
108: | } else {
|
109: | _writeln('Command "%s" not found.', $this->commandName);
|
110: | }
|
111: | }
|
112: | }
|
113: | }
|
114: | }
|
115: | |