[6351] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * Use the filesystem as resource representations |
---|
| 5 | * |
---|
| 6 | * This example loads files from the filesystem to be used as resource representations |
---|
| 7 | * and provides a resource collection to contain them within. It also demonstrates |
---|
| 8 | * the ability to add resources to the collection, and update and delete resources |
---|
| 9 | * from within the collection. |
---|
| 10 | * |
---|
| 11 | * @namespace Tonic\Examples\Filesystem |
---|
| 12 | * @uri /filesystem(/.*)? |
---|
| 13 | */ |
---|
| 14 | class FilesystemResource extends Resource { |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * Path to the files to use |
---|
| 18 | * @var str |
---|
| 19 | */ |
---|
| 20 | var $path; |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * URI stub |
---|
| 24 | * @var str |
---|
| 25 | */ |
---|
| 26 | var $uriStub = '/filesystem/'; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * The default document to use if the request is for a URI that maps to a directory |
---|
| 30 | * @var str |
---|
| 31 | */ |
---|
| 32 | var $defaultDocument = 'default.html'; |
---|
| 33 | |
---|
| 34 | function __construct($parameters) { |
---|
| 35 | parent::__construct($parameters); |
---|
| 36 | $this->path = dirname(__FILE__).'/representations/'; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | protected function turnUriIntoFilePath($uri, $request) { |
---|
| 40 | return $this->path.DIRECTORY_SEPARATOR.substr($uri, strlen($request->baseUri.$this->uriStub)); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | protected function turnFilePathIntoUri($path, $request) { |
---|
| 44 | return $request->baseUri.$this->uriStub.substr($path, strlen($this->path)); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Handle a GET request for this resource by returning the contents of a file matching the request URI |
---|
| 49 | * @param Request request |
---|
| 50 | * @return Response |
---|
| 51 | */ |
---|
| 52 | function get($request) { |
---|
| 53 | |
---|
| 54 | // look at all candidate URIs in turn and stop when we find a file that matches one |
---|
| 55 | foreach ($request->negotiatedUris as $uri) { |
---|
| 56 | |
---|
| 57 | // convert URI into filesystem path |
---|
| 58 | $filePath = $this->turnUriIntoFilePath($uri, $request); |
---|
| 59 | |
---|
| 60 | if (substr($filePath, -1, 1) == DIRECTORY_SEPARATOR) { // add a default filename to the path |
---|
| 61 | $filePath .= $this->defaultDocument; |
---|
| 62 | $uri .= $this->defaultDocument; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if (file_exists($filePath)) { // use this file |
---|
| 66 | |
---|
| 67 | $response = new Response($request, $uri); |
---|
| 68 | |
---|
| 69 | // generate etag for the resource based on the files modified date |
---|
| 70 | $etag = md5(filemtime($filePath)); |
---|
| 71 | if ($request->ifNoneMatch($etag)) { // client has matching etag |
---|
| 72 | |
---|
| 73 | $response->code = Response::NOTMODIFIED; |
---|
| 74 | |
---|
| 75 | } else { |
---|
| 76 | |
---|
| 77 | $explodedPath = explode('.', $filePath); |
---|
| 78 | $extension = array_pop($explodedPath); |
---|
| 79 | if (isset($request->mimetypes[$extension])) { // add content type header |
---|
| 80 | $response->addHeader('Content-Type', $request->mimetypes[$extension]); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | $response->addEtag($etag); // add etag header |
---|
| 84 | |
---|
| 85 | $response->body = file_get_contents($filePath); // set contents |
---|
| 86 | |
---|
| 87 | } |
---|
| 88 | return $response; |
---|
| 89 | |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // nothing found, send 404 response |
---|
| 95 | $response = new Response($request); |
---|
| 96 | $response->code = Response::NOTFOUND; |
---|
| 97 | $response->addHeader('Content-Type', $request->mimetypes['html']); |
---|
| 98 | $response->body = '<p>404, nothing found</p>'; |
---|
| 99 | return $response; |
---|
| 100 | |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Handle a PUT request for this resource by overwriting the resources contents |
---|
| 105 | * @param Request request |
---|
| 106 | * @return Response |
---|
| 107 | */ |
---|
| 108 | function put($request) { |
---|
| 109 | |
---|
| 110 | $response = new Response($request); |
---|
| 111 | |
---|
| 112 | if ($request->data) { |
---|
| 113 | |
---|
| 114 | $filePath = $this->turnUriIntoFilePath($request->uri, $request); |
---|
| 115 | |
---|
| 116 | file_put_contents($filePath, $request->data); |
---|
| 117 | |
---|
| 118 | $response->code = Response::NOCONTENT; |
---|
| 119 | |
---|
| 120 | } else { |
---|
| 121 | |
---|
| 122 | $response->code = Response::LENGTHREQUIRED; |
---|
| 123 | |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return $response; |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Handle a DELETE request for this resource by removing the resources file |
---|
| 132 | * @param Request request |
---|
| 133 | * @return Response |
---|
| 134 | */ |
---|
| 135 | function delete($request) { |
---|
| 136 | |
---|
| 137 | $response = new Response($request); |
---|
| 138 | |
---|
| 139 | $filePath = $this->turnUriIntoFilePath($request->uri, $request); |
---|
| 140 | |
---|
| 141 | if (file_exists($filePath)) { |
---|
| 142 | |
---|
| 143 | if (unlink($filePath)) { |
---|
| 144 | $response->code = Response::NOCONTENT; |
---|
| 145 | } else { |
---|
| 146 | $response->code = Response::INTERNALSERVERERROR; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return $response; |
---|
| 150 | |
---|
| 151 | } else { // nothing found, send 404 response |
---|
| 152 | $response->code = Response::NOTFOUND; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | return $response; |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | } |
---|
| 160 | |
---|