root/namespace-global-local

Revision 1511, 3.3 kB (checked in by Torben Brodt, 3 months ago)

update namespace tools

  • Property svn:executable set to *
Line 
1#!/usr/bin/php
2<?php
3# places real namespaces in use clauses, removes leading slashs in use statements
4# removes global classes from use clauses and adds leading backslash in code
5if(!isset($_SERVER['argv'][1]) || in_array($_SERVER['argv'][1], array('--help', '-h', '-?'))) {
6        help();
7        exit;
8}
9$dir = $_SERVER['argv'][1];
10$pattern = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : '/\.php$/';
11
12$ite = new RecursiveDirectoryIterator($dir);
13foreach (new RecursiveIteratorIterator($ite) as $filename => $cur) {
14        if(preg_match($pattern, $filename)) {
15                updateFile($filename);
16        }
17}
18
19function updateFile($filename) {
20        $useblock = $block = array();
21        $updated = false;
22        foreach(file($filename) as $line) {
23                if(preg_match('/^use /', $line)) {
24                        $useblock[] = $line;
25                } else {
26                        if($useblock) {
27                                $compare1 = implode("", $useblock);
28
29                                // strip leading backslash
30                                foreach($useblock as &$row) {
31                                        $row = preg_replace('/^use \\\/', 'use ', $row);
32                                }
33
34                                $compare2 = implode("", $useblock);
35                                $updated = $updated || $compare1 !== $compare2;
36                                $block = array_merge($block, $useblock);
37                                $useblock = array();
38                        }
39                        $block[] = $line;
40                }
41        }
42       
43        echo $filename;
44        if($updated) {
45                file_put_contents($filename, implode("", $block));
46                echo "\tchanged\n";
47        } else {
48                echo "\tunchanged\n";
49        }
50}
51
52function help() {
53        echo "orders use clauses";
54        echo "\n";
55        echo "Usage: " . basename($_SERVER['argv'][0]) . " folder [--help]\n";
56        echo "\n";
57        echo "The options are as follows:\n";
58        echo "  -?, --help\n";
59        echo "    Show this help and exit.\n\n";
60}
61
62/**
63 *
64 */
65class Tree {
66
67        /**
68         * @var array
69         */
70        protected $tree = array();
71
72        /**
73         *
74         * @param       $x              string
75         * @return                      array
76         */
77        protected function readConf($x) {
78                return explode('\\', $x);
79        }
80
81        /**
82         * config as string
83         * @param       $conf           string
84         */
85        public function __construct(array $lines) {
86                $in = array_map(array($this, 'readConf'), $lines);
87                foreach($in as $row) {
88                        $this->addIndex($this->tree, $row);
89                }
90        }
91       
92        protected function sort(&$out) {
93                // sort: directory first
94                $dir = $file = $keymap = array();
95                foreach(array_keys($out) as $i => $key) {
96                        if(strpos($key, ';') !== false) {
97                                $file[$i] = strtolower($key);
98                        } else {
99                                $dir[$i] = strtolower($key);
100                        }
101                        $keymap[$i] = $key;
102                }
103                // sort: alphabetical order
104                asort($dir);
105                asort($file);
106                $sort = array_merge(array_keys($dir), array_keys($file));
107
108                // link references
109                $copy = array();
110                foreach($sort as $i) {
111                        $copy[$keymap[$i]] = $out[$keymap[$i]];
112                }
113                $out = $copy;
114        }
115
116        /**
117         * recursive function
118         */
119        protected function addIndex(&$out, $rows, $prefixtree = array(), $level = 0) {
120                if(!isset($rows[$level])) {
121                        return;
122                }
123
124                $name = $rows[$level];
125                $prefixtree[] = $name;
126                if(!isset($out[$name])) {
127                        $out[$name] = array();
128                }
129                if(count($rows) - 1 == $level) {
130                        $old = $prefixtree;
131                        $out[$name]['index'] = implode('\\', $prefixtree);
132                }
133
134                $this->sort($out);
135                $this->addIndex($out[$name], $rows, $prefixtree, $level + 1);
136        }
137
138        /**
139         *
140         */
141        public function getTree() {
142                return $this->tree;
143        }
144       
145        /**
146         *
147         */
148        public function getLines($tree = null, &$str = array()) {
149                if($tree === null) {
150                        $tree = $this->tree;
151                }
152                foreach($tree as $val) {
153                        if(is_array($val)) {
154                                $this->getLines($val, $str);
155                        }
156                        else {
157                                $str[] = $val;
158                        }
159                }
160                return $str;
161        }
162}
Note: See TracBrowser for help on using the browser.