From d7b2c40c5775f1865de88c84555a712a5c132b23 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Sun, 2 Aug 2020 11:12:51 +0200 Subject: [PATCH] Fixed Resources being unable to serve static files when using more complicated URI's. --- src/FuzeWorks/Event/ResourceServeEvent.php | 6 +-- src/FuzeWorks/Resources.php | 43 ++++++++++------------ src/FuzeWorks/WebComponent.php | 13 ++++--- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/FuzeWorks/Event/ResourceServeEvent.php b/src/FuzeWorks/Event/ResourceServeEvent.php index 109dc51..913a320 100644 --- a/src/FuzeWorks/Event/ResourceServeEvent.php +++ b/src/FuzeWorks/Event/ResourceServeEvent.php @@ -47,17 +47,17 @@ class ResourceServeEvent extends Event /** * @var array */ - public $resourceUrlSegments; + public $requestURL; /** * @var string */ public $resourceFilePath; - public function init(string $resourceName, array $resourceUrlSegments, string $resourceFilePath) + public function init(string $resourceName, string $requestURL, string $resourceFilePath) { $this->resourceName = $resourceName; - $this->resourceUrlSegments = $resourceUrlSegments; + $this->requestURL = $requestURL; $this->resourceFilePath = $resourceFilePath; } } \ No newline at end of file diff --git a/src/FuzeWorks/Resources.php b/src/FuzeWorks/Resources.php index 4dd39eb..e47eb19 100644 --- a/src/FuzeWorks/Resources.php +++ b/src/FuzeWorks/Resources.php @@ -61,10 +61,10 @@ class Resources $this->output = Factory::getInstance()->output; } - public function resourceExists(array $resourceUrlSegments): bool + public function resourceExists(string $requestURL): bool { // First find the resource - $file = $this->findResource($resourceUrlSegments); + $file = $this->findResource($requestURL); // If not found, return false; if (is_null($file)) @@ -77,17 +77,17 @@ class Resources /** * Serves a static file if found. * - * @param array $resourceUrlSegments + * @param string $requestURL * @return bool * @throws WebException * * @todo Bypass the Output system and use the readFile() method. * @todo Run as FuzeWorks pre-code, before creating the container */ - public function serveResource(array $resourceUrlSegments): bool + public function serveResource(string $requestURL): bool { // First find the resource - $file = $this->findResource($resourceUrlSegments); + $file = $this->findResource($requestURL); // If not found return false if (is_null($file)) @@ -96,7 +96,7 @@ class Resources // If a file is found, fire a serveResourceEvent /** @var ResourceServeEvent $event */ try { - $event = Events::fireEvent('resourceServeEvent', $file['resourceName'], $file['segments'], $file['file']); + $event = Events::fireEvent('resourceServeEvent', $file['resourceName'], $file['requestURL'], $file['file']); } catch (Exception\EventException $e) { throw new WebException("Could not serve resource. resourceServeEvent threw exception: '" . $e->getMessage() . "'"); } @@ -106,7 +106,7 @@ class Resources return false; // Log the resource serving - Logger::log("Serving static resource '/" . $file['resourceName'] . '/' . implode('/', $file['segments']) . "'"); + Logger::log("Serving static resource '/" . $file['resourceName'] . '/' . $file['requestURL'] . "'"); // Serve file in accordance with event $fileExtension = pathinfo($event->resourceFilePath, PATHINFO_EXTENSION); @@ -118,25 +118,22 @@ class Resources return true; } - protected function findResource(array $resourceUrlSegments): ?array + protected function findResource(string $requestURL): ?array { - // If too few segments provided, don't even bother - if (count($resourceUrlSegments) < 2) - return null; - // First segment should be the resourceName, check if it exists - $resourceName = urldecode($resourceUrlSegments[1]); - if (!isset($this->resources[$resourceName])) - return null; + foreach ($this->resources as $resourceName => $resourceDir) + { + if (substr($requestURL, 0, strlen($resourceName)) === $resourceName) + { + $fileURL = ltrim(substr($requestURL, strlen($resourceName)), '/'); + $fileURL = str_replace('/', DS, $fileURL); + $file = $this->resources[$resourceName] . DS . $fileURL; - // If resource is found, generate file path - $resourceUrlSegmentsBck = $resourceUrlSegments; - array_shift($resourceUrlSegments); - $file = $this->resources[$resourceName] . DS . implode(DS, $resourceUrlSegments); - - // Test if file exists, if it does, return the string - if (file_exists($file) && is_file($file)) - return ['file' => $file, 'resourceName' => $resourceName, 'segments' => $resourceUrlSegments]; + // Test if file exists, if it does, return the string + if (file_exists($file) && is_file($file)) + return ['file' => $file, 'resourceName' => $resourceName, 'requestURL' => $fileURL]; + } + } return null; } diff --git a/src/FuzeWorks/WebComponent.php b/src/FuzeWorks/WebComponent.php index 682d572..2f22f88 100644 --- a/src/FuzeWorks/WebComponent.php +++ b/src/FuzeWorks/WebComponent.php @@ -242,7 +242,7 @@ class WebComponent implements iComponent return true; // Attempt to load a static resource - if ($resources->serveResource($uri->segmentArray())) + if ($resources->serveResource($uri->uriString())) return true; // First test for Cross Site Request Forgery @@ -343,14 +343,17 @@ class WebComponent implements iComponent /** @var Layout $layout */ $output = Factory::getInstance()->output; $router = Factory::getInstance()->router; - $layout = Factory::getInstance()->layouts; + + // Reset the layout engine + if (isset(Factory::getInstance()->layouts)) + { + $layout = Factory::getInstance()->layouts; + $layout->reset(); + } // Cancel event $event->setCancelled(true); - // Reset the layout engine - $layout->reset(); - // Remove listener so that error pages won't be intercepted Events::removeListener([$this, 'callViewEventListener'], 'routerCallViewEvent',Priority::HIGHEST);