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