mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 11:05:56 +02:00
APICHANGE: refactored names and structure of handleAction rerouting to a) be more clearer and b) to allow permalinks
This commit is contained in:
parent
0be89228e7
commit
8937d56547
@ -30,12 +30,25 @@ class DocumentationViewer extends Controller {
|
||||
'DocumentationSearchForm'
|
||||
);
|
||||
|
||||
static $casting = array(
|
||||
'Version' => 'Text',
|
||||
'Lang' => 'Text',
|
||||
'Module' => 'Text',
|
||||
'LanguageTitle' => 'Text'
|
||||
);
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $version = "current";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $language = "en";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $module = '';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $remaining = array();
|
||||
|
||||
/**
|
||||
* @var String Same as the routing pattern set through Director::addRules().
|
||||
@ -88,63 +101,63 @@ class DocumentationViewer extends Controller {
|
||||
public function handleRequest(SS_HTTPRequest $request) {
|
||||
|
||||
// if we submitted a form, let that pass
|
||||
if($request->httpMethod() != "GET") return parent::handleRequest($request);
|
||||
if(!$request->isGET()) return parent::handleRequest($request);
|
||||
|
||||
$firstParam = ($request->param('Action')) ? $request->param('Action') : $request->shift();
|
||||
$secondParam = $request->shift();
|
||||
$thirdParam = $request->shift();
|
||||
|
||||
$this->Version = ($request->param('Action')) ? $request->param('Action') : $request->shift();
|
||||
$this->Lang = $request->shift();
|
||||
$this->ModuleName = $request->shift();
|
||||
$this->Remaining = $request->shift(10);
|
||||
|
||||
DocumentationService::load_automatic_registration();
|
||||
|
||||
if(isset($this->Version)) {
|
||||
// check to see if its a valid version. If its not a float then its not actually a version
|
||||
// its actually a language and it needs to change. So this means we support 2 structures
|
||||
// /2.4/en/sapphire/page and
|
||||
// /en/sapphire/page which is a link to the latest one
|
||||
|
||||
if(isset($firstParam)) {
|
||||
if($link = DocumentationPermalinks::map($firstParam)) {
|
||||
// the first param is a shortcode for a page so redirect the user to
|
||||
// the short code.
|
||||
$this->response = new SS_HTTPResponse();
|
||||
|
||||
if(!is_numeric($this->Version) && $this->Version != 'current') {
|
||||
array_unshift($this->Remaining, $this->ModuleName);
|
||||
$this->redirect($link, 301); // permanent redirect
|
||||
|
||||
// not numeric so /en/sapphire/folder/page
|
||||
if(isset($this->Lang) && $this->Lang)
|
||||
$this->ModuleName = $this->Lang;
|
||||
|
||||
$this->Lang = $this->Version;
|
||||
$this->Version = null;
|
||||
|
||||
return $this->response;
|
||||
|
||||
}
|
||||
else if(is_numeric($firstParam)) {
|
||||
// its a version number first in the form 2.4/en/sapphire
|
||||
$this->version = $firstParam;
|
||||
$this->lang = $secondParam;
|
||||
$this->module = $thirdParam;
|
||||
}
|
||||
else {
|
||||
// if(!DocumentationService::is_registered_version($this->Version)) {
|
||||
// $this->httpError(404, 'The requested version could not be found.');
|
||||
// }
|
||||
// we have a language first in the form /en/sapphire
|
||||
array_unshift($this->Remaining, $thirdParam);
|
||||
|
||||
$this->lang = $firstParam;
|
||||
$this->module = $secondParam;
|
||||
}
|
||||
}
|
||||
if(isset($this->Lang)) {
|
||||
// check to see if its a valid language
|
||||
// if(!DocumentationService::is_registered_language($this->Lang)) {
|
||||
// $this->httpError(404, 'The requested language could not be found.');
|
||||
// }
|
||||
}
|
||||
else {
|
||||
$this->Lang = 'en';
|
||||
}
|
||||
|
||||
// 'current' version mapping
|
||||
$module = DocumentationService::is_registered_module($this->ModuleName, null, $this->Lang);
|
||||
if($this->Version && $module) {
|
||||
$module = DocumentationService::is_registered_module($this->module, null, $this->getLang());
|
||||
|
||||
if($module && $this->getVersion()) {
|
||||
$current = $module->getCurrentVersion();
|
||||
if($this->Version == 'current') {
|
||||
$this->Version = $current;
|
||||
} else {
|
||||
if($current == $this->Version) {
|
||||
$this->Version = 'current';
|
||||
$link = $this->Link($this->Remaining);
|
||||
$this->response = new SS_HTTPResponse();
|
||||
$this->redirect($link, 301); // permanent redirect
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
$version = $this->getVersion();
|
||||
|
||||
if($version == 'current') {
|
||||
$this->version = $current;
|
||||
} else if($current == $version) {
|
||||
$this->version = 'current';
|
||||
$link = $this->Link($this->Remaining);
|
||||
$this->response = new SS_HTTPResponse();
|
||||
$this->redirect($link, 301); // permanent redirect
|
||||
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return parent::handleRequest($request);
|
||||
}
|
||||
@ -156,7 +169,7 @@ class DocumentationViewer extends Controller {
|
||||
// count the number of parameters after the language, version are taken
|
||||
// into account. This automatically includes ' ' so all the counts
|
||||
// are 1 more than what you would expect
|
||||
if($this->ModuleName || $this->Remaining) {
|
||||
if($this->module || $this->Remaining) {
|
||||
|
||||
$paramCount = count($this->Remaining);
|
||||
|
||||
@ -178,6 +191,24 @@ class DocumentationViewer extends Controller {
|
||||
return parent::getViewer($action);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current version
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
function getVersion() {
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current language
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
function getLang() {
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the available languages. Optionally the languages which are
|
||||
* available for a given module
|
||||
@ -302,8 +333,8 @@ class DocumentationViewer extends Controller {
|
||||
* @return false|DocumentationEntity
|
||||
*/
|
||||
function getModule() {
|
||||
if($this->ModuleName) {
|
||||
return DocumentationService::is_registered_module($this->ModuleName, $this->Version, $this->Lang);
|
||||
if($this->module) {
|
||||
return DocumentationService::is_registered_module($this->module, $this->version, $this->lang);
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -404,7 +435,7 @@ class DocumentationViewer extends Controller {
|
||||
function getBreadcrumbs() {
|
||||
if(!$this->Remaining) $this->Remaining = array();
|
||||
|
||||
$pages = array_merge(array($this->ModuleName), $this->Remaining);
|
||||
$pages = array_merge(array($this->module), $this->Remaining);
|
||||
|
||||
$output = new DataObjectSet();
|
||||
|
||||
@ -441,14 +472,16 @@ class DocumentationViewer extends Controller {
|
||||
public function Link($path = false) {
|
||||
$base = Director::absoluteBaseURL();
|
||||
|
||||
$version = ($this->Version) ? $this->Version . '/' : false;
|
||||
$lang = ($this->Lang) ? $this->Lang .'/' : false;
|
||||
$module = ($this->ModuleName) ? $this->ModuleName .'/' : false;
|
||||
$version = ($this->version) ? $this->version . '/' : false;
|
||||
$lang = ($this->language) ? $this->language .'/' : false;
|
||||
$module = ($this->module) ? $this->module .'/' : false;
|
||||
|
||||
$action = '';
|
||||
if(is_string($path)) $action = $path . '/';
|
||||
|
||||
if(is_array($path)) {
|
||||
if(is_string($path)) {
|
||||
$action = $path . '/';
|
||||
}
|
||||
else if(is_array($path)) {
|
||||
foreach($path as $key => $value) {
|
||||
if($value) {
|
||||
$action .= $value .'/';
|
||||
|
@ -45,6 +45,7 @@ class DocumentationViewerTests extends FunctionalTest {
|
||||
|
||||
function testCurrentRedirection() {
|
||||
$response = $this->get('dev/docs/3.0/en/DocumentationViewerTests/test');
|
||||
|
||||
$this->assertEquals(301, $response->getStatusCode());
|
||||
$this->assertEquals(
|
||||
Director::absoluteBaseURL() . 'dev/docs/current/en/DocumentationViewerTests/test/',
|
||||
@ -68,26 +69,27 @@ class DocumentationViewerTests extends FunctionalTest {
|
||||
// Module index
|
||||
$v = new DocumentationViewer();
|
||||
$response = $v->handleRequest(new SS_HTTPRequest('GET', '2.3/en/DocumentationViewerTests/test'));
|
||||
$this->assertEquals('2.3', $v->Version);
|
||||
$this->assertEquals('en', $v->Lang);
|
||||
$this->assertEquals('DocumentationViewerTests', $v->ModuleName);
|
||||
$this->assertEquals(array('test'), $v->Remaining);
|
||||
|
||||
// Module index without version and language
|
||||
$v = new DocumentationViewer();
|
||||
$response = $v->handleRequest(new SS_HTTPRequest('GET', 'en/DocumentationViewerTests/test'));
|
||||
$this->assertEquals(null, $v->Version);
|
||||
$this->assertEquals('en', $v->Lang);
|
||||
$this->assertEquals('DocumentationViewerTests', $v->ModuleName);
|
||||
$this->assertEquals('2.3', $v->getVersion());
|
||||
$this->assertEquals('en', $v->getLang());
|
||||
$this->assertEquals('DocumentationViewerTests', $v->module);
|
||||
$this->assertEquals(array('test'), $v->Remaining);
|
||||
|
||||
// Module index without version and language. Should pick up the defaults
|
||||
$v2 = new DocumentationViewer();
|
||||
$response = $v2->handleRequest(new SS_HTTPRequest('GET', 'en/DocumentationViewerTests/test'));
|
||||
|
||||
$this->assertEquals('3.0', $v2->getVersion());
|
||||
$this->assertEquals('en', $v2->getLang());
|
||||
$this->assertEquals('DocumentationViewerTests', $v2->module);
|
||||
$this->assertEquals(array('test'), $v2->Remaining);
|
||||
|
||||
// Overall index
|
||||
// $v = new DocumentationViewer();
|
||||
// $response = $v->handleRequest(new SS_HTTPRequest('GET', ''));
|
||||
// $this->assertEquals(null, $v->Version);
|
||||
// $this->assertEquals(null, $v->Lang);
|
||||
// $this->assertEquals(null, $v->ModuleName);
|
||||
// $this->assertEquals(array(), $v->Remaining);
|
||||
$v = new DocumentationViewer();
|
||||
$response = $v->handleRequest(new SS_HTTPRequest('GET', ''));
|
||||
$this->assertEquals('current', $v->getVersion());
|
||||
$this->assertEquals('en', $v->getLang());
|
||||
$this->assertEquals('', $v->module);
|
||||
$this->assertEquals(array(), $v->Remaining);
|
||||
}
|
||||
|
||||
function testBreadcrumbs() {
|
||||
@ -95,6 +97,7 @@ class DocumentationViewerTests extends FunctionalTest {
|
||||
$v = new DocumentationViewer();
|
||||
$response = $v->handleRequest(new SS_HTTPRequest('GET', '2.4/en/DocumentationViewerTests/'));
|
||||
$crumbs = $v->getBreadcrumbs();
|
||||
|
||||
$this->assertEquals(1, $crumbs->Count());
|
||||
$crumbLinks = $crumbs->column('Link');
|
||||
$this->assertStringEndsWith('DocumentationViewerTests/', $crumbLinks[0]);
|
||||
|
Loading…
Reference in New Issue
Block a user