/** * method: initialise * * todo: write documentation */ public static function initialise() { if (self::$is_initialised) { return; } $search = array_merge($_SERVER, $_GET); self::$pathList["__WEBSITE_ROOT__"] = $search["__WEBSITE_ROOT__"]; self::$path = NULL; self::$base = self::getPath("__WEBSITE_ROOT__"); if (self::$base) { // Obtain the path within the website, without the website base // we use this to calculate the path inside the website, not relative to the document root self::$path = Amslib_String::lchop($_SERVER["REQUEST_URI"], self::$base); self::$path = Amslib_String::rchop(self::$path, "?"); self::$path = Amslib_File::reduceSlashes("/" . self::$path . "/"); } // Now automatically load the amslib routers configuration as the framework system // This allows amslib to add routes the system can use to import/export router configurations // This isn't part of your application, this is part of the system and used to self-configure self::load(Amslib::locate() . "/router/router.xml", "xml", "framework"); self::$is_initialised = true; // remove this from the $_GET superglobal, it's kind of annoying to keep finding it in the application code Amslib_GET::delete("__WEBSITE_ROOT__"); }
/** * 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; }
function TestOfAmslibStringLchop() { $this->caseTitle('Amslib_String::lchop'); $test = 'C:/Sites/crm-test/vendor/index.php'; $this->log($test); $documentRoot = $_SERVER['DOCUMENT_ROOT']; $result = Amslib_String::lchop($test, $documentRoot); $this->log($result); $this->assertFalse(strpos($result, $documentRoot) === 0); $test = "/Sites/crm-test/vendor/index.php"; $this->log($test); $documentRoot = Amslib_File::documentRoot($test); $result = Amslib_String::lchop($test, Amslib_File::documentRoot($test)); $this->log($result); $this->assertFalse(strpos($result, $documentRoot) === 0); }
/** * method: relative * * todo: write documentation */ public static function relative($path = "") { if (strpos($path, "http://") === 0) { return $path; } $root = self::documentRoot(); $path = self::win2unix($path); $rel = Amslib_String::lchop($path, $root); return Amslib_String::reduceSlashes("/{$rel}"); }