Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added ServerShell for PHP Built-in Server #713

Closed
wants to merge 14 commits into from

8 participants

Masashi Shinbara Florian Krämer Rachman Chavik Mark Story José Lorenzo Rodríguez Kyle Robinson Young Jeremy Harris Washington L Braga Jr
Masashi Shinbara

Hi, I would propose adding new Shell Command.It is ServerShell.

ServerShell provides a httpd server for CakePHP application without other httpd server(Apache, nginx etc).

It is use PHP CLI build-in server.

Use case is below.

1. default (http://localhost/)

$ cd /path/to/app
$ ./Console/cake server

Welcome to CakePHP v2.2.0 Console
---------------------------------------------------------------
App : app
Path: /path/to/app/
---------------------------------------------------------------
ServerHost  : localhost
ListenPort  : 80
DocumentRoot: /path/to/app/webroot
---------------------------------------------------------------
[WARNING] Don't use this at the production enviroment
built-in server is running...

2. specify ListenPort (http://localhost:8000/)

$ ./Console/cake server -p 8000

Welcome to CakePHP v2.2.0 Console
---------------------------------------------------------------
App : app
Path: /path/to/app/
---------------------------------------------------------------
ServerHost  : localhost
ListenPort  : 8000
DocumentRoot: /path/to/app/webroot
---------------------------------------------------------------
[WARNING] Don't use this at the production enviroment
built-in server is running...

3. usage

$ ./Console/cake -h

Welcome to CakePHP v2.2.0 Console
---------------------------------------------------------------
App : app
Path: /Users/shin/sandbox/cakephp_shin1x1/app/
---------------------------------------------------------------
PHP Built-in Server for CakePHP

Usage:
cake server [-h] [-v] [-q] [-H] [-p] [-d]

Options:

--help, -h           Display this help.
--verbose, -v        Enable verbose output.
--quiet, -q          Enable quiet output.
--host, -H           ServerHost
--port, -p           ListenPort
--document_root, -d  DocumentRoot 

4. PHP 5.3 or lower

$ ./Console/cake server

Welcome to CakePHP v2.2.0 Console
---------------------------------------------------------------
App : app
Path: /path/to/app/
---------------------------------------------------------------
ServerHost  : localhost
ListenPort  : 80
DocumentRoot: /path/to/app/webroot
---------------------------------------------------------------
This command is available on PHP5.4 or above    
Florian Krämer
Collaborator

+1 but give it a better name like PhpWebserverShell and change the pull request to merge it into 2.3, not master.

Rachman Chavik
Collaborator

Can we have shorter output, say:

    Welcome to CakePHP v2.2.0 Console
    ---------------------------------------------------------------
    App : app
    Path: /path/to/app/
    DocumentRoot: /path/to/app/webroot
    ---------------------------------------------------------------
    built-in server is running in http://somelocalsite.dev:80/

Also, [WARNING] Don't use this at the production enviroment is just noise imo, and should be documented in docs, not on each run.

lib/Cake/Console/Command/ServerShell.php
((18 lines not shown))
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+App::uses('AppShell', 'Console/Command');
+
+/**
+ * built-in Server Shell
+ *
+ * @package Cake.Console.Command
+ */
+class ServerShell extends AppShell {
+ const DEFAULT_HOST = 'localhost';
+ const DEFAULT_PORT = 80;
+
+ /**
+ * server host
José Lorenzo Rodríguez Owner
lorenzo added a note

This is not our indentation style for comments.

Kyle Robinson Young
shama added a note

Also please use hard tabs rather then spaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Cake/Console/Command/ServerShell.php
((15 lines not shown))
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v 1.2.0.5012
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+App::uses('AppShell', 'Console/Command');
+
+/**
+ * built-in Server Shell
+ *
+ * @package Cake.Console.Command
+ */
+class ServerShell extends AppShell {
+ const DEFAULT_HOST = 'localhost';
+ const DEFAULT_PORT = 80;
José Lorenzo Rodríguez Owner
lorenzo added a note

Missing doc blocks for constants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Cake/Console/Command/ServerShell.php
((2 lines not shown))
+/**
+ * built-in Server Shell
+ *
+ * Implementation of a Cake Shell to show CakePHP core method signatures.
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v 1.2.0.5012
José Lorenzo Rodríguez Owner
lorenzo added a note

1.2.0? You were a visionaire hehe, just select a correct version and year please :P

Washington L Braga Jr
huoxito added a note

I wonder if Im the only one who thinks all those license info on every CakePHP class are so useless .. I mean docs for methods and classes are great but that, what is that for? ps no big deal anyway

I think the idea is that each file needs to contain it so if a file is copied elsewhere, the license follows that file. Prevents people from disassembling pieces of the core code and changing the license to something else.

Florian Krämer Collaborator
burzum added a note

Exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Cake/Console/Command/ServerShell.php
((129 lines not shown))
+ $parser = parent::getOptionParser();
+
+ $parser->addOption('host', array(
+ 'short' => 'H',
+ 'help' => __d('cake_console', 'ServerHost')
+ ));
+ $parser->addOption('port', array(
+ 'short' => 'p',
+ 'help' => __d('cake_console', 'ListenPort')
+ ));
+ $parser->addOption('document_root', array(
+ 'short' => 'd',
+ 'help' => __d('cake_console', 'DocumentRoot')
+ ));
+
+ $parser->description(__('PHP Built-in Server for CakePHP'));
José Lorenzo Rodríguez Owner
lorenzo added a note

All other strings are using __d, why not this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
José Lorenzo Rodríguez lorenzo commented on the diff
app/webroot/index.php
@@ -70,6 +70,11 @@
define('WWW_ROOT', dirname(__FILE__) . DS);
}
+// for built-in server
José Lorenzo Rodríguez Owner
lorenzo added a note

If you change this file, please also change the file in Cake/Console/Templates/skel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
markstory added some commits
Mark Story markstory Fix missing line breaks.
Fixes #3028
b741552
Mark Story markstory Type check before unset()
Calling unset() on string indices fails fatally on 5.3.x and lower.

Fixes #3027
3baaecc
app/webroot/index.php
@@ -70,6 +70,11 @@
define('WWW_ROOT', dirname(__FILE__) . DS);
}
+// for built-in server
+if (php_sapi_name() == 'cli-server') {
+ $_SERVER['PHP_SELF'] = '/'.basename(__FILE__);
Kyle Robinson Young
shama added a note

Indentation is off here, Cake uses hard tabs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Cake/Console/Command/ServerShell.php
((103 lines not shown))
+ $this->out(__d('cake_console', '<warning>This command is available on PHP5.4 or above</warning>'));
+ return;
+ }
+
+ $this->out(__d('cake_console', 'ServerHost : %s', $this->_host));
+ $this->out(__d('cake_console', 'ListenPort : %d', $this->_port));
+ $this->out(__d('cake_console', 'DocumentRoot: %s', $this->_documentRoot));
+ $this->hr();
+
+ $command = sprintf("php -S %s:%d -t %s",
+ $this->_host,
+ $this->_port,
+ $this->_documentRoot
+ );
+
+ $this->out(__d('cake_console', '<warning>[WARNING] Don\'t use this at the production enviroment</warning>'));
Kyle Robinson Young
shama added a note

Typo; should be environment. Although I agree with @rchavik about excluding this message all together. :)

Mark Story Owner

I like this warning, external docs are good, but hitting people on the head everytime they run this is good as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Cake/Console/Command/ServerShell.php
((25 lines not shown))
+ *
+ * @package Cake.Console.Command
+ */
+class ServerShell extends AppShell {
+ const DEFAULT_HOST = 'localhost';
+ const DEFAULT_PORT = 80;
+
+ /**
+ * server host
+ *
+ * @var string
+ */
+ protected $_host = null;
+
+ /**
+ * listern port
Kyle Robinson Young
shama added a note

Typo, should be listen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Mark Story
Owner

This change is also similar to #634

Masashi Shinbara

Thanks for all the comments!
I will fix comment, indentation, tab, and typo:D

@burzum Hi, I change the pull request to 2.3 branch.
thank you new name! PhpWebserverShell is a good name.

but I will enter many characters.

$ ./Console/cake php_web_server

ServerShell is shorter.

$ ./Console/cake server

@rchavik good idea! I try to shorter output message.

Florian Krämer
Collaborator

@shin1x1 I clearly prefer to type a few characters more in favor for a better variable, class or shell name than ending up with non specific and generic names. To be honest, at least for me, and I do not think for any person who's programming for some time it really doesn't matter if you type 6 or 12 chars.

Mark Story
Owner

I'd rather have the shorter name personally. php_web_server is a bit redundant, what other kind of server would you be starting other than a php + web server?

Rachman Chavik
Collaborator

how about http_server ?

also, is it possible to squash these commits in a logical chunks?

Rachman Chavik
Collaborator

nevermind about the squash, i just saw your #714. nice.

Rachman Chavik rchavik closed this
Masashi Shinbara

@burzum I think that variable, class name should be a specific, too. I type a few chars with code completion(Vim, Emacs, Eclipse). But shell name is need to enter full chars. so, I like short name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 11, 2012
  1. Masashi Shinbara
  2. Mark Story

    Fix missing line breaks.

    markstory authored
    Fixes #3028
  3. Mark Story

    Type check before unset()

    markstory authored
    Calling unset() on string indices fails fatally on 5.3.x and lower.
    
    Fixes #3027
Commits on Jul 12, 2012
  1. Mark Story

    Remove trailing whitespace.

    markstory authored
  2. Masashi Shinbara

    fixed indent, tab, typo.

    shin1x1 authored
  3. Masashi Shinbara

    added skel.

    shin1x1 authored
  4. Masashi Shinbara
  5. Masashi Shinbara
  6. Masashi Shinbara

    fixed indent, tab, typo.

    shin1x1 authored
  7. Masashi Shinbara

    added skel.

    shin1x1 authored
  8. Masashi Shinbara
  9. Masashi Shinbara

    move Warning message.

    shin1x1 authored
  10. Masashi Shinbara

    Merge branch 'pr_server_shell' of https://github.com/shin1x1/cakephp

    shin1x1 authored
    …into pr_server_shell
    
    Conflicts:
    	lib/Cake/Console/Command/ServerShell.php
  11. Masashi Shinbara

    fixed WARN message.

    shin1x1 authored
This page is out of date. Refresh to see the latest.
5 app/webroot/index.php
View
@@ -70,6 +70,11 @@
define('WWW_ROOT', dirname(__FILE__) . DS);
}
+// for built-in server
José Lorenzo Rodríguez Owner
lorenzo added a note

If you change this file, please also change the file in Cake/Console/Templates/skel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+if (php_sapi_name() == 'cli-server') {
+ $_SERVER['PHP_SELF'] = '/'.basename(__FILE__);
+}
+
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
if (function_exists('ini_set')) {
ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
165 lib/Cake/Console/Command/ServerShell.php
View
@@ -0,0 +1,165 @@
+<?php
+/**
+ * built-in Server Shell
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v 2.3.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+App::uses('AppShell', 'Console/Command');
+
+/**
+ * built-in Server Shell
+ *
+ * @package Cake.Console.Command
+ */
+class ServerShell extends AppShell {
+/**
+ * Default ServerHost
+ */
+ const DEFAULT_HOST = 'localhost';
+/**
+ * Default ListenPort
+ */
+ const DEFAULT_PORT = 80;
+
+/**
+ * server host
+ *
+ * @var string
+ */
+ protected $_host = null;
+
+/**
+ * listen port
+ *
+ * @var string
+ */
+ protected $_port = null;
+
+/**
+ * document root
+ *
+ * @var string
+ */
+ protected $_documentRoot = null;
+
+/**
+ * Override initialize of the Shell
+ *
+ * @return void
+ */
+ public function initialize() {
+ $this->_host = self::DEFAULT_HOST;
+ $this->_port = self::DEFAULT_PORT;
+ $this->_documentRoot = WWW_ROOT;
+ }
+
+/**
+ * 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 http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::startup
+ */
+ 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'];
+ }
+
+ // 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];
+ }
+
+ parent::startup();
+ }
+
+/**
+ * Displays a header for the shell
+ *
+ * @return void
+ */
+ protected function _welcome() {
+ $this->out();
+ $this->out(__d('cake_console', '<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
+ $this->hr();
+ $this->out(__d('cake_console', 'App : %s', APP_DIR));
+ $this->out(__d('cake_console', 'Path: %s', APP));
+ $this->out(__d('cake_console', 'DocumentRoot: %s', $this->_documentRoot));
+ $this->hr();
+ }
+
+/**
+ * Override main() to handle action
+ *
+ * @return void
+ */
+ public function main() {
+ if (version_compare(PHP_VERSION, '5.4.0') < 0) {
+ $this->out(__d('cake_console', '<warning>This command is available on PHP5.4 or above</warning>'));
+ return;
+ }
+
+ $command = sprintf("php -S %s:%d -t %s",
+ $this->_host,
+ $this->_port,
+ $this->_documentRoot
+ );
+
+ $port = ($this->_port == self::DEFAULT_PORT) ? '' : ':'.$this->_port;
+ $this->out(__d('cake_console', 'built-in server is running in http://%s%s/', $this->_host, $port));
+ $ret = system($command);
+ }
+
+/**
+ * Get and configure the optionparser.
+ *
+ * @return ConsoleOptionParser
+ */
+ public function getOptionParser() {
+ $parser = parent::getOptionParser();
+
+ $parser->addOption('host', array(
+ 'short' => 'H',
+ 'help' => __d('cake_console', 'ServerHost')
+ ));
+ $parser->addOption('port', array(
+ 'short' => 'p',
+ 'help' => __d('cake_console', 'ListenPort')
+ ));
+ $parser->addOption('document_root', array(
+ 'short' => 'd',
+ 'help' => __d('cake_console', 'DocumentRoot')
+ ));
+
+ $parser->description(array(
+ __d('cake_console', 'PHP Built-in Server for CakePHP'),
+ __d('cake_console', '<warning>[WARN] Don\'t use this at the production environment</warning>'),
+ ));
+
+ return $parser;
+ }
+}
2  lib/Cake/Console/Command/Task/ModelTask.php
View
@@ -414,7 +414,7 @@ public function fieldValidation($fieldName, $metaData, $primaryKey = 'id') {
for ($i = 1, $m = $defaultChoice / 2; $i < $m; $i++) {
$line = sprintf("%2d. %s", $i, $this->_validations[$i]);
$optionText .= $line . str_repeat(" ", 31 - strlen($line));
- $optionText .= sprintf("%2d. %s", $m + $i, $this->_validations[$m + $i]);
+ $optionText .= sprintf("%2d. %s\n", $m + $i, $this->_validations[$m + $i]);
}
$this->out($optionText);
$this->out(__d('cake_console', "%s - Do not do any validation on this field.", $defaultChoice));
5 lib/Cake/Console/Templates/skel/webroot/index.php
View
@@ -72,6 +72,11 @@
define('WWW_ROOT', dirname(__FILE__) . DS);
}
+// for built-in server
+if (php_sapi_name() == 'cli-server') {
+ $_SERVER['PHP_SELF'] = '/'.basename(__FILE__);
+}
+
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
if (function_exists('ini_set')) {
ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
2  lib/Cake/Controller/Component/SecurityComponent.php
View
@@ -229,7 +229,7 @@ public function startup(Controller $controller) {
}
}
$this->generateToken($controller->request);
- if ($isPost) {
+ if ($isPost && is_array($controller->request->data)) {
unset($controller->request->data['_Token']);
}
}
2  lib/Cake/Model/Behavior/TranslateBehavior.php
View
@@ -392,7 +392,7 @@ public function afterSave(Model $model, $created) {
unset($this->runtime[$model->alias]['beforeValidate'], $this->runtime[$model->alias]['beforeSave']);
$conditions = array('model' => $model->alias, 'foreign_key' => $model->id);
$RuntimeModel = $this->translateModel($model);
-
+
$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
if ($created) {
foreach ($fields as $field) {
Something went wrong with that request. Please try again.