root/namespace-use-order

Revision 1511, 3.1 kB (checked in by Torben Brodt, 16 months ago)

update namespace tools

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