40ccde542c09bbe54857f9434382836d18e31051
[Pman.Core] / Process / FixDataObjectCtor.php
1 <?php
2
3 /*
4  *
5  *
6 find . | grep php | awk '{ print "php admin.php Core/Process/FixCode -f " $1 }' | sh
7 */
8
9 require_once 'Pman.php';
10
11 class Pman_Core_Process_FixDataObjectCtor extends Pman 
12 {
13     static $cli_desc = "Coverts old code using replacements"; 
14     
15     static $cli_opts = array(
16         'file' => array(
17             'desc' => 'File to process',
18             'short' => 'f',
19             'min' => 1,
20             'max' => 1,
21             
22         ),
23         
24     );
25     
26     function getAuth() {
27         $ff = HTML_FlexyFramework::get();
28         
29         if (!$ff->cli) {
30             die("cli only");
31         }
32         
33     }
34     
35     
36     
37     function get($p,$opts)
38     {
39         $file = realpath($opts['file']);
40         if (!file_exists($file) || !is_writable($file)) {
41             echo "$file: NOT readable or writable\n";
42             exit;
43         }
44         
45         $c = file_get_contents($file);
46         $old_c = $c;
47         if (strpos($c, '<?php') === false) {
48             echo "$file: NOT PHP\n";
49             exit;
50         }
51         $c = preg_replace("/new DataObjects_([a-z_]+)/i", "DB_DataObject::factory('\\1')", $c);
52         
53         $c = preg_replace("/DataObjects_([a-z_]+)::/i", "DB_DataObject::factory('\\1')::", $c);
54
55       
56         if ($old_c == $c) {
57             echo "$file: SKIP NO CHANGES\n";
58             exit;
59         }
60         echo "$file: WRITE NEW FILE\n";
61         file_put_contents($file,$c);
62         exit;
63         
64     }
65     
66 }