/** * Constructor * @param HttpRequest $pReq request * @param HttpRequest $pRootResource root resource * @param HttpRequest $pRealm name of the application */ public function __construct(RESTHttpRequest $pReq, $pRootResource, $pRealm) { $this->request = $pReq; // we compute the list of resources classes $this->listResources = ServiceEnumerator::listDefinedResources(); $this->realm = $pRealm; $this->rootResource = $pRootResource; }
/** * list of services * GET / * or HEAD / * @param boolean $pHead true if we return only headers */ public function index($pHead = false) { if ($pHead) { return; } echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; echo '<html><head><title>List of services</title></head><body><ul>'; foreach (ServiceEnumerator::listDefinedResources() as $i) { echo '<li><a href="./' . $i . '/">' . $i . '</a></li>'; } echo '</ul></body></html>'; }
/** * get the list of resources implemented in the folder rest/resources * @return array list of resources */ public static function listDefinedResources() { $prefix = null; if (defined('RESOURCES_NS')) { if (strlen(RESOURCES_NS) >= 2 && substr(RESOURCES_NS, -2, 2) != '::') { $pos = strpos(RESOURCES_NS, '::'); if ($pos !== false) { $prefix = substr(RESOURCES_NS, $pos + 2, strlen(RESOURCES_NS) - 1); } else { $prefix = RESOURCES_NS; } } } $resources = array(); if (self::$listResources === null) { // verify if RESOURCES_PATH is defined if (!defined('RESOURCES_PATH')) { throw new Exception('RESOURCES_PATH is not defined'); } $dir = new DirectoryIterator(RESOURCES_PATH); foreach ($dir as $file) { $filename = $file->getFileName(); $matches = array(); $regex = ''; if ($prefix !== null) { $regex = '/' . $prefix . '(.*)\\.class\\.php$/'; } else { $regex = '/(.*)\\.class\\.php$/'; } $res = preg_match($regex, $filename, $matches); if (isset($matches[1])) { $resources[] = $matches[1]; } } self::$listResources = $resources; } else { $resources = self::$listResources; } return $resources; }