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

  • AclShell
  • ApiShell
  • BakeShell
  • CommandListShell
  • ConsoleShell
  • I18nShell
  • SchemaShell
  • TestShell
  • TestsuiteShell
  • UpgradeShell
  1: <?php
  2: /**
  3:  * Upgrade Shell
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       Cake.Console.Command
 16:  * @since         CakePHP(tm) v 2.0
 17:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18:  */
 19: 
 20: App::uses('AppShell', 'Console/Command');
 21: App::uses('Folder', 'Utility');
 22: 
 23: /**
 24:  * A shell class to help developers upgrade applications to CakePHP 2.0
 25:  *
 26:  * @package       Cake.Console.Command
 27:  */
 28: class UpgradeShell extends AppShell {
 29: 
 30: /**
 31:  * Files
 32:  *
 33:  * @var array
 34:  */
 35:     protected $_files = array();
 36: 
 37: /**
 38:  * Paths
 39:  *
 40:  * @var array
 41:  */
 42:     protected $_paths = array();
 43: 
 44: /**
 45:  * Map
 46:  *
 47:  * @var array
 48:  */
 49:     protected $_map = array(
 50:         'Controller' => 'Controller',
 51:         'Component' => 'Controller/Component',
 52:         'Model' => 'Model',
 53:         'Behavior' => 'Model/Behavior',
 54:         'Datasource' => 'Model/Datasource',
 55:         'Dbo' => 'Model/Datasource/Database',
 56:         'View' => 'View',
 57:         'Helper' => 'View/Helper',
 58:         'Shell' => 'Console/Command',
 59:         'Task' => 'Console/Command/Task',
 60:         'Case' => 'Test/Case',
 61:         'Fixture' => 'Test/Fixture',
 62:         'Error' => 'Lib/Error',
 63:     );
 64: 
 65: /**
 66:  * Shell startup, prints info message about dry run.
 67:  *
 68:  * @return void
 69:  */
 70:     public function startup() {
 71:         parent::startup();
 72:         if ($this->params['dry-run']) {
 73:             $this->out(__d('cake_console', '<warning>Dry-run mode enabled!</warning>'), 1, Shell::QUIET);
 74:         }
 75:         if ($this->params['git'] && !is_dir('.git')) {
 76:             $this->out(__d('cake_console', '<warning>No git repository detected!</warning>'), 1, Shell::QUIET);
 77:         }
 78:     }
 79: 
 80: /**
 81:  * Run all upgrade steps one at a time
 82:  *
 83:  * @return void
 84:  */
 85:     public function all() {
 86:         foreach ($this->OptionParser->subcommands() as $command) {
 87:             $name = $command->name();
 88:             if ($name === 'all') {
 89:                 continue;
 90:             }
 91:             $this->out(__d('cake_console', 'Running %s', $name));
 92:             $this->$name();
 93:         }
 94:     }
 95: 
 96: /**
 97:  * Update tests.
 98:  *
 99:  * - Update tests class names to FooTest rather than FooTestCase.
100:  *
101:  * @return void
102:  */
103:     public function tests() {
104:         $this->_paths = array(APP . 'tests' . DS);
105:         if (!empty($this->params['plugin'])) {
106:             $this->_paths = array(App::pluginPath($this->params['plugin']) . 'tests' . DS);
107:         }
108:         $patterns = array(
109:             array(
110:                 '*TestCase extends CakeTestCase to *Test extends CakeTestCase',
111:                 '/([a-zA-Z]*Test)Case extends CakeTestCase/',
112:                 '\1 extends CakeTestCase'
113:             ),
114:         );
115: 
116:         $this->_filesRegexpUpdate($patterns);
117:     }
118: 
119: /**
120:  * Move files and folders to their new homes
121:  *
122:  * Moves folders containing files which cannot necessarily be auto-detected (libs and templates)
123:  * and then looks for all php files except vendors, and moves them to where Cake 2.0 expects
124:  * to find them.
125:  *
126:  * @return void
127:  */
128:     public function locations() {
129:         $cwd = getcwd();
130: 
131:         if (!empty($this->params['plugin'])) {
132:             chdir(App::pluginPath($this->params['plugin']));
133:         }
134: 
135:         if (is_dir('plugins')) {
136:             $Folder = new Folder('plugins');
137:             list($plugins) = $Folder->read();
138:             foreach ($plugins as $plugin) {
139:                 chdir($cwd . DS . 'plugins' . DS . $plugin);
140:                 $this->locations();
141:             }
142:             $this->_files = array();
143:             chdir($cwd);
144:         }
145:         $moves = array(
146:             'config' => 'Config',
147:             'Config' . DS . 'schema' => 'Config' . DS . 'Schema',
148:             'libs' => 'Lib',
149:             'tests' => 'Test',
150:             'views' => 'View',
151:             'models' => 'Model',
152:             'Model' . DS . 'behaviors' => 'Model' . DS . 'Behavior',
153:             'Model' . DS . 'datasources' => 'Model' . DS . 'Datasource',
154:             'Test' . DS . 'cases' => 'Test' . DS . 'Case',
155:             'Test' . DS . 'fixtures' => 'Test' . DS . 'Fixture',
156:             'vendors' . DS . 'shells' . DS . 'templates' => 'Console' . DS . 'Templates',
157:         );
158:         foreach ($moves as $old => $new) {
159:             if (is_dir($old)) {
160:                 $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
161:                 if (!$this->params['dry-run']) {
162:                     if ($this->params['git']) {
163:                         exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
164:                         exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
165:                     } else {
166:                         $Folder = new Folder($old);
167:                         $Folder->move($new);
168:                     }
169:                 }
170:             }
171:         }
172: 
173:         $this->_moveViewFiles();
174:         $this->_moveAppClasses();
175: 
176:         $sourceDirs = array(
177:             '.' => array('recursive' => false),
178:             'Console',
179:             'controllers',
180:             'Controller',
181:             'Lib' => array('checkFolder' => false),
182:             'models',
183:             'Model',
184:             'tests',
185:             'Test' => array('regex' => '@class (\S*Test) extends CakeTestCase@'),
186:             'views',
187:             'View',
188:             'vendors/shells',
189:         );
190: 
191:         $defaultOptions = array(
192:             'recursive' => true,
193:             'checkFolder' => true,
194:             'regex' => '@class (\S*) .*(\s|\v)*{@i'
195:         );
196:         foreach ($sourceDirs as $dir => $options) {
197:             if (is_numeric($dir)) {
198:                 $dir = $options;
199:                 $options = array();
200:             }
201:             $options = array_merge($defaultOptions, $options);
202:             $this->_movePhpFiles($dir, $options);
203:         }
204:     }
205: 
206: /**
207:  * Update helpers.
208:  *
209:  * - Converts helpers usage to new format.
210:  *
211:  * @return void
212:  */
213:     public function helpers() {
214:         $this->_paths = array_diff(App::path('views'), App::core('views'));
215: 
216:         if (!empty($this->params['plugin'])) {
217:             $this->_paths = array(App::pluginPath($this->params['plugin']) . 'views' . DS);
218:         }
219: 
220:         $patterns = array();
221:         App::build(array(
222:             'View/Helper' => App::core('View/Helper'),
223:         ), App::APPEND);
224:         $helpers = App::objects('helper');
225:         $plugins = App::objects('plugin');
226:         $pluginHelpers = array();
227:         foreach ($plugins as $plugin) {
228:             CakePlugin::load($plugin);
229:             $pluginHelpers = array_merge(
230:                 $pluginHelpers,
231:                 App::objects('helper', App::pluginPath($plugin) . DS . 'views' . DS . 'helpers' . DS, false)
232:             );
233:         }
234:         $helpers = array_merge($pluginHelpers, $helpers);
235:         foreach ($helpers as $helper) {
236:             $helper = preg_replace('/Helper$/', '', $helper);
237:             $oldHelper = $helper;
238:             $oldHelper{0} = strtolower($oldHelper{0});
239:             $patterns[] = array(
240:                 "\${$oldHelper} to \$this->{$helper}",
241:                 "/\\\${$oldHelper}->/",
242:                 "\\\$this->{$helper}->"
243:             );
244:         }
245: 
246:         $this->_filesRegexpUpdate($patterns);
247:     }
248: 
249: /**
250:  * Update i18n.
251:  *
252:  * - Removes extra true param.
253:  * - Add the echo to __*() calls that didn't need them before.
254:  *
255:  * @return void
256:  */
257:     public function i18n() {
258:         $this->_paths = array(
259:             APP
260:         );
261:         if (!empty($this->params['plugin'])) {
262:             $this->_paths = array(App::pluginPath($this->params['plugin']));
263:         }
264: 
265:         $patterns = array(
266:             array(
267:                 '<?php __*(*) to <?php echo __*(*)',
268:                 '/<\?php\s*(__[a-z]*\(.*?\))/',
269:                 '<?php echo \1'
270:             ),
271:             array(
272:                 '<?php __*(*, true) to <?php echo __*()',
273:                 '/<\?php\s*(__[a-z]*\(.*?)(,\s*true)(\))/',
274:                 '<?php echo \1\3'
275:             ),
276:             array('__*(*, true) to __*(*)', '/(__[a-z]*\(.*?)(,\s*true)(\))/', '\1\3')
277:         );
278: 
279:         $this->_filesRegexpUpdate($patterns);
280:     }
281: 
282: /**
283:  * Upgrade the removed basics functions.
284:  *
285:  * - a(*) -> array(*)
286:  * - e(*) -> echo *
287:  * - ife(*, *, *) -> !empty(*) ? * : *
288:  * - a(*) -> array(*)
289:  * - r(*, *, *) -> str_replace(*, *, *)
290:  * - up(*) -> strtoupper(*)
291:  * - low(*, *, *) -> strtolower(*)
292:  * - getMicrotime() -> microtime(true)
293:  *
294:  * @return void
295:  */
296:     public function basics() {
297:         $this->_paths = array(
298:             APP
299:         );
300:         if (!empty($this->params['plugin'])) {
301:             $this->_paths = array(App::pluginPath($this->params['plugin']));
302:         }
303:         $patterns = array(
304:             array(
305:                 'a(*) -> array(*)',
306:                 '/\ba\((.*)\)/',
307:                 'array(\1)'
308:             ),
309:             array(
310:                 'e(*) -> echo *',
311:                 '/\be\((.*)\)/',
312:                 'echo \1'
313:             ),
314:             array(
315:                 'ife(*, *, *) -> !empty(*) ? * : *',
316:                 '/ife\((.*), (.*), (.*)\)/',
317:                 '!empty(\1) ? \2 : \3'
318:             ),
319:             array(
320:                 'r(*, *, *) -> str_replace(*, *, *)',
321:                 '/\br\(/',
322:                 'str_replace('
323:             ),
324:             array(
325:                 'up(*) -> strtoupper(*)',
326:                 '/\bup\(/',
327:                 'strtoupper('
328:             ),
329:             array(
330:                 'low(*) -> strtolower(*)',
331:                 '/\blow\(/',
332:                 'strtolower('
333:             ),
334:             array(
335:                 'getMicrotime() -> microtime(true)',
336:                 '/getMicrotime\(\)/',
337:                 'microtime(true)'
338:             ),
339:         );
340:         $this->_filesRegexpUpdate($patterns);
341:     }
342: 
343: /**
344:  * Update the properties moved to CakeRequest.
345:  *
346:  * @return void
347:  */
348:     public function request() {
349:         $views = array_diff(App::path('views'), App::core('views'));
350:         $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
351:         $components = array_diff(App::path('components'), App::core('components'));
352: 
353:         $this->_paths = array_merge($views, $controllers, $components);
354: 
355:         if (!empty($this->params['plugin'])) {
356:             $pluginPath = App::pluginPath($this->params['plugin']);
357:             $this->_paths = array(
358:                 $pluginPath . 'controllers' . DS,
359:                 $pluginPath . 'controllers' . DS . 'components' . DS,
360:                 $pluginPath . 'views' . DS,
361:             );
362:         }
363:         $patterns = array(
364:             array(
365:                 '$this->data -> $this->request->data',
366:                 '/(\$this->data\b(?!\())/',
367:                 '$this->request->data'
368:             ),
369:             array(
370:                 '$this->params -> $this->request->params',
371:                 '/(\$this->params\b(?!\())/',
372:                 '$this->request->params'
373:             ),
374:             array(
375:                 '$this->webroot -> $this->request->webroot',
376:                 '/(\$this->webroot\b(?!\())/',
377:                 '$this->request->webroot'
378:             ),
379:             array(
380:                 '$this->base -> $this->request->base',
381:                 '/(\$this->base\b(?!\())/',
382:                 '$this->request->base'
383:             ),
384:             array(
385:                 '$this->here -> $this->request->here',
386:                 '/(\$this->here\b(?!\())/',
387:                 '$this->request->here'
388:             ),
389:             array(
390:                 '$this->action -> $this->request->action',
391:                 '/(\$this->action\b(?!\())/',
392:                 '$this->request->action'
393:             ),
394:         );
395:         $this->_filesRegexpUpdate($patterns);
396:     }
397: 
398: /**
399:  * Update Configure::read() calls with no params.
400:  *
401:  * @return void
402:  */
403:     public function configure() {
404:         $this->_paths = array(
405:             APP
406:         );
407:         if (!empty($this->params['plugin'])) {
408:             $this->_paths = array(App::pluginPath($this->params['plugin']));
409:         }
410:         $patterns = array(
411:             array(
412:                 "Configure::read() -> Configure::read('debug')",
413:                 '/Configure::read\(\)/',
414:                 'Configure::read(\'debug\')'
415:             ),
416:         );
417:         $this->_filesRegexpUpdate($patterns);
418:     }
419: 
420: /**
421:  * constants
422:  *
423:  * @return void
424:  */
425:     public function constants() {
426:         $this->_paths = array(
427:             APP
428:         );
429:         if (!empty($this->params['plugin'])) {
430:             $this->_paths = array(App::pluginPath($this->params['plugin']));
431:         }
432:         $patterns = array(
433:             array(
434:                 "LIBS -> CAKE",
435:                 '/\bLIBS\b/',
436:                 'CAKE'
437:             ),
438:             array(
439:                 "CONFIGS -> APP . 'Config' . DS",
440:                 '/\bCONFIGS\b/',
441:                 'APP . \'Config\' . DS'
442:             ),
443:             array(
444:                 "CONTROLLERS -> APP . 'Controller' . DS",
445:                 '/\bCONTROLLERS\b/',
446:                 'APP . \'Controller\' . DS'
447:             ),
448:             array(
449:                 "COMPONENTS -> APP . 'Controller' . DS . 'Component' . DS",
450:                 '/\bCOMPONENTS\b/',
451:                 'APP . \'Controller\' . DS . \'Component\''
452:             ),
453:             array(
454:                 "MODELS -> APP . 'Model' . DS",
455:                 '/\bMODELS\b/',
456:                 'APP . \'Model\' . DS'
457:             ),
458:             array(
459:                 "BEHAVIORS -> APP . 'Model' . DS . 'Behavior' . DS",
460:                 '/\bBEHAVIORS\b/',
461:                 'APP . \'Model\' . DS . \'Behavior\' . DS'
462:             ),
463:             array(
464:                 "VIEWS -> APP . 'View' . DS",
465:                 '/\bVIEWS\b/',
466:                 'APP . \'View\' . DS'
467:             ),
468:             array(
469:                 "HELPERS -> APP . 'View' . DS . 'Helper' . DS",
470:                 '/\bHELPERS\b/',
471:                 'APP . \'View\' . DS . \'Helper\' . DS'
472:             ),
473:             array(
474:                 "LAYOUTS -> APP . 'View' . DS . 'Layouts' . DS",
475:                 '/\bLAYOUTS\b/',
476:                 'APP . \'View\' . DS . \'Layouts\' . DS'
477:             ),
478:             array(
479:                 "ELEMENTS -> APP . 'View' . DS . 'Elements' . DS",
480:                 '/\bELEMENTS\b/',
481:                 'APP . \'View\' . DS . \'Elements\' . DS'
482:             ),
483:             array(
484:                 "CONSOLE_LIBS -> CAKE . 'Console' . DS",
485:                 '/\bCONSOLE_LIBS\b/',
486:                 'CAKE . \'Console\' . DS'
487:             ),
488:             array(
489:                 "CAKE_TESTS_LIB -> CAKE . 'TestSuite' . DS",
490:                 '/\bCAKE_TESTS_LIB\b/',
491:                 'CAKE . \'TestSuite\' . DS'
492:             ),
493:             array(
494:                 "CAKE_TESTS -> CAKE . 'Test' . DS",
495:                 '/\bCAKE_TESTS\b/',
496:                 'CAKE . \'Test\' . DS'
497:             )
498:         );
499:         $this->_filesRegexpUpdate($patterns);
500:     }
501: 
502: /**
503:  * Update components.
504:  *
505:  * - Make components that extend Object to extend Component.
506:  *
507:  * @return void
508:  */
509:     public function components() {
510:         $this->_paths = App::Path('Controller/Component');
511:         if (!empty($this->params['plugin'])) {
512:             $this->_paths = App::Path('Controller/Component', $this->params['plugin']);
513:         }
514:         $patterns = array(
515:             array(
516:                 '*Component extends Object to *Component extends Component',
517:                 '/([a-zA-Z]*Component extends) Object/',
518:                 '\1 Component'
519:             ),
520:         );
521: 
522:         $this->_filesRegexpUpdate($patterns);
523:     }
524: 
525: /**
526:  * Replace cakeError with built-in exceptions.
527:  * NOTE: this ignores calls where you've passed your own secondary parameters to cakeError().
528:  * @return void
529:  */
530:     public function exceptions() {
531:         $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
532:         $components = array_diff(App::path('components'), App::core('components'));
533: 
534:         $this->_paths = array_merge($controllers, $components);
535: 
536:         if (!empty($this->params['plugin'])) {
537:             $pluginPath = App::pluginPath($this->params['plugin']);
538:             $this->_paths = array(
539:                 $pluginPath . 'controllers' . DS,
540:                 $pluginPath . 'controllers' . DS . 'components' . DS,
541:             );
542:         }
543:         $patterns = array(
544:             array(
545:                 '$this->cakeError("error400") -> throw new BadRequestException()',
546:                 '/(\$this->cakeError\(["\']error400["\']\));/',
547:                 'throw new BadRequestException();'
548:             ),
549:             array(
550:                 '$this->cakeError("error404") -> throw new NotFoundException()',
551:                 '/(\$this->cakeError\(["\']error404["\']\));/',
552:                 'throw new NotFoundException();'
553:             ),
554:             array(
555:                 '$this->cakeError("error500") -> throw new InternalErrorException()',
556:                 '/(\$this->cakeError\(["\']error500["\']\));/',
557:                 'throw new InternalErrorException();'
558:             ),
559:         );
560:         $this->_filesRegexpUpdate($patterns);
561:     }
562: 
563: /**
564:  * Move application views files to where they now should be
565:  *
566:  * Find all view files in the folder and determine where cake expects the file to be
567:  *
568:  * @return void
569:  */
570:     protected function _moveViewFiles() {
571:         if (!is_dir('View')) {
572:             return;
573:         }
574: 
575:         $dirs = scandir('View');
576:         foreach ($dirs as $old) {
577:             if (!is_dir('View' . DS . $old) || $old === '.' || $old === '..') {
578:                 continue;
579:             }
580: 
581:             $new = 'View' . DS . Inflector::camelize($old);
582:             $old = 'View' . DS . $old;
583:             if ($new == $old) {
584:                 continue;
585:             }
586: 
587:             $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
588:             if (!$this->params['dry-run']) {
589:                 if ($this->params['git']) {
590:                     exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
591:                     exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
592:                 } else {
593:                     $Folder = new Folder($old);
594:                     $Folder->move($new);
595:                 }
596:             }
597:         }
598:     }
599: 
600: /**
601:  * Move the AppController, and AppModel classes.
602:  *
603:  * @return void
604:  */
605:     protected function _moveAppClasses() {
606:         $files = array(
607:             APP . 'app_controller.php' => APP . 'Controller' . DS . 'AppController.php',
608:             APP . 'controllers' . DS . 'app_controller.php' => APP . 'Controller' . DS . 'AppController.php',
609:             APP . 'app_model.php' => APP . 'Model' . DS . 'AppModel.php',
610:             APP . 'models' . DS . 'app_model.php' => APP . 'Model' . DS . 'AppModel.php',
611:         );
612:         foreach ($files as $old => $new) {
613:             if (file_exists($old)) {
614:                 $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
615: 
616:                 if ($this->params['dry-run']) {
617:                     continue;
618:                 }
619:                 if ($this->params['git']) {
620:                     exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
621:                     exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
622:                 } else {
623:                     rename($old, $new);
624:                 }
625:             }
626:         }
627:     }
628: 
629: /**
630:  * Move application php files to where they now should be
631:  *
632:  * Find all php files in the folder (honoring recursive) and determine where cake expects the file to be
633:  * If the file is not exactly where cake expects it - move it.
634:  *
635:  * @param mixed $path
636:  * @param mixed $options array(recursive, checkFolder)
637:  * @return void
638:  */
639:     protected function _movePhpFiles($path, $options) {
640:         if (!is_dir($path)) {
641:             return;
642:         }
643: 
644:         $paths = $this->_paths;
645: 
646:         $this->_paths = array($path);
647:         $this->_files = array();
648:         if ($options['recursive']) {
649:             $this->_findFiles('php');
650:         } else {
651:             $this->_files = scandir($path);
652:             foreach ($this->_files as $i => $file) {
653:                 if (strlen($file) < 5 || substr($file, -4) !== '.php') {
654:                     unset($this->_files[$i]);
655:                 }
656:             }
657:         }
658: 
659:         $cwd = getcwd();
660:         foreach ($this->_files as &$file) {
661:             $file = $cwd . DS . $file;
662: 
663:             $contents = file_get_contents($file);
664:             preg_match($options['regex'], $contents, $match);
665:             if (!$match) {
666:                 continue;
667:             }
668: 
669:             $class = $match[1];
670: 
671:             if (substr($class, 0, 3) === 'Dbo') {
672:                 $type = 'Dbo';
673:             } else {
674:                 preg_match('@([A-Z][^A-Z]*)$@', $class, $match);
675:                 if ($match) {
676:                     $type = $match[1];
677:                 } else {
678:                     $type = 'unknown';
679:                 }
680:             }
681: 
682:             preg_match('@^.*[\\\/]plugins[\\\/](.*?)[\\\/]@', $file, $match);
683:             $base = $cwd . DS;
684:             $plugin = false;
685:             if ($match) {
686:                 $base = $match[0];
687:                 $plugin = $match[1];
688:             }
689: 
690:             if ($options['checkFolder'] && !empty($this->_map[$type])) {
691:                 $folder = str_replace('/', DS, $this->_map[$type]);
692:                 $new = $base . $folder . DS . $class . '.php';
693:             } else {
694:                 $new = dirname($file) . DS . $class . '.php';
695:             }
696: 
697:             if ($file === $new) {
698:                 continue;
699:             }
700: 
701:             $dir = dirname($new);
702:             if (!is_dir($dir)) {
703:                 new Folder($dir, true);
704:             }
705: 
706:             $this->out(__d('cake_console', 'Moving %s to %s', $file, $new), 1, Shell::VERBOSE);
707:             if (!$this->params['dry-run']) {
708:                 if ($this->params['git']) {
709:                     exec('git mv -f ' . escapeshellarg($file) . ' ' . escapeshellarg($file . '__'));
710:                     exec('git mv -f ' . escapeshellarg($file . '__') . ' ' . escapeshellarg($new));
711:                 } else {
712:                     rename($file, $new);
713:                 }
714:             }
715:         }
716: 
717:         $this->_paths = $paths;
718:     }
719: 
720: /**
721:  * Updates files based on regular expressions.
722:  *
723:  * @param array $patterns Array of search and replacement patterns.
724:  * @return void
725:  */
726:     protected function _filesRegexpUpdate($patterns) {
727:         $this->_findFiles($this->params['ext']);
728:         foreach ($this->_files as $file) {
729:             $this->out(__d('cake_console', 'Updating %s...', $file), 1, Shell::VERBOSE);
730:             $this->_updateFile($file, $patterns);
731:         }
732:     }
733: 
734: /**
735:  * Searches the paths and finds files based on extension.
736:  *
737:  * @param string $extensions
738:  * @return void
739:  */
740:     protected function _findFiles($extensions = '') {
741:         $this->_files = array();
742:         foreach ($this->_paths as $path) {
743:             if (!is_dir($path)) {
744:                 continue;
745:             }
746:             $Iterator = new RegexIterator(
747:                 new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
748:                 '/^.+\.(' . $extensions . ')$/i',
749:                 RegexIterator::MATCH
750:             );
751:             foreach ($Iterator as $file) {
752:                 if ($file->isFile()) {
753:                     $this->_files[] = $file->getPathname();
754:                 }
755:             }
756:         }
757:     }
758: 
759: /**
760:  * Update a single file.
761:  *
762:  * @param string $file The file to update
763:  * @param array $patterns The replacement patterns to run.
764:  * @return void
765:  */
766:     protected function _updateFile($file, $patterns) {
767:         $contents = file_get_contents($file);
768: 
769:         foreach ($patterns as $pattern) {
770:             $this->out(__d('cake_console', ' * Updating %s', $pattern[0]), 1, Shell::VERBOSE);
771:             $contents = preg_replace($pattern[1], $pattern[2], $contents);
772:         }
773: 
774:         $this->out(__d('cake_console', 'Done updating %s', $file), 1);
775:         if (!$this->params['dry-run']) {
776:             file_put_contents($file, $contents);
777:         }
778:     }
779: 
780: /**
781:  * get the option parser
782:  *
783:  * @return ConsoleOptionParser
784:  */
785:     public function getOptionParser() {
786:         $subcommandParser = array(
787:             'options' => array(
788:                 'plugin' => array(
789:                     'short' => 'p',
790:                     'help' => __d('cake_console', 'The plugin to update. Only the specified plugin will be updated.')
791:                 ),
792:                 'ext' => array(
793:                     'short' => 'e',
794:                     'help' => __d('cake_console', 'The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
795:                     'default' => 'php|ctp|thtml|inc|tpl'
796:                 ),
797:                 'git' => array(
798:                     'short' => 'g',
799:                     'help' => __d('cake_console', 'Use git command for moving files around.'),
800:                     'boolean' => true
801:                 ),
802:                 'dry-run' => array(
803:                     'short' => 'd',
804:                     'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
805:                     'boolean' => true
806:                 )
807:             )
808:         );
809: 
810:         return parent::getOptionParser()
811:             ->description(__d('cake_console', "A shell to help automate upgrading from CakePHP 1.3 to 2.0. \n" .
812:                 "Be sure to have a backup of your application before running these commands."))
813:             ->addSubcommand('all', array(
814:                 'help' => __d('cake_console', 'Run all upgrade commands.'),
815:                 'parser' => $subcommandParser
816:             ))
817:             ->addSubcommand('tests', array(
818:                 'help' => __d('cake_console', 'Update tests class names to FooTest rather than FooTestCase.'),
819:                 'parser' => $subcommandParser
820:             ))
821:             ->addSubcommand('locations', array(
822:                 'help' => __d('cake_console', 'Move files and folders to their new homes.'),
823:                 'parser' => $subcommandParser
824:             ))
825:             ->addSubcommand('i18n', array(
826:                 'help' => __d('cake_console', 'Update the i18n translation method calls.'),
827:                 'parser' => $subcommandParser
828:             ))
829:             ->addSubcommand('helpers', array(
830:                 'help' => __d('cake_console', 'Update calls to helpers.'),
831:                 'parser' => $subcommandParser
832:             ))
833:             ->addSubcommand('basics', array(
834:                 'help' => __d('cake_console', 'Update removed basics functions to PHP native functions.'),
835:                 'parser' => $subcommandParser
836:             ))
837:             ->addSubcommand('request', array(
838:                 'help' => __d('cake_console', 'Update removed request access, and replace with $this->request.'),
839:                 'parser' => $subcommandParser
840:             ))
841:             ->addSubcommand('configure', array(
842:                 'help' => __d('cake_console', "Update Configure::read() to Configure::read('debug')"),
843:                 'parser' => $subcommandParser
844:             ))
845:             ->addSubcommand('constants', array(
846:                 'help' => __d('cake_console', "Replace Obsolete constants"),
847:                 'parser' => $subcommandParser
848:             ))
849:             ->addSubcommand('components', array(
850:                 'help' => __d('cake_console', 'Update components to extend Component class.'),
851:                 'parser' => $subcommandParser
852:             ))
853:             ->addSubcommand('exceptions', array(
854:                 'help' => __d('cake_console', 'Replace use of cakeError with exceptions.'),
855:                 'parser' => $subcommandParser
856:             ));
857:     }
858: 
859: }
860: 
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