mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 08:05:56 +02:00
add config var for namespace mapping page->control, add test to confirm
This commit is contained in:
parent
3b980c40c6
commit
863ed24920
@ -215,6 +215,22 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
|
|||||||
*/
|
*/
|
||||||
private static $controller_name = null;
|
private static $controller_name = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* You can define the a map of Page namespaces to Controller namespaces here
|
||||||
|
* This will apply after the magic of appending Controller, and in order
|
||||||
|
* Must be applied to SiteTree config e.g.
|
||||||
|
*
|
||||||
|
* SilverStripe\CMS\Model\SiteTree:
|
||||||
|
* namespace_map:
|
||||||
|
* "App\Pages": "App\Control"
|
||||||
|
*
|
||||||
|
* Will map App\Pages\MyPage to App\Control\MyPageController
|
||||||
|
*
|
||||||
|
* @config
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $namespace_map = null;
|
||||||
|
|
||||||
private static $db = [
|
private static $db = [
|
||||||
"URLSegment" => "Varchar(255)",
|
"URLSegment" => "Varchar(255)",
|
||||||
"Title" => "Varchar(255)",
|
"Title" => "Varchar(255)",
|
||||||
@ -2989,6 +3005,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
|
|||||||
return $controller;
|
return $controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$namespaceMap = Config::inst()->get(SiteTree::class, 'namespace_mapping');
|
||||||
|
|
||||||
//default controller for SiteTree objects
|
//default controller for SiteTree objects
|
||||||
$controller = ContentController::class;
|
$controller = ContentController::class;
|
||||||
|
|
||||||
@ -3002,8 +3020,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
|
|||||||
}
|
}
|
||||||
// If we have a class of "{$ClassName}Controller" then we found our controller
|
// If we have a class of "{$ClassName}Controller" then we found our controller
|
||||||
if (class_exists($candidate = sprintf('%sController', $class))) {
|
if (class_exists($candidate = sprintf('%sController', $class))) {
|
||||||
$controller = $candidate;
|
return $candidate;
|
||||||
break;
|
|
||||||
} elseif (class_exists($candidate = sprintf('%s_Controller', $class))) {
|
} elseif (class_exists($candidate = sprintf('%s_Controller', $class))) {
|
||||||
// Support the legacy underscored filename, but raise a deprecation notice
|
// Support the legacy underscored filename, but raise a deprecation notice
|
||||||
Deprecation::notice(
|
Deprecation::notice(
|
||||||
@ -3011,8 +3028,20 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
|
|||||||
'Underscored controller class names are deprecated. Use "MyController" instead of "My_Controller".',
|
'Underscored controller class names are deprecated. Use "MyController" instead of "My_Controller".',
|
||||||
Deprecation::SCOPE_GLOBAL
|
Deprecation::SCOPE_GLOBAL
|
||||||
);
|
);
|
||||||
$controller = $candidate;
|
return $candidate;
|
||||||
break;
|
} elseif (is_array($namespaceMap)) {
|
||||||
|
foreach ($namespaceMap as $pageNamespace => $controllerNamespace) {
|
||||||
|
if (strpos($class, $pageNamespace) !== 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$candidate = sprintf(
|
||||||
|
'%sController',
|
||||||
|
str_replace($pageNamespace, $controllerNamespace, $class)
|
||||||
|
);
|
||||||
|
if (class_exists($candidate)) {
|
||||||
|
return $candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\CMS\Tests\Controllers;
|
||||||
|
|
||||||
|
use SilverStripe\CMS\Controllers\ContentController;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class SiteTreeTest_NamespaceMapTestController extends ContentController implements TestOnly
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -9,6 +9,8 @@ use ReflectionMethod;
|
|||||||
use SilverStripe\CMS\Model\RedirectorPage;
|
use SilverStripe\CMS\Model\RedirectorPage;
|
||||||
use SilverStripe\CMS\Model\SiteTree;
|
use SilverStripe\CMS\Model\SiteTree;
|
||||||
use SilverStripe\CMS\Model\VirtualPage;
|
use SilverStripe\CMS\Model\VirtualPage;
|
||||||
|
use SilverStripe\CMS\Tests\Controllers\SiteTreeTest_NamespaceMapTestController;
|
||||||
|
use SilverStripe\CMS\Tests\Page\SiteTreeTest_NamespaceMapTest;
|
||||||
use SilverStripe\Control\ContentNegotiator;
|
use SilverStripe\Control\ContentNegotiator;
|
||||||
use SilverStripe\Control\Controller;
|
use SilverStripe\Control\Controller;
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
@ -1651,6 +1653,19 @@ class SiteTreeTest extends SapphireTest
|
|||||||
$this->assertSame('This\\Is\\A\\New\\Controller', $class->getControllerName());
|
$this->assertSame('This\\Is\\A\\New\\Controller', $class->getControllerName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the controller name for a Namespaced SiteTree instance can be gathered when set via namespace map
|
||||||
|
*/
|
||||||
|
public function testGetControllerNameFromNamespaceMappingConfig()
|
||||||
|
{
|
||||||
|
Config::inst()->update(SiteTree::class, 'namespace_mapping', [
|
||||||
|
'SilverStripe\\CMS\\Tests\\Page' => 'SilverStripe\\CMS\\Tests\\Controllers',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$namespacedSiteTree = new SiteTreeTest_NamespaceMapTest();
|
||||||
|
$this->assertSame(SiteTreeTest_NamespaceMapTestController::class, $namespacedSiteTree->getControllerName());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test that underscored class names (legacy) are still supported (deprecation notice is issued though).
|
* Test that underscored class names (legacy) are still supported (deprecation notice is issued though).
|
||||||
*/
|
*/
|
||||||
|
11
tests/php/Page/SiteTreeTest_NamespaceMapTest.php
Normal file
11
tests/php/Page/SiteTreeTest_NamespaceMapTest.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\CMS\Tests\Page;
|
||||||
|
|
||||||
|
use SilverStripe\CMS\Model\SiteTree;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class SiteTreeTest_NamespaceMapTest extends SiteTree implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'SiteTreeTest_NamespaceMapTestNode';
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user