/** * method: findResource * * todo: write documentation */ protected function findResource($resource, $absolute = false) { // If the resource has an attribute "absolute" don't process it, return it directly // An absolute resource will also have a protocol token inside the string if ($absolute || strpos($resource, "://") !== false) { return $resource; } $location = $this->getLocation(); // PREPARE THE STRING: expand any parameters inside the resource name $resource = self::expandPath($resource); // NOTE: we have to do this to get around a bug in lchop/rchop $query = ($str = Amslib_String::lchop($resource, "?")) == $resource ? false : $str; $resource = Amslib_String::rchop($resource, "?"); $output = false; // TEST 1: look in the package directory for the file $test1 = Amslib_File::reduceSlashes("{$location}/{$resource}"); if (!$output && file_exists($test1)) { $output = Amslib_File::relative($test1); } // TEST 2: Test whether the file "exists" without any assistance if (!$output && file_exists($resource)) { $output = Amslib_File::relative($resource); } // TEST 3: Does the file exists relative to the document root? $test3 = Amslib_File::absolute($resource); if (!$output && file_exists($test3)) { // Why do we see whether $test3 exists, then discard it and use $resource? $output = Amslib_File::relative($resource); } // TEST 4: search the include path for the file $test4 = Amslib_File::find($resource, true); if ($output && file_exists($test4)) { $output = Amslib_File::relative($test4); } // Return either $output value on it's own, or appended with the query if required // If output is false or valid string, it'll return $output at minimum, // only appending the query if it's valid too return $output && $query ? "{$output}?{$query}" : $output; }
/** * method: relative * * todo: write documentation */ public static function relative($url = "", $resolve = false) { if (!is_string($url)) { Amslib_Debug::log("stack_trace", $url); return false; } // When the string has this protocol marker, the url part is already relative to the document root if (strpos($url, "://") !== false) { list($protocol, $domain, $url) = self::parseURL($url); // Because we have a url with a protocol, it must mean the url parsed is already relative return $url; } $url = Amslib_File::relative(self::$location . $url); return $resolve ? Amslib_File::resolvePath($url) : $url; }