NEW Allow SSViewer and SSViewer_FromString to be injectable

This commit is contained in:
Robbie Averill 2017-07-10 17:45:35 +12:00 committed by Sam Minnée
parent 243304c633
commit 823e49526f
5 changed files with 12 additions and 10 deletions

View File

@ -414,7 +414,7 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
$templates = array_unique(array_merge($actionTemplates, $classTemplates)); $templates = array_unique(array_merge($actionTemplates, $classTemplates));
} }
return new SSViewer($templates); return SSViewer::create($templates);
} }
/** /**

View File

@ -1582,7 +1582,7 @@ class Form extends ViewableData implements HasRequestHandler
*/ */
public function forAjaxTemplate() public function forAjaxTemplate()
{ {
$view = new SSViewer($this->getTemplates()); $view = SSViewer::create($this->getTemplates());
$return = $view->dontRewriteHashlinks()->process($this); $return = $view->dontRewriteHashlinks()->process($this);
@ -1631,7 +1631,7 @@ class Form extends ViewableData implements HasRequestHandler
)); ));
if (is_string($template)) { if (is_string($template)) {
$template = new SSViewer($template); $template = SSViewer::create($template);
} }
return $template->process($custom); return $template->process($custom);

View File

@ -8,6 +8,7 @@ use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Core\Flushable; use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Dev\Deprecation; use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBField;
@ -40,6 +41,7 @@ use InvalidArgumentException;
class SSViewer implements Flushable class SSViewer implements Flushable
{ {
use Configurable; use Configurable;
use Injectable;
/** /**
* Identifier for the default theme * Identifier for the default theme
@ -65,17 +67,17 @@ class SSViewer implements Flushable
/** /**
* @var array $templates List of templates to select from * @var array $templates List of templates to select from
*/ */
private $templates = null; protected $templates = null;
/** /**
* @var string $chosen Absolute path to chosen template file * @var string $chosen Absolute path to chosen template file
*/ */
private $chosen = null; protected $chosen = null;
/** /**
* @var array Templates to use when looking up 'Layout' or 'Content' * @var array Templates to use when looking up 'Layout' or 'Content'
*/ */
private $subTemplates = null; protected $subTemplates = null;
/** /**
* @var boolean * @var boolean
@ -143,7 +145,7 @@ class SSViewer implements Flushable
*/ */
public static function fromString($content, $cacheTemplate = null) public static function fromString($content, $cacheTemplate = null)
{ {
$viewer = new SSViewer_FromString($content); $viewer = SSViewer_FromString::create($content);
if ($cacheTemplate !== null) { if ($cacheTemplate !== null) {
$viewer->setCacheTemplate($cacheTemplate); $viewer->setCacheTemplate($cacheTemplate);
} }
@ -574,7 +576,7 @@ class SSViewer implements Flushable
*/ */
public static function execute_template($template, $data, $arguments = null, $scope = null) public static function execute_template($template, $data, $arguments = null, $scope = null)
{ {
$v = new SSViewer($template); $v = SSViewer::create($template);
$v->includeRequirements(false); $v->includeRequirements(false);
return $v->process($data, $arguments, $scope); return $v->process($data, $arguments, $scope);

View File

@ -56,7 +56,7 @@ class SSViewer_FromString extends SSViewer
if ($this->cacheTemplate !== null) { if ($this->cacheTemplate !== null) {
$cacheTemplate = $this->cacheTemplate; $cacheTemplate = $this->cacheTemplate;
} else { } else {
$cacheTemplate = Config::inst()->get('SilverStripe\\View\\SSViewer_FromString', 'cache_template'); $cacheTemplate = static::config()->get('cache_template');
} }
if (!$cacheTemplate) { if (!$cacheTemplate) {

View File

@ -382,7 +382,7 @@ class ViewableData implements IteratorAggregate
public function renderWith($template, $customFields = null) public function renderWith($template, $customFields = null)
{ {
if (!is_object($template)) { if (!is_object($template)) {
$template = new SSViewer($template); $template = SSViewer::create($template);
} }
$data = $this->getCustomisedObj() ?: $this; $data = $this->getCustomisedObj() ?: $this;