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.2 API

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