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