From 20e9015f80ac88fbf20ae0382772dc2a38a6d7e3 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 30 Jul 2011 13:41:43 -0400 Subject: [PATCH] Making skel files generate hard coded paths, when CakePHP is not on the include_path, and exclude hard coded paths when it is. --- lib/Cake/Console/Command/Task/ProjectTask.php | 67 +++++++++++++------ .../Console/Templates/skel/Console/cake.php | 25 +++++-- .../Console/Templates/skel/webroot/index.php | 2 +- .../Console/Templates/skel/webroot/test.php | 2 +- 4 files changed, 69 insertions(+), 27 deletions(-) diff --git a/lib/Cake/Console/Command/Task/ProjectTask.php b/lib/Cake/Console/Command/Task/ProjectTask.php index 3aa6c5184..cb72a2809 100644 --- a/lib/Cake/Console/Command/Task/ProjectTask.php +++ b/lib/Cake/Console/Command/Task/ProjectTask.php @@ -97,20 +97,19 @@ class ProjectTask extends Shell { $success = false; } - $this->out(__d('cake_console', 'The value for CAKE_CORE_INCLUDE_PATH can be hardcoded set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH)); - $this->out(__d('cake_console', 'If you hard code it, the project will possibly run only in your computer.')); - $setConstants = $this->in(__d('cake_console', 'Do you want to set CAKE_CORE_INCLUDE_PATH in webroot/index.php?'), array('y', 'n'), 'n'); - if (strtolower($setConstants) === 'y') { - if ($this->corePath($path) === true) { - $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH)); - $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH)); - $this->out(__d('cake_console', ' * Remember to check these values after moving to production server')); - } else { - $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' .DS .'index.php')); - $success = false; - } + $hardCode = false; + if (!$this->cakeOnIncludePath()) { + $this->out(__d('cake_console', 'CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.')); + $this->out(__d('cake_console', 'You can fix this by adding CakePHP to your `include_path`.')); + $hardCode = true; + } + if ($this->corePath($path, $hardCode) === true) { + $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH)); + $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH)); + $this->out(__d('cake_console', ' * Remember to check these values after moving to production server')); } else { - $this->out(__d('cake_console', 'Please make sure your cake core is accessible, if you have problems edit CAKE_CORE_INCLUDE_PATH in webroot/index.php')); + $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' .DS .'index.php')); + $success = false; } $Folder = new Folder($path); @@ -128,6 +127,21 @@ class ProjectTask extends Shell { } } +/** + * Checks PHP's include_path for CakePHP. + * + * @return bool Indicates whether or not CakePHP exists on include_path + */ + public function cakeOnIncludePath() { + $paths = explode(PATH_SEPARATOR, ini_get('include_path')); + foreach ($paths as $path) { + if (file_exists($path . DS . 'Cake' . DS . 'bootstrap.php')) { + return true; + } + } + return false; + } + /** * Looks for a skeleton template of a Cake application, * and if not found asks the user for a path. When there is a path @@ -225,8 +239,8 @@ class ProjectTask extends Shell { $File = new File($path . 'Console' . DS . 'cake.php'); $contents = $File->read(); if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) { - $path = CAKE . 'Console' . DS; - $replacement = "'" . str_replace(DS, "' . DIRECTORY_SEPARATOR . '", $path) . "'"; + $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " \$ds . '" : "'"; + $replacement = $root . str_replace(DS, "' . \$ds . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'"; $result = str_replace($match[0], $replacement, $contents); if ($File->write($result)) { return true; @@ -285,13 +299,20 @@ class ProjectTask extends Shell { * @param string $path Project path * @return boolean Success */ - public function corePath($path) { + public function corePath($path, $hardCode = true) { + $prefix = $hardCode == true ? '' : '//'; + if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) { $File = new File($path . 'webroot' . DS . 'index.php'); $contents = $File->read(); - if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9\.]*\);)/', $contents, $match)) { - $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'"; - $result = str_replace($match[0], "\n\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents); + $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'"; + $corePath = $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'"; + if (preg_match('#(\s*[/]+define\(\'CAKE_CORE_INCLUDE_PATH\', .*?\);)#', $contents, $match)) { + $result = str_replace( + $match[0], + "\n\t" . $prefix . "define('CAKE_CORE_INCLUDE_PATH', " . $corePath . ");", + $contents + ); if (!$File->write($result)) { return false; } @@ -301,8 +322,12 @@ class ProjectTask extends Shell { $File = new File($path . 'webroot' . DS . 'test.php'); $contents = $File->read(); - if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9\.]*\);)/', $contents, $match)) { - $result = str_replace($match[0], "\n\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents); + if (preg_match('#(\s*[/]+define\(\'CAKE_CORE_INCLUDE_PATH\', .*?\);)#', $contents, $match)) { + $result = str_replace( + $match[0], + "\n\t" . $prefix . "define('CAKE_CORE_INCLUDE_PATH', " . $corePath . ");", + $contents + ); if (!$File->write($result)) { return false; } diff --git a/lib/Cake/Console/Templates/skel/Console/cake.php b/lib/Cake/Console/Templates/skel/Console/cake.php index 5c7d3f1f5..d77755334 100644 --- a/lib/Cake/Console/Templates/skel/Console/cake.php +++ b/lib/Cake/Console/Templates/skel/Console/cake.php @@ -3,8 +3,6 @@ /** * Command-line code generation utility to automate programmer chores. * - * Shell dispatcher class - * * PHP 5 * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) @@ -16,9 +14,28 @@ * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project * @package app.Console - * @since CakePHP(tm) v 1.2.0.5012 + * @since CakePHP(tm) v 2.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -require_once(__CAKE_PATH__ . 'ShellDispatcher.php'); +$ds = DIRECTORY_SEPARATOR; +$dispatcher = 'Cake' . $ds . 'Console' . $ds . 'ShellDispatcher.php'; +$found = false; +$paths = explode(PATH_SEPARATOR, ini_get('include_path')); + +foreach ($paths as $path) { + if (file_exists($path . $ds . $dispatcher)) { + $found = $path; + } +} + +if (!$found && function_exists('ini_set')) { + $root = dirname(dirname(dirname(__FILE__))); + ini_set('include_path', __CAKE_PATH__ . PATH_SEPARATOR . ini_get('include_path')); +} + +if (!include($dispatcher)) { + trigger_error('Could not locate CakePHP core files.', E_USER_ERROR); +} +unset($paths, $path, $found, $dispatcher, $root, $ds); return ShellDispatcher::run($argv); diff --git a/lib/Cake/Console/Templates/skel/webroot/index.php b/lib/Cake/Console/Templates/skel/webroot/index.php index 2dced12c7..824565bcc 100644 --- a/lib/Cake/Console/Templates/skel/webroot/index.php +++ b/lib/Cake/Console/Templates/skel/webroot/index.php @@ -54,7 +54,7 @@ * For ease of development CakePHP uses PHP's include_path. If you * need to squeeze a bit more performance you can set this path. */ - //define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib'); + //define('CAKE_CORE_INCLUDE_PATH', __CAKE_PATH__); /** * Editing below this line should NOT be necessary. diff --git a/lib/Cake/Console/Templates/skel/webroot/test.php b/lib/Cake/Console/Templates/skel/webroot/test.php index f48bfb593..9f83f4315 100644 --- a/lib/Cake/Console/Templates/skel/webroot/test.php +++ b/lib/Cake/Console/Templates/skel/webroot/test.php @@ -51,7 +51,7 @@ ini_set('display_errors', 1); * For ease of development CakePHP uses PHP's include_path. If you * need to cannot modify your include_path, you can set this path. */ - //define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib'); + //define('CAKE_CORE_INCLUDE_PATH', __CAKE_PATH__); /** * Editing below this line should not be necessary.