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