PluginProbe
WP Ultimate Post Grid / 2.8.2
WP Ultimate Post Grid v2.8.2
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / vendor / vafpress / classes / filesystem.php

filesystem.php in WP Ultimate Post Grid 2.8.2, at vendor/vafpress/classes/filesystem.php

150 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class VP_FileSystem
4 {
5
6 private static $_instance = null;
7
8 private $_lookup_dirs = array();
9
10 public static function instance()
11 {
12 if(is_null(self::$_instance))
13 {
14 self::$_instance = new self();
15 }
16 return self::$_instance;
17 }
18
19 public function get_first_non_empty_dir($key, $name = null)
20 {
21 if(!isset($this->_lookup_dirs[$key]))
22 return false;
23
24 foreach ($this->_lookup_dirs[$key] as $dir)
25 {
26 if (is_link($dir))
27 {
28 $dir = readlink($dir);
29 }
30 if(!is_null($name))
31 {
32 $dir = $dir . DIRECTORY_SEPARATOR . $name;
33 }
34 if($this->dir_contains_children($dir, 'php'))
35 {
36 return $dir;
37 }
38 }
39 }
40
41 public function resolve_path($key, $name, $ext = 'php')
42 {
43 if(!isset($this->_lookup_dirs[$key]))
44 return false;
45
46 $name = $this->normalize_path($name, $ext);
47
48 foreach ($this->_lookup_dirs[$key] as $dir)
49 {
50 $file = $dir . DIRECTORY_SEPARATOR . $name;
51 if (is_link($file))
52 {
53 $file = readlink($file);
54 }
55 if(file_exists($file))
56 {
57 return $file;
58 }
59 }
60 return false;
61 }
62
63 public function normalize_path($path, $ext)
64 {
65 $path = trim($path, '\\/');
66 return $path . '.' . $ext;
67 }
68
69 function dir_contains_children($dir, $ext = null)
70 {
71 $result = false;
72 if (is_link($dir))
73 {
74 $dir = readlink($dir);
75 }
76
77 if($dh = opendir($dir))
78 {
79 while(!$result && ($file = readdir($dh)) !== false)
80 {
81 $result = $file !== "." && $file !== "..";
82 if(!is_null($ext))
83 {
84 $result = pathinfo($file, PATHINFO_EXTENSION) === $ext;
85 }
86 }
87 closedir($dh);
88 }
89 return $result;
90 }
91
92 /**
93 * Add directories to the autoloader, loading process will be run in orderly fashion
94 * of directory addition.
95 *
96 * @param String|Array $directories
97 * @return void
98 */
99 public function add_directories($key, $directories)
100 {
101 if(!isset($this->_lookup_dirs[$key]))
102 {
103 $this->_lookup_dirs[$key] = array();
104 }
105 $this->_lookup_dirs[$key] = array_merge($this->_lookup_dirs[$key], (array) $directories);
106 $this->_lookup_dirs[$key] = array_unique($this->_lookup_dirs[$key]);
107 }
108
109 /**
110 * Remove directories.
111 *
112 * @param String|Array $directories
113 * @return void
114 */
115 public function remove_directories($key, $directories = null)
116 {
117 // annihilate everything if none / null passed
118 if(is_null($directories))
119 {
120 $this->_lookup_dirs[$key] = array();
121 }
122 else
123 {
124 // prepare directories to be filtered
125 $directories = (array) $directories;
126
127 // do the filtering
128 foreach ($this->_lookup_dirs[$key] as $name => $dir)
129 {
130 if(in_array($dir, $directories))
131 {
132 unset($this->_lookup_dirs[$key][$name]);
133 }
134 }
135 }
136 }
137
138 /**
139 * Get all directories
140 *
141 * @return Array
142 */
143 public function get_directories($key = null)
144 {
145 if(!is_null($key))
146 return $this->_lookup_dirs[$key];
147 return $this->_lookup_dirs;
148 }
149
150 }