source: branches/testa-single-bookmark/php/NorthernCross/ns/PathInfo.php

Last change on this file was 39, checked in by anonymous, 6 years ago

restrict basenames

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1<?php
2
3namespace ns;
4
5class PathInfo {
6    protected $root;
7    public $hier_hash;
8    protected $root_path;
9
10    function __construct( $part, $root_path , $restrict = null){
11        $this->part = $part;
12        $this->root_path = $root_path;
13        $this->root = new PathItem( "/" , "/");
14        $this->hier_hash = array();
15        $this->hier_hash["/"] = $this->root;
16        $this->restrict = null;
17        $this->restrict_dir = array();
18        if( $restrict !== null ){
19            $this->restrict = array();
20            foreach($restrict as $k => $v){
21                 $pathinfo = pathinfo($v);
22                 array_push( $this->restrict,  $pathinfo['filename'] );
23            }
24        }
25    }
26
27    public function match_in_list0( $path2, $list ){
28        $hit = false;
29
30        foreach( $list as $k => $r ){
31            if( preg_match($r , $path2) ){
32                $hit = true;
33                break;
34            }
35        }
36        return $hit;
37    }
38
39    public function match_in_list( $path, $list ){
40        $hit = false;
41
42        $pathinfo = pathinfo($path);
43        $index = array_search( $pathinfo['dirname'] , $list );
44        if( $index !== FALSE ){
45            $hit = true;
46        }
47        return $hit;
48    }
49
50    public function scan( $path ){
51        $pathinfo = pathinfo( $path );
52
53        $filename = $pathinfo['filename'];
54#        echo "filename=" . $filename . "\n";
55#        $basename = $pathinfo['basename'];
56#        echo "basename=" . $basename . "\n";
57
58        if( $this->restrict !== null ){
59            $index = array_search( $filename , $this->restrict );
60            if( $index !== FALSE ){
61                $dirname  = $pathinfo['dirname'];
62                $this->restrict_dir[ $dirname ] = "";
63            }
64        }
65    }
66
67    public function show_restrict() {
68        var_dump( $this->restrict );
69    }
70
71    public function show_restrict_dir() {
72        var_dump( $this->restrict_dir );
73    }
74
75    public function register( $path ){
76        $path2 = trim($path);
77
78        if( strlen($path2) == 0 ){
79            echo "size is 0 - A\n";
80            exit(0);
81        }
82
83        if( array_key_exists($path2,  $this->hier_hash)){
84            echo "register" . "\n";
85            return;
86        }
87
88        if( $this->restrict !== null ){
89#   echo "match_in_list\n";
90            if( ! $this->match_in_list( $path2, array_keys( $this->restrict_dir ) ) ){
91#                echo "!match path2=" . $path2 . "\n";
92                return;
93            }
94        }
95#        echo "path2=" . $path2 . "\n";
96       
97        $array = explode("/", $path2);
98        $name = array_pop($array);
99        $item = new PathItem( $name, $path2 , $path2 );
100        $this->hier_hash[$path2] = $item;
101
102        $child_item = $item;
103
104        $dosenot_exist = TRUE;
105        while( count($array) > 0 && $dosenot_exist ){
106            $hier = implode("/" , $array);
107            if( strlen($hier) == 0 ){
108                break;
109            }
110            if (array_key_exists($hier,  $this->hier_hash)){
111                $item = $this->hier_hash[$hier];
112                $dosenot_exist = FALSE;
113            }
114            else {
115                $name = array_pop($array);
116                $item = new PathItem( $name, $hier);
117                $this->hier_hash[$hier] = $item;
118            }
119
120            $child_item->set_parent($item);
121            $child_item = $item;
122        }
123        if( $dosenot_exist ){
124            $child_item->set_parent($this->root);
125        }
126    }
127
128    public function get_pathitem_name( $item ){
129        return $item->get_name();
130    }
131
132    public function pathinfo_list_in_json(){
133        $array = array();
134        $keys = array_keys( $this->hier_hash );
135        sort( $keys , SORT_STRING );
136        foreach( $keys as $key ){
137            $b = $this->hier_hash[ $key ];
138            $names = array_map(array($this, 'get_pathitem_name'), $b->get_children());
139            sort($names, SORT_STRING);
140            $array[$key]=$names;
141        }
142        return json_encode($array);
143    }
144
145    public function dump() {
146        foreach( $this->hier_hash as $a => $b ){
147            echo "###########\n";
148            echo "key=" . $a . "\n";
149            echo $b->dump();
150            echo $b->dump_children();
151        }
152    }
153
154    public function dump_root(){
155        $this->root->dump_children();
156    }
157}
158
159?>
Note: See TracBrowser for help on using the repository browser.