1: <?php
2: /**
3: * This file is part of the PHPLucidFrame library.
4: * Command-line code generation utility to automate programmer tasks.
5: *
6: * @package PHPLucidFrame\Console
7: * @since PHPLucidFrame v 1.11.0
8: * @copyright Copyright (c), PHPLucidFrame.
9: * @link http://phplucidframe.com
10: * @license http://www.opensource.org/licenses/mit-license.php MIT License
11: *
12: * This source file is subject to the MIT license that is bundled
13: * with this source code in the file LICENSE
14: */
15:
16: namespace LucidFrame\Console;
17:
18: /**
19: * Command-line code generation utility to automate programmer tasks.
20: */
21: class Console
22: {
23: /** @var LucidFrame\Console\Command The command being run */
24: protected $command;
25: /** @var string The command name */
26: private $commandName;
27: /** @var integer No of arguments passed to script */
28: protected $argc;
29: /** @var array Array of arguments passed to script */
30: protected $argv;
31: /** @var array list of registered commands */
32: protected static $commands = array();
33:
34: /**
35: * Constructor
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: * Register a command
51: * @param LucidFrame\Console\Command $command
52: * @return void
53: */
54: public static function registerCommand(\LucidFrame\Console\Command $command)
55: {
56: self::$commands[$command->getName()] = $command;
57: }
58:
59: /**
60: * Get all registered commands
61: * @return array Array of LucidFrame\Console\Command
62: */
63: public static function getCommands()
64: {
65: return self::$commands;
66: }
67:
68: /**
69: * Check a command name is already registered
70: * @param string $name The command name
71: * @return boolean
72: */
73: public function hasCommand($name)
74: {
75: return array_key_exists($name, self::$commands);
76: }
77:
78: /**
79: * Get the command by name
80: * @param string $name The command name
81: * @return object|null LucidFrame\Console\Command
82: */
83: public function getCommand($name)
84: {
85: return $this->hasCommand($name) ? self::$commands[$name] : null;
86: }
87:
88: /**
89: * Execute the current command
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: