-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInclude_Path.php
More file actions
186 lines (174 loc) · 5.7 KB
/
Copy pathInclude_Path.php
File metadata and controls
186 lines (174 loc) · 5.7 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace SAF\Framework;
use SAF\Framework\Php\Reflection_Class;
use SAF\Framework\Tools\OS;
/**
* Include path manager
*/
class Include_Path
{
//---------------------------------------------------------------------------- $application_class
/**
* @var string
*/
private $application_class;
//-------------------------------------------------------------------------- $origin_include_path
/**
* The original PHP include_path is kept here
*
* @var string
*/
private static $origin_include_path;
//----------------------------------------------------------------------------------- __construct
/**
* @param $application_class string
*/
public function __construct($application_class)
{
$this->application_class = $application_class;
}
//-------------------------------------------------------------------------------- getDirectories
/**
* This is called by getSourceDirectories() for recursive directories reading
*
* @param $path string base path
* @return string[] an array of directories names
*/
private function getDirectories($path)
{
if (file_exists($path . '/exclude')) {
return $directories = [];
}
else {
$directories[$path] = $path;
$dir = dir($path);
while ($entry = $dir->read()) if ($entry[0] != DOT) {
if (is_dir($path . SL . $entry) && ($entry != 'vendor') && ($entry != 'cache')) {
$directories = array_merge($directories, $this->getDirectories($path . SL . $entry));
}
}
$dir->close();
}
return $directories;
}
//-------------------------------------------------------------------------------- getIncludePath
/**
* @return string
*/
public function getIncludePath()
{
if (!isset($this->include_path)) {
$include_path = join(OS::$include_separator, $this->getSourceDirectories(true));
$this->include_path = self::getOriginIncludePath() . OS::$include_separator . $include_path;
return $this->include_path;
}
return $this->include_path;
}
//-------------------------------------------------------------------------- getOriginIncludePath
/**
* Returns PHP origin include path
*
* @return string
*/
public static function getOriginIncludePath()
{
if (!self::$origin_include_path) {
self::$origin_include_path = get_include_path();
}
return self::$origin_include_path;
}
//-------------------------------------------------------------------------- getSourceDirectories
/**
* Returns the full directory list for the application, including parent's applications directory
*
* Directory names are sorted from higher-level application to basis SAF 'framework' directory
* Inside an application, directories are sorted randomly (according to how the php
* Directory->read() call works)
*
* Paths are relative to the SAF index.php base script position
*
* @param $include_subdirectories boolean
* @param $application_class string
* @param $already string[] already scanned classes
* @return string[]
*/
public function getSourceDirectories(
$include_subdirectories = false, $application_class = null, &$already = []
) {
if (!isset($application_class)) {
$application_class = $this->application_class;
}
$already[$application_class] = true;
$app_dir = $this->getSourceDirectory($application_class);
$directories = [];
$app_dir_begin = '';
foreach (explode(SL, $app_dir) as $app_dir_part) {
$app_dir_begin .= ($app_dir_begin ? SL : '') . $app_dir_part;
$directories[$app_dir_begin] = $app_dir_begin;
}
if ($application_class != Application::class) {
// get source directories from main application extends
$extends = get_parent_class($application_class);
if ($extends && !isset($already[$extends])) {
$directories = $this->getSourceDirectories($include_subdirectories, $extends, $already);
}
// get source directories for secondary applications extends
$class = Reflection_Class::of($application_class);
$extends_annotations = $class->getListAnnotations('extends');
foreach ($extends_annotations as $extends_annotation) {
foreach ($extends_annotation->values() as $extends) {
if (!isset($already[$extends])) {
$directories = array_merge(
$this->getSourceDirectories($include_subdirectories, $extends, $already),
$directories
);
}
}
}
}
// get source directories from the application itself
return $include_subdirectories
? array_merge($this->getDirectories($app_dir), $directories)
: array_merge([$app_dir], $directories);
}
//---------------------------------------------------------------------------- getSourceDirectory
/**
* @param $application_class string
* @return string
*/
public function getSourceDirectory($application_class = null)
{
if (!isset($application_class)) {
$application_class = $this->application_class;
}
return strtolower(
str_replace(BS, SL, substr($application_class, 0, strrpos($application_class, BS)))
);
}
//-------------------------------------------------------------------------------- getSourceFiles
/**
* Returns the full files list for the application, including parent's applications directory
*
* File names are sorted from higher-level application to basis SAF 'framework' directory
* Inside an application, files are sorted randomly (according to how the php Directory->read() call works)
*
* Paths are relative to the SAF index.php base script position
*
* @return string[]
*/
public function getSourceFiles()
{
$files = [];
foreach ($this->getSourceDirectories(true) as $directory) {
$dir = dir($directory);
while ($entry = $dir->read()) if ($entry[0] != DOT) {
$file_path = $directory . SL . $entry;
if (is_file($file_path)) {
$files[] = $file_path;
}
}
$dir->close();
}
return $files;
}
}