22
33namespace Pdsinterop \Solid \Resources ;
44
5+ use EasyRdf_Exception ;
6+ use EasyRdf_Graph as Graph ;
7+ use Laminas \Diactoros \ServerRequest ;
58use League \Flysystem \FilesystemInterface as Filesystem ;
9+ use LogicException ;
610use Psr \Http \Message \ResponseInterface as Response ;
711use Psr \Http \Message \ServerRequestInterface as Request ;
12+ use Throwable ;
813use WebSocket \Client ;
914
1015class Server
@@ -35,6 +40,8 @@ class Server
3540 'PUT ' ,
3641 ];
3742 /** @var string */
43+ private $ basePath ;
44+ /** @var string */
3845 private $ baseUrl ;
3946 /** @var Filesystem */
4047 private $ filesystem ;
@@ -59,7 +66,7 @@ final public function setBaseUrl($url)
5966 {
6067 $ this ->baseUrl = $ url ;
6168
62- $ serverRequest = new \ Laminas \ Diactoros \ ServerRequest (array (),array (), $ this ->baseUrl );
69+ $ serverRequest = new ServerRequest (array (),array (), $ this ->baseUrl );
6370 $ this ->basePath = $ serverRequest ->getUri ()->getPath ();
6471 }
6572
@@ -123,15 +130,15 @@ private function handle(string $method, string $path, $contents, $request): Resp
123130 $ response = $ response ->withStatus (500 );
124131
125132 // Set Accept, Allow, and CORS headers
126- $ response = $ response
133+ // $response = $response
127134 // ->withHeader('Access-Control-Allow-Origin', '*')
128135 // ->withHeader('Access-Control-Allow-Credentials','true')
129136 // ->withHeader('Access-Control-Allow-Headers', '*, authorization, accept, content-type')
130137 // @FIXME: Add correct headers to resources (for instance allow DELETE on a GET resource)
131138 // ->withAddedHeader('Accept-Patch', 'text/ldpatch')
132139 // ->withAddedHeader('Accept-Post', 'text/turtle, application/ld+json, image/bmp, image/jpeg')
133140 // ->withHeader('Allow', 'GET, HEAD, OPTIONS, PATCH, POST, PUT');
134- ;
141+ // ;
135142
136143 switch ($ method ) {
137144 case 'DELETE ' :
@@ -232,7 +239,7 @@ private function handle(string $method, string $path, $contents, $request): Resp
232239 break ;
233240 default :
234241 $ message = vsprintf (self ::ERROR_UNKNOWN_HTTP_METHOD , [$ method ]);
235- throw new \ LogicException ($ message );
242+ throw new LogicException ($ message );
236243 break ;
237244 }
238245
@@ -242,7 +249,7 @@ private function handle(string $method, string $path, $contents, $request): Resp
242249 private function handleSparqlUpdate (Response $ response , string $ path , $ contents ): Response
243250 {
244251 $ filesystem = $ this ->filesystem ;
245- $ graph = new \ EasyRdf_Graph ();
252+ $ graph = new Graph ();
246253
247254 if ($ filesystem ->has ($ path ) === false ) {
248255 $ data = '' ;
@@ -272,19 +279,19 @@ private function handleSparqlUpdate(Response $response, string $path, $contents)
272279 break ;
273280 case "DELETE " :
274281 // delete $triples from $graph
275- $ deleteGraph = new \ EasyRdf_Graph ();
282+ $ deleteGraph = new Graph ();
276283 $ deleteGraph ->parse ($ triples , "turtle " ); // FIXME: The triples here are in sparql format, not in turtle;
277284 $ resources = $ deleteGraph ->resources ();
278285 foreach ($ resources as $ resource ) {
279286 $ properties = $ resource ->propertyUris ();
280287 foreach ($ properties as $ property ) {
281288 $ values = $ resource ->all ($ property );
282- if (!sizeof ($ values )) {
289+ if (!count ($ values )) {
283290 $ graph ->delete ($ resource , $ property );
284291 } else {
285292 foreach ($ values as $ value ) {
286293 $ count = $ graph ->delete ($ resource , $ property , $ value );
287- if ($ count == 0 ) {
294+ if ($ count === 0 ) {
288295 throw new \Exception ("Could not delete a value " , 500 );
289296 }
290297 }
@@ -305,18 +312,19 @@ private function handleSparqlUpdate(Response $response, string $path, $contents)
305312
306313 if ($ filesystem ->has ($ path ) === true ) {
307314 $ success = $ filesystem ->update ($ path , $ output );
308- $ response = $ response ->withStatus ($ success ? 201 : 500 );
309- } else {
315+ } else {
310316 $ success = $ filesystem ->write ($ path , $ output );
311- $ response = $ response ->withStatus ($ success ? 201 : 500 );
312- }
313- if ($ success ) {
317+ }
318+
319+ $ response = $ response ->withStatus ($ success ? 201 : 500 );
320+
321+ if ($ success ) {
314322 $ this ->sendWebsocketUpdate ($ path );
315323 }
316- } catch (\ EasyRdf_Exception $ exception ) {
324+ } catch (EasyRdf_Exception $ exception ) {
317325 $ response ->getBody ()->write (self ::ERROR_CAN_NOT_PARSE_FOR_PATCH );
318326 $ response = $ response ->withStatus (501 );
319- } catch (\ Exception $ exception ) {
327+ } catch (Throwable $ exception ) {
320328 $ response ->getBody ()->write (self ::ERROR_CAN_NOT_PARSE_FOR_PATCH );
321329 $ response = $ response ->withStatus (501 );
322330 }
@@ -349,12 +357,12 @@ private function handleCreateRequest(Response $response, string $path, $contents
349357
350358 private function parentPath ($ path )
351359 {
352- if ($ path == "/ " ) {
360+ if ($ path === "/ " ) {
353361 return "/ " ;
354362 }
355363 $ pathicles = explode ("/ " , $ path );
356364 $ end = array_pop ($ pathicles );
357- if ($ end == "" ) {
365+ if ($ end === "" ) {
358366 array_pop ($ pathicles );
359367 }
360368 return implode ("/ " , $ pathicles ) . "/ " ;
@@ -385,19 +393,18 @@ private function sendWebsocketUpdate($path)
385393 return ; // no pubsub server available, don't even try;
386394 }
387395
388- $ pubsub = str_replace ("https:// " , "ws:// " , $ pubsub );
389- $ pubsub = str_replace ("http:// " , "ws:// " , $ pubsub );
396+ $ pubsub = str_replace (["https:// " , "http:// " ], "ws:// " , $ pubsub );
390397
391- $ baseUrl = $ this ->baseUrl ;
398+ $ baseUrl = $ this ->baseUrl ;
392399
393- $ client = new \ WebSocket \ Client ($ pubsub , array (
400+ $ client = new Client ($ pubsub , array (
394401 'headers ' => array (
395402 'Sec-WebSocket-Protocol ' => 'solid-0.1 '
396403 )
397404 ));
398405 $ client ->send ("pub $ baseUrl$ path \n" );
399406
400- while ($ path != "/ " ) {
407+ while ($ path !== "/ " ) {
401408 $ path = $ this ->parentPath ($ path );
402409 $ client ->send ("pub $ baseUrl$ path \n" );
403410 }
@@ -482,7 +489,7 @@ private function getRequestedMimeType($accept)
482489 private function handleReadRequest (Response $ response , string $ path , $ contents , $ mime ='' ): Response
483490 {
484491 $ filesystem = $ this ->filesystem ;
485- if ($ path == "/ " ) { // FIXME: this is a patch to make it work for Solid-Nextcloud; we should be able to just list '/';
492+ if ($ path === "/ " ) { // FIXME: this is a patch to make it work for Solid-Nextcloud; we should be able to just list '/';
486493 $ contents = $ this ->listDirectoryAsTurtle ($ path );
487494 $ response ->getBody ()->write ($ contents );
488495 $ response = $ response ->withHeader ("Content-type " , "text/turtle " );
@@ -533,19 +540,21 @@ private function guid()
533540 private function listDirectoryAsTurtle ($ path )
534541 {
535542 $ filesystem = $ this ->filesystem ;
536- if ($ path == "/ " ) {
543+ if ($ path === "/ " ) {
537544 $ listContents = $ filesystem ->listContents (". " );// FIXME: this is a patch to make it work for Solid-Nextcloud; we should be able to just list '/';
538545 } else {
539546 $ listContents = $ filesystem ->listContents ($ path );
540547 }
541548 // CHECKME: maybe structure this data als RDF/PHP
542549 // https://www.easyrdf.org/docs/rdf-formats-php
543550
551+ // @FIXME: The $name variable is declared here but never used. Should it be removed or is there a bug further down?
544552 $ name = basename ($ path ) . ": " ;
545553 // turtle syntax doesn't allow labels that start with a number, so prefix it if it does;
546554 if (preg_match ("/^\d/ " , $ name )) {
547555 $ name = "container- " . $ name ;
548556 }
557+
549558 $ turtle = array (
550559 "<> " => array (
551560 "a " => array ("ldp:BasicContainer " , "ldp:Container " , "ldp:Resource " ),
@@ -586,7 +595,7 @@ private function listDirectoryAsTurtle($path)
586595 $ container .= "\n$ name \n" ;
587596 $ lines = [];
588597 foreach ($ item as $ property => $ values ) {
589- if (sizeof ($ values )) {
598+ if (count ($ values )) {
590599 $ lines [] = "\t" . $ property . " " . implode (", " , $ values );
591600 }
592601 }
0 commit comments