/** * method: load * * returns: * Boolean true or false depending on whether it succeeded, there are some codepaths which call setError() * this is because of serious errors which can't be handled at the moment * * NOTE: * - if language is false, you need to call setLanguage before you * call load otherwise the source can't load the correct file */ public function load() { if (!$this->language) { return false; } $dir = $this->getConfig("directory"); $source = Amslib_File::reduceSlashes("{$dir}/{$this->language}.xml"); $filename = false; try { // NOTE: This is ugly and I believe it's a failure of Amslib_File::find() to not do this automatically if (file_exists($source)) { $filename = $source; } else { if (file_exists($f = Amslib_File::find(Amslib_Website::relative($source), true))) { $filename = $f; } else { if (file_exists($f = Amslib_File::find(Amslib_Website::absolute($source), true))) { $filename = $f; } } } if (!$filename) { Amslib_Debug::log("stack_trace", "directory", $dir, "filename", $filename, "language", $this->language); } else { $this->qp = Amslib_QueryPath::qp($filename); return true; } } catch (Exception $e) { Amslib_Debug::log("Exception: ", $e->getMessage(), "file=", $filename, "source=", $source); Amslib_Debug::log("stack_trace"); } return false; }
/** * 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: openFile * * todo: write documentation */ public function openFile($filename) { // NOTE: Added a call to Amslib_Website::abs to fix finding the file, because in some cases, // the file cannot be found. But I am not sure of the side-effects (if any) of doing this $this->filename = Amslib_File::find(Amslib_Website::absolute($filename), true); $document = new DOMDocument('1.0', 'UTF-8'); if (is_file($this->filename) && $document->load($this->filename)) { $this->xpath = new DOMXPath($document); return true; } Amslib_Keystore::add("error", "Amslib_XML::openFile[{$filename}], path[{$this->filename}] failed"); return false; }
/** * method: __construct * * todo: write documentation */ public function __construct($source) { $this->route = false; $this->import = false; $this->valid = false; $filename = false; try { // NOTE: This is ugly and I believe it's a failure of Amslib_File::find() to not do this automatically if (file_exists($source)) { $filename = $source; } else { if (file_exists($f = Amslib_File::find(Amslib_Website::relative($source), true))) { $filename = $f; } else { if (file_exists($f = Amslib_File::find(Amslib_Website::absolute($source), true))) { $filename = $f; } } } if (!$filename) { Amslib_Debug::log("The filename was not valid, we could not getRoutes from this XML Source"); } else { Amslib_QueryPath::qp($filename); Amslib_QueryPath::execCallback("router > path[name]", array($this, "configPath"), $this); Amslib_QueryPath::execCallback("router > service[name]", array($this, "configService"), $this); Amslib_QueryPath::execCallback("router > callback", array($this, "configCallback"), $this); Amslib_QueryPath::execCallback("router > import", array($this, "configImport"), $this); Amslib_QueryPath::execCallback("router > export", array($this, "configExport"), $this); $this->valid = true; } } catch (Exception $e) { Amslib_Debug::log("Exception: ", $e->getMessage(), "file=", $filename, "source=", $source); Amslib_Debug::log("stack_trace"); } }