CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • BakeTask
  • ControllerTask
  • DbConfigTask
  • ExtractTask
  • FixtureTask
  • ModelTask
  • PluginTask
  • ProjectTask
  • TemplateTask
  • TestTask
  • ViewTask
  1: <?php
  2: /**
  3:  * The Project Task handles creating the base application
  4:  *
  5:  *
  6:  * PHP 5
  7:  *
  8:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  *
 11:  * Licensed under The MIT License
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @since         CakePHP(tm) v 1.2
 17:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18:  */
 19: 
 20: App::uses('AppShell', 'Console/Command');
 21: App::uses('File', 'Utility');
 22: App::uses('Folder', 'Utility');
 23: App::uses('String', 'Utility');
 24: App::uses('Security', 'Utility');
 25: 
 26: /**
 27:  * Task class for creating new project apps and plugins
 28:  *
 29:  * @package       Cake.Console.Command.Task
 30:  */
 31: class ProjectTask extends AppShell {
 32: 
 33: /**
 34:  * configs path (used in testing).
 35:  *
 36:  * @var string
 37:  */
 38:     public $configPath = null;
 39: 
 40: /**
 41:  * Checks that given project path does not already exist, and
 42:  * finds the app directory in it. Then it calls bake() with that information.
 43:  *
 44:  * @return mixed
 45:  */
 46:     public function execute() {
 47:         $project = null;
 48:         if (isset($this->args[0])) {
 49:             $project = $this->args[0];
 50:         }
 51: 
 52:         while (!$project) {
 53:             $prompt = __d('cake_console', "What is the path to the project you want to bake?");
 54:             $project = $this->in($prompt, null, APP . 'myapp');
 55:         }
 56: 
 57:         if ($project && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
 58:             $project = $_SERVER['PWD'] . DS . $project;
 59:         }
 60: 
 61:         $response = false;
 62:         while ($response == false && is_dir($project) === true && file_exists($project . 'Config' . 'core.php')) {
 63:             $prompt = __d('cake_console', '<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
 64:             $response = $this->in($prompt, array('y', 'n'), 'n');
 65:             if (strtolower($response) === 'n') {
 66:                 $response = $project = false;
 67:             }
 68:         }
 69: 
 70:         $success = true;
 71:         if ($this->bake($project)) {
 72:             $path = Folder::slashTerm($project);
 73:             if ($this->createHome($path)) {
 74:                 $this->out(__d('cake_console', ' * Welcome page created'));
 75:             } else {
 76:                 $this->err(__d('cake_console', 'The Welcome page was <error>NOT</error> created'));
 77:                 $success = false;
 78:             }
 79: 
 80:             if ($this->securitySalt($path) === true) {
 81:                 $this->out(__d('cake_console', ' * Random hash key created for \'Security.salt\''));
 82:             } else {
 83:                 $this->err(__d('cake_console', 'Unable to generate random hash for \'Security.salt\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
 84:                 $success = false;
 85:             }
 86: 
 87:             if ($this->securityCipherSeed($path) === true) {
 88:                 $this->out(__d('cake_console', ' * Random seed created for \'Security.cipherSeed\''));
 89:             } else {
 90:                 $this->err(__d('cake_console', 'Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
 91:                 $success = false;
 92:             }
 93: 
 94:             if ($this->consolePath($path) === true) {
 95:                 $this->out(__d('cake_console', ' * app/Console/cake.php path set.'));
 96:             } else {
 97:                 $this->err(__d('cake_console', 'Unable to set console path for app/Console.'));
 98:                 $success = false;
 99:             }
100: 
101:             $hardCode = false;
102:             if ($this->cakeOnIncludePath()) {
103:                 $this->out(__d('cake_console', '<info>CakePHP is on your `include_path`. CAKE_CORE_INCLUDE_PATH will be set, but commented out.</info>'));
104:             } else {
105:                 $this->out(__d('cake_console', '<warning>CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.</warning>'));
106:                 $this->out(__d('cake_console', 'You can fix this by adding CakePHP to your `include_path`.'));
107:                 $hardCode = true;
108:             }
109:             $success = $this->corePath($path, $hardCode) === true;
110:             if ($success) {
111:                 $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
112:                 $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH));
113:             } else {
114:                 $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' . DS . 'index.php'));
115:                 $success = false;
116:             }
117:             if ($success && $hardCode) {
118:                 $this->out(__d('cake_console', '   * <warning>Remember to check these values after moving to production server</warning>'));
119:             }
120: 
121:             $Folder = new Folder($path);
122:             if (!$Folder->chmod($path . 'tmp', 0777)) {
123:                 $this->err(__d('cake_console', 'Could not set permissions on %s', $path . DS . 'tmp'));
124:                 $this->out(__d('cake_console', 'chmod -R 0777 %s', $path . DS . 'tmp'));
125:                 $success = false;
126:             }
127:             if ($success) {
128:                 $this->out(__d('cake_console', '<success>Project baked successfully!</success>'));
129:             } else {
130:                 $this->out(__d('cake_console', 'Project baked but with <warning>some issues.</warning>.'));
131:             }
132:             return $path;
133:         }
134:     }
135: 
136: /**
137:  * Checks PHP's include_path for CakePHP.
138:  *
139:  * @return boolean Indicates whether or not CakePHP exists on include_path
140:  */
141:     public function cakeOnIncludePath() {
142:         $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
143:         foreach ($paths as $path) {
144:             if (file_exists($path . DS . 'Cake' . DS . 'bootstrap.php')) {
145:                 return true;
146:             }
147:         }
148:         return false;
149:     }
150: 
151: /**
152:  * Looks for a skeleton template of a Cake application,
153:  * and if not found asks the user for a path. When there is a path
154:  * this method will make a deep copy of the skeleton to the project directory.
155:  *
156:  * @param string $path Project path
157:  * @param string $skel Path to copy from
158:  * @param string $skip array of directories to skip when copying
159:  * @return mixed
160:  */
161:     public function bake($path, $skel = null, $skip = array('empty')) {
162:         if (!$skel && !empty($this->params['skel'])) {
163:             $skel = $this->params['skel'];
164:         }
165:         while (!$skel) {
166:             $skel = $this->in(
167:                 __d('cake_console', "What is the path to the directory layout you wish to copy?"),
168:                 null,
169:                 CAKE . 'Console' . DS . 'Templates' . DS . 'skel'
170:             );
171:             if (!$skel) {
172:                 $this->err(__d('cake_console', 'The directory path you supplied was empty. Please try again.'));
173:             } else {
174:                 while (is_dir($skel) === false) {
175:                     $skel = $this->in(
176:                         __d('cake_console', 'Directory path does not exist please choose another:'),
177:                         null,
178:                         CAKE . 'Console' . DS . 'Templates' . DS . 'skel'
179:                     );
180:                 }
181:             }
182:         }
183: 
184:         $app = basename($path);
185: 
186:         $this->out(__d('cake_console', '<info>Skel Directory</info>: ') . $skel);
187:         $this->out(__d('cake_console', '<info>Will be copied to</info>: ') . $path);
188:         $this->hr();
189: 
190:         $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
191: 
192:         switch (strtolower($looksGood)) {
193:             case 'y':
194:                 $Folder = new Folder($skel);
195:                 if (!empty($this->params['empty'])) {
196:                     $skip = array();
197:                 }
198: 
199:                 if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
200:                     $this->hr();
201:                     $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $app, $path));
202:                     $this->hr();
203:                 } else {
204:                     $this->err(__d('cake_console', "<error>Could not create</error> '%s' properly.", $app));
205:                     return false;
206:                 }
207: 
208:                 foreach ($Folder->messages() as $message) {
209:                     $this->out(String::wrap(' * ' . $message), 1, Shell::VERBOSE);
210:                 }
211: 
212:                 return true;
213:             case 'n':
214:                 unset($this->args[0]);
215:                 $this->execute();
216:                 return false;
217:             case 'q':
218:                 $this->out(__d('cake_console', '<error>Bake Aborted.</error>'));
219:                 return false;
220:         }
221:     }
222: 
223: /**
224:  * Writes a file with a default home page to the project.
225:  *
226:  * @param string $dir Path to project
227:  * @return boolean Success
228:  */
229:     public function createHome($dir) {
230:         $app = basename($dir);
231:         $path = $dir . 'View' . DS . 'Pages' . DS;
232:         $source = CAKE . 'Console' . DS . 'Templates' . DS . 'default' . DS . 'views' . DS . 'home.ctp';
233:         include $source;
234:         return $this->createFile($path . 'home.ctp', $output);
235:     }
236: 
237: /**
238:  * Generates the correct path to the CakePHP libs that are generating the project
239:  * and points app/console/cake.php to the right place
240:  *
241:  * @param string $path Project path.
242:  * @return boolean success
243:  */
244:     public function consolePath($path) {
245:         $File = new File($path . 'Console' . DS . 'cake.php');
246:         $contents = $File->read();
247:         if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) {
248:             $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " \$ds . '" : "'";
249:             $replacement = $root . str_replace(DS, "' . \$ds . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
250:             $result = str_replace($match[0], $replacement, $contents);
251:             if ($File->write($result)) {
252:                 return true;
253:             }
254:             return false;
255:         }
256:         return false;
257:     }
258: 
259: /**
260:  * Generates and writes 'Security.salt'
261:  *
262:  * @param string $path Project path
263:  * @return boolean Success
264:  */
265:     public function securitySalt($path) {
266:         $File = new File($path . 'Config' . DS . 'core.php');
267:         $contents = $File->read();
268:         if (preg_match('/([\s]*Configure::write\(\'Security.salt\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
269:             $string = Security::generateAuthKey();
270:             $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \'' . $string . '\');', $contents);
271:             if ($File->write($result)) {
272:                 return true;
273:             }
274:             return false;
275:         }
276:         return false;
277:     }
278: 
279: /**
280:  * Generates and writes 'Security.cipherSeed'
281:  *
282:  * @param string $path Project path
283:  * @return boolean Success
284:  */
285:     public function securityCipherSeed($path) {
286:         $File = new File($path . 'Config' . DS . 'core.php');
287:         $contents = $File->read();
288:         if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
289:             App::uses('Security', 'Utility');
290:             $string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
291:             $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \'' . $string . '\');', $contents);
292:             if ($File->write($result)) {
293:                 return true;
294:             }
295:             return false;
296:         }
297:         return false;
298:     }
299: 
300: /**
301:  * Generates and writes CAKE_CORE_INCLUDE_PATH
302:  *
303:  * @param string $path Project path
304:  * @param boolean $hardCode Wether or not define calls should be hardcoded.
305:  * @return boolean Success
306:  */
307:     public function corePath($path, $hardCode = true) {
308:         if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
309:             $filename = $path . 'webroot' . DS . 'index.php';
310:             if (!$this->_replaceCorePath($filename, $hardCode)) {
311:                 return false;
312:             }
313:             $filename = $path . 'webroot' . DS . 'test.php';
314:             if (!$this->_replaceCorePath($filename, $hardCode)) {
315:                 return false;
316:             }
317:             return true;
318:         }
319:     }
320: 
321: /**
322:  * Replaces the __CAKE_PATH__ placeholder in the template files.
323:  *
324:  * @param string $filename The filename to operate on.
325:  * @param boolean $hardCode Whether or not the define should be uncommented.
326:  * @return boolean Success
327:  */
328:     protected function _replaceCorePath($filename, $hardCode) {
329:         $contents = file_get_contents($filename);
330: 
331:         $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
332:         $corePath = $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
333: 
334:         $result = str_replace('__CAKE_PATH__', $corePath, $contents, $count);
335:         if ($hardCode) {
336:             $result = str_replace('//define(\'CAKE_CORE', 'define(\'CAKE_CORE', $result);
337:         }
338:         if (!file_put_contents($filename, $result)) {
339:             return false;
340:         }
341:         if ($count == 0) {
342:             return false;
343:         }
344:         return true;
345:     }
346: 
347: /**
348:  * Enables Configure::read('Routing.prefixes') in /app/Config/core.php
349:  *
350:  * @param string $name Name to use as admin routing
351:  * @return boolean Success
352:  */
353:     public function cakeAdmin($name) {
354:         $path = (empty($this->configPath)) ? APP . 'Config' . DS : $this->configPath;
355:         $File = new File($path . 'core.php');
356:         $contents = $File->read();
357:         if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
358:             $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\'' . $name . '\'));', $contents);
359:             if ($File->write($result)) {
360:                 Configure::write('Routing.prefixes', array($name));
361:                 return true;
362:             } else {
363:                 return false;
364:             }
365:         } else {
366:             return false;
367:         }
368:     }
369: 
370: /**
371:  * Checks for Configure::read('Routing.prefixes') and forces user to input it if not enabled
372:  *
373:  * @return string Admin route to use
374:  */
375:     public function getPrefix() {
376:         $admin = '';
377:         $prefixes = Configure::read('Routing.prefixes');
378:         if (!empty($prefixes)) {
379:             if (count($prefixes) == 1) {
380:                 return $prefixes[0] . '_';
381:             }
382:             if ($this->interactive) {
383:                 $this->out();
384:                 $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
385:             }
386:             $options = array();
387:             foreach ($prefixes as $i => $prefix) {
388:                 $options[] = $i + 1;
389:                 if ($this->interactive) {
390:                     $this->out($i + 1 . '. ' . $prefix);
391:                 }
392:             }
393:             $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
394:             return $prefixes[$selection - 1] . '_';
395:         }
396:         if ($this->interactive) {
397:             $this->hr();
398:             $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
399:             $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
400:             $this->out(__d('cake_console', 'Example: www.example.com/admin/controller'));
401:             while ($admin == '') {
402:                 $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
403:             }
404:             if ($this->cakeAdmin($admin) !== true) {
405:                 $this->out(__d('cake_console', '<error>Unable to write to</error> /app/Config/core.php.'));
406:                 $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
407:                 $this->_stop();
408:             }
409:             return $admin . '_';
410:         }
411:         return '';
412:     }
413: 
414: /**
415:  * get the option parser.
416:  *
417:  * @return ConsoleOptionParser
418:  */
419:     public function getOptionParser() {
420:         $parser = parent::getOptionParser();
421:         return $parser->description(
422:                 __d('cake_console', 'Generate a new CakePHP project skeleton.')
423:             )->addArgument('name', array(
424:                 'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
425:             ))->addOption('empty', array(
426:                 'boolean' => true,
427:                 'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
428:             ))->addOption('skel', array(
429:                 'default' => current(App::core('Console')) . 'Templates' . DS . 'skel',
430:                 'help' => __d('cake_console', 'The directory layout to use for the new application skeleton. Defaults to cake/Console/Templates/skel of CakePHP used to create the project.')
431:             ));
432:     }
433: 
434: }
435: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs