project.php

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

Generated on Sun Nov 22 00:30:52 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7