-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Allow redirectedurls to apply to assets (#86)
Co-authored-by: Matt Peel <[email protected]> Co-authored-by: Scott Hutchinson <[email protected]> Co-authored-by: Chris Penny <[email protected]>
- Loading branch information
1 parent
022d623
commit 415b43d
Showing
20 changed files
with
556 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
resources/ | ||
vendor/ | ||
/resources/ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
--- | ||
name: redirectedurls | ||
Name: redirectedurls | ||
--- | ||
SilverStripe\Control\RequestHandler: | ||
extensions: | ||
- SilverStripe\RedirectedURLs\Extension\RedirectedURLHandler | ||
|
||
SilverStripe\CMS\Controllers\ContentController: | ||
extensions: | ||
- SilverStripe\RedirectedURLs\Extension\RedirectedURLHandler | ||
|
||
SilverStripe\CMS\Controllers\ModelAsController: | ||
extensions: | ||
- SilverStripe\RedirectedURLs\Extension\RedirectedURLHandler | ||
|
||
SilverStripe\ORM\DatabaseAdmin: | ||
classname_value_remapping: | ||
RedirectedURL: 'SilverStripe\RedirectedURLs\Model\RedirectedURL' | ||
|
||
SilverStripe\RedirectedURLs\Model\RedirectedURL: | ||
default_redirect_code: 301 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,39 +9,19 @@ | |
|
||
/** | ||
* Provides CMS Administration of {@link: RedirectedURL} objects | ||
* | ||
* @package redirectedurls | ||
* @author [email protected] | ||
* @author [email protected] | ||
*/ | ||
class RedirectedURLAdmin extends ModelAdmin | ||
{ | ||
|
||
/** | ||
* @var string | ||
* @config | ||
*/ | ||
private static $url_segment = 'redirects'; | ||
private static string $url_segment = 'redirects'; | ||
|
||
/** | ||
* @var string | ||
* @config | ||
*/ | ||
private static $menu_title = 'Redirects'; | ||
private static string $menu_title = 'Redirects'; | ||
|
||
/** | ||
* @var string | ||
* @config | ||
*/ | ||
private static $menu_icon = 'silverstripe/redirectedurls:images/redirect.svg'; | ||
private static string $menu_icon = 'silverstripe/redirectedurls:images/redirect.svg'; | ||
|
||
/** | ||
* @var array | ||
* @config | ||
*/ | ||
private static $managed_models = array( | ||
private static array $managed_models = [ | ||
RedirectedURL::class, | ||
); | ||
]; | ||
|
||
/** | ||
* Overridden to add duplicate checking to the bulkloader to prevent | ||
|
@@ -57,6 +37,7 @@ public function getModelImporters() | |
$importer->duplicateChecks = [ | ||
'FromBase' => ['callback' => 'findByFrom'], | ||
]; | ||
|
||
return [ | ||
RedirectedURL::class => $importer | ||
]; | ||
|
@@ -73,9 +54,11 @@ public function getModelImporters() | |
public function getExportFields() | ||
{ | ||
$fields = array(); | ||
|
||
foreach (DataObject::getSchema()->databaseFields($this->modelClass) as $field => $spec) { | ||
$fields[$field] = $field; | ||
} | ||
|
||
return $fields; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace SilverStripe\RedirectedURLs\Extension; | ||
|
||
use SilverStripe\Control\Controller; | ||
use SilverStripe\Control\HTTPResponse; | ||
use SilverStripe\Core\Extension; | ||
use SilverStripe\RedirectedURLs\Model\RedirectedURL; | ||
use SilverStripe\RedirectedURLs\Service\RedirectedURLService; | ||
|
||
/** | ||
* This extension applies to FlysystemAssetStore, and ensures that an appropriate redirect response is returned when an | ||
* asset isn't found and the path matches a {@link RedirectedURL} object. | ||
*/ | ||
class AssetStoreURLHandler extends Extension | ||
{ | ||
/** | ||
* @var array An array of HTTP status codes that should be acted upon if they are returned by the AssetStore. | ||
* @config | ||
*/ | ||
private static array $act_upon = [ | ||
404, | ||
]; | ||
|
||
public function updateResponse(HTTPResponse &$response, string $asset, array $context = []) | ||
{ | ||
// Only change the response if the response provided by FlysystemAssetStore matches one we should act on | ||
if (!in_array($response->getStatusCode(), $this->owner->config()->act_upon)) { | ||
return; | ||
} | ||
|
||
// We are unable to progress if there is no current Controller | ||
if (!Controller::has_curr()) { | ||
return; | ||
} | ||
|
||
// Get the current request, then attempt to find a RedirectedURL object that matches | ||
$controller = Controller::curr(); | ||
$request = $controller->getRequest(); | ||
|
||
$service = RedirectedURLService::create(); | ||
$match = $service->findBestRedirectedURLMatch($request); | ||
|
||
if ($match) { | ||
// We have a matching RedirectedURL, so replace the base HTTPResponse provided by | ||
// FlysystemAssetStore with our redirect response | ||
$response = $service->getResponse($match); | ||
} | ||
} | ||
} |
Oops, something went wrong.