pictcode / lib / Cake / Console / Command / ServerShell.php @ 22fd3a57
履歴 | 表示 | アノテート | ダウンロード (4.053 KB)
| 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 | 
       * @var string
     | 
  
| 31 | 
       */
     | 
  
| 32 | 
      const DEFAULT_HOST = 'localhost';  | 
  
| 33 | 
       | 
  
| 34 | 
      /**
     | 
  
| 35 | 
       * Default ListenPort
     | 
  
| 36 | 
       *
     | 
  
| 37 | 
       * @var int
     | 
  
| 38 | 
       */
     | 
  
| 39 | 
      const DEFAULT_PORT = 80;  | 
  
| 40 | 
       | 
  
| 41 | 
      /**
     | 
  
| 42 | 
       * server host
     | 
  
| 43 | 
       *
     | 
  
| 44 | 
       * @var string
     | 
  
| 45 | 
       */
     | 
  
| 46 | 
      protected $_host = null;  | 
  
| 47 | 
       | 
  
| 48 | 
      /**
     | 
  
| 49 | 
       * listen port
     | 
  
| 50 | 
       *
     | 
  
| 51 | 
       * @var string
     | 
  
| 52 | 
       */
     | 
  
| 53 | 
      protected $_port = null;  | 
  
| 54 | 
       | 
  
| 55 | 
      /**
     | 
  
| 56 | 
       * document root
     | 
  
| 57 | 
       *
     | 
  
| 58 | 
       * @var string
     | 
  
| 59 | 
       */
     | 
  
| 60 | 
      protected $_documentRoot = null;  | 
  
| 61 | 
       | 
  
| 62 | 
      /**
     | 
  
| 63 | 
       * Override initialize of the Shell
     | 
  
| 64 | 
       *
     | 
  
| 65 | 
       * @return void
     | 
  
| 66 | 
       */
     | 
  
| 67 | 
      public function initialize() {  | 
  
| 68 | 
      $this->_host = static::DEFAULT_HOST;  | 
  
| 69 | 
      $this->_port = static::DEFAULT_PORT;  | 
  
| 70 | 
      $this->_documentRoot = WWW_ROOT;  | 
  
| 71 | 
      }  | 
  
| 72 | 
       | 
  
| 73 | 
      /**
     | 
  
| 74 | 
       * Starts up the Shell and displays the welcome message.
     | 
  
| 75 | 
       * Allows for checking and configuring prior to command or main execution
     | 
  
| 76 | 
       *
     | 
  
| 77 | 
       * Override this method if you want to remove the welcome information,
     | 
  
| 78 | 
       * or otherwise modify the pre-command flow.
     | 
  
| 79 | 
       *
     | 
  
| 80 | 
       * @return void
     | 
  
| 81 | 
       * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::startup
     | 
  
| 82 | 
       */
     | 
  
| 83 | 
      public function startup() {  | 
  
| 84 | 
      if (!empty($this->params['host'])) {  | 
  
| 85 | 
      $this->_host = $this->params['host'];  | 
  
| 86 | 
      }  | 
  
| 87 | 
      if (!empty($this->params['port'])) {  | 
  
| 88 | 
      $this->_port = $this->params['port'];  | 
  
| 89 | 
      }  | 
  
| 90 | 
      if (!empty($this->params['document_root'])) {  | 
  
| 91 | 
      $this->_documentRoot = $this->params['document_root'];  | 
  
| 92 | 
      }  | 
  
| 93 | 
       | 
  
| 94 | 
                      // for Windows
     | 
  
| 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 | 
                      parent::startup();
     | 
  
| 103 | 
      }  | 
  
| 104 | 
       | 
  
| 105 | 
      /**
     | 
  
| 106 | 
       * Displays a header for the shell
     | 
  
| 107 | 
       *
     | 
  
| 108 | 
       * @return void
     | 
  
| 109 | 
       */
     | 
  
| 110 | 
      protected function _welcome() {  | 
  
| 111 | 
                      $this->out();
     | 
  
| 112 | 
      $this->out(__d('cake_console', '<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));  | 
  
| 113 | 
                      $this->hr();
     | 
  
| 114 | 
      $this->out(__d('cake_console', 'App : %s', APP_DIR));  | 
  
| 115 | 
      $this->out(__d('cake_console', 'Path: %s', APP));  | 
  
| 116 | 
      $this->out(__d('cake_console', 'DocumentRoot: %s', $this->_documentRoot));  | 
  
| 117 | 
                      $this->hr();
     | 
  
| 118 | 
      }  | 
  
| 119 | 
       | 
  
| 120 | 
      /**
     | 
  
| 121 | 
       * Override main() to handle action
     | 
  
| 122 | 
       *
     | 
  
| 123 | 
       * @return void
     | 
  
| 124 | 
       */
     | 
  
| 125 | 
      public function main() {  | 
  
| 126 | 
      if (version_compare(PHP_VERSION, '5.4.0') < 0) {  | 
  
| 127 | 
      $this->out(__d('cake_console', '<warning>This command is available on %s or above</warning>', 'PHP5.4'));  | 
  
| 128 | 
                              return;
     | 
  
| 129 | 
      }  | 
  
| 130 | 
       | 
  
| 131 | 
      $command = sprintf("php -S %s:%d -t %s %s",  | 
  
| 132 | 
                              $this->_host,
     | 
  
| 133 | 
                              $this->_port,
     | 
  
| 134 | 
      escapeshellarg($this->_documentRoot),  | 
  
| 135 | 
      escapeshellarg($this->_documentRoot . '/index.php')  | 
  
| 136 | 
      );  | 
  
| 137 | 
       | 
  
| 138 | 
      $port = ($this->_port == static::DEFAULT_PORT) ? '' : ':' . $this->_port;  | 
  
| 139 | 
      $this->out(__d('cake_console', 'built-in server is running in http://%s%s/', $this->_host, $port));  | 
  
| 140 | 
      system($command);  | 
  
| 141 | 
      }  | 
  
| 142 | 
       | 
  
| 143 | 
      /**
     | 
  
| 144 | 
       * Gets the option parser instance and configures it.
     | 
  
| 145 | 
       *
     | 
  
| 146 | 
       * @return ConsoleOptionParser
     | 
  
| 147 | 
       */
     | 
  
| 148 | 
      public function getOptionParser() {  | 
  
| 149 | 
      $parser = parent::getOptionParser();  | 
  
| 150 | 
       | 
  
| 151 | 
      $parser->description(array(  | 
  
| 152 | 
      __d('cake_console', 'PHP Built-in Server for CakePHP'),  | 
  
| 153 | 
      __d('cake_console', '<warning>[WARN] Don\'t use this at the production environment</warning>')  | 
  
| 154 | 
      ))->addOption('host', array(  | 
  
| 155 | 
      'short' => 'H',  | 
  
| 156 | 
      'help' => __d('cake_console', 'ServerHost')  | 
  
| 157 | 
      ))->addOption('port', array(  | 
  
| 158 | 
      'short' => 'p',  | 
  
| 159 | 
      'help' => __d('cake_console', 'ListenPort')  | 
  
| 160 | 
      ))->addOption('document_root', array(  | 
  
| 161 | 
      'short' => 'd',  | 
  
| 162 | 
      'help' => __d('cake_console', 'DocumentRoot')  | 
  
| 163 | 
      ));  | 
  
| 164 | 
       | 
  
| 165 | 
      return $parser;  | 
  
| 166 | 
      }  | 
  
| 167 | 
      }  |