_host = self::DEFAULT_HOST; $this->_port = self::DEFAULT_PORT; $this->_documentRoot = WWW_ROOT; $this->_iniPath = ''; } /** * Starts up the Shell and displays the welcome message. * Allows for checking and configuring prior to command or main execution * * Override this method if you want to remove the welcome information, * or otherwise modify the pre-command flow. * * @return void * @link https://book.cakephp.org/3.0/en/console-and-shells.html#hook-methods */ public function startup() { if (!empty($this->params['host'])) { $this->_host = $this->params['host']; } if (!empty($this->params['port'])) { $this->_port = $this->params['port']; } if (!empty($this->params['document_root'])) { $this->_documentRoot = $this->params['document_root']; } if (!empty($this->params['ini_path'])) { $this->_iniPath = $this->params['ini_path']; } // For Windows if (substr($this->_documentRoot, -1, 1) === DIRECTORY_SEPARATOR) { $this->_documentRoot = substr($this->_documentRoot, 0, strlen($this->_documentRoot) - 1); } if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_documentRoot, $m)) { $this->_documentRoot = $m[1] . '\\' . $m[2]; } if (substr($this->_iniPath, -1, 1) === DIRECTORY_SEPARATOR) { $this->_iniPath = substr($this->_iniPath, 0, strlen($this->_iniPath) - 1); } if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_iniPath, $m)) { $this->_iniPath = $m[1] . '\\' . $m[2]; } parent::startup(); } /** * Displays a header for the shell * * @return void */ protected function _welcome() { $this->out(); $this->out(sprintf('Welcome to CakePHP %s Console', 'v' . Configure::version())); $this->hr(); $this->out(sprintf('App : %s', APP_DIR)); $this->out(sprintf('Path: %s', APP)); $this->out(sprintf('DocumentRoot: %s', $this->_documentRoot)); $this->out(sprintf('Ini Path: %s', $this->_iniPath)); $this->hr(); } /** * Override main() to handle action * * @return void */ public function main() { $command = sprintf( 'php -S %s:%d -t %s', $this->_host, $this->_port, escapeshellarg($this->_documentRoot) ); if (!empty($this->_iniPath)) { $command = sprintf('%s -c %s', $command, $this->_iniPath); } $command = sprintf('%s %s', $command, escapeshellarg($this->_documentRoot . '/index.php')); $port = ':' . $this->_port; $this->out(sprintf('built-in server is running in http://%s%s/', $this->_host, $port)); $this->out(sprintf('You can exit with `CTRL-C`')); system($command); } /** * Gets the option parser instance and configures it. * * @return \Cake\Console\ConsoleOptionParser */ public function getOptionParser() { $parser = parent::getOptionParser(); $parser->setDescription([ 'PHP Built-in Server for CakePHP', '[WARN] Don\'t use this in a production environment', ])->addOption('host', [ 'short' => 'H', 'help' => 'ServerHost' ])->addOption('port', [ 'short' => 'p', 'help' => 'ListenPort' ])->addOption('ini_path', [ 'short' => 'I', 'help' => 'php.ini path' ])->addOption('document_root', [ 'short' => 'd', 'help' => 'DocumentRoot' ]); return $parser; } }