00001 <?php
00002
00003
00004
00005
00006
00007
00008 class PathStack {
00009 private $path_front = null;
00010 private $path_back = null;
00011
00012 public function __construct($path = '') {
00013 $this->set_path($path);
00014 }
00015
00016
00017
00018
00019 public function set_path($path) {
00020 $path = trim($path, '/');
00021 if (!empty($path)) {
00022 $this->path_front = explode('/', $path);
00023 }
00024 else {
00025 $this->path_front = array();
00026 }
00027 $this->path_back = array();
00028 }
00029
00030
00031
00032
00033 public function get_path() {
00034 return $this->append_to_back($this->implode_front());
00035 }
00036
00037
00038
00039
00040
00041 public function current() {
00042 if (count($this->path_front) > 0) {
00043 return $this->path_front[0];
00044 }
00045
00046 return false;
00047 }
00048
00049
00050
00051
00052 private function do_next() {
00053 $ret = array_shift($this->path_front);
00054 if (!empty($ret)) {
00055 array_push($this->path_back, $ret);
00056 }
00057 }
00058
00059
00060
00061
00062
00063 public function next() {
00064 $this->do_next();
00065 return $this->current();
00066 }
00067
00068
00069
00070
00071 public function shift() {
00072 $ret = $this->current();
00073 if ($ret !== false) {
00074 $this->do_next();
00075 }
00076 return $ret;
00077 }
00078
00079
00080
00081
00082
00083
00084 public function adjust($path) {
00085 $adjust_stack = new PathStack($path);
00086 $this_stack = clone($this);
00087
00088 $cur = $adjust_stack->current();
00089 while($cur !== false) {
00090 if ($cur !== $this_stack->current()) {
00091 return false;
00092 }
00093
00094 $cur = $adjust_stack->next();
00095 $this_stack->do_next();
00096 }
00097
00098 $this->path_back = $this_stack->path_back;
00099 $this->path_front = $this_stack->path_front;
00100 return true;
00101 }
00102
00103
00104
00105
00106 public function implode_back() {
00107 return implode('/' , $this->path_back);
00108 }
00109
00110
00111
00112
00113 public function append_to_back($path) {
00114 $arr = array($this->implode_back());
00115 if ($path !== '') {
00116 $arr[] = $path;
00117 }
00118 return implode('/', $arr);
00119 }
00120
00121
00122
00123
00124 public function prepend_to_front($path) {
00125 $arr = array($this->implode_front());
00126 if ($path !== '') {
00127 array_unshift($arr, $path);
00128 }
00129 return implode('/', $arr);
00130 }
00131
00132
00133
00134
00135 public function implode_front() {
00136 return implode('/' , $this->path_front);
00137 }
00138
00139
00140
00141
00142 public function count_front() {
00143 return count($this->path_front);
00144 }
00145
00146
00147
00148
00149 public function clear_front() {
00150 while($this->next()) { }
00151 }
00152 }
00153 ?>