From 42ab496f3e8de8c4ac614f795527c63c0c883b9c Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 24 Dec 2019 14:45:34 +1100 Subject: [PATCH] Added support for non-document datafiles. --- src/Asset.php | 47 ++++++++++++++++++++++-------- src/AssetInterface.php | 5 ++++ src/Controller/AliasController.php | 10 +++---- tests/src/Functional/AssetTest.php | 1 + 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/Asset.php b/src/Asset.php index cde802a..2b689aa 100644 --- a/src/Asset.php +++ b/src/Asset.php @@ -217,9 +217,9 @@ public static function loadByAlias($alias) { * {@inheritdoc} */ public function save() { - // We are tracking only documents. Non-document assets are accessed - // directly. - if (!$this->isDocument()) { + // We are tracking only documents and datafiles. Non-document assets are + // accessed directly. + if (!$this->isDocument() && !$this->isDatafile()) { return NULL; } @@ -294,20 +294,23 @@ public function render() { // We should never render non-document files - those files are expected to // be accessed directly (not through aliased path). - if (!$this->isDocument()) { + if (!$this->isDocument() && !$this->isDatafile()) { throw new AssetException(sprintf('Unable to render a non-document file "%s"', $file_uri)); } $content = file_get_contents($file_uri); - try { - $processor = new PageProcessor($content, $this->urlBag); - $processor->process(); - $content = $processor->content(); - } - catch (PageProcessorException $exception) { - // Simply pass-through as unprocessed content on processor exception and - // fail for anything else. + // Only process documents. + if ($this->isDocument()) { + try { + $processor = new PageProcessor($content, $this->urlBag); + $processor->process(); + $content = $processor->content(); + } + catch (PageProcessorException $exception) { + // Simply pass-through as unprocessed content on processor exception and + // fail for anything else. + } } return $content; @@ -404,4 +407,24 @@ public function isDocument() { return FALSE; } + /** + * {@inheritdoc} + */ + public function isDatafile() { + try { + FileValidator::validateFileExtension($this->urlBag->getUri(), [ + 'json', + 'txt', + 'xml', + ]); + + return TRUE; + } + catch (InvalidExtensionValidatorException $exception) { + // Do nothing as this is expected. + } + + return FALSE; + } + } diff --git a/src/AssetInterface.php b/src/AssetInterface.php index 6f8695a..7ad570b 100644 --- a/src/AssetInterface.php +++ b/src/AssetInterface.php @@ -176,4 +176,9 @@ public function isIndex(); */ public function isDocument(); + /** + * Check if asset is a data file and can be served directly. + */ + public function isDatafile(); + } diff --git a/src/Controller/AliasController.php b/src/Controller/AliasController.php index c1e50ea..a8f15df 100644 --- a/src/Controller/AliasController.php +++ b/src/Controller/AliasController.php @@ -33,11 +33,11 @@ class AliasController implements ContainerAwareInterface { public function deliverAsset($asset_id) { $asset = Asset::load($asset_id); - // Only deliver documents through alias controller. There are other checks - // in the class itself, but this is one last gate keeping check to - // explicitly prevent non-documents from being served through alias - // callback. - if (!$asset || !$asset->isDocument()) { + // Only deliver documents and datafiles through alias controller. + // There are other checks in the class itself, but this is one last gate + // keeping check to explicitly prevent non-documents from being served + // through alias callback. + if (!$asset || (!$asset->isDocument() && !$asset->isDatafile())) { throw new NotFoundHttpException(); } diff --git a/tests/src/Functional/AssetTest.php b/tests/src/Functional/AssetTest.php index fab0b00..0acb919 100644 --- a/tests/src/Functional/AssetTest.php +++ b/tests/src/Functional/AssetTest.php @@ -45,6 +45,7 @@ public function testAssetInstance() { // Assert other getters. $this->assertEquals(Language::LANGCODE_DEFAULT, $asset->getLanguage()); $this->assertTrue($asset->isDocument()); + $this->assertFalse($asset->isDatafile()); $this->assertFalse($asset->isIndex()); // Assert saving.