Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract static content handling into reusable class #102

Merged
merged 4 commits into from
Jan 30, 2024

Conversation

thekid
Copy link
Member

@thekid thekid commented Oct 15, 2023

This pull request extracts the handling of files, including mime type detection, HEAD, conditional and range request handling from the web.handlers.FilesFrom class into a reusable class web.io.StaticContent. Its standalone usage is:

$content= (new StaticContent())->with(['Cache-Control' => '...']);
return $content->serve($req, $res, new File('...'), 'image/gif');

Instead of having to inherit from FilesFrom, implementations can now use StaticContent, e.g.:

diff --git a/src/main/php/web/frontend/AssetsFrom.class.php b/src/main/php/web/frontend/AssetsFrom.class.php
index 442631f..08e0528 100755
--- a/src/main/php/web/frontend/AssetsFrom.class.php
+++ b/src/main/php/web/frontend/AssetsFrom.class.php
@@ -2,7 +2,8 @@
 
 use io\Path;
 use util\MimeType;
-use web\handler\FilesFrom;
+use web\Handler;
+use web\io\StaticContent;
 
 /**
  * Serves assets from a given path. Checks for files with extensions matching
@@ -14,7 +15,7 @@ use web\handler\FilesFrom;
  * @see  https://www.rootusers.com/gzip-vs-bzip2-vs-xz-performance-comparison/
  * @see  https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
  */
-class AssetsFrom extends FilesFrom {
+class AssetsFrom implements Handler {
   const PREFERENCE= ['br', 'bzip2', 'gzip', 'deflate'];
   const ENCODINGS= [
     'br'       => '.br',
@@ -26,7 +27,7 @@ class AssetsFrom extends FilesFrom {
   ];
 
   private $sources= [];
-  private $preference;
+  private $preference, $content;
 
   /** @param io.Path|io.Folder|string|io.Path[]|io.Folder[]|string[] $sources */
   public function __construct($sources) {
@@ -34,7 +35,18 @@ class AssetsFrom extends FilesFrom {
     foreach (is_array($sources) ? $sources : [$sources] as $source) {
       $this->sources[]= $source instanceof Path ? $source : new Path($source);
     }
-    parent::__construct($this->sources[0] ?? '.');
+    $this->content= new StaticContent();
+  }
+
+  /**
+   * Adds headers to successful responses, either from an array or a function.
+   *
+   * @param  [:string]|function(util.URI, io.File, string): iterable $headers
+   * @return self
+   */
+  public function with($headers) {
+    $this->content->with($headers);
+    return $this;
   }
 
   /**
@@ -122,12 +134,12 @@ class AssetsFrom extends FilesFrom {
           $response->header('Vary', 'Accept-Encoding');
           '*' === $encoding || $response->header('Content-Encoding', $encoding);
 
-          return $this->serve($request, $response, $target->asFile(), $this->mime($path));
+          return $this->content->serve($request, $response, $target->asFile(), $this->mime($path));
         }
       }
     }
 
     // Target does not exist in any of the sources, generate a 404
-    return $this->serve($request, $response, null);
+    return $this->content->serve($request, $response, null);
   }
 }
\ No newline at end of file

See also xp-forge/frontend#39

@thekid thekid merged commit 43e8e15 into master Jan 30, 2024
20 checks passed
@thekid thekid deleted the refactor/static-content branch January 30, 2024 17:35
@thekid
Copy link
Member Author

thekid commented Jan 30, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant