| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* built-in Server Shell
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice.
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
14 |
* @since CakePHP(tm) v 2.3.0
|
|
|
15 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
App::uses('AppShell', 'Console/Command');
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* built-in Server Shell
|
|
|
22 |
*
|
|
|
23 |
* @package Cake.Console.Command
|
|
|
24 |
*/
|
|
|
25 |
class ServerShell extends AppShell {
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Default ServerHost
|
|
|
29 |
*/
|
|
|
30 |
const DEFAULT_HOST = 'localhost';
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Default ListenPort
|
|
|
34 |
*/
|
|
|
35 |
const DEFAULT_PORT = 80;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* server host
|
|
|
39 |
*
|
|
|
40 |
* @var string
|
|
|
41 |
*/
|
|
|
42 |
protected $_host = null;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* listen port
|
|
|
46 |
*
|
|
|
47 |
* @var string
|
|
|
48 |
*/
|
|
|
49 |
protected $_port = null;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* document root
|
|
|
53 |
*
|
|
|
54 |
* @var string
|
|
|
55 |
*/
|
|
|
56 |
protected $_documentRoot = null;
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Override initialize of the Shell
|
|
|
60 |
*
|
|
|
61 |
* @return void
|
|
|
62 |
*/
|
|
|
63 |
public function initialize() {
|
|
|
64 |
$this->_host = self::DEFAULT_HOST;
|
|
|
65 |
$this->_port = self::DEFAULT_PORT;
|
|
|
66 |
$this->_documentRoot = WWW_ROOT;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Starts up the Shell and displays the welcome message.
|
|
|
71 |
* Allows for checking and configuring prior to command or main execution
|
|
|
72 |
*
|
|
|
73 |
* Override this method if you want to remove the welcome information,
|
|
|
74 |
* or otherwise modify the pre-command flow.
|
|
|
75 |
*
|
|
|
76 |
* @return void
|
|
|
77 |
* @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::startup
|
|
|
78 |
*/
|
|
|
79 |
public function startup() {
|
|
|
80 |
if (!empty($this->params['host'])) {
|
|
|
81 |
$this->_host = $this->params['host'];
|
|
|
82 |
}
|
|
|
83 |
if (!empty($this->params['port'])) {
|
|
|
84 |
$this->_port = $this->params['port'];
|
|
|
85 |
}
|
|
|
86 |
if (!empty($this->params['document_root'])) {
|
|
|
87 |
$this->_documentRoot = $this->params['document_root'];
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
// for windows
|
|
|
91 |
if (substr($this->_documentRoot, -1, 1) == DIRECTORY_SEPARATOR) {
|
|
|
92 |
$this->_documentRoot = substr($this->_documentRoot, 0, strlen($this->_documentRoot) - 1);
|
|
|
93 |
}
|
|
|
94 |
if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_documentRoot, $m)) {
|
|
|
95 |
$this->_documentRoot = $m[1] . '\\' . $m[2];
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
parent::startup();
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Displays a header for the shell
|
|
|
103 |
*
|
|
|
104 |
* @return void
|
|
|
105 |
*/
|
|
|
106 |
protected function _welcome() {
|
|
|
107 |
$this->out();
|
|
|
108 |
$this->out(__d('cake_console', '<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
|
|
|
109 |
$this->hr();
|
|
|
110 |
$this->out(__d('cake_console', 'App : %s', APP_DIR));
|
|
|
111 |
$this->out(__d('cake_console', 'Path: %s', APP));
|
|
|
112 |
$this->out(__d('cake_console', 'DocumentRoot: %s', $this->_documentRoot));
|
|
|
113 |
$this->hr();
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Override main() to handle action
|
|
|
118 |
*
|
|
|
119 |
* @return void
|
|
|
120 |
*/
|
|
|
121 |
public function main() {
|
|
|
122 |
if (version_compare(PHP_VERSION, '5.4.0') < 0) {
|
|
|
123 |
$this->out(__d('cake_console', '<warning>This command is available on %s or above</warning>', 'PHP5.4'));
|
|
|
124 |
return;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
$command = sprintf("php -S %s:%d -t %s %s",
|
|
|
128 |
$this->_host,
|
|
|
129 |
$this->_port,
|
|
|
130 |
escapeshellarg($this->_documentRoot),
|
|
|
131 |
escapeshellarg($this->_documentRoot . '/index.php')
|
|
|
132 |
);
|
|
|
133 |
|
|
|
134 |
$port = ($this->_port == self::DEFAULT_PORT) ? '' : ':' . $this->_port;
|
|
|
135 |
$this->out(__d('cake_console', 'built-in server is running in http://%s%s/', $this->_host, $port));
|
|
|
136 |
system($command);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Get and configure the optionparser.
|
|
|
141 |
*
|
|
|
142 |
* @return ConsoleOptionParser
|
|
|
143 |
*/
|
|
|
144 |
public function getOptionParser() {
|
|
|
145 |
$parser = parent::getOptionParser();
|
|
|
146 |
|
|
|
147 |
$parser->addOption('host', array(
|
|
|
148 |
'short' => 'H',
|
|
|
149 |
'help' => __d('cake_console', 'ServerHost')
|
|
|
150 |
));
|
|
|
151 |
$parser->addOption('port', array(
|
|
|
152 |
'short' => 'p',
|
|
|
153 |
'help' => __d('cake_console', 'ListenPort')
|
|
|
154 |
));
|
|
|
155 |
$parser->addOption('document_root', array(
|
|
|
156 |
'short' => 'd',
|
|
|
157 |
'help' => __d('cake_console', 'DocumentRoot')
|
|
|
158 |
));
|
|
|
159 |
|
|
|
160 |
$parser->description(array(
|
|
|
161 |
__d('cake_console', 'PHP Built-in Server for CakePHP'),
|
|
|
162 |
__d('cake_console', '<warning>[WARN] Don\'t use this at the production environment</warning>'),
|
|
|
163 |
));
|
|
|
164 |
|
|
|
165 |
return $parser;
|
|
|
166 |
}
|
|
|
167 |
}
|