From a44c98efb358df16d6e16747de00135f8b06d1f4 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sun, 27 Jan 2013 18:07:07 -0500 Subject: [PATCH] Fix error in Request.php where wrong namespace was used for Exceptions Remove sample APi --- .gitignore | 9 +- samples/apisample/.htaccess | 8 - samples/apisample/ApiSampleRouter.php | 50 ----- samples/apisample/ArticlesController.php | 110 ---------- samples/apisample/data/articles.json | 11 - .../handlers/ArticleCollectionHandler.php | 104 --------- .../apisample/handlers/ArticleItemHandler.php | 204 ------------------ samples/apisample/index.php | 15 -- samples/client-side-request-and-response.php | 55 ----- samples/client-side-request.php | 27 --- samples/server-side-request-and-response.php | 46 ---- samples/server-side-response.php | 18 -- src/pjdietz/WellRESTed/Request.php | 2 +- 13 files changed, 7 insertions(+), 652 deletions(-) delete mode 100644 samples/apisample/.htaccess delete mode 100644 samples/apisample/ApiSampleRouter.php delete mode 100644 samples/apisample/ArticlesController.php delete mode 100644 samples/apisample/data/articles.json delete mode 100644 samples/apisample/handlers/ArticleCollectionHandler.php delete mode 100644 samples/apisample/handlers/ArticleItemHandler.php delete mode 100644 samples/apisample/index.php delete mode 100644 samples/client-side-request-and-response.php delete mode 100644 samples/client-side-request.php delete mode 100644 samples/server-side-request-and-response.php delete mode 100644 samples/server-side-response.php diff --git a/.gitignore b/.gitignore index 57800fe..1f1115d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,12 @@ # Composer vendor/ -# Doxygen -docs/html +# Generated documentation +docs/ + +# OSX +.DS_Store +.AppleDouble # PhpStorm workspace.xml - diff --git a/samples/apisample/.htaccess b/samples/apisample/.htaccess deleted file mode 100644 index 2e96de0..0000000 --- a/samples/apisample/.htaccess +++ /dev/null @@ -1,8 +0,0 @@ -RewriteEngine on -RewriteBase /wellrested/samples/apisample/ - -# Send all requests to non-regular files and directories to router.php -RewriteCond %{REQUEST_FILENAME} !-f -RewriteCond %{REQUEST_FILENAME} !-d -RewriteRule ^.+$ index.php [L,QSA] - diff --git a/samples/apisample/ApiSampleRouter.php b/samples/apisample/ApiSampleRouter.php deleted file mode 100644 index e6dff38..0000000 --- a/samples/apisample/ApiSampleRouter.php +++ /dev/null @@ -1,50 +0,0 @@ -addTemplate( - '/articles/', - 'ArticleCollectionHandler' - ); - $this->addTemplate( - '/articles/{id}', - 'ArticleItemHandler', - array('id' => Route::RE_NUM) - ); - $this->addTemplate( - '/articles/{slug}', - 'ArticleItemHandler', - array('slug' => Route::RE_SLUG) - ); - } - - public function addTemplate($template, $handlerClassName, $variables = null) - { - // Customize as needed based on your server. - $template = '/wellrested/samples/apisample' . $template; - $handlerClassName = '\apisample\handlers\\' . $handlerClassName; - - $this->addRoute( - Route::newFromUriTemplate( - $template, - $handlerClassName, - $variables - ) - ); - } - -} diff --git a/samples/apisample/ArticlesController.php b/samples/apisample/ArticlesController.php deleted file mode 100644 index 740a306..0000000 --- a/samples/apisample/ArticlesController.php +++ /dev/null @@ -1,110 +0,0 @@ -path = dirname(__FILE__) . '/data/articles.json'; - $this->load(); - } - - public function load() - { - if (file_exists($this->path)) { - $data = file_get_contents($this->path); - $this->data = json_decode($data, true); - } - } - - public function save() - { - if (is_writable($this->path)) { - $data = json_encode($this->data); - return file_put_contents($this->path, $data); - } - - return false; - } - - public function getArticleById($id) - { - foreach ($this->data as $article) { - if ($article['articleId'] == $id) { - return $article; - } - } - return false; - } - - public function getArticleBySlug($slug) - { - foreach ($this->data as $article) { - if ($article['slug'] == $slug) { - return $article; - } - } - return false; - } - - public function addArticle($newArticle) - { - $validatedArticle = array( - 'articleId' => $this->getNewId(), - 'slug' => $newArticle['slug'], - 'title' => $newArticle['title'], - 'excerpt' => $newArticle['excerpt'] - ); - - $this->data[] = $validatedArticle; - - return $validatedArticle; - } - - public function updateArticle($newArticle) - { - foreach ($this->data as &$oldArticle) { - - if ($oldArticle['articleId'] == $newArticle['articleId']) { - $oldArticle['slug'] = $newArticle['slug']; - $oldArticle['title'] = $newArticle['title']; - $oldArticle['excerpt'] = $newArticle['excerpt']; - return $newArticle; - } - - } - - return false; - } - - public function removeArticle($id) - { - foreach ($this->data as $index => $article) { - if ($article['articleId'] == $id) { - unset($this->data[$index]); - return true; - } - } - - return false; - } - - protected function getNewId() - { - $maxId = 0; - - foreach ($this->data as $article) { - $maxId = max($maxId, $article['articleId']); - } - - return $maxId + 1; - } - -} diff --git a/samples/apisample/data/articles.json b/samples/apisample/data/articles.json deleted file mode 100644 index 174289e..0000000 --- a/samples/apisample/data/articles.json +++ /dev/null @@ -1,11 +0,0 @@ -[{ - "articleId": 1, - "slug": "good-movie", - "title": "Reports Of Movie Being Good Reach Area Man", - "excerpt": "Local resident Daniel Paxson has reportedly heard dozens of accounts from numerous friendly sources in the past two weeks confirming that the new James Bond film is pretty good. According to persons with knowledge of the situation, an unnamed friend of Paxson’s coworker Wendy Mathers watched the movie on opening weekend and found it to be “decent enough.”" -}, { - "articleId": 2, - "slug": "species-protection", - "title": "Endangered Wildlife To Be Given New Identities In Species Protection Program", - "excerpt": "In an effort to protect at-risk animals from those who might wish to do them harm, the U.S. Fish and Wildlife Service announced Friday it had launched a program that provides endangered species with new names and habitats to ensure their anonymity." -}] \ No newline at end of file diff --git a/samples/apisample/handlers/ArticleCollectionHandler.php b/samples/apisample/handlers/ArticleCollectionHandler.php deleted file mode 100644 index 3e813d4..0000000 --- a/samples/apisample/handlers/ArticleCollectionHandler.php +++ /dev/null @@ -1,104 +0,0 @@ -data)) { - - $this->response->statusCode = 200; - $this->response->setHeader('Content-type', 'application/json'); - $this->response->body = json_encode($articles->data); - - } else { - - $this->response->statusCode = 500; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to read the articles.'; - - } - - } - - /** - * Respond to a POST request. - */ - protected function post() - { - - // Read the request body, and ensure it is in the proper format. - $article = json_decode($this->request->body, true); - - // Ensure the JSON is well-formed. - if (!$article) { - $this->response->statusCode = 400; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to parse JSON from request body.'; - return; - } - - // Ensure requied fields are present. - if (!isset($article['slug']) || $article['slug'] === '') { - $this->response->statusCode = 400; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Request body missing slug.'; - return; - } - - if (!isset($article['title'])) { - $this->response->statusCode = 400; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Request body missing title.'; - return; - } - - if (!isset($article['excerpt'])) { - $this->response->statusCode = 400; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Request body missing excerpt.'; - return; - } - - // Ensure slug is not a duplicate. - $articles = new \apisample\ArticlesController(); - if ($articles->getArticleBySlug($article['slug']) !== false) { - $this->response->statusCode = 409; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to store article. Slug "' . $article['slug'] . '" is already in use.'; - return; - } - - // All looks good! Add this to the articles and save! - $article = $articles->addArticle($article); - - if ($articles->save() === false) { - $this->response->statusCode = 500; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to write to file. Make sure permissions are set properly.'; - return; - } - - // Ok! - $this->response->statusCode = 201; - $this->response->setHeader('Content-type', 'application/json'); - $this->response->body = json_encode($article); - return; - - } - -} diff --git a/samples/apisample/handlers/ArticleItemHandler.php b/samples/apisample/handlers/ArticleItemHandler.php deleted file mode 100644 index 135a70b..0000000 --- a/samples/apisample/handlers/ArticleItemHandler.php +++ /dev/null @@ -1,204 +0,0 @@ -data)) { - - if (isset($this->args['id'])) { - $article = $articles->getArticleById($this->args['id']); - } elseif (isset($this->args['slug'])) { - $article = $articles->getArticleBySlug($this->args['slug']); - } - - } - - if ($article !== false) { - - $this->response->statusCode = 200; - $this->response->setHeader('Content-type', 'application/json'); - $this->response->body = json_encode($article); - - } else { - - $this->response->statusCode = 404; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to locate the article.'; - - } - - } - - /** - * Respond to a PUT request. - */ - protected function put() - { - - // Read the request body, and ensure it is in the proper format. - $article = json_decode($this->request->body, true); - - // Ensure the JSON is well-formed. - if (!$article) { - $this->response->statusCode = 400; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to parse JSON from request body.'; - return; - } - - // Ensure required fields are present. - if (!isset($article['slug']) || $article['slug'] === '') { - $this->response->statusCode = 400; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Request body missing slug.'; - return; - } - - if (!isset($article['title'])) { - $this->response->statusCode = 400; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Request body missing title.'; - return; - } - - if (!isset($article['excerpt'])) { - $this->response->statusCode = 400; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Request body missing excerpt.'; - return; - } - - // Read the list of articles. - $articles = new \apisample\ArticlesController(); - - $oldArticle = false; - - // Locate the article by ID or slug - if (isset($articles->data)) { - - if (isset($this->args['id'])) { - $oldArticle = $articles->getArticleById($this->args['id']); - } elseif (isset($this->args['slug'])) { - $oldArticle = $articles->getArticleBySlug($this->args['slug']); - } - - } - - // Fail if the article identified by the URI does not exist. - if ($oldArticle === false) { - - $this->response->statusCode = 404; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to locate the article.'; - return; - - } - - // If the user located the resource by ID and has passed a slug, - // make sure the new slug is not already in use. - if (isset($this->args['id'])) { - - $slugArticle = $articles->getArticleBySlug($article['slug']); - - if ($slugArticle && $slugArticle['articleId'] != $article['articleId']) { - $this->response->statusCode = 409; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to store article. Slug "' . $article['slug'] . '" is already in use.'; - return; - } - - } - - // Update the article. - - // First, ensure the articleId is set. - // It must match the existing article found earlier. - $article['articleId'] = $oldArticle['articleId']; - - // Keep the results from the update for the response. - $article = $articles->updateArticle($article); - - if ($articles->save() === false) { - $this->response->statusCode = 500; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to write to file. Make sure permissions are set properly.'; - return; - } - - // Ok! - $this->response->statusCode = 200; - $this->response->setHeader('Content-type', 'application/json'); - $this->response->body = json_encode($article); - return; - - } - - /** - * Respond to a DELETE request. - */ - protected function delete() - { - - // Read the list of articles. - $articles = new \apisample\ArticlesController(); - - $article = false; - - // Locate the article by ID or slug - if (isset($articles->data)) { - - if (isset($this->args['id'])) { - $article = $articles->getArticleById($this->args['id']); - } elseif (isset($this->args['slug'])) { - $article = $articles->getArticleBySlug($this->args['slug']); - } - - } - - // Ensure the article exists. - if ($article === false) { - $this->response->statusCode = 404; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to locate the article.'; - return; - } - - // Remove the article and save. - $articles->removeArticle($article['articleId']); - - if ($articles->save() === false) { - $this->response->statusCode = 500; - $this->response->setHeader('Content-type', 'text/plain'); - $this->response->body = 'Unable to write to file. Make sure permissions are set properly.'; - return; - } - - // Ok! - $this->response->statusCode = 200; - return; - - } - -} diff --git a/samples/apisample/index.php b/samples/apisample/index.php deleted file mode 100644 index cf90e5a..0000000 --- a/samples/apisample/index.php +++ /dev/null @@ -1,15 +0,0 @@ -getResponse(); -$response->respond(); -exit; diff --git a/samples/client-side-request-and-response.php b/samples/client-side-request-and-response.php deleted file mode 100644 index c235c2a..0000000 --- a/samples/client-side-request-and-response.php +++ /dev/null @@ -1,55 +0,0 @@ -hostname = $_SERVER['HTTP_HOST']; -$rqst->path = '/wellrested/samples/server-side-response.php'; - -// Issue the request, and read the response returned by the server. -try { - $resp = $rqst->request(); -} catch (CurlException $e) { - - // Explain the cURL error and provide an error status code. - $myResponse = new Response(); - $myResponse->statusCode = 500; - $myResponse->setHeader('Content-Type', 'text/plain'); - $myResponse->body = 'Message: ' .$e->getMessage() ."\n"; - $myResponse->body .= 'Code: ' . $e->getCode() . "\n"; - $myResponse->respond(); - exit; - -} - -// Create new response to send to output to the browser. -$myResponse = new Response(); -$myResponse->statusCode = 200; -$myResponse->setHeader('Content-Type', 'application/json'); - -$json = array( - 'Status Code' => $resp->statusCode, - 'Body' => $resp->body, - 'Headers' => $resp->headers -); -$myResponse->body = json_encode($json); - -$myResponse->respond(); -exit; diff --git a/samples/client-side-request.php b/samples/client-side-request.php deleted file mode 100644 index 156d17d..0000000 --- a/samples/client-side-request.php +++ /dev/null @@ -1,27 +0,0 @@ -uri = 'https://www.google.com/search?q=my+search+terms'; - -// You could also set the members individually, like this: -//$rqst->protocol = 'https'; -//$rqst->hostname = 'www.google.com'; -//$rqst->path = '/search'; -//$rqst->query = array('q' => 'my search terms'); - -// Make the request and obtain an Response instance. -$resp = $rqst->request(); - -// Output the response body and exit. -print $resp->body; -exit; diff --git a/samples/server-side-request-and-response.php b/samples/server-side-request-and-response.php deleted file mode 100644 index b50da20..0000000 --- a/samples/server-side-request-and-response.php +++ /dev/null @@ -1,46 +0,0 @@ -readHttpRequest(); - -// Read some info from the request and store it to an associative array. -$rtn = array( - 'Path' => $rqst->path, - 'URI' => $rqst->uri, - 'Body' => $rqst->body, - 'Method' => $rqst->method, - 'Headers' => $rqst->headers -); - -// Create a new Response instance. -$resp = new Response(); - -// Set the status code to 200 OK. -$resp->statusCode = 200; - -// Set the content type for JSON. -$resp->setHeader('Content-Type', 'application/json'); - -// Add the associative array, encoded as JSON, as the body. -// (Note, setting the body automatically adds a Content-Length header.) -$resp->body = json_encode($rtn); - -// Output the response. -$resp->respond(); diff --git a/samples/server-side-response.php b/samples/server-side-response.php deleted file mode 100644 index dd9bf16..0000000 --- a/samples/server-side-response.php +++ /dev/null @@ -1,18 +0,0 @@ -statusCode = 200; -$resp->setHeader('Content-Type', 'text/plain'); -$resp->body = 'This is a response.'; -$resp->respond(); -exit; diff --git a/src/pjdietz/WellRESTed/Request.php b/src/pjdietz/WellRESTed/Request.php index 4c5856a..801b5a5 100644 --- a/src/pjdietz/WellRESTed/Request.php +++ b/src/pjdietz/WellRESTed/Request.php @@ -307,7 +307,7 @@ class Request extends Message $error = curl_error($ch); $errno = curl_errno($ch); curl_close($ch); - throw new exceptions\CurlException($error, $errno); + throw new Exceptions\CurlException($error, $errno); } // Make a reponse to populate and return with data obtained via cURL.