API Convert most of Subsite public statics to config properties

This commit is contained in:
Robbie Averill 2018-01-18 17:16:13 +13:00
parent 73f5bcaecc
commit 6bbf988fda
7 changed files with 69 additions and 42 deletions

View File

@ -8,6 +8,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* Updating to be compatible with SilverStripe 4 * Updating to be compatible with SilverStripe 4
* Subsite specific theme is now added to default theme, as themes are now cascadable * Subsite specific theme is now added to default theme, as themes are now cascadable
* Global subsite information moved to injectable `SubsiteState` singleton service
* `FileExtension:::default_root_folders_global` converted to a configuration property
* `Subsite::$check_is_public` converted to a configuration property
* `Subsite::$strict_subdomain_matching` converted to a configuration property
* `Subsite::$force_subsite` deprecated and will be removed in future - use `SubsiteState::singleton()->withState()` instead
* `Subsite::$write_hostmap` converted to a configuration property
* `Subsite::$allowed_themes` made protected
## [1.2.3] ## [1.2.3]

View File

@ -16,9 +16,13 @@ use SilverStripe\Subsites\State\SubsiteState;
*/ */
class FileSubsites extends DataExtension class FileSubsites extends DataExtension
{ {
// If this is set to true, all folders created will be default be /**
// considered 'global', unless set otherwise * If this is set to true, all folders created will be default be considered 'global', unless set otherwise
public static $default_root_folders_global = false; *
* @config
* @var bool
*/
private static $default_root_folders_global = false;
private static $has_one = [ private static $has_one = [
'Subsite' => Subsite::class, 'Subsite' => Subsite::class,
@ -81,7 +85,7 @@ class FileSubsites extends DataExtension
public function onBeforeWrite() public function onBeforeWrite()
{ {
if (!$this->owner->ID && !$this->owner->SubsiteID) { if (!$this->owner->ID && !$this->owner->SubsiteID) {
if (self::$default_root_folders_global) { if ($this->owner->config()->get('default_root_folders_global')) {
$this->owner->SubsiteID = 0; $this->owner->SubsiteID = 0;
} else { } else {
$this->owner->SubsiteID = SubsiteState::singleton()->getSubsiteId(); $this->owner->SubsiteID = SubsiteState::singleton()->getSubsiteId();

View File

@ -8,6 +8,7 @@ use SilverStripe\Control\Director;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Dev\Deprecation; use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\CheckboxSetField; use SilverStripe\Forms\CheckboxSetField;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
@ -46,21 +47,27 @@ class Subsite extends DataObject
/** /**
* @var boolean $disable_subsite_filter If enabled, bypasses the query decoration * @var boolean $disable_subsite_filter If enabled, bypasses the query decoration
* to limit DataObject::get*() calls to a specific subsite. Useful for debugging. * to limit DataObject::get*() calls to a specific subsite. Useful for debugging. Note that
* for now this is left as a public static property to avoid having to nest and mutate the
* configuration manifest.
*/ */
public static $disable_subsite_filter = false; public static $disable_subsite_filter = false;
/** /**
* Allows you to force a specific subsite ID, or comma separated list of IDs. * Allows you to force a specific subsite ID, or comma separated list of IDs.
* Only works for reading. An object cannot be written to more than 1 subsite. * Only works for reading. An object cannot be written to more than 1 subsite.
*
* @deprecated 2.0.0..3.0.0 Use SubsiteState::singleton()->withState() instead.
*/ */
public static $force_subsite = null; public static $force_subsite = null;
/** /**
* Whether to write a host-map.php file
* *
* @config
* @var boolean * @var boolean
*/ */
public static $write_hostmap = true; private static $write_hostmap = true;
/** /**
* Memory cache of accessible sites * Memory cache of accessible sites
@ -77,23 +84,31 @@ class Subsite extends DataObject
private static $_cache_subsite_for_domain = []; private static $_cache_subsite_for_domain = [];
/** /**
* @var array $allowed_themes Numeric array of all themes which are allowed to be selected for all subsites. * Numeric array of all themes which are allowed to be selected for all subsites.
* Corresponds to subfolder names within the /themes folder. By default, all themes contained in this folder * Corresponds to subfolder names within the /themes folder. By default, all themes contained in this folder
* are listed. * are listed.
*
* @var array
*/ */
private static $allowed_themes = []; protected static $allowed_themes = [];
/** /**
* @var Boolean If set to TRUE, don't assume 'www.example.com' and 'example.com' are the same. * If set to TRUE, don't assume 'www.example.com' and 'example.com' are the same.
* Doesn't affect wildcard matching, so '*.example.com' will match 'www.example.com' (but not 'example.com') * Doesn't affect wildcard matching, so '*.example.com' will match 'www.example.com' (but not 'example.com')
* in both TRUE or FALSE setting. * in both TRUE or FALSE setting.
*
* @config
* @var boolean
*/ */
public static $strict_subdomain_matching = false; private static $strict_subdomain_matching = false;
/** /**
* @var boolean Respects the IsPublic flag when retrieving subsites * Respects the IsPublic flag when retrieving subsites
*
* @config
* @var boolean
*/ */
public static $check_is_public = true; private static $check_is_public = true;
/*** @return array /*** @return array
*/ */
@ -247,32 +262,37 @@ class Subsite extends DataObject
$matchingDomains = null; $matchingDomains = null;
$cacheKey = null; $cacheKey = null;
if ($host) { if ($host) {
if (!self::$strict_subdomain_matching) { if (!static::config()->get('strict_subdomain_matching')) {
$host = preg_replace('/^www\./', '', $host); $host = preg_replace('/^www\./', '', $host);
} }
$currentUserId = Security::getCurrentUser() ? Security::getCurrentUser()->ID : 0; $currentUserId = Security::getCurrentUser() ? Security::getCurrentUser()->ID : 0;
$cacheKey = implode('_', [$host, $currentUserId, self::$check_is_public]); $cacheKey = implode('_', [$host, $currentUserId, static::config()->get('check_is_public')]);
if (isset(self::$_cache_subsite_for_domain[$cacheKey])) { if (isset(self::$_cache_subsite_for_domain[$cacheKey])) {
return self::$_cache_subsite_for_domain[$cacheKey]; return self::$_cache_subsite_for_domain[$cacheKey];
} }
$SQL_host = Convert::raw2sql($host); $SQL_host = Convert::raw2sql($host);
$schema = DataObject::getSchema();
/** @skipUpgrade */ /** @skipUpgrade */
if (!in_array('SubsiteDomain', DB::table_list())) { $domainTableName = $schema->tableName(SubsiteDomain::class);
if (!in_array($domainTableName, DB::table_list())) {
// Table hasn't been created yet. Might be a dev/build, skip. // Table hasn't been created yet. Might be a dev/build, skip.
return 0; return 0;
} }
$subsiteTableName = $schema->tableName(__CLASS__);
/** @skipUpgrade */ /** @skipUpgrade */
$matchingDomains = DataObject::get( $matchingDomains = DataObject::get(
SubsiteDomain::class, SubsiteDomain::class,
"'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')", "'$SQL_host' LIKE replace(\"{$domainTableName}\".\"Domain\",'*','%')",
'"IsPrimary" DESC' '"IsPrimary" DESC'
)->innerJoin( )->innerJoin(
'Subsite', $subsiteTableName,
'"Subsite"."ID" = "SubsiteDomain"."SubsiteID" AND "Subsite"."IsPublic"=1' '"' . $subsiteTableName . '"."ID" = "SubsiteDomain"."SubsiteID" AND "'
. $subsiteTableName . '"."IsPublic"=1'
); );
} }
@ -530,12 +550,13 @@ class Subsite extends DataObject
*/ */
public static function writeHostMap($file = null) public static function writeHostMap($file = null)
{ {
if (!self::$write_hostmap) { if (!static::config()->get('write_hostmap')) {
return; return;
} }
if (!$file) { if (!$file) {
$file = Director::baseFolder() . '/subsites/host-map.php'; $subsitesPath = ModuleLoader::getModule('silverstripe/subsites')->getRelativePath();
$file = Director::baseFolder() . $subsitesPath . '/host-map.php';
} }
$hostmap = []; $hostmap = [];
@ -547,7 +568,7 @@ class Subsite extends DataObject
if ($domains) { if ($domains) {
foreach ($domains as $domain) { foreach ($domains as $domain) {
$domainStr = $domain->Domain; $domainStr = $domain->Domain;
if (!self::$strict_subdomain_matching) { if (!static::config()->get('strict_subdomain_matching')) {
$domainStr = preg_replace('/^www\./', '', $domainStr); $domainStr = preg_replace('/^www\./', '', $domainStr);
} }
$hostmap[$domainStr] = $subsite->domain(); $hostmap[$domainStr] = $subsite->domain();
@ -758,7 +779,7 @@ class Subsite extends DataObject
*/ */
public function allowedThemes() public function allowedThemes()
{ {
if ($themes = $this->stat('allowed_themes')) { if ($themes = self::$allowed_themes) {
return ArrayLib::valuekey($themes); return ArrayLib::valuekey($themes);
} }

View File

@ -4,6 +4,7 @@ namespace SilverStripe\Subsites\Tests;
use SilverStripe\Assets\File; use SilverStripe\Assets\File;
use SilverStripe\Assets\Folder; use SilverStripe\Assets\Folder;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Subsites\Extensions\FileSubsites; use SilverStripe\Subsites\Extensions\FileSubsites;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
@ -31,7 +32,7 @@ class FileSubsitesTest extends BaseSubsiteTest
$this->logInAs('admin'); $this->logInAs('admin');
$subsite = $this->objFromFixture(Subsite::class, 'domaintest1'); $subsite = $this->objFromFixture(Subsite::class, 'domaintest1');
FileSubsites::$default_root_folders_global = true; Config::modify()->set(FileSubsites::class, 'default_root_folders_global', true);
Subsite::changeSubsite(0); Subsite::changeSubsite(0);
$file = new File(); $file = new File();
@ -47,7 +48,7 @@ class FileSubsitesTest extends BaseSubsiteTest
$this->assertEquals((int)$file->SubsiteID, 0); $this->assertEquals((int)$file->SubsiteID, 0);
$this->assertTrue($file->canEdit()); $this->assertTrue($file->canEdit());
FileSubsites::$default_root_folders_global = false; Config::modify()->set(FileSubsites::class, 'default_root_folders_global', false);
Subsite::changeSubsite($subsite->ID); Subsite::changeSubsite($subsite->ID);
$file = new File(); $file = new File();
@ -58,7 +59,7 @@ class FileSubsitesTest extends BaseSubsiteTest
$folder = new Folder(); $folder = new Folder();
$folder->write(); $folder->write();
$this->assertEquals($folder->SubsiteID, $subsite->ID); $this->assertEquals($folder->SubsiteID, $subsite->ID);
FileSubsites::$default_root_folders_global = true; Config::modify()->set(FileSubsites::class, 'default_root_folders_global', true);
$file = new File(); $file = new File();
$file->ParentID = $folder->ID; $file->ParentID = $folder->ID;
$file->onAfterUpload(); $file->onAfterUpload();

View File

@ -5,6 +5,7 @@ namespace SilverStripe\Subsites\Tests;
use SilverStripe\CMS\Controllers\CMSMain; use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
@ -24,7 +25,7 @@ class SubsiteAdminTest extends BaseSubsiteTest
*/ */
public function testBasicView() public function testBasicView()
{ {
Subsite::$write_hostmap = false; Config::modify()->set(Subsite::class, 'write_hostmap', false);
$subsite1ID = $this->objFromFixture(Subsite::class, 'domaintest1')->ID; $subsite1ID = $this->objFromFixture(Subsite::class, 'domaintest1')->ID;
// Open the admin area logged in as admin // Open the admin area logged in as admin

View File

@ -17,13 +17,6 @@ class SubsiteTest extends BaseSubsiteTest
{ {
protected static $fixture_file = 'SubsiteTest.yml'; protected static $fixture_file = 'SubsiteTest.yml';
/**
* Original value of {@see SubSite::$strict_subdomain_matching}
*
* @var bool
*/
protected $origStrictSubdomainMatching = null;
/** /**
* Original value of $_REQUEST * Original value of $_REQUEST
* *
@ -35,16 +28,16 @@ class SubsiteTest extends BaseSubsiteTest
{ {
parent::setUp(); parent::setUp();
Config::modify()->set(Director::class, 'alternate_base_url', '/'); Config::modify()
$this->origStrictSubdomainMatching = Subsite::$strict_subdomain_matching; ->set(Director::class, 'alternate_base_url', '/')
->set(Subsite::class, 'strict_subdomain_matching', false);
$this->origServer = $_SERVER; $this->origServer = $_SERVER;
Subsite::$strict_subdomain_matching = false;
} }
protected function tearDown() protected function tearDown()
{ {
$_SERVER = $this->origServer; $_SERVER = $this->origServer;
Subsite::$strict_subdomain_matching = $this->origStrictSubdomainMatching;
parent::tearDown(); parent::tearDown();
} }
@ -54,7 +47,7 @@ class SubsiteTest extends BaseSubsiteTest
*/ */
public function testSubsiteCreation() public function testSubsiteCreation()
{ {
Subsite::$write_hostmap = false; Config::modify()->set(Subsite::class, 'write_hostmap', false);
// Create the instance // Create the instance
$template = $this->objFromFixture(Subsite::class, 'main'); $template = $this->objFromFixture(Subsite::class, 'main');
@ -191,7 +184,7 @@ class SubsiteTest extends BaseSubsiteTest
'www.wildcard.com' => false, 'www.wildcard.com' => false,
]); ]);
Subsite::$strict_subdomain_matching = false; Config::modify()->set(Subsite::class, 'strict_subdomain_matching', false);
$this->assertEquals( $this->assertEquals(
$subsite1->ID, $subsite1->ID,
@ -214,7 +207,7 @@ class SubsiteTest extends BaseSubsiteTest
'Doesn\'t match www prefix without strict check, even if a wildcard subdomain is in place' 'Doesn\'t match www prefix without strict check, even if a wildcard subdomain is in place'
); );
Subsite::$strict_subdomain_matching = true; Config::modify()->set(Subsite::class, 'strict_subdomain_matching', true);
$this->assertEquals( $this->assertEquals(
$subsite1->ID, $subsite1->ID,

View File

@ -54,7 +54,7 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
// Attempt to bring main:linky to subsite2:linky // Attempt to bring main:linky to subsite2:linky
public function testVirtualPageFromAnotherSubsite() public function testVirtualPageFromAnotherSubsite()
{ {
Subsite::$write_hostmap = false; Config::modify()->set(Subsite::class, 'write_hostmap', false);
$subsite = $this->objFromFixture(Subsite::class, 'subsite2'); $subsite = $this->objFromFixture(Subsite::class, 'subsite2');
@ -261,7 +261,7 @@ class SubsitesVirtualPageTest extends BaseSubsiteTest
{ {
$this->markTestIncomplete('@todo fix this test'); $this->markTestIncomplete('@todo fix this test');
Subsite::$write_hostmap = false; Config::modify()->set(Subsite::class, 'write_hostmap', false);
$subsite1 = $this->objFromFixture(Subsite::class, 'subsite1'); $subsite1 = $this->objFromFixture(Subsite::class, 'subsite1');
$subsite2 = $this->objFromFixture(Subsite::class, 'subsite2'); $subsite2 = $this->objFromFixture(Subsite::class, 'subsite2');
Subsite::changeSubsite($subsite1->ID); Subsite::changeSubsite($subsite1->ID);