Skip to content

Commit

Permalink
Making skel files generate hard coded paths, when CakePHP
Browse files Browse the repository at this point in the history
is not on the include_path, and exclude hard coded paths when it is.
  • Loading branch information
markstory committed Aug 4, 2011
1 parent 721c438 commit 20e9015
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 27 deletions.
67 changes: 46 additions & 21 deletions lib/Cake/Console/Command/Task/ProjectTask.php
Expand Up @@ -97,20 +97,19 @@ public function execute() {
$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', '<warning>If you hard code it, the project will possibly run only in your computer.</warning>'));
$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', ' * <warning>Remember to check these values after moving to production server</warning>'));
} 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', '<warning>CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.</warning>'));
$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', ' * <warning>Remember to check these values after moving to production server</warning>'));
} else {
$this->out(__d('cake_console', '<warning>Please make sure your cake core is accessible, if you have problems edit CAKE_CORE_INCLUDE_PATH in webroot/index.php</warning>'));
$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);
Expand All @@ -128,6 +127,21 @@ public function execute() {
}
}

/**
* 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
Expand Down Expand Up @@ -225,8 +239,8 @@ public function consolePath($path) {
$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;
Expand Down Expand Up @@ -285,13 +299,20 @@ public function securityCipherSeed($path) {
* @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;
}
Expand All @@ -301,8 +322,12 @@ public function corePath($path) {

$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;
}
Expand Down
25 changes: 21 additions & 4 deletions lib/Cake/Console/Templates/skel/Console/cake.php
Expand Up @@ -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)
Expand All @@ -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);
2 changes: 1 addition & 1 deletion lib/Cake/Console/Templates/skel/webroot/index.php
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Console/Templates/skel/webroot/test.php
Expand Up @@ -51,7 +51,7 @@
* 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.
Expand Down

0 comments on commit 20e9015

Please sign in to comment.