Skip to content

Commit b8b4f9e

Browse files
committed
Recursive dependencies
1 parent 58db539 commit b8b4f9e

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/class-dependency-manager.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct($fnames = null, $wdir = null)
2424
// print "\n<br/>Initializing..";
2525
$this->ensure_config();
2626
$this->load_internal_resources();
27-
$this->include_autoloads();
27+
$this->include_autoloads(); // for the internal resources, if needed. Others will follow.
2828
// print "\n<br/>Loading sources..";
2929
$this->load_sources();
3030
// print "\n<br/>Loaded sources..";
@@ -49,7 +49,7 @@ protected function ensure_config()
4949

5050
protected function internal_resource_list() {
5151
$d = (strpos(__FILE__, ".phar") === false ? __DIR__ : "phar://" . __FILE__ . "/src");
52-
$f = $d . "/" . $this::DEPXML;
52+
$f = $d . "/" . self::DEPXML;
5353
// print "\n<br/>source::internal_resource_list - f=$f";
5454
$src = file_get_contents($f);
5555
$src = str_replace('<?xml version="1.0" ?>', "", $src);
@@ -76,7 +76,6 @@ protected function internal_resource_list() {
7676
$internal_resources[] = $ref;
7777
}
7878

79-
8079
// print_r($internal_resources);die();
8180
return $internal_resources;
8281
}
@@ -90,31 +89,36 @@ protected function load_internal_resources()
9089
public function default_source()
9190
{
9291
if (file_exists($v = ($this->workingDir . "/" . dependency_manager::DEPXML))) return $v;
93-
if (file_exists($v = (__DIR__ . "/" . dependency_manager::DEPXML))) return $v;
92+
if (file_exists($v = (__DIR__ . "/" . self::DEPXML))) return $v;
9493
$d = realpath(__DIR__);
9594
while (strlen($d) >= strlen($_SERVER["DOCUMENT_ROOT"])) {
9695
if ($d == ".") break;
9796
$dd = dirname($d);
9897
if ($dd == $d) break;
9998
$d = $dd;
10099
//print ("\nd=$d");
101-
if (file_exists($v = ("$d/" . dependency_manager::DEPXML))) return $v;
100+
if (file_exists($v = ("$d/" . self::DEPXML))) return $v;
102101
}
103-
return __DIR__ . "/" . dependency_manager::DEPXML;
102+
return __DIR__ . "/" . self::DEPXML;
104103
}
105104

106105
public function load_sources()
107106
{
107+
$sources_loaded = array();
108108
$this->dependencies = array();
109-
// print "\n<br/>load_sources()";
110-
if (is_array($this->sources))
111-
foreach ($this->sources as $source)
112-
{
113-
// print "\n<br/>load_sources(), loading source=$source";
114-
$this->dependencies[] = new xml_file($source);
109+
// print "\n<br/>load_sources()";
110+
if (is_array($this->sources)) {
111+
$this->sources = array_unique($this->sources);
112+
while (count($to_load = array_diff($this->sources, $sources_loaded)) > 0) {
113+
foreach ($to_load as $source) {
114+
// print "\n<br/>load_sources(), loading source=$source";
115+
$sources_loaded[] = $source;
116+
$this->dependencies[] = new xml_file($source);
117+
}
115118
}
116-
// print "\n<br/>load_sources(), ensuring dependencies...";
117-
$this->ensure_dependencies();
119+
}
120+
// print "\n<br/>load_sources(), ensuring dependencies...";
121+
$this->ensure_dependencies();
118122
}
119123

120124
public function ensure_dependencies()
@@ -218,7 +222,7 @@ public function fetch_dependency($url, $local_file)
218222
// print("\n$result");
219223
// print("\n================");
220224
if ($result === false || $result == "") {
221-
throw new Excpetion("Error requiring dependency: $url - $err");
225+
throw new Exception("Error requiring dependency: $url - $err");
222226
// print("<br/>ERROR REQURING DEPENDENCY: $url - $err");
223227
// die();
224228
}
@@ -233,12 +237,12 @@ public function process_dependency($resourceFile, $type, $name) {
233237
switch($type)
234238
{
235239
case "phar":
236-
$this->scan_phar_files($resourceFile, $name);
240+
$this->scan_phar_file($resourceFile, $name);
237241
break;
238242
}
239243
}
240244

241-
public function scan_phar_files($phar_file, $name)
245+
public function scan_phar_file($phar_file, $name)
242246
{
243247
// print("\n<br/>Reading PHAR: $phar_file\n");
244248
$phar = new Phar($phar_file, FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, $name);
@@ -247,9 +251,14 @@ public function scan_phar_files($phar_file, $name)
247251
$basepath = "phar://" . $phar->getPath() . "/";
248252
foreach (new RecursiveIteratorIterator($phar) as $file) {
249253
$filename = str_replace($basepath, "", $file->getPath() . '/' . $file->getFilename());
250-
// print_r("\n$basepath");
251-
// print_r("\nfilename=$filename");
254+
// print("\n$basepath");
255+
// print("\nfilename=$filename");
252256
$this->resources[$filename] = $name;
257+
258+
if ($filename == self::DEPXML) {
259+
// print "\n<br/>Found module dependencies: " . $file->getPath() . '/' . $file->getFilename();
260+
$this->sources[] = $file->getPath() . '/' . $file->getFilename();
261+
}
253262
}
254263
}
255264

0 commit comments

Comments
 (0)