Merge pull request #9717 from bancer/shell-webroot

accept webroot shell parameter
This commit is contained in:
Mark Story 2016-11-08 21:12:50 -05:00 committed by GitHub
commit f46f042001
3 changed files with 65 additions and 9 deletions

View file

@ -54,6 +54,7 @@ class CommandListShell extends AppShell {
$this->out(" -working: " . rtrim(APP, DS)); $this->out(" -working: " . rtrim(APP, DS));
$this->out(" -root: " . rtrim(ROOT, DS)); $this->out(" -root: " . rtrim(ROOT, DS));
$this->out(" -core: " . rtrim(CORE_PATH, DS)); $this->out(" -core: " . rtrim(CORE_PATH, DS));
$this->out(" -webroot: " . rtrim(WWW_ROOT, DS));
$this->out(""); $this->out("");
$this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2); $this->out(__d('cake_console', "<info>Changing Paths:</info>"), 2);
$this->out(__d('cake_console', "Your working path should be the same as your application path. To change your path use the '-app' param.")); $this->out(__d('cake_console', "Your working path should be the same as your application path. To change your path use the '-app' param."));

View file

@ -129,7 +129,12 @@ class ShellDispatcher {
define('APP', $this->params['working'] . DS); define('APP', $this->params['working'] . DS);
} }
if (!defined('WWW_ROOT')) { if (!defined('WWW_ROOT')) {
define('WWW_ROOT', APP . $this->params['webroot'] . DS); if (!$this->_isAbsolutePath($this->params['webroot'])) {
$webroot = realpath(APP . $this->params['webroot']);
} else {
$webroot = $this->params['webroot'];
}
define('WWW_ROOT', $webroot . DS);
} }
if (!defined('TMP') && !is_dir(APP . 'tmp')) { if (!defined('TMP') && !is_dir(APP . 'tmp')) {
define('TMP', CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'tmp' . DS); define('TMP', CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'tmp' . DS);
@ -305,25 +310,45 @@ class ShellDispatcher {
} }
} }
if ($params['app'][0] === '/' || preg_match('/([a-z])(:)/i', $params['app'], $matches)) { if ($this->_isAbsolutePath($params['app'])) {
$params['root'] = dirname($params['app']); $params['root'] = dirname($params['app']);
} elseif (strpos($params['app'], '/')) { } elseif (strpos($params['app'], '/')) {
$params['root'] .= '/' . dirname($params['app']); $params['root'] .= '/' . dirname($params['app']);
} }
$isWindowsAppPath = $this->_isWindowsPath($params['app']);
$params['app'] = basename($params['app']); $params['app'] = basename($params['app']);
$params['working'] = rtrim($params['root'], '/'); $params['working'] = rtrim($params['root'], '/');
if (!$isWin || !preg_match('/^[A-Z]:$/i', $params['app'])) { if (!$isWin || !preg_match('/^[A-Z]:$/i', $params['app'])) {
$params['working'] .= '/' . $params['app']; $params['working'] .= '/' . $params['app'];
} }
if (!empty($matches[0]) || !empty($isWin)) { if ($isWindowsAppPath || !empty($isWin)) {
$params = str_replace('/', '\\', $params); $params = str_replace('/', '\\', $params);
} }
$this->params = $params + $this->params; $this->params = $params + $this->params;
} }
/**
* Checks whether the given path is absolute or relative.
*
* @param string $path absolute or relative path.
* @return bool
*/
protected function _isAbsolutePath($path) {
return $path[0] === '/' || $this->_isWindowsPath($path);
}
/**
* Checks whether the given path is Window OS path.
*
* @param string $path absolute path.
* @return bool
*/
protected function _isWindowsPath($path) {
return preg_match('/([a-z])(:)/i', $path) == 1;
}
/** /**
* Parses out the paths from from the argv * Parses out the paths from from the argv
* *
@ -332,7 +357,7 @@ class ShellDispatcher {
*/ */
protected function _parsePaths($args) { protected function _parsePaths($args) {
$parsed = array(); $parsed = array();
$keys = array('-working', '--working', '-app', '--app', '-root', '--root'); $keys = array('-working', '--working', '-app', '--app', '-root', '--root', '-webroot', '--webroot');
$args = (array)$args; $args = (array)$args;
foreach ($keys as $key) { foreach ($keys as $key) {
while (($index = array_search($key, $args)) !== false) { while (($index = array_search($key, $args)) !== false) {

View file

@ -137,9 +137,8 @@ class ShellDispatcherTest extends CakeTestCase {
* *
* @return void * @return void
*/ */
public function testParseParams() { public function testParseParamsAppWorkingAbsolute() {
$Dispatcher = new TestShellDispatcher(); $Dispatcher = new TestShellDispatcher();
$params = array( $params = array(
'/cake/1.2.x.x/cake/console/cake.php', '/cake/1.2.x.x/cake/console/cake.php',
'bake', 'bake',
@ -156,7 +155,15 @@ class ShellDispatcherTest extends CakeTestCase {
); );
$Dispatcher->parseParams($params); $Dispatcher->parseParams($params);
$this->assertEquals($expected, $Dispatcher->params); $this->assertEquals($expected, $Dispatcher->params);
}
/**
* testParseParams method
*
* @return void
*/
public function testParseParamsNone() {
$Dispatcher = new TestShellDispatcher();
$params = array('cake.php'); $params = array('cake.php');
$expected = array( $expected = array(
'app' => 'app', 'app' => 'app',
@ -167,7 +174,15 @@ class ShellDispatcherTest extends CakeTestCase {
$Dispatcher->params = $Dispatcher->args = array(); $Dispatcher->params = $Dispatcher->args = array();
$Dispatcher->parseParams($params); $Dispatcher->parseParams($params);
$this->assertEquals($expected, $Dispatcher->params); $this->assertEquals($expected, $Dispatcher->params);
}
/**
* testParseParams method
*
* @return void
*/
public function testParseParamsApp() {
$Dispatcher = new TestShellDispatcher();
$params = array( $params = array(
'cake.php', 'cake.php',
'-app', '-app',
@ -182,7 +197,15 @@ class ShellDispatcherTest extends CakeTestCase {
$Dispatcher->params = $Dispatcher->args = array(); $Dispatcher->params = $Dispatcher->args = array();
$Dispatcher->parseParams($params); $Dispatcher->parseParams($params);
$this->assertEquals($expected, $Dispatcher->params); $this->assertEquals($expected, $Dispatcher->params);
}
/**
* testParseParams method
*
* @return void
*/
public function testParseParamsAppWorkingRelative() {
$Dispatcher = new TestShellDispatcher();
$params = array( $params = array(
'./cake.php', './cake.php',
'bake', 'bake',
@ -191,17 +214,24 @@ class ShellDispatcherTest extends CakeTestCase {
'-working', '-working',
'/cake/1.2.x.x/cake/console' '/cake/1.2.x.x/cake/console'
); );
$expected = array( $expected = array(
'app' => 'new', 'app' => 'new',
'webroot' => 'webroot', 'webroot' => 'webroot',
'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'), 'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH)) 'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH))
); );
$Dispatcher->params = $Dispatcher->args = array(); $Dispatcher->params = $Dispatcher->args = array();
$Dispatcher->parseParams($params); $Dispatcher->parseParams($params);
$this->assertEquals($expected, $Dispatcher->params); $this->assertEquals($expected, $Dispatcher->params);
}
/**
* testParseParams method
*
* @return void
*/
public function testParseParams() {
$Dispatcher = new TestShellDispatcher();
$params = array( $params = array(
'./console/cake.php', './console/cake.php',