project.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: project_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * The Project Task handles creating the base application
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.scripts.bake
00023  * @since           CakePHP(tm) v 1.2
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 if (!class_exists('File')) {
00030     uses('file');
00031 }
00032 /**
00033  * Task class for creating new project apps and plugins
00034  *
00035  * @package     cake
00036  * @subpackage  cake.cake.console.libs.tasks
00037  */
00038 class ProjectTask extends Shell {
00039 /**
00040  * Checks that given project path does not already exist, and
00041  * finds the app directory in it. Then it calls bake() with that information.
00042  *
00043  * @param string $project Project path
00044  * @access public
00045  */
00046     function execute($project = null) {
00047         if ($project === null) {
00048             if (isset($this->args[0])) {
00049                 $project = $this->args[0];
00050                 $this->Dispatch->shiftArgs();
00051             }
00052         }
00053 
00054         if ($project) {
00055             $this->Dispatch->parseParams(array('-app', $project));
00056             $project = $this->params['working'];
00057         }
00058 
00059         if (empty($this->params['skel'])) {
00060             $this->params['skel'] = '';
00061             if (is_dir(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel') === true) {
00062                 $this->params['skel'] = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel';
00063             }
00064         }
00065 
00066         while (!$project) {
00067             $project = $this->in("What is the full path for this app including the app directory name?\nExample: ".$this->params['working'] . DS . "myapp", null, $this->params['working'] . DS . 'myapp');
00068         }
00069 
00070         if ($project) {
00071             $response = false;
00072             while ($response == false && is_dir($project) === true && file_exists($project . 'config' . 'core.php')) {
00073                 $response = $this->in('A project already exists in this location: '.$project.' Overwrite?', array('y','n'), 'n');
00074                 if (strtolower($response) === 'n') {
00075                     $response = $project = false;
00076                 }
00077             }
00078         }
00079 
00080         if($this->bake($project)) {
00081             $path = Folder::slashTerm($project);
00082             if ($this->createHome($path)) {
00083                 $this->out(__('Welcome page created', true));
00084             } else {
00085                 $this->out(__('The Welcome page was NOT created', true));
00086             }
00087 
00088             if ($this->securitySalt($path) === true ) {
00089                 $this->out(__('Random hash key created for \'Security.salt\'', true));
00090             } else {
00091                 $this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php'));
00092             }
00093 
00094             $corePath = $this->corePath($path);
00095             if ($corePath === true ) {
00096                 $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', true), CAKE_CORE_INCLUDE_PATH));
00097                 $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', true), CAKE_CORE_INCLUDE_PATH));
00098                 $this->out(__('Remember to check these value after moving to production server', true));
00099             } elseif ($corePath === false) {
00100                 $this->err(sprintf(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', true), $path . 'webroot' .DS .'index.php'));
00101             }
00102             $Folder = new Folder($path);
00103             if (!$Folder->chmod($path . 'tmp', 0777)) {
00104                 $this->err(sprintf(__('Could not set permissions on %s', true), $path . DS .'tmp'));
00105                 $this->out(sprintf(__('chmod -R 0777 %s', true), $path . DS .'tmp'));
00106             }
00107             return true;
00108         }
00109     }
00110 /**
00111  * Looks for a skeleton template of a Cake application,
00112  * and if not found asks the user for a path. When there is a path
00113  * this method will make a deep copy of the skeleton to the project directory.
00114  * A default home page will be added, and the tmp file storage will be chmod'ed to 0777.
00115  *
00116  * @param string $path Project path
00117  * @param string $skel Path to copy from
00118  * @param string $skip array of directories to skip when copying
00119  * @access private
00120  */
00121     function bake($path, $skel = null, $skip = array('empty')) {
00122         if(!$skel) {
00123             $skel = $this->params['skel'];
00124         }
00125 
00126         while (!$skel) {
00127             $skel = $this->in(sprintf(__("What is the path to the directory layout you wish to copy?\nExample: %s"), APP, null, ROOT . DS . 'myapp' . DS));
00128             if ($skel == '') {
00129                 $this->out(__('The directory path you supplied was empty. Please try again.', true));
00130             } else {
00131                 while (is_dir($skel) === false) {
00132                     $skel = $this->in(__('Directory path does not exist please choose another:', true));
00133                 }
00134             }
00135         }
00136 
00137         $app = basename($path);
00138 
00139         $this->out('Bake Project');
00140         $this->out("Skel Directory: $skel");
00141         $this->out("Will be copied to: {$path}");
00142         $this->hr();
00143 
00144         $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');
00145 
00146         if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
00147             $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
00148 
00149             $Folder = new Folder($skel);
00150             if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
00151                 $this->hr();
00152                 $this->out(sprintf(__("Created: %s in %s", true), $app, $path));
00153                 $this->hr();
00154             } else {
00155                 $this->err(" '".$app."' could not be created properly");
00156                 return false;
00157             }
00158 
00159             if (low($verbose) == 'y' || low($verbose) == 'yes') {
00160                 foreach ($Folder->messages() as $message) {
00161                     $this->out($message);
00162                 }
00163             }
00164 
00165             return true;
00166         } elseif (low($looksGood) == 'q' || low($looksGood) == 'quit') {
00167             $this->out('Bake Aborted.');
00168         } else {
00169             $this->execute(false);
00170             return false;
00171         }
00172     }
00173 /**
00174  * Writes a file with a default home page to the project.
00175  *
00176  * @param string $dir Path to project
00177  * @return boolean Success
00178  * @access public
00179  */
00180     function createHome($dir) {
00181         $app = basename($dir);
00182         $path = $dir . 'views' . DS . 'pages' . DS;
00183         include(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'views'.DS.'home.ctp');
00184         return $this->createFile($path.'home.ctp', $output);
00185     }
00186 /**
00187  * Generates and writes 'Security.salt'
00188  *
00189  * @param string $path Project path
00190  * @return boolean Success
00191  * @access public
00192  */
00193     function securitySalt($path) {
00194         $File =& new File($path . 'config' . DS . 'core.php');
00195         $contents = $File->read();
00196         if (preg_match('/([\\t\\x20]*Configure::write\\(\\\'Security.salt\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
00197             if (!class_exists('Security')) {
00198                 uses('Security');
00199             }
00200             $string = Security::generateAuthKey();
00201             $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents);
00202             if ($File->write($result)) {
00203                 return true;
00204             } else {
00205                 return false;
00206             }
00207         } else {
00208             return false;
00209         }
00210     }
00211 /**
00212  * Generates and writes CAKE_CORE_INCLUDE_PATH
00213  *
00214  * @param string $path Project path
00215  * @return boolean Success
00216  * @access public
00217  */
00218     function corePath($path) {
00219         if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
00220             $File =& new File($path . 'webroot' . DS . 'index.php');
00221             $contents = $File->read();
00222             if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
00223                 $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', '".CAKE_CORE_INCLUDE_PATH."');", $contents);
00224                 if (!$File->write($result)) {
00225                     return false;
00226                 }
00227             } else {
00228                 return false;
00229             }
00230 
00231             $File =& new File($path . 'webroot' . DS . 'test.php');
00232             $contents = $File->read();
00233             if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
00234                 $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', '".CAKE_CORE_INCLUDE_PATH."');", $contents);
00235                 if (!$File->write($result)) {
00236                     return false;
00237                 }
00238             } else {
00239                 return false;
00240             }
00241             return true;
00242         }
00243     }
00244 /**
00245  * Enables Configure::read('Routing.admin') in /app/config/core.php
00246  *
00247  * @param string $name Name to use as admin routing
00248  * @return boolean Success
00249  * @access public
00250  */
00251     function cakeAdmin($name) {
00252         $File =& new File(CONFIGS . 'core.php');
00253         $contents = $File->read();
00254         if (preg_match('%([/\\t\\x20]*Configure::write\(\'Routing.admin\',[\\t\\x20\'a-z]*\\);)%', $contents, $match)) {
00255             $result = str_replace($match[0], "\t" . 'Configure::write(\'Routing.admin\', \''.$name.'\');', $contents);
00256             if ($File->write($result)) {
00257                 Configure::write('Routing.admin', $name);
00258                 return true;
00259             } else {
00260                 return false;
00261             }
00262         } else {
00263             return false;
00264         }
00265     }
00266 /**
00267  * Help
00268  *
00269  * @return void
00270  * @access public
00271  */
00272     function help() {
00273         $this->hr();
00274         $this->out("Usage: cake bake project <arg1>");
00275         $this->hr();
00276         $this->out('Commands:');
00277         $this->out("\n\tproject <name>\n\t\tbakes app directory structure.\n\t\tif <name> begins with '/' path is absolute.");
00278         $this->out("");
00279         $this->_stop();
00280     }
00281 
00282 }
00283 ?>