1 | <?php |
---|
2 | header("Content-Type: application/json"); |
---|
3 | |
---|
4 | include_once('lib/parser2/dojo2.inc'); |
---|
5 | |
---|
6 | // no dojo.require() call made? |
---|
7 | $files = dojo_get_files(); |
---|
8 | foreach ($files as $set){ |
---|
9 | list($namespace, $file) = $set; |
---|
10 | $data[$namespace][] = $file; |
---|
11 | } |
---|
12 | $namespaces = array_keys($data); |
---|
13 | |
---|
14 | // $data takes the form of $namespace[], where the value is the rest of the file. So let's break it up. |
---|
15 | // note that $namespaces is the root of each branch. |
---|
16 | $id_counter = 0; |
---|
17 | $tree = array(); |
---|
18 | $folders = array(); |
---|
19 | $indexed = array(); |
---|
20 | |
---|
21 | // temporary |
---|
22 | //$namespaces = array("dojo"); |
---|
23 | foreach($namespaces as $nm){ |
---|
24 | // loop through the namespaces, build up our tree "json" |
---|
25 | $obj = array( |
---|
26 | "id" => "data-" . $id_counter++, |
---|
27 | "name" => $nm, |
---|
28 | "full_name" => $nm, |
---|
29 | "type" => "namespace", |
---|
30 | "children" => array() |
---|
31 | ); |
---|
32 | $tree[] = &$obj; |
---|
33 | foreach($data[$nm] as $f){ |
---|
34 | // we know each one is unique here, but we have to break it up. |
---|
35 | $pieces = explode("/", $f); |
---|
36 | $name = array_pop($pieces); |
---|
37 | $file_obj = array( |
---|
38 | "id" => "data-" . $id_counter++, |
---|
39 | "name" => $name, |
---|
40 | "full_name" => $f, |
---|
41 | "ns" => $nm, |
---|
42 | "type" => "file" |
---|
43 | ); |
---|
44 | |
---|
45 | // push it into the tree regardless |
---|
46 | $tree[] = $file_obj; |
---|
47 | $indexed[$file_obj["id"]] = $file_obj; |
---|
48 | if(!count($pieces)){ |
---|
49 | // this is a direct child of the namespace, so just add it. |
---|
50 | $obj["children"][] = array("_reference" => $file_obj["id"]); |
---|
51 | } else { |
---|
52 | while(count($pieces)){ |
---|
53 | $full_name = implode("/", $pieces); |
---|
54 | $name = array_pop($pieces); |
---|
55 | if(!array_key_exists($full_name, $folders)){ |
---|
56 | // add this directory object |
---|
57 | $folder_obj = array( |
---|
58 | "id" => "data-" . $id_counter++, |
---|
59 | "name" => $name, |
---|
60 | "full_name" => $full_name, |
---|
61 | "ns" => $nm, |
---|
62 | "type" => "folder", |
---|
63 | "added" => false, |
---|
64 | "children" => array() |
---|
65 | ); |
---|
66 | // keep track of it. |
---|
67 | $folders[$full_name] = $folder_obj; |
---|
68 | } |
---|
69 | |
---|
70 | // check to see if there's a parent folder |
---|
71 | if(count($pieces)){ |
---|
72 | // there should be a parent |
---|
73 | $tmp = explode("/", $full_name); |
---|
74 | array_pop($tmp); |
---|
75 | $tmp = implode("/", $tmp); |
---|
76 | if(array_key_exists($tmp, $folders) && !$folders[$full_name]["added"]){ |
---|
77 | $folders[$tmp]["children"][] = array("_reference"=>$folders[$full_name]["id"]); |
---|
78 | $folders[$full_name]["added"] = true; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | // finally, add our file to the right folder. |
---|
84 | $tmp = explode("/", $f); |
---|
85 | array_pop($tmp); |
---|
86 | $tmp = implode("/", $tmp); |
---|
87 | $folders[$tmp]["children"][] = array("_reference" => $file_obj["id"]); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | // add in our folder objects and merge with the main namespace |
---|
92 | $tmp = array(); |
---|
93 | foreach($folders as $f=>$folder){ |
---|
94 | $tree[] = $folder; |
---|
95 | $indexed[$folder["id"]] = $folder; |
---|
96 | if(strpos($f, "/") === false){ |
---|
97 | $tmp[] = array("_reference"=>$folder["id"]); |
---|
98 | } |
---|
99 | } |
---|
100 | $obj["children"] = array_merge($tmp, $obj["children"]); |
---|
101 | |
---|
102 | // fugly sorting by rebuilding all children. |
---|
103 | foreach($tree as $key=>&$item){ |
---|
104 | if(array_key_exists("children", $item) && count($item["children"])){ |
---|
105 | $folder_objs = array(); |
---|
106 | $privates = array(); |
---|
107 | $file_objs = array(); |
---|
108 | |
---|
109 | // here's where it gets ugly. Loop through the children, get the indexed object and push |
---|
110 | // into the various arrays in order to rebuild the children. |
---|
111 | foreach($item["children"] as $child=>$value){ |
---|
112 | $test = $indexed[$value["_reference"]]; |
---|
113 | if($test["type"] == "folder"){ |
---|
114 | $folder_objs[] = $value; |
---|
115 | } |
---|
116 | else if(strpos($test["name"], "_")===0){ |
---|
117 | $privates[] = $value; |
---|
118 | } |
---|
119 | else { |
---|
120 | $file_objs[] = $value; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | // TODO: we need the file objects to be sorted case-insensitive. |
---|
125 | |
---|
126 | |
---|
127 | // rebuild the children array |
---|
128 | $item["children"] = array_merge($folder_objs, $privates, $file_objs); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | $folders = array(); |
---|
133 | $indexed = array(); |
---|
134 | unset($obj); |
---|
135 | } |
---|
136 | $storeData = array( |
---|
137 | "identifier"=>"id", |
---|
138 | "label"=>"name", |
---|
139 | "items"=>&$tree |
---|
140 | ); |
---|
141 | print json_encode($storeData); |
---|
142 | ?> |
---|