collection = dirname(__FILE__).'/representations/collection';
}
/**
* Handle a GET request
* @param Request request
* @return Response
*/
function get($request) {
$response = new Response($request);
$collection = str_replace('/', DIRECTORY_SEPARATOR, $this->collection);
$resourceUris = '';
$files = glob($collection.DIRECTORY_SEPARATOR.'*');
if ($files) {
foreach ($files as $filepath) {
$filepath = str_replace(DIRECTORY_SEPARATOR, '/', $filepath);
$resourceUris .= '
'.basename($filepath).'';
}
} else {
$resourceUris .= 'Empty collection';
}
$response->body = '';
return $response;
}
protected function getNextAvailableItemUri() {
$collection = str_replace('/', DIRECTORY_SEPARATOR, $this->collection);
$filename = 1;
while (file_exists($collection.DIRECTORY_SEPARATOR.$filename)) {
$filename++;
}
return $this->uriStub.substr($this->collection, strlen($this->path)).'/'.$filename;
}
function post($request) {
$response = new Response($request);
if ($request->data) {
$uri = $this->getNextAvailableItemUri();
$filePath = $this->turnUriIntoFilePath($uri, $request);
if (file_put_contents($filePath, $request->data)) {
$response->code = Response::CREATED;
$response->addHeader('Location', $uri);
} else {
$response->code = Response::INTERNALSERVERERROR;
}
} else {
$response->code = Response::LENGTHREQUIRED;
}
return $response;
}
}