1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: namespace Cake\Shell;
17:
18: use Cake\Console\Shell;
19: use Cake\Core\Configure;
20:
21: 22: 23:
24: class ServerShell extends Shell
25: {
26:
27: 28: 29: 30: 31:
32: const DEFAULT_HOST = 'localhost';
33:
34: 35: 36: 37: 38:
39: const DEFAULT_PORT = 8765;
40:
41: 42: 43: 44: 45:
46: protected $_host = self::DEFAULT_HOST;
47:
48: 49: 50: 51: 52:
53: protected $_port = self::DEFAULT_PORT;
54:
55: 56: 57: 58: 59:
60: protected $_documentRoot = WWW_ROOT;
61:
62: 63: 64: 65: 66:
67: protected $_iniPath = '';
68:
69: 70: 71: 72: 73: 74: 75: 76: 77: 78:
79: public function startup()
80: {
81: if ($this->param('host')) {
82: $this->_host = $this->param('host');
83: }
84: if ($this->param('port')) {
85: $this->_port = (int)$this->param('port');
86: }
87: if ($this->param('document_root')) {
88: $this->_documentRoot = $this->param('document_root');
89: }
90: if ($this->param('ini_path')) {
91: $this->_iniPath = $this->param('ini_path');
92: }
93:
94:
95: if (substr($this->_documentRoot, -1, 1) === DIRECTORY_SEPARATOR) {
96: $this->_documentRoot = substr($this->_documentRoot, 0, strlen($this->_documentRoot) - 1);
97: }
98: if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_documentRoot, $m)) {
99: $this->_documentRoot = $m[1] . '\\' . $m[2];
100: }
101:
102: $this->_iniPath = rtrim($this->_iniPath, DIRECTORY_SEPARATOR);
103: if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_iniPath, $m)) {
104: $this->_iniPath = $m[1] . '\\' . $m[2];
105: }
106:
107: parent::startup();
108: }
109:
110: 111: 112: 113: 114:
115: protected function _welcome()
116: {
117: $this->out();
118: $this->out(sprintf('<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
119: $this->hr();
120: $this->out(sprintf('App : %s', APP_DIR));
121: $this->out(sprintf('Path: %s', APP));
122: $this->out(sprintf('DocumentRoot: %s', $this->_documentRoot));
123: $this->out(sprintf('Ini Path: %s', $this->_iniPath));
124: $this->hr();
125: }
126:
127: 128: 129: 130: 131:
132: public function main()
133: {
134: $command = sprintf(
135: 'php -S %s:%d -t %s',
136: $this->_host,
137: $this->_port,
138: escapeshellarg($this->_documentRoot)
139: );
140:
141: if (!empty($this->_iniPath)) {
142: $command = sprintf('%s -c %s', $command, $this->_iniPath);
143: }
144:
145: $command = sprintf('%s %s', $command, escapeshellarg($this->_documentRoot . '/index.php'));
146:
147: $port = ':' . $this->_port;
148: $this->out(sprintf('built-in server is running in http://%s%s/', $this->_host, $port));
149: $this->out(sprintf('You can exit with <info>`CTRL-C`</info>'));
150: system($command);
151: }
152:
153: 154: 155: 156: 157:
158: public function getOptionParser()
159: {
160: $parser = parent::getOptionParser();
161:
162: $parser->setDescription([
163: 'PHP Built-in Server for CakePHP',
164: '<warning>[WARN] Don\'t use this in a production environment</warning>',
165: ])->addOption('host', [
166: 'short' => 'H',
167: 'help' => 'ServerHost'
168: ])->addOption('port', [
169: 'short' => 'p',
170: 'help' => 'ListenPort'
171: ])->addOption('ini_path', [
172: 'short' => 'I',
173: 'help' => 'php.ini path'
174: ])->addOption('document_root', [
175: 'short' => 'd',
176: 'help' => 'DocumentRoot'
177: ]);
178:
179: return $parser;
180: }
181: }
182: