Merge pull request #7392 from open-sausages/pulls/4.0/there-is-no-leftandmain-config

Enhancement Add setter and getter for certain classes
This commit is contained in:
Damian Mooyman 2017-09-29 09:25:36 +13:00 committed by GitHub
commit 91943c03e5
11 changed files with 235 additions and 58 deletions

View File

@ -9,6 +9,7 @@ SilverStripe\Core\Injector\Injector:
extensions: '%$SilverStripe\Dev\State\ExtensionTestState' extensions: '%$SilverStripe\Dev\State\ExtensionTestState'
flushable: '%$SilverStripe\Dev\State\FlushableTestState' flushable: '%$SilverStripe\Dev\State\FlushableTestState'
requirements: '%$SilverStripe\View\Dev\RequirementsTestState' requirements: '%$SilverStripe\View\Dev\RequirementsTestState'
ssviewer: '%$SilverStripe\View\Dev\SSViewerTestState'
--- ---
Name: kerneltest Name: kerneltest
Before: '*' Before: '*'

View File

@ -42,9 +42,9 @@ Or, a better way is to call this just for the rendering phase of this particular
```php ```php
public function RenderCustomTemplate() public function RenderCustomTemplate()
{ {
Config::inst()->update('SSViewer', 'rewrite_hash_links', false); SSViewer::setRewriteHashLinks(false);
$html = $this->renderWith('MyCustomTemplate'); $html = $this->renderWith('MyCustomTemplate');
Config::inst()->update('SSViewer', 'rewrite_hash_links', true); SSViewer::setRewriteHashLinks(true);
return $html; return $html;
} }

View File

@ -39,28 +39,29 @@ class ContentNegotiator
/** /**
* @config * @config
*
* @var string * @var string
*/ */
private static $content_type = ''; private static $content_type = '';
/** /**
* @config * @config
*
* @var string * @var string
*/ */
private static $encoding = 'utf-8'; private static $encoding = 'utf-8';
/** /**
* @config * @config
*
* @var bool * @var bool
*/ */
private static $enabled = false; private static $enabled = false;
/**
* @var bool
*/
protected static $current_enabled = null;
/** /**
* @config * @config
*
* @var string * @var string
*/ */
private static $default_format = 'html'; private static $default_format = 'html';
@ -84,13 +85,36 @@ class ContentNegotiator
return false; return false;
} }
if (static::config()->get('enabled')) { if (ContentNegotiator::getEnabled()) {
return true; return true;
} else { } else {
return (substr($response->getBody(), 0, 5) == '<' . '?xml'); return (substr($response->getBody(), 0, 5) == '<' . '?xml');
} }
} }
/**
* Gets the current enabled status, if it is not set this will fallback to config
*
* @return bool
*/
public static function getEnabled()
{
if (isset(static::$current_enabled)) {
return static::$current_enabled;
}
return Config::inst()->get(static::class, 'enabled');
}
/**
* Sets the current enabled status
*
* @param bool $enabled
*/
public static function setEnabled($enabled)
{
static::$current_enabled = $enabled;
}
/** /**
* @param HTTPResponse $response * @param HTTPResponse $response
*/ */

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Forms\HTMLEditor; namespace SilverStripe\Forms\HTMLEditor;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injectable;
@ -59,6 +60,13 @@ abstract class HTMLEditorConfig
*/ */
private static $user_themes = []; private static $user_themes = [];
/**
* List of the current themes set for this config
*
* @var array
*/
protected static $current_themes = null;
/** /**
* Get the HTMLEditorConfig object for the given identifier. This is a correct way to get an HTMLEditorConfig * Get the HTMLEditorConfig object for the given identifier. This is a correct way to get an HTMLEditorConfig
* instance - do not call 'new' * instance - do not call 'new'
@ -96,6 +104,28 @@ abstract class HTMLEditorConfig
return $config; return $config;
} }
/**
* Gets the current themes, if it is not set this will fallback to config
* @return array
*/
public static function getThemes()
{
if (isset(static::$current_themes)) {
return static::$current_themes;
}
return Config::inst()->get(static::class, 'user_themes');
}
/**
* Sets the current theme
*
* @param array $themes
*/
public static function setThemes($themes)
{
static::$current_themes = $themes;
}
/** /**
* Set the currently active configuration object. Note that the existing active * Set the currently active configuration object. Note that the existing active
* config will not be renamed to the new identifier. * config will not be renamed to the new identifier.

View File

@ -635,7 +635,7 @@ class TinyMCEConfig extends HTMLEditorConfig
} }
// Themed editor.css // Themed editor.css
$themes = $this->config()->get('user_themes') ?: SSViewer::get_themes(); $themes = HTMLEditorConfig::getThemes() ?: SSViewer::get_themes();
$themedEditor = ThemeResourceLoader::inst()->findThemedCSS('editor', $themes); $themedEditor = ThemeResourceLoader::inst()->findThemedCSS('editor', $themes);
if ($themedEditor) { if ($themedEditor) {
$editor[] = Director::absoluteURL($themedEditor); $editor[] = Director::absoluteURL($themedEditor);

View File

@ -0,0 +1,30 @@
<?php
namespace SilverStripe\View\Dev;
use SilverStripe\Control\ContentNegotiator;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\State\TestState;
use SilverStripe\View\SSViewer;
class SSViewerTestState implements TestState
{
public function setUp(SapphireTest $test)
{
SSViewer::set_themes(null);
SSViewer::setRewriteHashLinksDefault(null);
ContentNegotiator::setEnabled(null);
}
public function tearDown(SapphireTest $test)
{
}
public function setUpOnce($class)
{
}
public function tearDownOnce($class)
{
}
}

View File

@ -1213,7 +1213,7 @@ class SSTemplateParser extends Parser implements TemplateParser
// TODO: This is pretty ugly & gets applied on all files not just html. I wonder if we can make this // TODO: This is pretty ugly & gets applied on all files not just html. I wonder if we can make this
// non-dynamically calculated // non-dynamically calculated
$code = <<<'EOC' $code = <<<'EOC'
(\SilverStripe\View\SSViewer::config()->get('rewrite_hash_links') (\SilverStripe\View\SSViewer::getRewriteHashLinksDefault()
? \SilverStripe\Core\Convert::raw2att( preg_replace("/^(\\/)+/", "/", $_SERVER['REQUEST_URI'] ) ) ? \SilverStripe\Core\Convert::raw2att( preg_replace("/^(\\/)+/", "/", $_SERVER['REQUEST_URI'] ) )
: "") : "")
EOC; EOC;

View File

@ -4834,7 +4834,7 @@ class SSTemplateParser extends Parser implements TemplateParser
// TODO: This is pretty ugly & gets applied on all files not just html. I wonder if we can make this // TODO: This is pretty ugly & gets applied on all files not just html. I wonder if we can make this
// non-dynamically calculated // non-dynamically calculated
$code = <<<'EOC' $code = <<<'EOC'
(\SilverStripe\View\SSViewer::config()->get('rewrite_hash_links') (\SilverStripe\View\SSViewer::getRewriteHashLinksDefault()
? \SilverStripe\Core\Convert::raw2att( preg_replace("/^(\\/)+/", "/", $_SERVER['REQUEST_URI'] ) ) ? \SilverStripe\Core\Convert::raw2att( preg_replace("/^(\\/)+/", "/", $_SERVER['REQUEST_URI'] ) )
: "") : "")
EOC; EOC;

View File

@ -2,6 +2,7 @@
namespace SilverStripe\View; namespace SilverStripe\View;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\CacheInterface;
@ -49,27 +50,40 @@ class SSViewer implements Flushable
const DEFAULT_THEME = '$default'; const DEFAULT_THEME = '$default';
/** /**
* @config * A list (highest priority first) of themes to use
* @var string A list (highest priority first) of themes to use
* Only used when {@link $theme_enabled} is set to TRUE. * Only used when {@link $theme_enabled} is set to TRUE.
*
* @config
* @var string
*/ */
private static $themes = []; private static $themes = [];
/** /**
* Overridden value of $themes config
*
* @var array
*/
protected static $current_themes = null;
/**
* The used "theme", which usually consists of templates, images and stylesheets.
* Only used when {@link $theme_enabled} is set to TRUE, and $themes is empty
*
* @deprecated 4.0..5.0 * @deprecated 4.0..5.0
* @config * @config
* @var string The used "theme", which usually consists of templates, images and stylesheets. * @var string
* Only used when {@link $theme_enabled} is set to TRUE, and $themes is empty
*/ */
private static $theme = null; private static $theme = null;
/** /**
* @config * Use the theme. Set to FALSE in order to disable themes,
* @var boolean Use the theme. Set to FALSE in order to disable themes,
* which can be useful for scenarios where theme overrides are temporarily undesired, * which can be useful for scenarios where theme overrides are temporarily undesired,
* such as an administrative interface separate from the website theme. * such as an administrative interface separate from the website theme.
* It retains the theme settings to be re-enabled, for example when a website content * It retains the theme settings to be re-enabled, for example when a website content
* needs to be rendered from within this administrative interface. * needs to be rendered from within this administrative interface.
*
* @config
* @var bool
*/ */
private static $theme_enabled = true; private static $theme_enabled = true;
@ -83,53 +97,75 @@ class SSViewer implements Flushable
/** /**
* @config * @config
* @var boolean $source_file_comments * @var bool
*/ */
private static $source_file_comments = false; private static $source_file_comments = false;
/** /**
* Set if hash links should be rewritten
*
* @config * @config
* @var boolean * @var bool
*/ */
private static $rewrite_hash_links = true; private static $rewrite_hash_links = true;
/** /**
* Overridden value of rewrite_hash_links config
*
* @var bool
*/
protected static $current_rewrite_hash_links = null;
/**
* Instance variable to disable rewrite_hash_links (overrides global default)
* Leave null to use global state.
*
* @var bool|null
*/
protected $rewriteHashlinks = null;
/**
* @internal
* @ignore * @ignore
*/ */
private static $template_cache_flushed = false; private static $template_cache_flushed = false;
/** /**
* @internal
* @ignore * @ignore
*/ */
private static $cacheblock_cache_flushed = false; private static $cacheblock_cache_flushed = false;
/** /**
* @var array $topLevel List of items being processed * List of items being processed
*
* @var array
*/ */
protected static $topLevel = []; protected static $topLevel = [];
/** /**
* @var array $templates List of templates to select from * List of templates to select from
*
* @var array
*/ */
protected $templates = null; protected $templates = null;
/** /**
* @var string $chosen Absolute path to chosen template file * Absolute path to chosen template file
*
* @var string
*/ */
protected $chosen = null; protected $chosen = null;
/** /**
* @var array Templates to use when looking up 'Layout' or 'Content' * Templates to use when looking up 'Layout' or 'Content'
*
* @var array
*/ */
protected $subTemplates = null; protected $subTemplates = null;
/** /**
* @var boolean * @var bool
*/
protected $rewriteHashlinks = true;
/**
* @var boolean
*/ */
protected $includeRequirements = true; protected $includeRequirements = true;
@ -208,7 +244,7 @@ class SSViewer implements Flushable
*/ */
public static function set_themes($themes = []) public static function set_themes($themes = [])
{ {
SSViewer::config()->set('themes', $themes); static::$current_themes = $themes;
} }
/** /**
@ -221,7 +257,7 @@ class SSViewer implements Flushable
$currentThemes = SSViewer::get_themes(); $currentThemes = SSViewer::get_themes();
$finalThemes = array_merge($themes, $currentThemes); $finalThemes = array_merge($themes, $currentThemes);
// array_values is used to ensure sequential array keys as array_unique can leave gaps // array_values is used to ensure sequential array keys as array_unique can leave gaps
SSViewer::set_themes(array_values(array_unique($finalThemes))); static::set_themes(array_values(array_unique($finalThemes)));
} }
/** /**
@ -238,8 +274,12 @@ class SSViewer implements Flushable
} }
// Explicit list is assigned // Explicit list is assigned
if ($list = SSViewer::config()->uninherited('themes')) { $themes = static::$current_themes;
return $list; if (!isset($themes)) {
$themes = SSViewer::config()->uninherited('themes');
}
if ($themes) {
return $themes;
} }
// Support legacy behaviour // Support legacy behaviour
@ -315,6 +355,55 @@ class SSViewer implements Flushable
return null; return null;
} }
/**
* Check if rewrite hash links are enabled on this instance
*
* @return bool
*/
public function getRewriteHashLinks()
{
if (isset($this->rewriteHashlinks)) {
return $this->rewriteHashlinks;
}
return static::getRewriteHashLinksDefault();
}
/**
* Set if hash links are rewritten for this instance
*
* @param bool $rewrite
* @return $this
*/
public function setRewriteHashLinks($rewrite)
{
$this->rewriteHashlinks = $rewrite;
return $this;
}
/**
* Get default value for rewrite hash links for all modules
*
* @return bool
*/
public static function getRewriteHashLinksDefault()
{
// Check if config overridden
if (isset(static::$current_rewrite_hash_links)) {
return static::$current_rewrite_hash_links;
}
return Config::inst()->get(static::class, 'rewrite_hash_links');
}
/**
* Set default rewrite hash links
*
* @param bool $rewrite
*/
public static function setRewriteHashLinksDefault($rewrite)
{
static::$current_rewrite_hash_links = $rewrite;
}
/** /**
* @param string|array $templates * @param string|array $templates
*/ */
@ -364,7 +453,7 @@ class SSViewer implements Flushable
* *
* @param array|string $templates * @param array|string $templates
* *
* @return boolean * @return bool
*/ */
public static function hasTemplate($templates) public static function hasTemplate($templates)
{ {
@ -374,12 +463,12 @@ class SSViewer implements Flushable
/** /**
* Call this to disable rewriting of <a href="#xxx"> links. This is useful in Ajax applications. * Call this to disable rewriting of <a href="#xxx"> links. This is useful in Ajax applications.
* It returns the SSViewer objects, so that you can call new SSViewer("X")->dontRewriteHashlinks()->process(); * It returns the SSViewer objects, so that you can call new SSViewer("X")->dontRewriteHashlinks()->process();
*
* @return $this
*/ */
public function dontRewriteHashlinks() public function dontRewriteHashlinks()
{ {
$this->rewriteHashlinks = false; return $this->setRewriteHashLinks(false);
SSViewer::config()->update('rewrite_hash_links', false);
return $this;
} }
/** /**
@ -467,7 +556,7 @@ class SSViewer implements Flushable
/** /**
* Flag whether to include the requirements in this response. * Flag whether to include the requirements in this response.
* *
* @param boolean * @param bool
*/ */
public function includeRequirements($incl = true) public function includeRequirements($incl = true)
{ {
@ -528,6 +617,11 @@ class SSViewer implements Flushable
*/ */
public function process($item, $arguments = null, $inheritedScope = null) public function process($item, $arguments = null, $inheritedScope = null)
{ {
// Set hashlinks and temporarily modify global state
$rewrite = $this->getRewriteHashLinks();
$origRewriteDefault = static::getRewriteHashLinksDefault();
static::setRewriteHashLinksDefault($rewrite);
SSViewer::$topLevel[] = $item; SSViewer::$topLevel[] = $item;
$template = $this->chosen; $template = $this->chosen;
@ -581,9 +675,7 @@ class SSViewer implements Flushable
array_pop(SSViewer::$topLevel); array_pop(SSViewer::$topLevel);
// If we have our crazy base tag, then fix # links referencing the current page. // If we have our crazy base tag, then fix # links referencing the current page.
if ($rewrite) {
$rewrite = SSViewer::config()->uninherited('rewrite_hash_links');
if ($this->rewriteHashlinks && $rewrite) {
if (strpos($output, '<base') !== false) { if (strpos($output, '<base') !== false) {
if ($rewrite === 'php') { if ($rewrite === 'php') {
$thisURLRelativeToBase = <<<PHP $thisURLRelativeToBase = <<<PHP
@ -599,6 +691,9 @@ PHP;
/** @var DBHTMLText $html */ /** @var DBHTMLText $html */
$html = DBField::create_field('HTMLFragment', $output); $html = DBField::create_field('HTMLFragment', $output);
// Reset global state
static::setRewriteHashLinksDefault($origRewriteDefault);
return $html; return $html;
} }
@ -678,6 +773,7 @@ PHP;
* *
* @param string $content The template contents * @param string $content The template contents
* @param string $template The template file name * @param string $template The template file name
* @return string
*/ */
public function parseTemplateContent($content, $template = "") public function parseTemplateContent($content, $template = "")
{ {

View File

@ -54,11 +54,7 @@ class TinyMCECombinedGeneratorTest extends SapphireTest
// Get config for this // Get config for this
/** @var TinyMCECombinedGenerator $generator */ /** @var TinyMCECombinedGenerator $generator */
$generator = Injector::inst()->create(TinyMCECombinedGenerator::class); $generator = Injector::inst()->create(TinyMCECombinedGenerator::class);
$this->assertEquals( $this->assertRegExp('#_tinymce/tinymce-testconfig-[0-9a-z]{10,10}#', $generator->generateFilename($c));
'_tinymce/tinymce-testconfig-6422b3814d.js',
$generator->generateFilename($c),
"Filename for config: " . json_encode($c->getAttributes()) . " should match expected value"
);
$content = $generator->generateContent($c); $content = $generator->generateContent($c);
$this->assertContains( $this->assertContains(
"var baseURL = baseTag.length ? baseTag[0].baseURI : 'http://www.mysite.com/basedir/';\n", "var baseURL = baseTag.length ? baseTag[0].baseURI : 'http://www.mysite.com/basedir/';\n",

View File

@ -1679,7 +1679,7 @@ after'
public function testRewriteHashlinks() public function testRewriteHashlinks()
{ {
SSViewer::config()->update('rewrite_hash_links', true); SSViewer::setRewriteHashLinksDefault(true);
$_SERVER['HTTP_HOST'] = 'www.mysite.com'; $_SERVER['HTTP_HOST'] = 'www.mysite.com';
$_SERVER['REQUEST_URI'] = '//file.com?foo"onclick="alert(\'xss\')""'; $_SERVER['REQUEST_URI'] = '//file.com?foo"onclick="alert(\'xss\')""';
@ -1744,7 +1744,7 @@ after'
public function testRewriteHashlinksInPhpMode() public function testRewriteHashlinksInPhpMode()
{ {
SSViewer::config()->update('rewrite_hash_links', 'php'); SSViewer::setRewriteHashLinksDefault('php');
$tmplFile = TEMP_FOLDER . '/SSViewerTest_testRewriteHashlinksInPhpMode_' . sha1(rand()) . '.ss'; $tmplFile = TEMP_FOLDER . '/SSViewerTest_testRewriteHashlinksInPhpMode_' . sha1(rand()) . '.ss';