-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathFind.php
165 lines (107 loc) · 3.26 KB
/
PathFind.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* PathFind.php file,
* located in directories with dependencies
*
* MIT License
* Copyright 2023 Nathan Wooten
*/
namespace nathanwooten\pathfind;
if ( ! defined( 'DS' ) ) define( 'DS', DIRECTORY_SEPARATOR );
if ( ! class_exists( 'nathanwooten\pathfind\PathFind' ) ) {
class PathFind
{
public static array $pathFind = [];
public string $response;
public string $path;
public array $targetContains;
public function pathFind( $path, array $targetContains ) : PathFind
{
$path = (string) $path;
if ( ! is_string( $path ) ) {
throw new Exception( 'Path must be stringable, ' . __CLASS__ . '::' . 'pathFind' );
}
if ( $has = $this->has( $path, $targetContains ) ) {
return $has;
}
$this->path = $path;
$this->targetContains = $targetContains;
static::$pathFind[ $path ] = $this;
static::$pathFind[ implode( ',', $targetContains ) ] = $this;
if ( is_file( $path ) ) {
$path = dirname( $path ) . DIRECTORY_SEPARATOR;
}
// no contents, no search
if ( empty( $targetContains ) ) {
return false;
}
while( $path && ( ! isset( $count ) || ! $count ) ) {
$path = rtrim( $path, DIRECTORY_SEPARATOR . '\\/' ) . DIRECTORY_SEPARATOR;
$is = [];
// loop through 'contains'
foreach ( $targetContains as $contains ) {
$item = $path . $contains;
// readable item?, add to $is
if ( is_readable( $item ) ) {
$is[] = $item;
}
}
// expected versus is
$isCount = count( $is );
$containsCount = count( $targetContains );
$count = ( $isCount === $containsCount );
if ( $count ) {
break;
} else {
$parent = dirname( $path );
if ( $parent === $path ) {
// if root reached break the loop
throw new Exception( 'Reached root in, ' . __FILE__ . ' ' . __FUNCTION__ );
} else {
// continue up
$path = $parent;
}
continue;
}
}
if ( $path ) {
$path = rtrim( $path, '\\/' );
}
$this->response = $path;
return $this;
}
public function withPath( $path ) : PathFind
{
if ( $has = $this->has( $path, $this->targetContains ) ) {
return $has;
}
$clone = clone $this;
$clone->pathFind( $path, $this->targetContains );
return $clone;
}
public function withContains( array $targetContains ) : PathFind
{
if ( $has = $this->has( $this->path, $targetContains ) ) {
return $has;
}
$clone = clone $this;
$clone->pathFind( $this->path, $targetContains );
return $clone;
}
public function has( $path, $targetContains )
{
if ( isset( static::$pathFind[ $path ] ) && static::$pathFind[ $path ]->targetContains === $targetContains ) {
return static::$pathFind[ $path ];
}
$tc = implode( ',', $targetContains );
if ( $tc && isset( static::$pathFind[ $tc ] ) && static::$pathFind[ $tc ]->path === $path ) {
return static::$pathFind[ $tc ];
}
return false;
}
public function __toString()
{
return $this->response;
}
}
}