diff --git a/_config/view.yml b/_config/view.yml new file mode 100644 index 00000000000..fd8293c9f3d --- /dev/null +++ b/_config/view.yml @@ -0,0 +1,6 @@ +--- +Name: view-config +--- +SilverStripe\Core\Injector\Injector: + SilverStripe\View\TemplateEngine: + class: 'SilverStripe\View\SSTemplateEngine' diff --git a/src/Control/ContentNegotiator.php b/src/Control/ContentNegotiator.php index aeb3b9f54a8..4bb9b46c1c0 100644 --- a/src/Control/ContentNegotiator.php +++ b/src/Control/ContentNegotiator.php @@ -225,7 +225,7 @@ public function html(HTTPResponse $response) // Fix base tag $content = preg_replace( '//', - '', + '', $content ?? '' ); diff --git a/src/Control/Controller.php b/src/Control/Controller.php index b66ae94424d..2ebbe9c9479 100644 --- a/src/Control/Controller.php +++ b/src/Control/Controller.php @@ -3,11 +3,14 @@ namespace SilverStripe\Control; use SilverStripe\Core\ClassInfo; +use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\Debug; +use SilverStripe\Model\ModelData; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\Security\Member; use SilverStripe\Security\Security; use SilverStripe\View\SSViewer; +use SilverStripe\View\TemplateEngine; use SilverStripe\View\TemplateGlobalProvider; /** @@ -87,6 +90,8 @@ class Controller extends RequestHandler implements TemplateGlobalProvider 'handleIndex', ]; + protected ?TemplateEngine $templateEngine = null; + public function __construct() { parent::__construct(); @@ -400,7 +405,7 @@ public function getViewer($action) $templates = array_unique(array_merge($actionTemplates, $classTemplates)); } - return SSViewer::create($templates); + return SSViewer::create($templates, $this->getTemplateEngine()); } /** @@ -452,9 +457,10 @@ protected function definingClassForAction($action) } $class = static::class; - while ($class != 'SilverStripe\\Control\\RequestHandler') { + $engine = $this->getTemplateEngine(); + while ($class !== RequestHandler::class) { $templateName = strtok($class ?? '', '_') . '_' . $action; - if (SSViewer::hasTemplate($templateName)) { + if ($engine->hasTemplate($templateName)) { return $class; } @@ -486,17 +492,25 @@ public function hasActionTemplate($action) $parentClass = get_parent_class($parentClass ?? ''); } - return SSViewer::hasTemplate($templates); + $engine = $this->getTemplateEngine(); + return $engine->hasTemplate($templates); + } + + public function renderWith($template, ModelData|array $customFields = []): DBHTMLText + { + // Ensure template engine is used, unless the viewer was already explicitly instantiated + if (!($template instanceof SSViewer)) { + $template = SSViewer::create($template, $this->getTemplateEngine()); + } + return parent::renderWith($template, $customFields); } /** * Render the current controller with the templates determined by {@link getViewer()}. * * @param array $params - * - * @return string */ - public function render($params = null) + public function render($params = null): DBHTMLText { $template = $this->getViewer($this->getAction()); @@ -735,4 +749,12 @@ public static function get_template_global_variables() 'CurrentPage' => 'curr', ]; } + + protected function getTemplateEngine(): TemplateEngine + { + if (!$this->templateEngine) { + $this->templateEngine = Injector::inst()->create(TemplateEngine::class); + } + return $this->templateEngine; + } } diff --git a/src/Control/Email/Email.php b/src/Control/Email/Email.php index aa8bddd5c82..8ee6040583e 100644 --- a/src/Control/Email/Email.php +++ b/src/Control/Email/Email.php @@ -46,7 +46,7 @@ class Email extends SymfonyEmail private static string|array $admin_email = ''; /** - * The name of the HTML template to render the email with (without *.ss extension) + * The name of the HTML template to render the email with */ private string $HTMLTemplate = ''; @@ -398,26 +398,21 @@ public function removeData(string $name) return $this; } - public function getHTMLTemplate(): string + public function getHTMLTemplate(): string|array { if ($this->HTMLTemplate) { return $this->HTMLTemplate; } - return ThemeResourceLoader::inst()->findTemplate( - SSViewer::get_templates_by_class(static::class, '', Email::class), - SSViewer::get_themes() - ); + return SSViewer::get_templates_by_class(static::class, '', Email::class); } /** - * Set the template to render the email with + * Set the template to render the email with. + * Do not include a file extension unless you are referencing a full absolute file path. */ public function setHTMLTemplate(string $template): static { - if (substr($template ?? '', -3) == '.ss') { - $template = substr($template ?? '', 0, -3); - } $this->HTMLTemplate = $template; return $this; } @@ -431,13 +426,11 @@ public function getPlainTemplate(): string } /** - * Set the template to render the plain part with + * Set the template to render the plain part with. + * Do not include a file extension unless you are referencing a full absolute file path. */ public function setPlainTemplate(string $template): static { - if (substr($template ?? '', -3) == '.ss') { - $template = substr($template ?? '', 0, -3); - } $this->plainTemplate = $template; return $this; } diff --git a/src/Control/HTTPResponse.php b/src/Control/HTTPResponse.php index 3cb4a498bbc..5e657ef6b36 100644 --- a/src/Control/HTTPResponse.php +++ b/src/Control/HTTPResponse.php @@ -444,8 +444,6 @@ public function isRedirect() /** * The HTTP response represented as a raw string - * - * @return string */ public function __toString() { diff --git a/src/Control/RSS/RSSFeed_Entry.php b/src/Control/RSS/RSSFeed_Entry.php index 1ebaae7e7de..66034d711ec 100644 --- a/src/Control/RSS/RSSFeed_Entry.php +++ b/src/Control/RSS/RSSFeed_Entry.php @@ -47,7 +47,7 @@ class RSSFeed_Entry extends ModelData */ public function __construct($entry, $titleField, $descriptionField, $authorField) { - $this->failover = $entry; + $this->setFailover($entry); $this->titleField = $titleField; $this->descriptionField = $descriptionField; $this->authorField = $authorField; @@ -58,7 +58,7 @@ public function __construct($entry, $titleField, $descriptionField, $authorField /** * Get the description of this entry * - * @return DBField Returns the description of the entry. + * @return DBField|null Returns the description of the entry. */ public function Title() { @@ -68,7 +68,7 @@ public function Title() /** * Get the description of this entry * - * @return DBField Returns the description of the entry. + * @return DBField|null Returns the description of the entry. */ public function Description() { @@ -85,7 +85,7 @@ public function Description() /** * Get the author of this entry * - * @return DBField Returns the author of the entry. + * @return DBField|null Returns the author of the entry. */ public function Author() { @@ -96,7 +96,7 @@ public function Author() * Return the safely casted field * * @param string $fieldName Name of field - * @return DBField + * @return DBField|null */ public function rssField($fieldName) { diff --git a/src/Core/Manifest/ModuleResource.php b/src/Core/Manifest/ModuleResource.php index e89b90ac547..54756184bc1 100644 --- a/src/Core/Manifest/ModuleResource.php +++ b/src/Core/Manifest/ModuleResource.php @@ -114,8 +114,6 @@ public function exists() /** * Get relative path - * - * @return string */ public function __toString() { diff --git a/src/Dev/Backtrace.php b/src/Dev/Backtrace.php index 62d402efc51..9aa7b85ad81 100644 --- a/src/Dev/Backtrace.php +++ b/src/Dev/Backtrace.php @@ -149,11 +149,11 @@ public static function full_func_name($item, $showArgs = false, $argCharLimit = if ($showArgs && isset($item['args'])) { $args = []; foreach ($item['args'] as $arg) { - if (!is_object($arg) || method_exists($arg, '__toString')) { + if (is_object($arg)) { + $args[] = get_class($arg); + } else { $sarg = is_array($arg) ? 'Array' : strval($arg); $args[] = (strlen($sarg ?? '') > $argCharLimit) ? substr($sarg, 0, $argCharLimit) . '...' : $sarg; - } else { - $args[] = get_class($arg); } } diff --git a/src/Dev/TestSession.php b/src/Dev/TestSession.php index 2c1ff07a659..ae61630e4ba 100644 --- a/src/Dev/TestSession.php +++ b/src/Dev/TestSession.php @@ -4,6 +4,7 @@ use Exception; use InvalidArgumentException; +use LogicException; use SilverStripe\Control\Controller; use SilverStripe\Control\Cookie_Backend; use SilverStripe\Control\Director; @@ -214,7 +215,7 @@ public function submitForm(string $formID, string $button = null, array $data = $formCrawler = $page->filterXPath("//form[@id='$formID']"); $form = $formCrawler->form(); } catch (InvalidArgumentException $e) { - user_error("TestSession::submitForm failed to find the form {$formID}"); + throw new LogicException("TestSession::submitForm failed to find the form '{$formID}'"); } foreach ($data as $fieldName => $value) { @@ -235,7 +236,7 @@ public function submitForm(string $formID, string $button = null, array $data = if ($button) { $btnXpath = "//button[@name='$button'] | //input[@name='$button'][@type='button' or @type='submit']"; if (!$formCrawler->children()->filterXPath($btnXpath)->count()) { - throw new Exception("Can't find button '$button' to submit as part of test."); + throw new LogicException("Can't find button '$button' to submit as part of test."); } $values[$button] = true; } diff --git a/src/Forms/DropdownField.php b/src/Forms/DropdownField.php index ed5da300034..9e31245250f 100644 --- a/src/Forms/DropdownField.php +++ b/src/Forms/DropdownField.php @@ -68,7 +68,7 @@ * DropdownField::create( * 'Country', * 'Country', - * singleton(MyObject::class)->dbObject('Country')->enumValues() + * singleton(MyObject::class)->dbObject('Country')?->enumValues() * ); * * diff --git a/src/Forms/FieldGroup.php b/src/Forms/FieldGroup.php index 9a0d6c67588..c61de2136b9 100644 --- a/src/Forms/FieldGroup.php +++ b/src/Forms/FieldGroup.php @@ -154,7 +154,7 @@ public function getMessage() /** @var FormField $subfield */ $messages = []; foreach ($dataFields as $subfield) { - $message = $subfield->obj('Message')->forTemplate(); + $message = $subfield->obj('Message')?->forTemplate(); if ($message) { $messages[] = rtrim($message ?? '', "."); } diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 7ce206f8d57..a0483b68cc6 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -82,7 +82,7 @@ class Form extends ModelData implements HasRequestHandler const ENC_TYPE_MULTIPART = 'multipart/form-data'; /** - * Accessed by Form.ss. + * Accessed by Form template. * A performance enhancement over the generate-the-form-tag-and-then-remove-it code that was there previously * * @var bool @@ -159,7 +159,7 @@ class Form extends ModelData implements HasRequestHandler /** * Legend value, to be inserted into the * element before the
- * in Form.ss template. + * in Form template. * * @var string|null */ @@ -888,7 +888,7 @@ public function setTarget($target) /** * Set the legend value to be inserted into - * the element in the Form.ss template. + * the element in the Form template. * @param string $legend * @return $this */ @@ -899,10 +899,10 @@ public function setLegend($legend) } /** - * Set the SS template that this form should use + * Set the template or template candidates that this form should use * to render with. The default is "Form". * - * @param string|array $template The name of the template (without the .ss extension) or array form + * @param string|array $template The name of the template (without the file extension) or array of candidates * @return $this */ public function setTemplate($template) @@ -1234,7 +1234,7 @@ public function getRecord() /** * Get the legend value to be inserted into the - * element in Form.ss + * element in Form template * * @return string */ diff --git a/src/Forms/FormField.php b/src/Forms/FormField.php index 0d210436b0f..55d43e56ccc 100644 --- a/src/Forms/FormField.php +++ b/src/Forms/FormField.php @@ -15,6 +15,7 @@ use SilverStripe\View\AttributesHTML; use SilverStripe\View\SSViewer; use SilverStripe\Model\ModelData; +use SilverStripe\ORM\DataObject; /** * Represents a field in a form. @@ -273,6 +274,8 @@ class FormField extends RequestHandler 'Title' => 'Text', 'RightTitle' => 'Text', 'Description' => 'HTMLFragment', + // This is an associative arrays, but we cast to Text so we can get a JSON string representation + 'SchemaData' => 'Text', ]; /** @@ -458,7 +461,7 @@ public function Value() * * By default, makes use of $this->dataValue() * - * @param ModelData|DataObjectInterface $record Record to save data into + * @param DataObjectInterface $record Record to save data into */ public function saveInto(DataObjectInterface $record) { @@ -469,7 +472,9 @@ public function saveInto(DataObjectInterface $record) if (($pos = strrpos($this->name ?? '', '.')) !== false) { $relation = substr($this->name ?? '', 0, $pos); $fieldName = substr($this->name ?? '', $pos + 1); - $component = $record->relObject($relation); + if ($record instanceof DataObject) { + $component = $record->relObject($relation); + } } if ($fieldName && $component) { @@ -943,7 +948,7 @@ public function Field($properties = []) * * The default field holder is a label and a form field inside a div. * - * @see FieldHolder.ss + * see FieldHolder template * * @param array $properties * @@ -1027,7 +1032,7 @@ public function getSmallFieldHolderTemplates() */ protected function _templates($customTemplate = null, $customTemplateSuffix = null) { - $templates = SSViewer::get_templates_by_class(static::class, $customTemplateSuffix, __CLASS__); + $templates = SSViewer::get_templates_by_class(static::class, $customTemplateSuffix ?? '', __CLASS__); // Prefer any custom template if ($customTemplate) { // Prioritise direct template @@ -1469,12 +1474,12 @@ public function getSchemaDataDefaults() 'schemaType' => $this->getSchemaDataType(), 'component' => $this->getSchemaComponent(), 'holderId' => $this->HolderID(), - 'title' => $this->obj('Title')->getSchemaValue(), + 'title' => $this->obj('Title')?->getSchemaValue(), 'source' => null, 'extraClass' => $this->extraClass(), - 'description' => $this->obj('Description')->getSchemaValue(), - 'rightTitle' => $this->obj('RightTitle')->getSchemaValue(), - 'leftTitle' => $this->obj('LeftTitle')->getSchemaValue(), + 'description' => $this->obj('Description')?->getSchemaValue(), + 'rightTitle' => $this->obj('RightTitle')?->getSchemaValue(), + 'leftTitle' => $this->obj('LeftTitle')?->getSchemaValue(), 'readOnly' => $this->isReadonly(), 'disabled' => $this->isDisabled(), 'customValidationMessage' => $this->getCustomValidationMessage(), diff --git a/src/Forms/FormScaffolder.php b/src/Forms/FormScaffolder.php index 099dabf5d6e..db43a88e88d 100644 --- a/src/Forms/FormScaffolder.php +++ b/src/Forms/FormScaffolder.php @@ -115,7 +115,7 @@ public function getFieldList() $fieldObject = $this ->obj ->dbObject($fieldName) - ->scaffoldFormField(null, $this->getParamsArray()); + ?->scaffoldFormField(null, $this->getParamsArray()); } // Allow fields to opt-out of scaffolding if (!$fieldObject) { @@ -145,7 +145,7 @@ public function getFieldList() $fieldClass = $this->fieldClasses[$fieldName]; $hasOneField = new $fieldClass($fieldName); } else { - $hasOneField = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray()); + $hasOneField = $this->obj->dbObject($fieldName)?->scaffoldFormField(null, $this->getParamsArray()); } if (empty($hasOneField)) { continue; // Allow fields to opt out of scaffolding diff --git a/src/Forms/GridField/GridFieldAddExistingAutocompleter.php b/src/Forms/GridField/GridFieldAddExistingAutocompleter.php index 3c8b0aac0b3..df39adba765 100644 --- a/src/Forms/GridField/GridFieldAddExistingAutocompleter.php +++ b/src/Forms/GridField/GridFieldAddExistingAutocompleter.php @@ -17,6 +17,9 @@ use SilverStripe\View\SSViewer; use LogicException; use SilverStripe\Control\HTTPResponse_Exception; +use SilverStripe\Core\Injector\Injector; +use SilverStripe\View\TemplateEngine; +use SilverStripe\View\ViewLayerData; /** * This class is is responsible for adding objects to another object's has_many @@ -283,12 +286,15 @@ public function doSearch($gridField, $request) $json = []; Config::nest(); SSViewer::config()->set('source_file_comments', false); - $viewer = SSViewer::fromString($this->resultsFormat); + + $engine = Injector::inst()->create(TemplateEngine::class); foreach ($results as $result) { if (!$result->canView()) { continue; } - $title = Convert::html2raw($viewer->process($result)); + $title = Convert::html2raw( + $engine->renderString($this->resultsFormat, ViewLayerData::create($result), cache: false) + ); $json[] = [ 'label' => $title, 'value' => $title, diff --git a/src/Forms/GridField/GridFieldDataColumns.php b/src/Forms/GridField/GridFieldDataColumns.php index ed827fca258..09e5e8680e8 100644 --- a/src/Forms/GridField/GridFieldDataColumns.php +++ b/src/Forms/GridField/GridFieldDataColumns.php @@ -223,31 +223,6 @@ public function getColumnMetadata($gridField, $column) ]; } - /** - * Translate a Object.RelationName.ColumnName $columnName into the value that ColumnName returns - * - * @param ModelData $record - * @param string $columnName - * @return string|null - returns null if it could not found a value - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it. - */ - protected function getValueFromRelation($record, $columnName) - { - Deprecation::notice('5.4.0', 'Will be removed without equivalent functionality to replace it.'); - $fieldNameParts = explode('.', $columnName ?? ''); - $tmpItem = clone($record); - for ($idx = 0; $idx < sizeof($fieldNameParts ?? []); $idx++) { - $methodName = $fieldNameParts[$idx]; - // Last mmethod call from $columnName return what that method is returning - if ($idx == sizeof($fieldNameParts ?? []) - 1) { - return $tmpItem->XML_val($methodName); - } - // else get the object from this $methodName - $tmpItem = $tmpItem->$methodName(); - } - return null; - } - /** * Casts a field to a string which is safe to insert into HTML * diff --git a/src/Forms/GridField/GridState.php b/src/Forms/GridField/GridState.php index 1c0d9c64078..a5f5725970a 100644 --- a/src/Forms/GridField/GridState.php +++ b/src/Forms/GridField/GridState.php @@ -129,10 +129,6 @@ public function attrValue() return Convert::raw2att($this->Value()); } - /** - * - * @return string - */ public function __toString() { return $this->Value(); diff --git a/src/Forms/HTMLEditor/HTMLEditorField.php b/src/Forms/HTMLEditor/HTMLEditorField.php index 90c3fad75c1..4527dd1de6a 100644 --- a/src/Forms/HTMLEditor/HTMLEditorField.php +++ b/src/Forms/HTMLEditor/HTMLEditorField.php @@ -5,9 +5,11 @@ use SilverStripe\Assets\Shortcodes\ImageShortcodeProvider; use SilverStripe\Forms\FormField; use SilverStripe\Forms\TextareaField; -use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObjectInterface; use Exception; +use SilverStripe\Model\ModelData; +use SilverStripe\ORM\FieldType\DBField; +use SilverStripe\View\CastingService; use SilverStripe\View\Parsers\HTMLValue; /** @@ -123,13 +125,9 @@ public function getAttributes() ); } - /** - * @param DataObject|DataObjectInterface $record - * @throws Exception - */ public function saveInto(DataObjectInterface $record) { - if ($record->hasField($this->name) && $record->escapeTypeForField($this->name) != 'xml') { + if (!$this->usesXmlFriendlyField($record)) { throw new Exception( 'HTMLEditorField->saveInto(): This field should save into a HTMLText or HTMLVarchar field.' ); @@ -225,4 +223,15 @@ private function setEditorHeight(HTMLEditorConfig $config): HTMLEditorConfig return $config; } + + private function usesXmlFriendlyField(DataObjectInterface $record): bool + { + if ($record instanceof ModelData && !$record->hasField($this->getName())) { + return true; + } + + $castingService = CastingService::singleton(); + $castValue = $castingService->cast($this->Value(), $record, $this->getName()); + return $castValue instanceof DBField && $castValue::config()->get('escape_type') === 'xml'; + } } diff --git a/src/Forms/TreeDropdownField.php b/src/Forms/TreeDropdownField.php index c503c591aa3..5bf454f5513 100644 --- a/src/Forms/TreeDropdownField.php +++ b/src/Forms/TreeDropdownField.php @@ -7,6 +7,7 @@ use SilverStripe\Assets\Folder; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; +use SilverStripe\Model\List\SS_List; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBDatetime; @@ -519,13 +520,20 @@ public function tree(HTTPRequest $request) // Allow to pass values to be selected within the ajax request $value = $request->requestVar('forceValue') ?: $this->value; - if ($value && ($values = preg_split('/,\s*/', $value ?? ''))) { + if ($value instanceof SS_List) { + $values = $value; + } elseif ($value) { + $values = preg_split('/,\s*/', $value ?? ''); + } else { + $values = []; + } + if (!empty($values)) { foreach ($values as $value) { if (!$value || $value == 'unchanged') { continue; } - $object = $this->objectForKey($value); + $object = is_object($value) ? $value : $this->objectForKey($value); if (!$object) { continue; } @@ -870,14 +878,14 @@ public function getSchemaStateDefaults() $ancestors = $record->getAncestors(true)->reverse(); foreach ($ancestors as $parent) { - $title = $parent->obj($this->getTitleField())->getValue(); + $title = $parent->obj($this->getTitleField())?->getValue(); $titlePath .= $title . '/'; } } $data['data']['valueObject'] = [ - 'id' => $record->obj($this->getKeyField())->getValue(), - 'title' => $record->obj($this->getTitleField())->getValue(), - 'treetitle' => $record->obj($this->getLabelField())->getSchemaValue(), + 'id' => $record->obj($this->getKeyField())?->getValue(), + 'title' => $record->obj($this->getTitleField())?->getValue(), + 'treetitle' => $record->obj($this->getLabelField())?->getSchemaValue(), 'titlePath' => $titlePath, ]; } diff --git a/src/Forms/TreeMultiselectField.php b/src/Forms/TreeMultiselectField.php index a1362f24715..449a275fe45 100644 --- a/src/Forms/TreeMultiselectField.php +++ b/src/Forms/TreeMultiselectField.php @@ -92,10 +92,10 @@ public function getSchemaStateDefaults() foreach ($items as $item) { if ($item instanceof DataObject) { $values[] = [ - 'id' => $item->obj($this->getKeyField())->getValue(), - 'title' => $item->obj($this->getTitleField())->getValue(), + 'id' => $item->obj($this->getKeyField())?->getValue(), + 'title' => $item->obj($this->getTitleField())?->getValue(), 'parentid' => $item->ParentID, - 'treetitle' => $item->obj($this->getLabelField())->getSchemaValue(), + 'treetitle' => $item->obj($this->getLabelField())?->getSchemaValue(), ]; } else { $values[] = $item; @@ -212,7 +212,7 @@ public function Field($properties = []) foreach ($items as $item) { $idArray[] = $item->ID; $titleArray[] = ($item instanceof ModelData) - ? $item->obj($this->getLabelField())->forTemplate() + ? $item->obj($this->getLabelField())?->forTemplate() : Convert::raw2xml($item->{$this->getLabelField()}); } diff --git a/src/Model/ArrayData.php b/src/Model/ArrayData.php index 185eebf7b79..ce4597af71f 100644 --- a/src/Model/ArrayData.php +++ b/src/Model/ArrayData.php @@ -4,6 +4,7 @@ use SilverStripe\Core\ArrayLib; use InvalidArgumentException; +use JsonSerializable; use stdClass; /** @@ -16,14 +17,9 @@ * )); * */ -class ArrayData extends ModelData +class ArrayData extends ModelData implements JsonSerializable { - - /** - * @var array - * @see ArrayData::_construct() - */ - protected $array; + protected array $array; /** * @param object|array $value An associative array, or an object with simple properties. @@ -52,10 +48,8 @@ enumerated array passed instead. Did you mean to use ArrayList?'; /** * Get the source array - * - * @return array */ - public function toMap() + public function toMap(): array { return $this->array; } @@ -87,6 +81,7 @@ public function getField(string $fieldName): mixed */ public function setField(string $fieldName, mixed $value): static { + $this->objCacheClear(); $this->array[$fieldName] = $value; return $this; } @@ -102,6 +97,16 @@ public function hasField(string $fieldName): bool return isset($this->array[$fieldName]); } + public function exists(): bool + { + return !empty($this->array); + } + + public function jsonSerialize(): array + { + return $this->array; + } + /** * Converts an associative array to a simple object * diff --git a/src/Model/List/ListDecorator.php b/src/Model/List/ListDecorator.php index b03a0cabaa0..63084d52824 100644 --- a/src/Model/List/ListDecorator.php +++ b/src/Model/List/ListDecorator.php @@ -54,7 +54,9 @@ public function getList(): SS_List public function setList(SS_List $list): ListDecorator { $this->list = $list; - $this->failover = $this->list; + if ($list instanceof ModelData) { + $this->setFailover($list); + } return $this; } diff --git a/src/Model/ModelData.php b/src/Model/ModelData.php index 9ae5cde65d4..454d00f8792 100644 --- a/src/Model/ModelData.php +++ b/src/Model/ModelData.php @@ -2,7 +2,6 @@ namespace SilverStripe\Model; -use Exception; use InvalidArgumentException; use LogicException; use ReflectionMethod; @@ -12,14 +11,12 @@ use SilverStripe\Core\Convert; use SilverStripe\Core\Extensible; use SilverStripe\Core\Injector\Injectable; -use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\Debug; use SilverStripe\Core\ArrayLib; -use SilverStripe\Dev\Deprecation; -use SilverStripe\Model\List\ArrayList; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\Model\ArrayData; +use SilverStripe\View\CastingService; use SilverStripe\View\SSViewer; use UnexpectedValueException; @@ -39,7 +36,7 @@ class ModelData use Configurable; /** - * An array of objects to cast certain fields to. This is set up as an array in the format: + * An array of DBField classes to cast certain fields to. This is set up as an array in the format: * * * public static $casting = array ( @@ -48,16 +45,18 @@ class ModelData * */ private static array $casting = [ - 'CSSClasses' => 'Varchar' + 'CSSClasses' => 'Varchar', + 'forTemplate' => 'HTMLText', ]; /** - * The default object to cast scalar fields to if casting information is not specified, and casting to an object + * The default class to cast scalar fields to if casting information is not specified, and casting to an object * is required. + * This can be any injectable service name but must resolve to a DBField subclass. + * + * If null, casting will be determined based on the type of value (e.g. integers will be cast to DBInt) */ - private static string $default_cast = 'Text'; - - private static array $casting_cache = []; + private static ?string $default_cast = null; /** * Acts as a PHP 8.2+ compliant replacement for dynamic properties @@ -205,6 +204,7 @@ public function getDynamicData(string $field): mixed public function setDynamicData(string $field, mixed $value): static { + $this->objCacheClear(); $this->dynamicData[$field] = $value; return $this; } @@ -252,8 +252,7 @@ private function isAccessibleProperty(string $property): bool // ----------------------------------------------------------------------------------------------------------------- /** - * Add methods from the {@link ModelData::$failover} object, as well as wrapping any methods prefixed with an - * underscore into a {@link ModelData::cachedCall()}. + * Add methods from the {@link ModelData::$failover} object * * @throws LogicException */ @@ -314,6 +313,15 @@ public function __toString(): string return static::class; } + /** + * Return the HTML markup that represents this model when it is directly injected into a template (e.g. using $Me). + * By default this attempts to render the model using templates based on the class hierarchy. + */ + public function forTemplate(): string + { + return $this->renderWith($this->getViewerTemplates()); + } + public function getCustomisedObj(): ?ModelData { return $this->customisedObject; @@ -327,14 +335,10 @@ public function setCustomisedObj(ModelData $object) // CASTING --------------------------------------------------------------------------------------------------------- /** - * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) + * Return the "casting helper" (an injectable service name) * for a field on this object. This helper will be a subclass of DBField. - * - * @param bool $useFallback If true, fall back on the default casting helper if there isn't an explicit one. - * @return string|null Casting helper As a constructor pattern, and may include arguments. - * @throws Exception */ - public function castingHelper(string $field, bool $useFallback = true): ?string + public function castingHelper(string $field): ?string { // Get casting if it has been configured. // DB fields and PHP methods are all case insensitive so we normalise casing before checking. @@ -347,72 +351,15 @@ public function castingHelper(string $field, bool $useFallback = true): ?string // If no specific cast is declared, fall back to failover. $failover = $this->getFailover(); if ($failover) { - $cast = $failover->castingHelper($field, $useFallback); + $cast = $failover->castingHelper($field); if ($cast) { return $cast; } } - if ($useFallback) { - return $this->defaultCastingHelper($field); - } - return null; } - /** - * Return the default "casting helper" for use when no explicit casting helper is defined. - * This helper will be a subclass of DBField. See castingHelper() - */ - protected function defaultCastingHelper(string $field): string - { - // If there is a failover, the default_cast will always - // be drawn from this object instead of the top level object. - $failover = $this->getFailover(); - if ($failover) { - $cast = $failover->defaultCastingHelper($field); - if ($cast) { - return $cast; - } - } - - // Fall back to raw default_cast - $default = $this->config()->get('default_cast'); - if (empty($default)) { - throw new Exception('No default_cast'); - } - return $default; - } - - /** - * Get the class name a field on this object will be casted to. - * - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it. - */ - public function castingClass(string $field): string - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be removed without equivalent functionality to replace it.'); - // Strip arguments - $spec = $this->castingHelper($field); - return trim(strtok($spec ?? '', '(') ?? ''); - } - - /** - * Return the string-format type for the given field. - * - * @return string 'xml'|'raw' - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it. - */ - public function escapeTypeForField(string $field): string - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be removed without equivalent functionality to replace it.'); - $class = $this->castingClass($field) ?: $this->config()->get('default_cast'); - - /** @var DBField $type */ - $type = Injector::inst()->get($class, true); - return $type->config()->get('escape_type'); - } - // TEMPLATE ACCESS LAYER ------------------------------------------------------------------------------------------- /** @@ -423,9 +370,9 @@ public function escapeTypeForField(string $field): string * - an SSViewer instance * * @param string|array|SSViewer $template the template to render into - * @param ModelData|array|null $customFields fields to customise() the object with before rendering + * @param ModelData|array $customFields fields to customise() the object with before rendering */ - public function renderWith($template, ModelData|array|null $customFields = null): DBHTMLText + public function renderWith($template, ModelData|array $customFields = []): DBHTMLText { if (!is_object($template)) { $template = SSViewer::create($template); @@ -435,9 +382,10 @@ public function renderWith($template, ModelData|array|null $customFields = null) if ($customFields instanceof ModelData) { $data = $data->customise($customFields); + $customFields = []; } if ($template instanceof SSViewer) { - return $template->process($data, is_array($customFields) ? $customFields : null); + return $template->process($data, $customFields); } throw new UnexpectedValueException( @@ -446,29 +394,11 @@ public function renderWith($template, ModelData|array|null $customFields = null) } /** - * Generate the cache name for a field - * - * @param string $fieldName Name of field - * @param array $arguments List of optional arguments given - * @return string - * @deprecated 5.4.0 Will be made private + * Get a cached value from the field cache for a field */ - protected function objCacheName($fieldName, $arguments) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be made private'); - return $arguments - ? $fieldName . ":" . var_export($arguments, true) - : $fieldName; - } - - /** - * Get a cached value from the field cache - * - * @param string $key Cache key - * @return mixed - */ - protected function objCacheGet($key) + public function objCacheGet(string $fieldName, array $arguments = []): mixed { + $key = $this->objCacheName($fieldName, $arguments); if (isset($this->objCache[$key])) { return $this->objCache[$key]; } @@ -476,24 +406,19 @@ protected function objCacheGet($key) } /** - * Store a value in the field cache - * - * @param string $key Cache key - * @param mixed $value - * @return $this + * Store a value in the field cache for a field */ - protected function objCacheSet($key, $value) + public function objCacheSet(string $fieldName, array $arguments, mixed $value): static { + $key = $this->objCacheName($fieldName, $arguments); $this->objCache[$key] = $value; return $this; } /** * Clear object cache - * - * @return $this */ - protected function objCacheClear() + public function objCacheClear(): static { $this->objCache = []; return $this; @@ -505,87 +430,46 @@ protected function objCacheClear() * * @return object|DBField|null The specific object representing the field, or null if there is no * property, method, or dynamic data available for that field. - * Note that if there is a property or method that returns null, a relevant DBField instance will - * be returned. */ public function obj( string $fieldName, array $arguments = [], - bool $cache = false, - ?string $cacheName = null + bool $cache = false ): ?object { - if ($cacheName !== null) { - Deprecation::noticeWithNoReplacment('5.4.0', 'The $cacheName parameter has been deprecated and will be removed'); - } - $hasObj = false; - if (!$cacheName && $cache) { - $cacheName = $this->objCacheName($fieldName, $arguments); - } - // Check pre-cached value - $value = $cache ? $this->objCacheGet($cacheName) : null; - if ($value !== null) { - return $value; - } - - // Load value from record - if ($this->hasMethod($fieldName)) { - $hasObj = true; - $value = call_user_func_array([$this, $fieldName], $arguments ?: []); - } else { - $hasObj = $this->hasField($fieldName) || ($this->hasMethod("get{$fieldName}") && $this->isAccessibleMethod("get{$fieldName}")); - $value = $this->$fieldName; - } - - // Return null early if there's no backing for this field - // i.e. no poperty, no method, etc - it just doesn't exist on this model. - if (!$hasObj && $value === null) { - return null; - } - - // Try to cast object if we have an explicit cast set - if (!is_object($value)) { - $castingHelper = $this->castingHelper($fieldName, false); - if ($castingHelper !== null) { - $valueObject = Injector::inst()->create($castingHelper, $fieldName); - $valueObject->setValue($value, $this); - $value = $valueObject; + $value = $cache ? $this->objCacheGet($fieldName, $arguments) : null; + if ($value === null) { + $hasObj = false; + // Load value from record + if ($this->hasMethod($fieldName)) { + // Try methods first - there's a LOT of logic that assumes this will be checked first. + $hasObj = true; + $value = call_user_func_array([$this, $fieldName], $arguments ?: []); + } else { + $getter = "get{$fieldName}"; + $hasGetter = $this->hasMethod($getter) && $this->isAccessibleMethod($getter); + // Try fields and getters if there was no method with that name. + $hasObj = $hasGetter || $this->hasField($fieldName); + if ($hasGetter && !empty($arguments)) { + $value = $this->$getter(...$arguments); + } else { + $value = $this->$fieldName; + } } - } - // Wrap list arrays in ModelData so templates can handle them - if (is_array($value) && array_is_list($value)) { - $value = ArrayList::create($value); - } - - // Fallback on default casting - if (!is_object($value)) { - // Force cast - $castingHelper = $this->defaultCastingHelper($fieldName); - $valueObject = Injector::inst()->create($castingHelper, $fieldName); - $valueObject->setValue($value, $this); - $value = $valueObject; - } + // Record in cache + if ($value !== null && $cache) { + $this->objCacheSet($fieldName, $arguments, $value); + } - // Record in cache - if ($cache) { - $this->objCacheSet($cacheName, $value); + // Return null early if there's no backing for this field + // i.e. no poperty, no method, etc - it just doesn't exist on this model. + if (!$hasObj && $value === null) { + return null; + } } - return $value; - } - - /** - * A simple wrapper around {@link ModelData::obj()} that automatically caches the result so it can be used again - * without re-running the method. - * - * @return Object|DBField - * @deprecated 5.4.0 use obj() instead - */ - public function cachedCall(string $fieldName, array $arguments = [], ?string $cacheName = null): object - { - Deprecation::notice('5.4.0', 'Use obj() instead'); - return $this->obj($fieldName, $arguments, true, $cacheName); + return CastingService::singleton()->cast($value, $this, $fieldName, true); } /** @@ -601,41 +485,6 @@ public function hasValue(string $field, array $arguments = [], bool $cache = tru return (bool) $result; } - /** - * Get the string value of a field on this object that has been suitable escaped to be inserted directly into a - * template. - * - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it - */ - public function XML_val(string $field, array $arguments = [], bool $cache = false): string - { - Deprecation::noticeWithNoReplacment('5.4.0'); - $result = $this->obj($field, $arguments, $cache); - if (!$result) { - return ''; - } - // Might contain additional formatting over ->XML(). E.g. parse shortcodes, nl2br() - return $result->forTemplate(); - } - - /** - * Get an array of XML-escaped values by field name - * - * @param array $fields an array of field names - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it - */ - public function getXMLValues(array $fields): array - { - Deprecation::noticeWithNoReplacment('5.4.0'); - $result = []; - - foreach ($fields as $field) { - $result[$field] = $this->XML_val($field); - } - - return $result; - } - // UTILITY METHODS ------------------------------------------------------------------------------------------------- /** @@ -695,4 +544,15 @@ public function Debug(): ModelData|string { return ModelDataDebugger::create($this); } + + /** + * Generate the cache name for a field + */ + private function objCacheName(string $fieldName, array $arguments = []): string + { + $name = empty($arguments) + ? $fieldName + : $fieldName . ":" . var_export($arguments, true); + return md5($name); + } } diff --git a/src/Model/ModelDataCustomised.php b/src/Model/ModelDataCustomised.php index 6ae73be21ac..8af4d6ebe4c 100644 --- a/src/Model/ModelDataCustomised.php +++ b/src/Model/ModelDataCustomised.php @@ -49,17 +49,30 @@ public function __isset(string $property): bool return isset($this->customised->$property) || isset($this->original->$property) || parent::__isset($property); } + /** + * Renders the template of the original model. + */ + public function forTemplate(): string + { + // Both the original and customised model have `forTemplate()` implemented + // through the superclass, so we can't use method_exists to dynamically pick + // the "right" model to call this method on. + // Since the customised model is for customising the data but not necessarily + // for customising the template, we call forTemplate() on the original. + return $this->original->forTemplate(); + } + public function hasMethod($method) { return $this->customised->hasMethod($method) || $this->original->hasMethod($method); } - public function cachedCall(string $fieldName, array $arguments = [], ?string $cacheName = null): object + public function castingHelper(string $field): ?string { - if ($this->customisedHas($fieldName)) { - return $this->customised->cachedCall($fieldName, $arguments, $cacheName); + if ($this->customisedHas($field)) { + return $this->customised->castingHelper($field); } - return $this->original->cachedCall($fieldName, $arguments, $cacheName); + return $this->original->castingHelper($field); } public function obj( @@ -74,10 +87,15 @@ public function obj( return $this->original->obj($fieldName, $arguments, $cache, $cacheName); } - private function customisedHas(string $fieldName): bool + public function customisedHas(string $fieldName): bool { return property_exists($this->customised, $fieldName) || $this->customised->hasField($fieldName) || $this->customised->hasMethod($fieldName); } + + public function getCustomisedModelData(): ?ModelData + { + return $this->customised; + } } diff --git a/src/ORM/DataList.php b/src/ORM/DataList.php index f136663bc73..0bb36d4e088 100644 --- a/src/ORM/DataList.php +++ b/src/ORM/DataList.php @@ -17,6 +17,7 @@ use SilverStripe\Model\List\ArrayList; use SilverStripe\Model\List\Map; use SilverStripe\Model\List\SS_List; +use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\Filters\SearchFilterable; /** @@ -1828,7 +1829,7 @@ public function relation($relationName) return $relation; } - public function dbObject($fieldName) + public function dbObject(string $fieldName): ?DBField { return singleton($this->dataClass)->dbObject($fieldName); } diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index e0d2b755d61..ffd8f7afc37 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -104,9 +104,6 @@ * } * * - * If any public method on this class is prefixed with an underscore, - * the results are cached in memory through {@link cachedCall()}. - * * @property int $ID ID of the DataObject, 0 if the DataObject doesn't exist in database. * @property int $OldID ID of object, if deleted * @property string $Title @@ -1945,6 +1942,7 @@ public function setEagerLoadedData( string $eagerLoadRelation, EagerLoadedList|DataObject $eagerLoadedData ): void { + $this->objCacheClear(); $this->eagerLoadedData[$eagerLoadRelation] = $eagerLoadedData; } @@ -3041,7 +3039,7 @@ public function setCastedField($fieldName, $value) /** * {@inheritdoc} */ - public function castingHelper(string $field, bool $useFallback = true): ?string + public function castingHelper(string $field): ?string { $fieldSpec = static::getSchema()->fieldSpec(static::class, $field); if ($fieldSpec) { @@ -3059,7 +3057,7 @@ public function castingHelper(string $field, bool $useFallback = true): ?string } } - return parent::castingHelper($field, $useFallback); + return parent::castingHelper($field); } /** @@ -3242,11 +3240,11 @@ public function debug(): string * - it still returns an object even when the field has no value. * - it only matches fields and not methods * - it matches foreign keys generated by has_one relationships, eg, "ParentID" + * - if the field exists, the return value is ALWAYS a DBField instance * - * @param string $fieldName Name of the field - * @return DBField The field as a DBField object + * Returns null if the field doesn't exist */ - public function dbObject($fieldName) + public function dbObject(string $fieldName): ?DBField { // Check for field in DB $schema = static::getSchema(); @@ -3314,7 +3312,7 @@ public function relObject($fieldPath) } elseif ($component instanceof Relation || $component instanceof DataList) { // $relation could either be a field (aggregate), or another relation $singleton = DataObject::singleton($component->dataClass()); - $component = $singleton->dbObject($relation) ?: $component->relation($relation); + $component = $singleton->dbObject($relation) ?? $component->relation($relation); } elseif ($component instanceof DataObject && ($dbObject = $component->dbObject($relation))) { $component = $dbObject; } elseif ($component instanceof ModelData && $component->hasField($relation)) { @@ -4407,7 +4405,7 @@ public function hasValue(string $field, array $arguments = [], bool $cache = tru // has_one fields should not use dbObject to check if a value is given $hasOne = static::getSchema()->hasOneComponent(static::class, $field); if (!$hasOne && ($obj = $this->dbObject($field))) { - return $obj->exists(); + return $obj && $obj->exists(); } else { return parent::hasValue($field, $arguments, $cache); } diff --git a/src/ORM/EagerLoadedList.php b/src/ORM/EagerLoadedList.php index f3aec251e3b..396ef3df5cb 100644 --- a/src/ORM/EagerLoadedList.php +++ b/src/ORM/EagerLoadedList.php @@ -165,7 +165,7 @@ public function dataClass(): string return $this->dataClass; } - public function dbObject($fieldName): ?DBField + public function dbObject(string $fieldName): ?DBField { return singleton($this->dataClass)->dbObject($fieldName); } diff --git a/src/ORM/FieldType/DBComposite.php b/src/ORM/FieldType/DBComposite.php index 7060417eadc..6c9ea2a05d4 100644 --- a/src/ORM/FieldType/DBComposite.php +++ b/src/ORM/FieldType/DBComposite.php @@ -73,7 +73,7 @@ public function writeToManipulation(array &$manipulation): void foreach ($this->compositeDatabaseFields() as $field => $spec) { // Write sub-manipulation $fieldObject = $this->dbObject($field); - $fieldObject->writeToManipulation($manipulation); + $fieldObject?->writeToManipulation($manipulation); } } @@ -137,7 +137,7 @@ public function exists(): bool // By default all fields foreach ($this->compositeDatabaseFields() as $field => $spec) { $fieldObject = $this->dbObject($field); - if (!$fieldObject->exists()) { + if (!$fieldObject?->exists()) { return false; } } diff --git a/src/ORM/FieldType/DBVarchar.php b/src/ORM/FieldType/DBVarchar.php index 3081ad34be0..86608a19739 100644 --- a/src/ORM/FieldType/DBVarchar.php +++ b/src/ORM/FieldType/DBVarchar.php @@ -47,7 +47,7 @@ public function __construct(?string $name = null, int $size = 255, array $option * can be useful if you want to have text fields with a length limit that * is dictated by the DB field. * - * TextField::create('Title')->setMaxLength(singleton('SiteTree')->dbObject('Title')->getSize()) + * TextField::create('Title')->setMaxLength(singleton('SiteTree')->dbObject('Title')?->getSize()) * * @return int The size of the field */ diff --git a/src/ORM/Filters/SearchFilter.php b/src/ORM/Filters/SearchFilter.php index f622252fbb0..bc70ec5d438 100644 --- a/src/ORM/Filters/SearchFilter.php +++ b/src/ORM/Filters/SearchFilter.php @@ -339,7 +339,7 @@ public function getDbFormattedValue() /** @var DBField $dbField */ $dbField = singleton($this->model)->dbObject($this->name); - $dbField->setValue($this->value); + $dbField?->setValue($this->value); return $dbField->RAW(); } diff --git a/src/ORM/Queries/SQLExpression.php b/src/ORM/Queries/SQLExpression.php index 168f943d8c5..6f67afd1a3d 100644 --- a/src/ORM/Queries/SQLExpression.php +++ b/src/ORM/Queries/SQLExpression.php @@ -44,8 +44,6 @@ public function replaceText($old, $new) /** * Return the generated SQL string for this query - * - * @return string */ public function __toString() { diff --git a/src/ORM/Relation.php b/src/ORM/Relation.php index 0bc0d3b02fc..124020f5e36 100644 --- a/src/ORM/Relation.php +++ b/src/ORM/Relation.php @@ -39,9 +39,6 @@ public function getIDList(); /** * Return the DBField object that represents the given field on the related class. - * - * @param string $fieldName Name of the field - * @return DBField The field as a DBField object */ - public function dbObject($fieldName); + public function dbObject(string $fieldName): ?DBField; } diff --git a/src/ORM/UnsavedRelationList.php b/src/ORM/UnsavedRelationList.php index d7303e87496..eb489efd964 100644 --- a/src/ORM/UnsavedRelationList.php +++ b/src/ORM/UnsavedRelationList.php @@ -300,11 +300,8 @@ public function relation($relationName) /** * Return the DBField object that represents the given field on the related class. - * - * @param string $fieldName Name of the field - * @return DBField The field as a DBField object */ - public function dbObject($fieldName) + public function dbObject(string $fieldName): ?DBField { return DataObject::singleton($this->dataClass)->dbObject($fieldName); } diff --git a/src/PolyExecution/PolyOutput.php b/src/PolyExecution/PolyOutput.php index a10d4646e54..35b52af39e1 100644 --- a/src/PolyExecution/PolyOutput.php +++ b/src/PolyExecution/PolyOutput.php @@ -226,9 +226,6 @@ private function writeListItemAnsi(iterable $items, ?int $options): void { $listInfo = $this->listTypeStack[array_key_last($this->listTypeStack)]; $listType = $listInfo['type']; - if ($listType === PolyOutput::LIST_ORDERED) { - echo ''; - } if ($options === null) { $options = $listInfo['options']; } diff --git a/src/Security/Member.php b/src/Security/Member.php index 94ea15ccfda..7764a9112d3 100644 --- a/src/Security/Member.php +++ b/src/Security/Member.php @@ -345,7 +345,7 @@ public function isLockedOut() { /** @var DBDatetime $lockedOutUntilObj */ $lockedOutUntilObj = $this->dbObject('LockedOutUntil'); - if ($lockedOutUntilObj->InFuture()) { + if ($lockedOutUntilObj?->InFuture()) { return true; } @@ -372,7 +372,7 @@ public function isLockedOut() /** @var DBDatetime $firstFailureDate */ $firstFailureDate = $attempts->first()->dbObject('Created'); $maxAgeSeconds = $this->config()->get('lock_out_delay_mins') * 60; - $lockedOutUntil = $firstFailureDate->getTimestamp() + $maxAgeSeconds; + $lockedOutUntil = $firstFailureDate?->getTimestamp() + $maxAgeSeconds; $now = DBDatetime::now()->getTimestamp(); if ($now < $lockedOutUntil) { return true; @@ -428,7 +428,7 @@ public function saveRequiresPasswordChangeOnNextLogin(?int $dataValue): static $currentValue = $this->PasswordExpiry; $currentDate = $this->dbObject('PasswordExpiry'); - if ($dataValue && (!$currentValue || $currentDate->inFuture())) { + if ($dataValue && (!$currentValue || $currentDate?->inFuture())) { // Only alter future expiries - this way an admin could see how long ago a password expired still $this->PasswordExpiry = DBDatetime::now()->Rfc2822(); } elseif (!$dataValue && $this->isPasswordExpired()) { diff --git a/src/Security/PermissionCheckboxSetField.php b/src/Security/PermissionCheckboxSetField.php index bad09fa4f3b..7592dc68170 100644 --- a/src/Security/PermissionCheckboxSetField.php +++ b/src/Security/PermissionCheckboxSetField.php @@ -117,7 +117,7 @@ public function Field($properties = []) $uninheritedCodes[$permission->Code][] = _t( 'SilverStripe\\Security\\PermissionCheckboxSetField.AssignedTo', 'assigned to "{title}"', - ['title' => $record->dbObject('Title')->forTemplate()] + ['title' => $record->dbObject('Title')?->forTemplate()] ); } @@ -135,7 +135,7 @@ public function Field($properties = []) 'SilverStripe\\Security\\PermissionCheckboxSetField.FromRole', 'inherited from role "{title}"', 'A permission inherited from a certain permission role', - ['title' => $role->dbObject('Title')->forTemplate()] + ['title' => $role->dbObject('Title')?->forTemplate()] ); } } @@ -159,8 +159,8 @@ public function Field($properties = []) 'inherited from role "{roletitle}" on group "{grouptitle}"', 'A permission inherited from a role on a certain group', [ - 'roletitle' => $role->dbObject('Title')->forTemplate(), - 'grouptitle' => $parent->dbObject('Title')->forTemplate() + 'roletitle' => $role->dbObject('Title')?->forTemplate(), + 'grouptitle' => $parent->dbObject('Title')?->forTemplate() ] ); } @@ -176,7 +176,7 @@ public function Field($properties = []) 'SilverStripe\\Security\\PermissionCheckboxSetField.FromGroup', 'inherited from group "{title}"', 'A permission inherited from a certain group', - ['title' => $parent->dbObject('Title')->forTemplate()] + ['title' => $parent->dbObject('Title')?->forTemplate()] ); } } diff --git a/src/View/CastingService.php b/src/View/CastingService.php new file mode 100644 index 00000000000..2015cefbf24 --- /dev/null +++ b/src/View/CastingService.php @@ -0,0 +1,104 @@ +castingHelper($fieldName); + } + + // Cast to object if there's an explicit casting for this field + // Explicit casts take precedence over array casting + if ($serviceKey) { + $castObject = Injector::inst()->create($serviceKey, $fieldName); + if (!ClassInfo::hasMethod($castObject, 'setValue')) { + throw new LogicException('Explicit casting service must have a setValue method.'); + } + $castObject->setValue($data, $source); + return $castObject; + } + + // Wrap arrays in ModelData so templates can handle them + if (is_array($data)) { + return array_is_list($data) ? ArrayList::create($data) : ArrayData::create($data); + } + + // Fall back to default casting + $serviceKey = $this->getDefaultServiceKey($data, $source, $fieldName); + $castObject = Injector::inst()->create($serviceKey, $fieldName); + if (!ClassInfo::hasMethod($castObject, 'setValue')) { + throw new LogicException('Default service must have a setValue method.'); + } + $castObject->setValue($data, $source); + return $castObject; + } + + /** + * Get the default service to use if no explicit service is declared for this field on the source model. + */ + private function getDefaultServiceKey(mixed $data, mixed $source = null, string $fieldName = ''): ?string + { + $default = null; + if ($source instanceof ModelData) { + $default = $source::config()->get('default_cast'); + if ($default === null) { + $failover = $source->getFailover(); + if ($failover) { + $default = $this->getDefaultServiceKey($data, $failover, $fieldName); + } + } + } + if ($default !== null) { + return $default; + } + + return match (gettype($data)) { + 'boolean' => DBBoolean::class, + 'string' => DBText::class, + 'double' => DBFloat::class, + 'integer' => DBInt::class, + default => DBText::class, + }; + } +} diff --git a/src/View/Dev/SSViewerTestState.php b/src/View/Dev/SSViewerTestState.php index 56f946e4611..bb4b8e5f719 100644 --- a/src/View/Dev/SSViewerTestState.php +++ b/src/View/Dev/SSViewerTestState.php @@ -11,7 +11,7 @@ class SSViewerTestState implements TestState { public function setUp(SapphireTest $test) { - SSViewer::set_themes(null); + SSViewer::set_themes([]); SSViewer::setRewriteHashLinksDefault(null); ContentNegotiator::setEnabled(null); } diff --git a/src/View/Exception/MissingTemplateException.php b/src/View/Exception/MissingTemplateException.php new file mode 100644 index 00000000000..8115aa71fcf --- /dev/null +++ b/src/View/Exception/MissingTemplateException.php @@ -0,0 +1,12 @@ +` template commands. + * + * Caching + * + * Compiled templates are cached, usually on the filesystem. + * If you put ?flush=1 on your URL, it will force the template to be recompiled. + * + */ +class SSTemplateEngine implements TemplateEngine, Flushable +{ + use Injectable; + use Configurable; + + /** + * Default prepended cache key for partial caching + */ + private static string $global_key = '$CurrentReadingMode, $CurrentUser.ID'; + + /** + * List of models being processed + */ + protected static array $topLevel = []; + + /** + * @internal + */ + private static bool $template_cache_flushed = false; + + /** + * @internal + */ + private static bool $cacheblock_cache_flushed = false; + + private ?CacheInterface $partialCacheStore = null; + + private ?TemplateParser $parser = null; + + /** + * A template or pool of candidate templates to choose from. + */ + private string|array $templateCandidates = []; + + /** + * Absolute path to chosen template file which will be used in the call to render() + */ + private ?string $chosen = null; + + /** + * Templates to use when looking up 'Layout' or 'Content' + */ + private array $subTemplates = []; + + public function __construct(string|array $templateCandidates = []) + { + if (!empty($templateCandidates)) { + $this->setTemplate($templateCandidates); + } + } + + /** + * Execute the given template, passing it the given data. + * Used by the <% include %> template tag to process included templates. + * + * @param array $overlay Associative array of fields (e.g. args into an include template) to inject into the + * template as properties. These override properties and methods with the same name from $data and from global + * template providers. + */ + public static function execute_template(array|string $template, ViewLayerData $data, array $overlay = [], ?SSViewer_Scope $scope = null): string + { + $engine = static::create($template); + return $engine->render($data, $overlay, $scope); + } + + /** + * Triggered early in the request when someone requests a flush. + */ + public static function flush(): void + { + SSTemplateEngine::flushTemplateCache(true); + SSTemplateEngine::flushCacheBlockCache(true); + } + + /** + * Clears all parsed template files in the cache folder. + * + * @param bool $force Set this to true to force a re-flush. If left to false, flushing + * will only be performed once a request. + */ + public static function flushTemplateCache(bool $force = false): void + { + if (!SSTemplateEngine::$template_cache_flushed || $force) { + $dir = dir(TEMP_PATH); + while (false !== ($file = $dir->read())) { + if (strstr($file ?? '', '.cache')) { + unlink(TEMP_PATH . DIRECTORY_SEPARATOR . $file); + } + } + SSTemplateEngine::$template_cache_flushed = true; + } + } + + /** + * Clears all partial cache blocks. + * + * @param bool $force Set this to true to force a re-flush. If left to false, flushing + * will only be performed once a request. + */ + public static function flushCacheBlockCache(bool $force = false): void + { + if (!SSTemplateEngine::$cacheblock_cache_flushed || $force) { + $cache = Injector::inst()->get(CacheInterface::class . '.cacheblock'); + $cache->clear(); + SSTemplateEngine::$cacheblock_cache_flushed = true; + } + } + + public function hasTemplate(array|string $templateCandidates): bool + { + return (bool) $this->findTemplate($templateCandidates); + } + + public function renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true): string + { + $hash = sha1($template); + $cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . ".cache.$hash"; + + // Generate a file whether we're caching or not. + // This is an inefficiency that's required due to the way rendered templates get processed. + if (!file_exists($cacheFile) || Injector::inst()->get(Kernel::class)->isFlushed()) { + $content = $this->parseTemplateContent($template, "string sha1=$hash"); + $fh = fopen($cacheFile, 'w'); + fwrite($fh, $content); + fclose($fh); + } + + $output = $this->includeGeneratedTemplate($cacheFile, $model, $overlay, []); + + if (!$cache) { + unlink($cacheFile); + } + + return $output; + } + + public function render(ViewLayerData $model, array $overlay = [], ?SSViewer_Scope $scope = null): string + { + SSTemplateEngine::$topLevel[] = $model; + $template = $this->chosen; + + // If there's no template, throw an exception + if (!$template) { + if (empty($this->templateCandidates)) { + throw new MissingTemplateException( + 'No template to render. ' + . 'Try calling setTemplate() or passing template candidates into the constructor.' + ); + } + $message = 'None of the following templates could be found: '; + $message .= print_r($this->templateCandidates, true); + $themes = SSViewer::get_themes(); + if (!$themes) { + $message .= ' (no theme in use)'; + } else { + $message .= ' in themes "' . print_r($themes, true) . '"'; + } + throw new MissingTemplateException($message); + } + + $cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . '.cache' + . str_replace(['\\','/',':'], '.', Director::makeRelative(realpath($template ?? '')) ?? ''); + $lastEdited = filemtime($template ?? ''); + + if (!file_exists($cacheFile ?? '') || filemtime($cacheFile ?? '') < $lastEdited) { + $content = file_get_contents($template ?? ''); + $content = $this->parseTemplateContent($content, $template); + + $fh = fopen($cacheFile ?? '', 'w'); + fwrite($fh, $content ?? ''); + fclose($fh); + } + + $underlay = ['I18NNamespace' => basename($template ?? '')]; + + // Makes the rendered sub-templates available on the parent model, + // through $Content and $Layout placeholders. + foreach (['Content', 'Layout'] as $subtemplate) { + // Detect sub-template to use + $sub = $this->getSubtemplateFor($subtemplate); + if (!$sub) { + continue; + } + + // Create lazy-evaluated underlay for this subtemplate + $underlay[$subtemplate] = function () use ($model, $overlay, $sub) { + $subtemplateViewer = clone $this; + // Select the right template and render if the template exists + $subtemplateViewer->setTemplate($sub); + // If there's no template for that underlay, just don't render anything. + // This mirrors how SSViewer_Scope handles null values. + if (!$subtemplateViewer->chosen) { + return null; + } + // Render and wrap in DBHTMLText so it doesn't get escaped + return DBHTMLText::create()->setValue($subtemplateViewer->render($model, $overlay)); + }; + } + + $output = $this->includeGeneratedTemplate($cacheFile, $model, $overlay, $underlay, $scope); + + array_pop(SSTemplateEngine::$topLevel); + + return $output; + } + + public function setTemplate(string|array $templateCandidates): static + { + $this->templateCandidates = $templateCandidates; + $this->chosen = $this->findTemplate($templateCandidates); + $this->subTemplates = []; + return $this; + } + + /** + * Set the template parser that will be used in template generation + */ + public function setParser(TemplateParser $parser): static + { + $this->parser = $parser; + return $this; + } + + /** + * Returns the parser that is set for template generation + */ + public function getParser(): TemplateParser + { + if (!$this->parser) { + $this->setParser(Injector::inst()->get(SSTemplateParser::class)); + } + return $this->parser; + } + + /** + * Set the cache object to use when storing / retrieving partial cache blocks. + */ + public function setPartialCacheStore(CacheInterface $cache): static + { + $this->partialCacheStore = $cache; + return $this; + } + + /** + * Get the cache object to use when storing / retrieving partial cache blocks. + */ + public function getPartialCacheStore(): CacheInterface + { + if (!$this->partialCacheStore) { + $this->partialCacheStore = Injector::inst()->get(CacheInterface::class . '.cacheblock'); + } + return $this->partialCacheStore; + } + + /** + * An internal utility function to set up variables in preparation for including a compiled + * template, then do the include + * + * @param string $cacheFile The path to the file that contains the template compiled to PHP + * @param ViewLayerData $model The model to use as the root scope for the template + * @param array $overlay Any variables to layer on top of the scope + * @param array $underlay Any variables to layer underneath the scope + * @param SSViewer_Scope|null $inheritedScope The current scope of a parent template including a sub-template + */ + protected function includeGeneratedTemplate( + string $cacheFile, + ViewLayerData $model, + array $overlay, + array $underlay, + ?SSViewer_Scope $inheritedScope = null + ): string { + if (isset($_GET['showtemplate']) && $_GET['showtemplate'] && Permission::check('ADMIN')) { + $lines = file($cacheFile ?? ''); + echo "

Template: $cacheFile

"; + echo '
';
+            foreach ($lines as $num => $line) {
+                echo str_pad($num+1, 5) . htmlentities($line, ENT_COMPAT, 'UTF-8');
+            }
+            echo '
'; + } + + $cache = $this->getPartialCacheStore(); + $scope = new SSViewer_Scope($model, $overlay, $underlay, $inheritedScope); + $val = ''; + + // Placeholder for values exposed to $cacheFile + [$cache, $scope, $val]; + include($cacheFile); + + return $val; + } + + /** + * Get the appropriate template to use for the named sub-template, or null if none are appropriate + */ + protected function getSubtemplateFor(string $subtemplate): ?array + { + // Get explicit subtemplate name + if (isset($this->subTemplates[$subtemplate])) { + return $this->subTemplates[$subtemplate]; + } + + // Don't apply sub-templates if type is already specified (e.g. 'Includes') + if (isset($this->templateCandidates['type'])) { + return null; + } + + // Filter out any other typed templates as we can only add, not change type + $templates = array_filter( + (array) $this->templateCandidates, + function ($template) { + return !isset($template['type']); + } + ); + if (empty($templates)) { + return null; + } + + // Set type to subtemplate + $templates['type'] = $subtemplate; + return $templates; + } + + /** + * Parse given template contents + * + * @param string $content The template contents + * @param string $template The template file name + */ + protected function parseTemplateContent(string $content, string $template = ""): string + { + return $this->getParser()->compileString( + $content, + $template, + Director::isDev() && SSViewer::config()->uninherited('source_file_comments') + ); + } + + /** + * Attempts to find possible candidate templates from a set of template + * names from modules, current theme directory and finally the application + * folder. + * + * The template names can be passed in as plain strings, or be in the + * format "type/name", where type is the type of template to search for + * (e.g. Includes, Layout). + * + * The results of this method will be cached for future use. + * + * @param string|array $template Template name, or template spec in array format with the keys + * 'type' (type string) and 'templates' (template hierarchy in order of precedence). + * If 'templates' is omitted then any other item in the array will be treated as the template + * list, or list of templates each in the array spec given. + * Templates with an .ss extension will be treated as file paths, and will bypass + * theme-coupled resolution. + * @param array $themes List of themes to use to resolve themes. Defaults to {@see SSViewer::get_themes()} + * @return string Absolute path to resolved template file, or null if not resolved. + * File location will be in the format themes//templates///.ss + * Note that type (e.g. 'Layout') is not the root level directory under 'templates'. + * Returns null if no template was found. + */ + private function findTemplate(string|array $template, array $themes = []): ?string + { + if (empty($themes)) { + $themes = SSViewer::get_themes(); + } + + $cacheAdapter = ThemeResourceLoader::inst()->getCache(); + $cacheKey = 'findTemplate_' . md5(json_encode($template) . json_encode($themes)); + + // Look for a cached result for this data set + if ($cacheAdapter->has($cacheKey)) { + return $cacheAdapter->get($cacheKey); + } + + $type = ''; + if (is_array($template)) { + // Check if templates has type specified + if (array_key_exists('type', $template ?? [])) { + $type = $template['type']; + unset($template['type']); + } + // Templates are either nested in 'templates' or just the rest of the list + $templateList = array_key_exists('templates', $template ?? []) ? $template['templates'] : $template; + } else { + $templateList = [$template]; + } + + $themePaths = ThemeResourceLoader::inst()->getThemePaths($themes); + $baseDir = ThemeResourceLoader::inst()->getBase(); + foreach ($templateList as $i => $template) { + // Check if passed list of templates in array format + if (is_array($template)) { + $path = $this->findTemplate($template, $themes); + if ($path) { + $cacheAdapter->set($cacheKey, $path); + return $path; + } + continue; + } + + // If we have an .ss extension, this is a path, not a template name. We should + // pass in templates without extensions in order for template manifest to find + // files dynamically. + if (substr($template ?? '', -3) == '.ss' && file_exists($template ?? '')) { + $cacheAdapter->set($cacheKey, $template); + return $template; + } + + // Check string template identifier + $template = str_replace('\\', '/', $template ?? ''); + $parts = explode('/', $template ?? ''); + + $tail = array_pop($parts); + $head = implode('/', $parts); + foreach ($themePaths as $themePath) { + // Join path + $pathParts = [ $baseDir, $themePath, 'templates', $head, $type, $tail ]; + try { + $path = Path::join($pathParts) . '.ss'; + if (file_exists($path ?? '')) { + $cacheAdapter->set($cacheKey, $path); + return $path; + } + } catch (InvalidArgumentException $e) { + // No-op + } + } + } + + // No template found + $cacheAdapter->set($cacheKey, null); + return null; + } +} diff --git a/src/View/SSTemplateParser.peg b/src/View/SSTemplateParser.peg index a17a308f3d1..f53d64b6097 100644 --- a/src/View/SSTemplateParser.peg +++ b/src/View/SSTemplateParser.peg @@ -16,15 +16,6 @@ this is: framework/src/View): See the php-peg docs for more information on the parser format, and how to convert this file into SSTemplateParser.php -TODO: - Template comments - <%-- --%> - $Iteration - Partial cache blocks - i18n - we dont support then deprecated _t() or sprintf(_t()) methods; or the new <% t %> block yet - Add with and loop blocks - Add Up and Top - More error detection? - This comment will not appear in the output */ @@ -247,7 +238,7 @@ class SSTemplateParser extends Parser implements TemplateParser } $res['php'] .= ($sub['ArgumentMode'] == 'default') ? $sub['string_php'] : - str_replace('$$FINAL', 'XML_val', $sub['php'] ?? ''); + str_replace('$$FINAL', 'getValueAsArgument', $sub['php'] ?? ''); } /*!* @@ -274,8 +265,8 @@ class SSTemplateParser extends Parser implements TemplateParser } /** - * The basic generated PHP of LookupStep and LastLookupStep is the same, except that LookupStep calls 'obj' to - * get the next ModelData in the sequence, and LastLookupStep calls different methods (XML_val, hasValue, obj) + * The basic generated PHP of LookupStep and LastLookupStep is the same, except that LookupStep calls 'scopeToIntermediateValue' to + * get the next ModelData in the sequence, and LastLookupStep calls different methods (getOutputValue, hasValue, scopeToIntermediateValue) * depending on the context the lookup is used in. */ function Lookup_AddLookupStep(&$res, $sub, $method) @@ -284,17 +275,16 @@ class SSTemplateParser extends Parser implements TemplateParser $property = $sub['Call']['Method']['text']; + $arguments = ''; if (isset($sub['Call']['CallArguments']) && isset($sub['Call']['CallArguments']['php'])) { $arguments = $sub['Call']['CallArguments']['php']; - $res['php'] .= "->$method('$property', [$arguments], true)"; - } else { - $res['php'] .= "->$method('$property', [], true)"; } + $res['php'] .= "->$method('$property', [$arguments])"; } function Lookup_LookupStep(&$res, $sub) { - $this->Lookup_AddLookupStep($res, $sub, 'obj'); + $this->Lookup_AddLookupStep($res, $sub, 'scopeToIntermediateValue'); } function Lookup_LastLookupStep(&$res, $sub) @@ -357,7 +347,7 @@ class SSTemplateParser extends Parser implements TemplateParser function InjectionVariables_Argument(&$res, $sub) { - $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '') . ','; + $res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? '') . ','; } function InjectionVariables__finalise(&$res) @@ -392,7 +382,7 @@ class SSTemplateParser extends Parser implements TemplateParser */ function Injection_STR(&$res, $sub) { - $res['php'] = '$val .= '. str_replace('$$FINAL', 'XML_val', $sub['Lookup']['php'] ?? '') . ';'; + $res['php'] = '$val .= '. str_replace('$$FINAL', 'getOutputValue', $sub['Lookup']['php'] ?? '') . ';'; } /*!* @@ -535,10 +525,10 @@ class SSTemplateParser extends Parser implements TemplateParser if (!empty($res['php'])) { $res['php'] .= $sub['string_php']; } else { - $res['php'] = str_replace('$$FINAL', 'XML_val', $sub['lookup_php'] ?? ''); + $res['php'] = str_replace('$$FINAL', 'getOutputValue', $sub['lookup_php'] ?? ''); } } else { - $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? ''); + $res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? ''); } } @@ -566,8 +556,6 @@ class SSTemplateParser extends Parser implements TemplateParser $res['php'] .= '((bool)'.$sub['php'].')'; } else { $php = ($sub['ArgumentMode'] == 'default' ? $sub['lookup_php'] : $sub['php']); - // TODO: kinda hacky - maybe we need a way to pass state down the parse chain so - // Lookup_LastLookupStep and Argument_BareWord can produce hasValue instead of XML_val $res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? ''); } } @@ -697,7 +685,7 @@ class SSTemplateParser extends Parser implements TemplateParser $res['php'] = ''; } - $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? ''); + $res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? ''); } /*!* @@ -779,7 +767,7 @@ class SSTemplateParser extends Parser implements TemplateParser // the passed cache key, the block index, and the sha hash of the template. $res['php'] .= '$keyExpression = function() use ($scope, $cache) {' . PHP_EOL; $res['php'] .= '$val = \'\';' . PHP_EOL; - if ($globalKey = SSViewer::config()->get('global_key')) { + if ($globalKey = SSTemplateEngine::config()->get('global_key')) { // Embed the code necessary to evaluate the globalKey directly into the template, // so that SSTemplateParser only needs to be called during template regeneration. // Warning: If the global key is changed, it's necessary to flush the template cache. @@ -827,7 +815,7 @@ class SSTemplateParser extends Parser implements TemplateParser { $entity = $sub['String']['text']; if (strpos($entity ?? '', '.') === false) { - $res['php'] .= "\$scope->XML_val('I18NNamespace').'.$entity'"; + $res['php'] .= "\$scope->getOutputValue('I18NNamespace').'.$entity'"; } else { $res['php'] .= "'$entity'"; } @@ -915,7 +903,7 @@ class SSTemplateParser extends Parser implements TemplateParser break; default: - $res['php'] .= str_replace('$$FINAL', 'obj', $sub['php'] ?? '') . '->self()'; + $res['php'] .= str_replace('$$FINAL', 'scopeToIntermediateValue', $sub['php'] ?? '') . '->self()'; break; } } @@ -947,8 +935,8 @@ class SSTemplateParser extends Parser implements TemplateParser $template = $res['template']; $arguments = $res['arguments']; - // Note: 'type' here is important to disable subTemplates in SSViewer::getSubtemplateFor() - $res['php'] = '$val .= \\SilverStripe\\View\\SSViewer::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' . + // Note: 'type' here is important to disable subTemplates in SSTemplateEngine::getSubtemplateFor() + $res['php'] = '$val .= \\SilverStripe\\View\\SSTemplateEngine::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' . implode(',', $arguments)."], \$scope, true);\n"; if ($this->includeDebuggingComments) { // Add include filename comments on dev sites @@ -1035,9 +1023,9 @@ class SSTemplateParser extends Parser implements TemplateParser 'arguments only.', $this); } - //loop without arguments loops on the current scope + // loop without arguments loops on the current scope if ($res['ArgumentCount'] == 0) { - $on = '$scope->locally()->obj(\'Me\', [], true)'; + $on = '$scope->locally()->self()'; } else { //loop in the normal way $arg = $res['Arguments'][0]; if ($arg['ArgumentMode'] == 'string') { @@ -1045,13 +1033,13 @@ class SSTemplateParser extends Parser implements TemplateParser } $on = str_replace( '$$FINAL', - 'obj', + 'scopeToIntermediateValue', ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php'] ); } return - $on . '; $scope->pushScope(); while (($key = $scope->next()) !== false) {' . PHP_EOL . + $on . '; $scope->pushScope(); while ($scope->next() !== false) {' . PHP_EOL . $res['Template']['php'] . PHP_EOL . '}; $scope->popScope(); '; } @@ -1071,7 +1059,7 @@ class SSTemplateParser extends Parser implements TemplateParser throw new SSTemplateParseException('Control block cant take string as argument.', $this); } - $on = str_replace('$$FINAL', 'obj', ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']); + $on = str_replace('$$FINAL', 'scopeToIntermediateValue', ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']); return $on . '; $scope->pushScope();' . PHP_EOL . $res['Template']['php'] . PHP_EOL . @@ -1116,27 +1104,6 @@ class SSTemplateParser extends Parser implements TemplateParser } } - /** - * This is an open block handler, for the <% debug %> utility tag - */ - function OpenBlock_Handle_Debug(&$res) - { - if ($res['ArgumentCount'] == 0) { - return '$scope->debug();'; - } elseif ($res['ArgumentCount'] == 1) { - $arg = $res['Arguments'][0]; - - if ($arg['ArgumentMode'] == 'string') { - return 'Debug::show('.$arg['php'].');'; - } - - $php = ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']; - return '$val .= Debug::show('.str_replace('FINALGET!', 'cachedCall', $php ?? '').');'; - } else { - throw new SSTemplateParseException('Debug takes 0 or 1 argument only.', $this); - } - } - /** * This is an open block handler, for the <% base_tag %> tag */ @@ -1145,7 +1112,9 @@ class SSTemplateParser extends Parser implements TemplateParser if ($res['ArgumentCount'] != 0) { throw new SSTemplateParseException('Base_tag takes no arguments', $this); } - return '$val .= \\SilverStripe\\View\\SSViewer::get_base_tag($val);'; + $code = '$isXhtml = preg_match(\'/]+xhtml/i\', $val);'; + $code .= PHP_EOL . '$val .= \\SilverStripe\\View\\SSViewer::getBaseTag($isXhtml);'; + return $code; } /** @@ -1297,9 +1266,9 @@ EOC; * @param string $templateName The name of the template, normally the filename the template source was loaded from * @param bool $includeDebuggingComments True is debugging comments should be included in the output * @param bool $topTemplate True if this is a top template, false if it's just a template - * @return mixed|string The php that, when executed (via include or exec) will behave as per the template source + * @return string The php that, when executed (via include or exec) will behave as per the template source */ - public function compileString($string, $templateName = "", $includeDebuggingComments = false, $topTemplate = true) + public function compileString(string $string, string $templateName = "", bool $includeDebuggingComments = false, bool $topTemplate = true): string { if (!trim($string ?? '')) { $code = ''; @@ -1308,8 +1277,7 @@ EOC; $this->includeDebuggingComments = $includeDebuggingComments; - // Ignore UTF8 BOM at beginning of string. TODO: Confirm this is needed, make sure SSViewer handles UTF - // (and other encodings) properly + // Ignore UTF8 BOM at beginning of string. if (substr($string ?? '', 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { $this->pos = 3; } @@ -1341,7 +1309,7 @@ EOC; * @param string $templateName * @return string $code */ - protected function includeDebuggingComments($code, $templateName) + protected function includeDebuggingComments(string $code, string $templateName): string { // If this template contains a doctype, put it right after it, // if not, put it after the tag to avoid IE glitches @@ -1375,11 +1343,10 @@ EOC; * Compiles some file that contains template source code, and returns the php code that will execute as per that * source * - * @static - * @param $template - A file path that contains template source code - * @return mixed|string - The php that, when executed (via include or exec) will behave as per the template source + * @param string $template - A file path that contains template source code + * @return string - The php that, when executed (via include or exec) will behave as per the template source */ - public function compileFile($template) + public function compileFile(string $template): string { return $this->compileString(file_get_contents($template ?? ''), $template); } diff --git a/src/View/SSTemplateParser.php b/src/View/SSTemplateParser.php index 2b71082607c..d391af043e9 100644 --- a/src/View/SSTemplateParser.php +++ b/src/View/SSTemplateParser.php @@ -572,7 +572,7 @@ function CallArguments_Argument(&$res, $sub) } $res['php'] .= ($sub['ArgumentMode'] == 'default') ? $sub['string_php'] : - str_replace('$$FINAL', 'XML_val', $sub['php'] ?? ''); + str_replace('$$FINAL', 'getValueAsArgument', $sub['php'] ?? ''); } /* Call: Method:Word ( "(" < :CallArguments? > ")" )? */ @@ -765,8 +765,8 @@ function Lookup__construct(&$res) } /** - * The basic generated PHP of LookupStep and LastLookupStep is the same, except that LookupStep calls 'obj' to - * get the next ModelData in the sequence, and LastLookupStep calls different methods (XML_val, hasValue, obj) + * The basic generated PHP of LookupStep and LastLookupStep is the same, except that LookupStep calls 'scopeToIntermediateValue' to + * get the next ModelData in the sequence, and LastLookupStep calls different methods (getOutputValue, hasValue, scopeToIntermediateValue) * depending on the context the lookup is used in. */ function Lookup_AddLookupStep(&$res, $sub, $method) @@ -775,17 +775,16 @@ function Lookup_AddLookupStep(&$res, $sub, $method) $property = $sub['Call']['Method']['text']; + $arguments = ''; if (isset($sub['Call']['CallArguments']) && isset($sub['Call']['CallArguments']['php'])) { $arguments = $sub['Call']['CallArguments']['php']; - $res['php'] .= "->$method('$property', [$arguments], true)"; - } else { - $res['php'] .= "->$method('$property', [], true)"; } + $res['php'] .= "->$method('$property', [$arguments])"; } function Lookup_LookupStep(&$res, $sub) { - $this->Lookup_AddLookupStep($res, $sub, 'obj'); + $this->Lookup_AddLookupStep($res, $sub, 'scopeToIntermediateValue'); } function Lookup_LastLookupStep(&$res, $sub) @@ -1009,7 +1008,7 @@ function InjectionVariables_InjectionName(&$res, $sub) function InjectionVariables_Argument(&$res, $sub) { - $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '') . ','; + $res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? '') . ','; } function InjectionVariables__finalise(&$res) @@ -1158,7 +1157,7 @@ function match_Injection ($stack = array()) { function Injection_STR(&$res, $sub) { - $res['php'] = '$val .= '. str_replace('$$FINAL', 'XML_val', $sub['Lookup']['php'] ?? '') . ';'; + $res['php'] = '$val .= '. str_replace('$$FINAL', 'getOutputValue', $sub['Lookup']['php'] ?? '') . ';'; } /* DollarMarkedLookup: SimpleInjection */ @@ -1187,7 +1186,7 @@ function match_QuotedString ($stack = array()) { $matchrule = "QuotedString"; $result = $this->construct($matchrule, $matchrule, null); $_154 = NULL; do { - $stack[] = $result; $result = $this->construct( $matchrule, "q" ); + $stack[] = $result; $result = $this->construct( $matchrule, "q" ); if (( $subres = $this->rx( '/[\'"]/' ) ) !== FALSE) { $result["text"] .= $subres; $subres = $result; $result = array_pop($stack); @@ -1197,7 +1196,7 @@ function match_QuotedString ($stack = array()) { $result = array_pop($stack); $_154 = FALSE; break; } - $stack[] = $result; $result = $this->construct( $matchrule, "String" ); + $stack[] = $result; $result = $this->construct( $matchrule, "String" ); if (( $subres = $this->rx( '/ (\\\\\\\\ | \\\\. | [^'.$this->expression($result, $stack, 'q').'\\\\])* /' ) ) !== FALSE) { $result["text"] .= $subres; $subres = $result; $result = array_pop($stack); @@ -1818,10 +1817,10 @@ function Comparison_Argument(&$res, $sub) if (!empty($res['php'])) { $res['php'] .= $sub['string_php']; } else { - $res['php'] = str_replace('$$FINAL', 'XML_val', $sub['lookup_php'] ?? ''); + $res['php'] = str_replace('$$FINAL', 'getOutputValue', $sub['lookup_php'] ?? ''); } } else { - $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? ''); + $res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? ''); } } @@ -1840,7 +1839,7 @@ function match_PresenceCheck ($stack = array()) { $pos_255 = $this->pos; $_254 = NULL; do { - $stack[] = $result; $result = $this->construct( $matchrule, "Not" ); + $stack[] = $result; $result = $this->construct( $matchrule, "Not" ); if (( $subres = $this->literal( 'not' ) ) !== FALSE) { $result["text"] .= $subres; $subres = $result; $result = array_pop($stack); @@ -1886,8 +1885,6 @@ function PresenceCheck_Argument(&$res, $sub) $res['php'] .= '((bool)'.$sub['php'].')'; } else { $php = ($sub['ArgumentMode'] == 'default' ? $sub['lookup_php'] : $sub['php']); - // TODO: kinda hacky - maybe we need a way to pass state down the parse chain so - // Lookup_LastLookupStep and Argument_BareWord can produce hasValue instead of XML_val $res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? ''); } } @@ -2235,7 +2232,7 @@ function match_Require ($stack = array()) { else { $_330 = FALSE; break; } if (( $subres = $this->whitespace( ) ) !== FALSE) { $result["text"] .= $subres; } else { $_330 = FALSE; break; } - $stack[] = $result; $result = $this->construct( $matchrule, "Call" ); + $stack[] = $result; $result = $this->construct( $matchrule, "Call" ); $_326 = NULL; do { $matcher = 'match_'.'Word'; $key = $matcher; $pos = $this->pos; @@ -2470,7 +2467,7 @@ function CacheBlockArguments_CacheBlockArgument(&$res, $sub) $res['php'] = ''; } - $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? ''); + $res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? ''); } /* CacheBlockTemplate: (Comment | Translate | If | Require | OldI18NTag | Include | ClosedBlock | @@ -2740,7 +2737,7 @@ function match_UncachedBlock ($stack = array()) { $_423 = NULL; do { if (( $subres = $this->whitespace( ) ) !== FALSE) { $result["text"] .= $subres; } - $stack[] = $result; $result = $this->construct( $matchrule, "Conditional" ); + $stack[] = $result; $result = $this->construct( $matchrule, "Conditional" ); $_419 = NULL; do { $_417 = NULL; @@ -3166,7 +3163,7 @@ function match_CacheBlock ($stack = array()) { if (( $subres = $this->literal( '<%' ) ) !== FALSE) { $result["text"] .= $subres; } else { $_555 = FALSE; break; } if (( $subres = $this->whitespace( ) ) !== FALSE) { $result["text"] .= $subres; } - $stack[] = $result; $result = $this->construct( $matchrule, "CacheTag" ); + $stack[] = $result; $result = $this->construct( $matchrule, "CacheTag" ); $_508 = NULL; do { $_506 = NULL; @@ -3225,7 +3222,7 @@ function match_CacheBlock ($stack = array()) { $_524 = NULL; do { if (( $subres = $this->whitespace( ) ) !== FALSE) { $result["text"] .= $subres; } - $stack[] = $result; $result = $this->construct( $matchrule, "Conditional" ); + $stack[] = $result; $result = $this->construct( $matchrule, "Conditional" ); $_520 = NULL; do { $_518 = NULL; @@ -3428,7 +3425,7 @@ function CacheBlock_CacheBlockTemplate(&$res, $sub) // the passed cache key, the block index, and the sha hash of the template. $res['php'] .= '$keyExpression = function() use ($scope, $cache) {' . PHP_EOL; $res['php'] .= '$val = \'\';' . PHP_EOL; - if ($globalKey = SSViewer::config()->get('global_key')) { + if ($globalKey = SSTemplateEngine::config()->get('global_key')) { // Embed the code necessary to evaluate the globalKey directly into the template, // so that SSTemplateParser only needs to be called during template regeneration. // Warning: If the global key is changed, it's necessary to flush the template cache. @@ -3587,7 +3584,7 @@ function OldTPart_QuotedString(&$res, $sub) { $entity = $sub['String']['text']; if (strpos($entity ?? '', '.') === false) { - $res['php'] .= "\$scope->XML_val('I18NNamespace').'.$entity'"; + $res['php'] .= "\$scope->getOutputValue('I18NNamespace').'.$entity'"; } else { $res['php'] .= "'$entity'"; } @@ -3792,7 +3789,7 @@ function NamedArgument_Value(&$res, $sub) break; default: - $res['php'] .= str_replace('$$FINAL', 'obj', $sub['php'] ?? '') . '->self()'; + $res['php'] .= str_replace('$$FINAL', 'scopeToIntermediateValue', $sub['php'] ?? '') . '->self()'; break; } } @@ -3896,8 +3893,8 @@ function Include__finalise(&$res) $template = $res['template']; $arguments = $res['arguments']; - // Note: 'type' here is important to disable subTemplates in SSViewer::getSubtemplateFor() - $res['php'] = '$val .= \\SilverStripe\\View\\SSViewer::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' . + // Note: 'type' here is important to disable subTemplates in SSTemplateEngine::getSubtemplateFor() + $res['php'] = '$val .= \\SilverStripe\\View\\SSTemplateEngine::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' . implode(',', $arguments)."], \$scope, true);\n"; if ($this->includeDebuggingComments) { // Add include filename comments on dev sites @@ -4165,7 +4162,7 @@ function match_ClosedBlock ($stack = array()) { unset( $pos_685 ); } if (( $subres = $this->whitespace( ) ) !== FALSE) { $result["text"] .= $subres; } - $stack[] = $result; $result = $this->construct( $matchrule, "Zap" ); + $stack[] = $result; $result = $this->construct( $matchrule, "Zap" ); if (( $subres = $this->literal( '%>' ) ) !== FALSE) { $result["text"] .= $subres; $subres = $result; $result = array_pop($stack); @@ -4263,9 +4260,9 @@ function ClosedBlock_Handle_Loop(&$res) 'arguments only.', $this); } - //loop without arguments loops on the current scope + // loop without arguments loops on the current scope if ($res['ArgumentCount'] == 0) { - $on = '$scope->locally()->obj(\'Me\', [], true)'; + $on = '$scope->locally()->self()'; } else { //loop in the normal way $arg = $res['Arguments'][0]; if ($arg['ArgumentMode'] == 'string') { @@ -4273,13 +4270,13 @@ function ClosedBlock_Handle_Loop(&$res) } $on = str_replace( '$$FINAL', - 'obj', + 'scopeToIntermediateValue', ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php'] ); } return - $on . '; $scope->pushScope(); while (($key = $scope->next()) !== false) {' . PHP_EOL . + $on . '; $scope->pushScope(); while ($scope->next() !== false) {' . PHP_EOL . $res['Template']['php'] . PHP_EOL . '}; $scope->popScope(); '; } @@ -4299,7 +4296,7 @@ function ClosedBlock_Handle_With(&$res) throw new SSTemplateParseException('Control block cant take string as argument.', $this); } - $on = str_replace('$$FINAL', 'obj', ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']); + $on = str_replace('$$FINAL', 'scopeToIntermediateValue', ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']); return $on . '; $scope->pushScope();' . PHP_EOL . $res['Template']['php'] . PHP_EOL . @@ -4401,27 +4398,6 @@ function OpenBlock__finalise(&$res) } } - /** - * This is an open block handler, for the <% debug %> utility tag - */ - function OpenBlock_Handle_Debug(&$res) - { - if ($res['ArgumentCount'] == 0) { - return '$scope->debug();'; - } elseif ($res['ArgumentCount'] == 1) { - $arg = $res['Arguments'][0]; - - if ($arg['ArgumentMode'] == 'string') { - return 'Debug::show('.$arg['php'].');'; - } - - $php = ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']; - return '$val .= Debug::show('.str_replace('FINALGET!', 'cachedCall', $php ?? '').');'; - } else { - throw new SSTemplateParseException('Debug takes 0 or 1 argument only.', $this); - } - } - /** * This is an open block handler, for the <% base_tag %> tag */ @@ -4430,7 +4406,9 @@ function OpenBlock_Handle_Base_tag(&$res) if ($res['ArgumentCount'] != 0) { throw new SSTemplateParseException('Base_tag takes no arguments', $this); } - return '$val .= \\SilverStripe\\View\\SSViewer::get_base_tag($val);'; + $code = '$isXhtml = preg_match(\'/]+xhtml/i\', $val);'; + $code .= PHP_EOL . '$val .= \\SilverStripe\\View\\SSViewer::getBaseTag($isXhtml);'; + return $code; } /** @@ -4575,7 +4553,7 @@ function match_MalformedCloseTag ($stack = array()) { if (( $subres = $this->literal( '<%' ) ) !== FALSE) { $result["text"] .= $subres; } else { $_743 = FALSE; break; } if (( $subres = $this->whitespace( ) ) !== FALSE) { $result["text"] .= $subres; } - $stack[] = $result; $result = $this->construct( $matchrule, "Tag" ); + $stack[] = $result; $result = $this->construct( $matchrule, "Tag" ); $_737 = NULL; do { if (( $subres = $this->literal( 'end_' ) ) !== FALSE) { $result["text"] .= $subres; } @@ -5321,9 +5299,9 @@ function Text__finalise(&$res) * @param string $templateName The name of the template, normally the filename the template source was loaded from * @param bool $includeDebuggingComments True is debugging comments should be included in the output * @param bool $topTemplate True if this is a top template, false if it's just a template - * @return mixed|string The php that, when executed (via include or exec) will behave as per the template source + * @return string The php that, when executed (via include or exec) will behave as per the template source */ - public function compileString($string, $templateName = "", $includeDebuggingComments = false, $topTemplate = true) + public function compileString(string $string, string $templateName = "", bool $includeDebuggingComments = false, bool $topTemplate = true): string { if (!trim($string ?? '')) { $code = ''; @@ -5332,8 +5310,7 @@ public function compileString($string, $templateName = "", $includeDebuggingComm $this->includeDebuggingComments = $includeDebuggingComments; - // Ignore UTF8 BOM at beginning of string. TODO: Confirm this is needed, make sure SSViewer handles UTF - // (and other encodings) properly + // Ignore UTF8 BOM at beginning of string. if (substr($string ?? '', 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { $this->pos = 3; } @@ -5365,7 +5342,7 @@ public function compileString($string, $templateName = "", $includeDebuggingComm * @param string $templateName * @return string $code */ - protected function includeDebuggingComments($code, $templateName) + protected function includeDebuggingComments(string $code, string $templateName): string { // If this template contains a doctype, put it right after it, // if not, put it after the tag to avoid IE glitches @@ -5399,11 +5376,10 @@ protected function includeDebuggingComments($code, $templateName) * Compiles some file that contains template source code, and returns the php code that will execute as per that * source * - * @static - * @param $template - A file path that contains template source code - * @return mixed|string - The php that, when executed (via include or exec) will behave as per the template source + * @param string $template - A file path that contains template source code + * @return string - The php that, when executed (via include or exec) will behave as per the template source */ - public function compileFile($template) + public function compileFile(string $template): string { return $this->compileString(file_get_contents($template ?? ''), $template); } diff --git a/src/View/SSViewer.php b/src/View/SSViewer.php index e276b7fe273..1d37971a2ed 100644 --- a/src/View/SSViewer.php +++ b/src/View/SSViewer.php @@ -5,42 +5,20 @@ use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\ClassInfo; -use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Convert; -use SilverStripe\Core\Flushable; -use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Control\Director; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBHTMLText; -use SilverStripe\Security\Permission; use InvalidArgumentException; -use SilverStripe\Model\ModelData; -use SilverStripe\Dev\Deprecation; +use SilverStripe\Core\Injector\Injector; /** - * Parses a template file with an *.ss file extension. - * - * In addition to a full template in the templates/ folder, a template in - * templates/Content or templates/Layout will be rendered into $Content and - * $Layout, respectively. - * - * A single template can be parsed by multiple nested {@link SSViewer} instances - * through $Layout/$Content placeholders, as well as <% include MyTemplateFile %> template commands. - * - * Themes + * Class that manages themes and interacts with TemplateEngine classes to render templates. * - * See http://doc.silverstripe.org/themes and http://doc.silverstripe.org/themes:developing - * - * Caching - * - * Compiled templates are cached via {@link Cache}, usually on the filesystem. - * If you put ?flush=1 on your URL, it will force the template to be recompiled. - * - * @see http://doc.silverstripe.org/themes - * @see http://doc.silverstripe.org/themes:developing + * Ensures rendered templates are normalised, e.g have appropriate resources from the Requirements API. */ -class SSViewer implements Flushable +class SSViewer { use Configurable; use Injectable; @@ -58,18 +36,8 @@ class SSViewer implements Flushable /** * A list (highest priority first) of themes to use * Only used when {@link $theme_enabled} is set to TRUE. - * - * @config - * @var string */ - private static $themes = []; - - /** - * Overridden value of $themes config - * - * @var array - */ - protected static $current_themes = null; + private static array $themes = []; /** * Use the theme. Set to FALSE in order to disable themes, @@ -77,203 +45,89 @@ class SSViewer implements Flushable * 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 * needs to be rendered from within this administrative interface. - * - * @config - * @var bool */ - private static $theme_enabled = true; + private static bool $theme_enabled = true; /** - * Default prepended cache key for partial caching - * - * @config - * @var string - * @deprecated 5.4.0 Will be moved to SilverStripe\View\SSTemplateEngine.global_key + * If true, rendered templates will include comments indicating which template file was used. + * May not be supported for some rendering engines. */ - private static $global_key = '$CurrentReadingMode, $CurrentUser.ID'; + private static bool $source_file_comments = false; /** - * @config - * @var bool + * Set if hash links should be rewritten */ - private static $source_file_comments = false; + private static bool $rewrite_hash_links = true; /** - * Set if hash links should be rewritten - * - * @config - * @var bool + * Overridden value of $themes config */ - private static $rewrite_hash_links = true; + protected static array $current_themes = []; /** * Overridden value of rewrite_hash_links config * - * @var bool + * Can be set to "php" to rewrite hash links with PHP executable code. */ - protected static $current_rewrite_hash_links = null; + protected static null|bool|string $current_rewrite_hash_links = null; /** * Instance variable to disable rewrite_hash_links (overrides global default) * Leave null to use global state. * - * @var bool|null + * Can be set to "php" to rewrite hash links with PHP executable code. */ - protected $rewriteHashlinks = null; + protected null|bool|string $rewriteHashlinks = null; /** - * @internal - * @ignore + * Determines whether resources from the Requirements API are included in a processed result. */ - private static $template_cache_flushed = false; + protected bool $includeRequirements = true; - /** - * @internal - * @ignore - */ - private static $cacheblock_cache_flushed = false; - - /** - * List of items being processed - * - * @var array - * @deprecated 5.4.0 Will be moved to SilverStripe\View\SSTemplateEngine - */ - protected static $topLevel = []; - - /** - * List of templates to select from - * - * @var array - * @deprecated 5.4.0 Will be moved to SilverStripe\View\SSTemplateEngine - */ - protected $templates = null; - - /** - * Absolute path to chosen template file - * - * @var string - * @deprecated 5.4.0 Will be moved to SilverStripe\View\SSTemplateEngine - */ - protected $chosen = null; + private TemplateEngine $templateEngine; /** - * Templates to use when looking up 'Layout' or 'Content' - * - * @var array - * @deprecated 5.4.0 Will be moved to SilverStripe\View\SSTemplateEngine - */ - protected $subTemplates = []; - - /** - * @var bool - */ - protected $includeRequirements = true; - - /** - * @var TemplateParser - * @deprecated 5.4.0 Will be moved to SilverStripe\View\SSTemplateEngine - */ - protected $parser; - - /** - * @var CacheInterface - * @deprecated 5.4.0 Will be moved to SilverStripe\View\SSTemplateEngine - */ - protected $partialCacheStore = null; - - /** - * @param string|array $templates If passed as a string with .ss extension, used as the "main" template. + * @param string|array $templates If passed as a string, used as the "main" template. * If passed as an array, it can be used for template inheritance (first found template "wins"). * Usually the array values are PHP class names, which directly correlate to template names. * * array('MySpecificPage', 'MyPage', 'Page') * - * @param TemplateParser $parser */ - public function __construct($templates, TemplateParser $parser = null) + public function __construct(string|array $templates, ?TemplateEngine $templateEngine = null) { - if ($parser) { - Deprecation::noticeWithNoReplacment('5.4.0', 'The $parser parameter is deprecated and will be removed'); - $this->setParser($parser); - } - - $this->setTemplate($templates); - - if (!$this->chosen) { - $message = 'None of the following templates could be found: '; - $message .= print_r($templates, true); - - $themes = SSViewer::get_themes(); - if (!$themes) { - $message .= ' (no theme in use)'; - } else { - $message .= ' in themes "' . print_r($themes, true) . '"'; - } - - user_error($message ?? '', E_USER_WARNING); - } - } - - /** - * Triggered early in the request when someone requests a flush. - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::flush() - */ - public static function flush() - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::flush()'); - SSViewer::flush_template_cache(true); - SSViewer::flush_cacheblock_cache(true); - } - - /** - * Create a template from a string instead of a .ss file - * - * @param string $content The template content - * @param bool|void $cacheTemplate Whether or not to cache the template from string - * @return SSViewer - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::renderString() - */ - public static function fromString($content, $cacheTemplate = null) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::renderString()'); - $viewer = SSViewer_FromString::create($content); - if ($cacheTemplate !== null) { - $viewer->setCacheTemplate($cacheTemplate); + if ($templateEngine) { + $templateEngine->setTemplate($templates); + } else { + $templateEngine = Injector::inst()->create(TemplateEngine::class, $templates); } - return $viewer; + $this->setTemplateEngine($templateEngine); } /** * Assign the list of active themes to apply. * If default themes should be included add $default as the last entry. - * - * @param array $themes */ - public static function set_themes($themes = []) + public static function set_themes(array $themes): void { static::$current_themes = $themes; } /** * Add to the list of active themes to apply - * - * @param array $themes */ - public static function add_themes($themes = []) + public static function add_themes(array $themes) { $currentThemes = SSViewer::get_themes(); $finalThemes = array_merge($themes, $currentThemes); // array_values is used to ensure sequential array keys as array_unique can leave gaps - static::set_themes(array_values(array_unique($finalThemes ?? []))); + static::set_themes(array_values(array_unique($finalThemes))); } /** * Get the list of active themes - * - * @return array */ - public static function get_themes() + public static function get_themes(): array { $default = [SSViewer::PUBLIC_THEME, SSViewer::DEFAULT_THEME]; @@ -283,7 +137,7 @@ public static function get_themes() // Explicit list is assigned $themes = static::$current_themes; - if (!isset($themes)) { + if (empty($themes)) { $themes = SSViewer::config()->uninherited('themes'); } if ($themes) { @@ -295,23 +149,26 @@ public static function get_themes() /** * Traverses the given the given class context looking for candidate template names - * which match each item in the class hierarchy. The resulting list of template candidates - * may or may not exist, but you can invoke {@see SSViewer::chooseTemplate} on any list - * to determine the best candidate based on the current themes. + * which match each item in the class hierarchy. + * + * This method does NOT check the filesystem, so the resulting list of template candidates + * may or may not exist - but you can pass these template candidates into the SSViewer + * constructor or into a TemplateEngine. + * + * If you really need know if a template file exists, you can call hasTemplate() on a TemplateEngine. * * @param string|object $classOrObject Valid class name, or object - * @param string $suffix * @param string $baseClass Class to halt ancestry search at - * @return array */ - public static function get_templates_by_class($classOrObject, $suffix = '', $baseClass = null) - { + public static function get_templates_by_class( + string|object $classOrObject, + string $suffix = '', + ?string $baseClass = null + ): array { // Figure out the class name from the supplied context. - if (!is_object($classOrObject) && !( - is_string($classOrObject) && class_exists($classOrObject ?? '') - )) { + if (is_string($classOrObject) && !class_exists($classOrObject ?? '')) { throw new InvalidArgumentException( - 'SSViewer::get_templates_by_class() expects a valid class name as its first parameter.' + 'SSViewer::get_templates_by_class() expects a valid class name or instantiated object as its first parameter.' ); } @@ -322,12 +179,12 @@ public static function get_templates_by_class($classOrObject, $suffix = '', $bas $templates[] = $template; $templates[] = ['type' => 'Includes', $template]; - // If the class is "PageController" (PSR-2 compatibility) or "Page_Controller" (legacy), look for Page.ss + // If the class is "PageController" (PSR-2 compatibility) or "Page_Controller" (legacy), look for Page template if (preg_match('/^(?.+[^\\\\])_?Controller$/iU', $class ?? '', $matches)) { $templates[] = $matches['name'] . $suffix; } - if ($baseClass && $class == $baseClass) { + if ($baseClass && $class === $baseClass) { break; } } @@ -336,28 +193,66 @@ public static function get_templates_by_class($classOrObject, $suffix = '', $bas } /** - * Get the current item being processed + * Get an associative array of names to information about callable template provider methods. * - * @return ModelData - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it. + * @var boolean $createObject If true, methods will be called on instantiated objects rather than statically on the class. */ - public static function topLevel() + public static function getMethodsFromProvider(string $providerInterface, $methodName, bool $createObject = false): array { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be removed without equivalent functionality to replace it.'); - if (SSViewer::$topLevel) { - return SSViewer::$topLevel[sizeof(SSViewer::$topLevel)-1]; + $implementors = ClassInfo::implementorsOf($providerInterface); + if ($implementors) { + foreach ($implementors as $implementor) { + // Create a new instance of the object for method calls + if ($createObject) { + $implementor = new $implementor(); + $exposedVariables = $implementor->$methodName(); + } else { + $exposedVariables = $implementor::$methodName(); + } + + foreach ($exposedVariables as $varName => $details) { + if (!is_array($details)) { + $details = ['method' => $details]; + } + + // If just a value (and not a key => value pair), use method name for both key and value + if (is_numeric($varName)) { + $varName = $details['method']; + } + + // Add in a reference to the implementing class (might be a string class name or an instance) + $details['implementor'] = $implementor; + + // And a callable array + if (isset($details['method'])) { + $details['callable'] = [$implementor, $details['method']]; + } + + // Save with both uppercase & lowercase first letter, so either works + $lcFirst = strtolower($varName[0] ?? '') . substr($varName ?? '', 1); + $result[$lcFirst] = $details; + $result[ucfirst($varName)] = $details; + } + } } - return null; + + return $result; + } + + /** + * Get the template engine used to render templates for this viewer + */ + public function getTemplateEngine(): TemplateEngine + { + return $this->templateEngine; } /** * Check if rewrite hash links are enabled on this instance - * - * @return bool */ - public function getRewriteHashLinks() + public function getRewriteHashLinks(): null|bool|string { - if (isset($this->rewriteHashlinks)) { + if ($this->rewriteHashlinks !== null) { return $this->rewriteHashlinks; } return static::getRewriteHashLinksDefault(); @@ -365,11 +260,8 @@ public function getRewriteHashLinks() /** * Set if hash links are rewritten for this instance - * - * @param bool $rewrite - * @return $this */ - public function setRewriteHashLinks($rewrite) + public function setRewriteHashLinks(null|bool|string $rewrite): static { $this->rewriteHashlinks = $rewrite; return $this; @@ -377,13 +269,11 @@ public function setRewriteHashLinks($rewrite) /** * Get default value for rewrite hash links for all modules - * - * @return bool */ - public static function getRewriteHashLinksDefault() + public static function getRewriteHashLinksDefault(): null|bool|string { // Check if config overridden - if (isset(static::$current_rewrite_hash_links)) { + if (static::$current_rewrite_hash_links !== null) { return static::$current_rewrite_hash_links; } return Config::inst()->get(static::class, 'rewrite_hash_links'); @@ -391,233 +281,29 @@ public static function getRewriteHashLinksDefault() /** * Set default rewrite hash links - * - * @param bool $rewrite */ - public static function setRewriteHashLinksDefault($rewrite) + public static function setRewriteHashLinksDefault(null|bool|string $rewrite) { static::$current_rewrite_hash_links = $rewrite; } - /** - * @param string|array $templates - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::setTemplate() - */ - public function setTemplate($templates) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::setTemplate()'); - $this->templates = $templates; - $this->chosen = $this->chooseTemplate($templates); - $this->subTemplates = []; - } - - /** - * Find the template to use for a given list - * - * @param array|string $templates - * @return string - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it - */ - public static function chooseTemplate($templates) - { - Deprecation::noticeWithNoReplacment('5.4.0'); - return ThemeResourceLoader::inst()->findTemplate($templates, SSViewer::get_themes()); - } - - /** - * Set the template parser that will be used in template generation - * - * @param TemplateParser $parser - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::setParser() - */ - public function setParser(TemplateParser $parser) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::setParser()'); - $this->parser = $parser; - } - - /** - * Returns the parser that is set for template generation - * - * @return TemplateParser - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::getParser() - */ - public function getParser() - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::getParser()'); - if (!$this->parser) { - $this->setParser(Injector::inst()->get('SilverStripe\\View\\SSTemplateParser')); - } - return $this->parser; - } - - /** - * Returns true if at least one of the listed templates exists. - * - * @param array|string $templates - * - * @return bool - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::hasTemplate() - */ - public static function hasTemplate($templates) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::hasTemplate()'); - return (bool)ThemeResourceLoader::inst()->findTemplate($templates, SSViewer::get_themes()); - } - /** * Call this to disable rewriting of links. This is useful in Ajax applications. * It returns the SSViewer objects, so that you can call new SSViewer("X")->dontRewriteHashlinks()->process(); - * - * @return $this */ - public function dontRewriteHashlinks() + public function dontRewriteHashlinks(): static { return $this->setRewriteHashLinks(false); } - /** - * @return string - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it - */ - public function exists() - { - Deprecation::noticeWithNoReplacment('5.4.0'); - return $this->chosen; - } - - /** - * @param string $identifier A template name without '.ss' extension or path - * @param string $type The template type, either "main", "Includes" or "Layout" - * @return string Full system path to a template file - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it - */ - public static function getTemplateFileByType($identifier, $type = null) - { - Deprecation::noticeWithNoReplacment('5.4.0'); - return ThemeResourceLoader::inst()->findTemplate(['type' => $type, $identifier], SSViewer::get_themes()); - } - - /** - * Clears all parsed template files in the cache folder. - * - * Can only be called once per request (there may be multiple SSViewer instances). - * - * @param bool $force Set this to true to force a re-flush. If left to false, flushing - * may only be performed once a request. - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::flushTemplateCache() - */ - public static function flush_template_cache($force = false) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::flushTemplateCache()'); - if (!SSViewer::$template_cache_flushed || $force) { - $dir = dir(TEMP_PATH); - while (false !== ($file = $dir->read())) { - if (strstr($file ?? '', '.cache')) { - unlink(TEMP_PATH . DIRECTORY_SEPARATOR . $file); - } - } - SSViewer::$template_cache_flushed = true; - } - } - - /** - * Clears all partial cache blocks. - * - * Can only be called once per request (there may be multiple SSViewer instances). - * - * @param bool $force Set this to true to force a re-flush. If left to false, flushing - * may only be performed once a request. - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::flushCacheBlockCache() - */ - public static function flush_cacheblock_cache($force = false) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::flushCacheBlockCache()'); - if (!SSViewer::$cacheblock_cache_flushed || $force) { - $cache = Injector::inst()->get(CacheInterface::class . '.cacheblock'); - $cache->clear(); - - - SSViewer::$cacheblock_cache_flushed = true; - } - } - - /** - * Set the cache object to use when storing / retrieving partial cache blocks. - * - * @param CacheInterface $cache - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::setPartialCacheStore() - */ - public function setPartialCacheStore($cache) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::setPartialCacheStore()'); - $this->partialCacheStore = $cache; - } - - /** - * Get the cache object to use when storing / retrieving partial cache blocks. - * - * @return CacheInterface - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::getPartialCacheStore() - */ - public function getPartialCacheStore() - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::getPartialCacheStore()'); - if ($this->partialCacheStore) { - return $this->partialCacheStore; - } - - return Injector::inst()->get(CacheInterface::class . '.cacheblock'); - } - /** * Flag whether to include the requirements in this response. - * - * @param bool $incl */ - public function includeRequirements($incl = true) + public function includeRequirements(bool $incl = true) { $this->includeRequirements = $incl; } - /** - * An internal utility function to set up variables in preparation for including a compiled - * template, then do the include - * - * Effectively this is the common code that both SSViewer#process and SSViewer_FromString#process call - * - * @param string $cacheFile The path to the file that contains the template compiled to PHP - * @param ModelData $item The item to use as the root scope for the template - * @param array $overlay Any variables to layer on top of the scope - * @param array $underlay Any variables to layer underneath the scope - * @param SSViewer_Scope|null $inheritedScope The current scope of a parent template including a sub-template - * @return string The result of executing the template - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::includeGeneratedTemplate() - */ - protected function includeGeneratedTemplate($cacheFile, $item, $overlay, $underlay, $inheritedScope = null) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::includeGeneratedTemplate()'); - if (isset($_GET['showtemplate']) && $_GET['showtemplate'] && Permission::check('ADMIN')) { - $lines = file($cacheFile ?? ''); - echo "

Template: $cacheFile

"; - echo "
";
-            foreach ($lines as $num => $line) {
-                echo str_pad($num+1, 5) . htmlentities($line, ENT_COMPAT, 'UTF-8');
-            }
-            echo "
"; - } - - $cache = $this->getPartialCacheStore(); - $scope = new SSViewer_DataPresenter($item, $overlay, $underlay, $inheritedScope); - $val = ''; - - // Placeholder for values exposed to $cacheFile - [$cache, $scope, $val]; - include($cacheFile); - - return $val; - } - /** * The process() method handles the "meat" of the template processing. * @@ -629,73 +315,25 @@ protected function includeGeneratedTemplate($cacheFile, $item, $overlay, $underl * * Note: You can call this method indirectly by {@link ModelData->renderWith()}. * - * @param ModelData $item - * @param array|null $arguments Arguments to an included template - * @param ModelData $inheritedScope The current scope of a parent template including a sub-template - * @return DBHTMLText Parsed template output. + * @param array $overlay Associative array of fields for use in the template. + * These will override properties and methods with the same name from $data and from global + * template providers. */ - public function process($item, $arguments = null, $inheritedScope = null) + public function process(mixed $item, array $overlay = []): DBHTMLText { - if ($inheritedScope !== null) { - Deprecation::noticeWithNoReplacment('5.4.0', 'The $inheritedScope parameter is deprecated and will be removed'); - } + $item = ViewLayerData::create($item); // Set hashlinks and temporarily modify global state $rewrite = $this->getRewriteHashLinks(); $origRewriteDefault = static::getRewriteHashLinksDefault(); static::setRewriteHashLinksDefault($rewrite); - SSViewer::$topLevel[] = $item; - - $template = $this->chosen; - - $cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . '.cache' - . str_replace(['\\','/',':'], '.', Director::makeRelative(realpath($template ?? '')) ?? ''); - $lastEdited = filemtime($template ?? ''); - - if (!file_exists($cacheFile ?? '') || filemtime($cacheFile ?? '') < $lastEdited) { - $content = file_get_contents($template ?? ''); - $content = $this->parseTemplateContent($content, $template); - - $fh = fopen($cacheFile ?? '', 'w'); - fwrite($fh, $content ?? ''); - fclose($fh); - } - - $underlay = ['I18NNamespace' => basename($template ?? '')]; - - // Makes the rendered sub-templates available on the parent item, - // through $Content and $Layout placeholders. - foreach (['Content', 'Layout'] as $subtemplate) { - // Detect sub-template to use - $sub = $this->getSubtemplateFor($subtemplate); - if (!$sub) { - continue; - } - - // Create lazy-evaluated underlay for this subtemplate - $underlay[$subtemplate] = function () use ($item, $arguments, $sub) { - $subtemplateViewer = clone $this; - // Disable requirements - this will be handled by the parent template - $subtemplateViewer->includeRequirements(false); - // Select the right template - $subtemplateViewer->setTemplate($sub); - - // Render if available - if ($subtemplateViewer->exists()) { - return $subtemplateViewer->process($item, $arguments); - } - return null; - }; - } - - $output = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, $underlay, $inheritedScope); + // Actually render the template + $output = $this->getTemplateEngine()->render($item, $overlay); if ($this->includeRequirements) { $output = Requirements::includeInHTML($output); } - array_pop(SSViewer::$topLevel); - // If we have our crazy base tag, then fix # links referencing the current page. if ($rewrite) { if (strpos($output ?? '', 'subTemplates[$subtemplate])) { - return $this->subTemplates[$subtemplate]; - } - - // Don't apply sub-templates if type is already specified (e.g. 'Includes') - if (isset($this->templates['type'])) { - return null; - } - - // Filter out any other typed templates as we can only add, not change type - $templates = array_filter( - (array)$this->templates, - function ($template) { - return !isset($template['type']); - } - ); - if (empty($templates)) { - return null; - } - - // Set type to subtemplate - $templates['type'] = $subtemplate; - return $templates; - } - - /** - * Execute the given template, passing it the given data. - * Used by the <% include %> template tag to process templates. - * - * @param string $template Template name - * @param mixed $data Data context - * @param array $arguments Additional arguments - * @param Object $scope - * @param bool $globalRequirements - * - * @return string Evaluated result - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::execute_template() - */ - public static function execute_template($template, $data, $arguments = null, $scope = null, $globalRequirements = false) - { - Deprecation::noticeWithNoReplacment( - '5.4.0', - 'Will be replaced with SilverStripe\View\SSTemplateEngine::execute_template()' - ); - $v = SSViewer::create($template); - - if ($globalRequirements) { - $v->includeRequirements(false); - } else { - //nest a requirements backend for our template rendering - $origBackend = Requirements::backend(); - Requirements::set_backend(Requirements_Backend::create()); - } - try { - return $v->process($data, $arguments, $scope); - } finally { - if (!$globalRequirements) { - Requirements::set_backend($origBackend); - } - } - } - - /** - * Execute the evaluated string, passing it the given data. - * Used by partial caching to evaluate custom cache keys expressed using - * template expressions - * - * @param string $content Input string - * @param mixed $data Data context - * @param array $arguments Additional arguments - * @param bool $globalRequirements - * - * @return string Evaluated result - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::renderString() - */ - public static function execute_string($content, $data, $arguments = null, $globalRequirements = false) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::renderString()'); - $v = SSViewer::fromString($content); - - if ($globalRequirements) { - $v->includeRequirements(false); - } else { - //nest a requirements backend for our template rendering - $origBackend = Requirements::backend(); - Requirements::set_backend(Requirements_Backend::create()); - } - try { - return $v->process($data, $arguments); - } finally { - if (!$globalRequirements) { - Requirements::set_backend($origBackend); - } - } - } - - /** - * Parse given template contents - * - * @param string $content The template contents - * @param string $template The template file name - * @return string - * @deprecated 5.4.0 Will be replaced with SilverStripe\View\SSTemplateEngine::parseTemplateContent() - */ - public function parseTemplateContent($content, $template = "") - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with SilverStripe\View\SSTemplateEngine::parseTemplateContent()'); - return $this->getParser()->compileString( - $content, - $template, - Director::isDev() && SSViewer::config()->uninherited('source_file_comments') - ); - } - - /** - * Returns the filenames of the template that will be rendered. It is a map that may contain - * 'Content' & 'Layout', and will have to contain 'main' - * - * @return array - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it - */ - public function templates() - { - Deprecation::noticeWithNoReplacment('5.4.0'); - return array_merge(['main' => $this->chosen], $this->subTemplates); - } - - /** - * @param string $type "Layout" or "main" - * @param string $file Full system path to the template file - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it - */ - public function setTemplateFile($type, $file) - { - Deprecation::noticeWithNoReplacment('5.4.0'); - if (!$type || $type == 'main') { - $this->chosen = $file; - } else { - $this->subTemplates[$type] = $file; - } - } - - /** - * Return an appropriate base tag for the given template. - * It will be closed on an XHTML document, and unclosed on an HTML document. - * - * @param string $contentGeneratedSoFar The content of the template generated so far; it should contain - * the DOCTYPE declaration. - * @return string - * @deprecated 5.4.0 Use getBaseTag() instead - */ - public static function get_base_tag($contentGeneratedSoFar) - { - Deprecation::notice('5.4.0', 'Use getBaseTag() instead'); - // Is the document XHTML? - $isXhtml = preg_match('/]+xhtml/i', $contentGeneratedSoFar ?? ''); - return static::getBaseTag($isXhtml); - } - /** * Return an appropriate base tag for the given template. * It will be closed on an XHTML document, and unclosed on an HTML document. @@ -900,9 +369,20 @@ public static function getBaseTag(bool $isXhtml = false): string { // Base href should always have a trailing slash $base = rtrim(Director::absoluteBaseURL(), '/') . '/'; + if ($isXhtml) { return ""; } - return ""; + return ""; + } + + /** + * Get the engine used to render templates for this viewer. + * Note that this is intentionally not public to avoid the engine being set after instantiation. + */ + protected function setTemplateEngine(TemplateEngine $engine): static + { + $this->templateEngine = $engine; + return $this; } } diff --git a/src/View/SSViewer_DataPresenter.php b/src/View/SSViewer_DataPresenter.php deleted file mode 100644 index 7bd55f94862..00000000000 --- a/src/View/SSViewer_DataPresenter.php +++ /dev/null @@ -1,451 +0,0 @@ -overlay = $overlay ?: []; - $this->underlay = $underlay ?: []; - - $this->cacheGlobalProperties(); - $this->cacheIteratorProperties(); - } - - /** - * Build cache of global properties - */ - protected function cacheGlobalProperties() - { - if (SSViewer_DataPresenter::$globalProperties !== null) { - return; - } - - SSViewer_DataPresenter::$globalProperties = $this->getPropertiesFromProvider( - TemplateGlobalProvider::class, - 'get_template_global_variables' - ); - } - - /** - * Build cache of global iterator properties - */ - protected function cacheIteratorProperties() - { - if (SSViewer_DataPresenter::$iteratorProperties !== null) { - return; - } - - SSViewer_DataPresenter::$iteratorProperties = $this->getPropertiesFromProvider( - TemplateIteratorProvider::class, - 'get_template_iterator_variables', - true // Call non-statically - ); - } - - /** - * @var string $interfaceToQuery - * @var string $variableMethod - * @var boolean $createObject - * @return array - */ - protected function getPropertiesFromProvider($interfaceToQuery, $variableMethod, $createObject = false) - { - $methods = []; - - $implementors = ClassInfo::implementorsOf($interfaceToQuery); - if ($implementors) { - foreach ($implementors as $implementor) { - // Create a new instance of the object for method calls - if ($createObject) { - $implementor = new $implementor(); - $exposedVariables = $implementor->$variableMethod(); - } else { - $exposedVariables = $implementor::$variableMethod(); - } - - foreach ($exposedVariables as $varName => $details) { - if (!is_array($details)) { - $details = [ - 'method' => $details, - 'casting' => ModelData::config()->uninherited('default_cast') - ]; - } - - // If just a value (and not a key => value pair), use method name for both key and value - if (is_numeric($varName)) { - $varName = $details['method']; - } - - // Add in a reference to the implementing class (might be a string class name or an instance) - $details['implementor'] = $implementor; - - // And a callable array - if (isset($details['method'])) { - $details['callable'] = [$implementor, $details['method']]; - } - - // Save with both uppercase & lowercase first letter, so either works - $lcFirst = strtolower($varName[0] ?? '') . substr($varName ?? '', 1); - $result[$lcFirst] = $details; - $result[ucfirst($varName)] = $details; - } - } - } - - return $result; - } - - /** - * Look up injected value - it may be part of an "overlay" (arguments passed to <% include %>), - * set on the current item, part of an "underlay" ($Layout or $Content), or an iterator/global property - * - * @param string $property Name of property - * @param array $params - * @param bool $cast If true, an object is always returned even if not an object. - * @return array|null - */ - public function getInjectedValue($property, array $params, $cast = true) - { - // Get source for this value - $result = $this->getValueSource($property); - if (!array_key_exists('source', $result)) { - return null; - } - - // Look up the value - either from a callable, or from a directly provided value - $source = $result['source']; - $res = []; - if (isset($source['callable'])) { - $res['value'] = $source['callable'](...$params); - } elseif (array_key_exists('value', $source)) { - $res['value'] = $source['value']; - } else { - throw new InvalidArgumentException( - "Injected property $property doesn't have a value or callable value source provided" - ); - } - - // If we want to provide a casted object, look up what type object to use - if ($cast) { - $res['obj'] = $this->castValue($res['value'], $source); - } - - return $res; - } - - /** - * Store the current overlay (as it doesn't directly apply to the new scope - * that's being pushed). We want to store the overlay against the next item - * "up" in the stack (hence upIndex), rather than the current item, because - * SSViewer_Scope::obj() has already been called and pushed the new item to - * the stack by this point - * - * @return SSViewer_Scope - */ - public function pushScope() - { - $scope = parent::pushScope(); - $upIndex = $this->getUpIndex() ?: 0; - - $itemStack = $this->getItemStack(); - $itemStack[$upIndex][SSViewer_Scope::ITEM_OVERLAY] = $this->overlay; - $this->setItemStack($itemStack); - - // Remove the overlay when we're changing to a new scope, as values in - // that scope take priority. The exceptions that set this flag are $Up - // and $Top as they require that the new scope inherits the overlay - if (!$this->preserveOverlay) { - $this->overlay = []; - } - - return $scope; - } - - /** - * Now that we're going to jump up an item in the item stack, we need to - * restore the overlay that was previously stored against the next item "up" - * in the stack from the current one - * - * @return SSViewer_Scope - */ - public function popScope() - { - $upIndex = $this->getUpIndex(); - - if ($upIndex !== null) { - $itemStack = $this->getItemStack(); - $this->overlay = $itemStack[$upIndex][SSViewer_Scope::ITEM_OVERLAY]; - } - - return parent::popScope(); - } - - /** - * $Up and $Top need to restore the overlay from the parent and top-level - * scope respectively. - * - * @param string $name - * @param array $arguments - * @param bool $cache - * @param string $cacheName - * @return $this - */ - public function obj($name, $arguments = [], $cache = false, $cacheName = null) - { - $overlayIndex = false; - - switch ($name) { - case 'Up': - $upIndex = $this->getUpIndex(); - if ($upIndex === null) { - throw new \LogicException('Up called when we\'re already at the top of the scope'); - } - $overlayIndex = $upIndex; // Parent scope - $this->preserveOverlay = true; // Preserve overlay - break; - case 'Top': - $overlayIndex = 0; // Top-level scope - $this->preserveOverlay = true; // Preserve overlay - break; - default: - $this->preserveOverlay = false; - break; - } - - if ($overlayIndex !== false) { - $itemStack = $this->getItemStack(); - if (!$this->overlay && isset($itemStack[$overlayIndex][SSViewer_Scope::ITEM_OVERLAY])) { - $this->overlay = $itemStack[$overlayIndex][SSViewer_Scope::ITEM_OVERLAY]; - } - } - - parent::obj($name, $arguments, $cache, $cacheName); - return $this; - } - - /** - * {@inheritdoc} - */ - public function getObj($name, $arguments = [], $cache = false, $cacheName = null) - { - $result = $this->getInjectedValue($name, (array)$arguments); - if ($result) { - return $result['obj']; - } - return parent::getObj($name, $arguments, $cache, $cacheName); - } - - /** - * {@inheritdoc} - */ - public function __call($name, $arguments) - { - // Extract the method name and parameters - $property = $arguments[0]; // The name of the public function being called - - // The public function parameters in an array - $params = (isset($arguments[1])) ? (array)$arguments[1] : []; - - $val = $this->getInjectedValue($property, $params); - if ($val) { - $obj = $val['obj']; - if ($name === 'hasValue') { - $result = ($obj instanceof ModelData) ? $obj->exists() : (bool)$obj; - } elseif (is_null($obj) || (is_scalar($obj) && !is_string($obj))) { - $result = $obj; // Nulls and non-string scalars don't need casting - } else { - $result = $obj->forTemplate(); // XML_val - } - - $this->resetLocalScope(); - return $result; - } - - return parent::__call($name, $arguments); - } - - /** - * Evaluate a template override. Returns an array where the presence of - * a 'value' key indiciates whether an override was successfully found, - * as null is a valid override value - * - * @param string $property Name of override requested - * @param array $overrides List of overrides available - * @return array An array with a 'value' key if a value has been found, or empty if not - */ - protected function processTemplateOverride($property, $overrides) - { - if (!array_key_exists($property, $overrides)) { - return []; - } - - // Detect override type - $override = $overrides[$property]; - - // Late-evaluate this value - if (!is_string($override) && is_callable($override)) { - $override = $override(); - - // Late override may yet return null - if (!isset($override)) { - return []; - } - } - - return ['value' => $override]; - } - - /** - * Determine source to use for getInjectedValue. Returns an array where the presence of - * a 'source' key indiciates whether a value source was successfully found, as a source - * may be a null value returned from an override - * - * @param string $property - * @return array An array with a 'source' key if a value source has been found, or empty if not - */ - protected function getValueSource($property) - { - // Check for a presenter-specific override - $result = $this->processTemplateOverride($property, $this->overlay); - if (array_key_exists('value', $result)) { - return ['source' => $result]; - } - - // Check if the method to-be-called exists on the target object - if so, don't check any further - // injection locations - $on = $this->getItem(); - if (is_object($on) && (isset($on->$property) || method_exists($on, $property ?? ''))) { - return []; - } - - // Check for a presenter-specific override - $result = $this->processTemplateOverride($property, $this->underlay); - if (array_key_exists('value', $result)) { - return ['source' => $result]; - } - - // Then for iterator-specific overrides - if (array_key_exists($property, SSViewer_DataPresenter::$iteratorProperties)) { - $source = SSViewer_DataPresenter::$iteratorProperties[$property]; - /** @var TemplateIteratorProvider $implementor */ - $implementor = $source['implementor']; - if ($this->itemIterator) { - // Set the current iterator position and total (the object instance is the first item in - // the callable array) - $implementor->iteratorProperties( - $this->itemIterator->key(), - $this->itemIteratorTotal - ); - } else { - // If we don't actually have an iterator at the moment, act like a list of length 1 - $implementor->iteratorProperties(0, 1); - } - - return ($source) ? ['source' => $source] : []; - } - - // And finally for global overrides - if (array_key_exists($property, SSViewer_DataPresenter::$globalProperties)) { - return [ - 'source' => SSViewer_DataPresenter::$globalProperties[$property] // get the method call - ]; - } - - // No value - return []; - } - - /** - * Ensure the value is cast safely - * - * @param mixed $value - * @param array $source - * @return DBField - */ - protected function castValue($value, $source) - { - // If the value has already been cast, is null, or is a non-string scalar - if (is_object($value) || is_null($value) || (is_scalar($value) && !is_string($value))) { - return $value; - } - - // Wrap list arrays in ModelData so templates can handle them - if (is_array($value) && array_is_list($value)) { - return ArrayList::create($value); - } - - // Get provided or default cast - $casting = empty($source['casting']) - ? ModelData::config()->uninherited('default_cast') - : $source['casting']; - - return DBField::create_field($casting, $value); - } -} diff --git a/src/View/SSViewer_FromString.php b/src/View/SSViewer_FromString.php deleted file mode 100644 index b9e421f9c7f..00000000000 --- a/src/View/SSViewer_FromString.php +++ /dev/null @@ -1,104 +0,0 @@ -setParser($parser); - } - - $this->content = $content; - } - - /** - * {@inheritdoc} - */ - public function process($item, $arguments = null, $scope = null) - { - $hash = sha1($this->content ?? ''); - $cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . ".cache.$hash"; - - if (!file_exists($cacheFile ?? '') || Injector::inst()->get(Kernel::class)->isFlushed()) { - $content = $this->parseTemplateContent($this->content, "string sha1=$hash"); - $fh = fopen($cacheFile ?? '', 'w'); - fwrite($fh, $content ?? ''); - fclose($fh); - } - - $val = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, null, $scope); - - if ($this->cacheTemplate !== null) { - $cacheTemplate = $this->cacheTemplate; - } else { - $cacheTemplate = static::config()->get('cache_template'); - } - - if (!$cacheTemplate) { - unlink($cacheFile ?? ''); - } - - $html = DBField::create_field('HTMLFragment', $val); - - return $html; - } - - /** - * @param boolean $cacheTemplate - */ - public function setCacheTemplate($cacheTemplate) - { - $this->cacheTemplate = (bool)$cacheTemplate; - } - - /** - * @return boolean - */ - public function getCacheTemplate() - { - return $this->cacheTemplate; - } -} diff --git a/src/View/SSViewer_Scope.php b/src/View/SSViewer_Scope.php index 915697be0a7..5f7836c8c68 100644 --- a/src/View/SSViewer_Scope.php +++ b/src/View/SSViewer_Scope.php @@ -2,16 +2,11 @@ namespace SilverStripe\View; -use ArrayIterator; -use Countable; +use InvalidArgumentException; use Iterator; -use SilverStripe\Model\List\ArrayList; -use SilverStripe\Dev\Deprecation; -use SilverStripe\ORM\FieldType\DBBoolean; -use SilverStripe\ORM\FieldType\DBText; -use SilverStripe\ORM\FieldType\DBFloat; -use SilverStripe\ORM\FieldType\DBInt; -use SilverStripe\ORM\FieldType\DBField; +use LogicException; +use SilverStripe\Core\ClassInfo; +use SilverStripe\Core\Injector\Injector; /** * This tracks the current scope for an SSViewer instance. It has three goals: @@ -19,6 +14,10 @@ * - Track Up and Top * - (As a side effect) Inject data that needs to be available globally (used to live in ModelData) * + * It is also responsible for mixing in data on top of what the item provides. This can be "global" + * data that is scope-independant (like BaseURL), or type-specific data that is layered on top cross-cut like + * (like $FirstLast etc). + * * In order to handle up, rather than tracking it using a tree, which would involve constructing new objects * for each step, we use indexes into the itemStack (which already has to exist). * @@ -45,107 +44,107 @@ class SSViewer_Scope /** * The stack of previous items ("scopes") - an indexed array of: item, item iterator, item iterator total, * pop index, up index, current index & parent overlay - * - * @var array */ - private $itemStack = []; + private array $itemStack = []; /** * The current "global" item (the one any lookup starts from) - * - * @var object */ - protected $item; + protected ?ViewLayerData $item; /** * If we're looping over the current "global" item, here's the iterator that tracks with item we're up to - * - * @var Iterator */ - protected $itemIterator; + protected ?Iterator $itemIterator; /** * Total number of items in the iterator - * - * @var int */ - protected $itemIteratorTotal; + protected int $itemIteratorTotal; /** * A pointer into the item stack for the item that will become the active scope on the next pop call - * - * @var int */ - private $popIndex; + private ?int $popIndex; /** * A pointer into the item stack for which item is "up" from this one - * - * @var int */ - private $upIndex; + private ?int $upIndex; /** * A pointer into the item stack for which the active item (or null if not in stack yet) - * - * @var int */ - private $currentIndex; + private int $currentIndex; /** * A store of copies of the main item stack, so it's preserved during a lookup from local scope * (which may push/pop items to/from the main item stack) - * - * @var array */ - private $localStack = []; + private array $localStack = []; /** * The index of the current item in the main item stack, so we know where to restore the scope * stored in $localStack. + */ + private int $localIndex = 0; + + /** + * List of global property providers * - * @var int + * @internal + * @var TemplateGlobalProvider[]|null */ - private $localIndex = 0; + private static $globalProperties = null; /** - * @var object $item - * @var SSViewer_Scope $inheritedScope + * List of global iterator providers + * + * @internal + * @var TemplateIteratorProvider[]|null */ - public function __construct($item, SSViewer_Scope $inheritedScope = null) - { + private static $iteratorProperties = null; + + /** + * Overlay variables. Take precedence over anything from the current scope + */ + protected array $overlay; + + /** + * Flag for whether overlay should be preserved when pushing a new scope + */ + protected bool $preserveOverlay = false; + + /** + * Underlay variables. Concede precedence to overlay variables or anything from the current scope + */ + protected array $underlay; + + public function __construct( + ?ViewLayerData $item, + array $overlay = [], + array $underlay = [], + ?SSViewer_Scope $inheritedScope = null + ) { $this->item = $item; $this->itemIterator = ($inheritedScope) ? $inheritedScope->itemIterator : null; $this->itemIteratorTotal = ($inheritedScope) ? $inheritedScope->itemIteratorTotal : 0; $this->itemStack[] = [$this->item, $this->itemIterator, $this->itemIteratorTotal, null, null, 0]; - } - /** - * Returns the current "active" item - * - * @return object - * @deprecated 5.4.0 use getCurrentItem() instead. - */ - public function getItem() - { - Deprecation::notice('5.4.0', 'use getCurrentItem() instead.'); - $item = $this->itemIterator ? $this->itemIterator->current() : $this->item; - if (is_scalar($item)) { - $item = $this->convertScalarToDBField($item); - } - - // Wrap list arrays in ModelData so templates can handle them - if (is_array($item) && array_is_list($item)) { - $item = ArrayList::create($item); - } + $this->overlay = $overlay; + $this->underlay = $underlay; - return $item; + $this->cacheGlobalProperties(); + $this->cacheIteratorProperties(); } - public function getCurrentItem() + /** + * Returns the current "current" item in scope + */ + public function getCurrentItem(): ?ViewLayerData { - return $this->getItem(); + return $this->itemIterator ? $this->itemIterator->current() : $this->item; } /** @@ -172,58 +171,21 @@ public function locally() } /** - * Reset the local scope - restores saved state to the "global" item stack. Typically called after - * a lookup chain has been completed + * Set scope to an intermediate value, which will be used for getting output later on. */ - public function resetLocalScope() + public function scopeToIntermediateValue(string $name, array $arguments): static { - // Restore previous un-completed lookup chain if set - $previousLocalState = $this->localStack ? array_pop($this->localStack) : null; - array_splice($this->itemStack, $this->localIndex + 1, count($this->itemStack ?? []), $previousLocalState); + $overlayIndex = false; - list( - $this->item, - $this->itemIterator, - $this->itemIteratorTotal, - $this->popIndex, - $this->upIndex, - $this->currentIndex - ) = end($this->itemStack); - } - - /** - * @param string $name - * @param array $arguments - * @param bool $cache - * @param string $cacheName - * @return mixed - */ - public function getObj($name, $arguments = [], $cache = false, $cacheName = null) - { - $on = $this->getCurrentItem(); - if ($on === null) { - return null; - } - return $on->obj($name, $arguments, $cache, $cacheName); - } - - /** - * @param string $name - * @param array $arguments - * @param bool $cache - * @param string $cacheName - * @return $this - * @deprecated 5.4.0 Will be renamed scopeToIntermediateValue() - */ - public function obj($name, $arguments = [], $cache = false, $cacheName = null) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be renamed scopeToIntermediateValue()'); + // $Up and $Top need to restore the overlay from the parent and top-level scope respectively. switch ($name) { case 'Up': - if ($this->upIndex === null) { + $upIndex = $this->getUpIndex(); + if ($upIndex === null) { throw new \LogicException('Up called when we\'re already at the top of the scope'); } - + $overlayIndex = $upIndex; // Parent scope + $this->preserveOverlay = true; // Preserve overlay list( $this->item, $this->itemIterator, @@ -234,6 +196,8 @@ public function obj($name, $arguments = [], $cache = false, $cacheName = null) ) = $this->itemStack[$this->upIndex]; break; case 'Top': + $overlayIndex = 0; // Top-level scope + $this->preserveOverlay = true; // Preserve overlay list( $this->item, $this->itemIterator, @@ -244,13 +208,21 @@ public function obj($name, $arguments = [], $cache = false, $cacheName = null) ) = $this->itemStack[0]; break; default: - $this->item = $this->getObj($name, $arguments, $cache, $cacheName); + $this->preserveOverlay = false; + $this->item = $this->getObj($name, $arguments); $this->itemIterator = null; $this->upIndex = $this->currentIndex ? $this->currentIndex : count($this->itemStack) - 1; $this->currentIndex = count($this->itemStack); break; } + if ($overlayIndex !== false) { + $itemStack = $this->getItemStack(); + if (!$this->overlay && isset($itemStack[$overlayIndex][SSViewer_Scope::ITEM_OVERLAY])) { + $this->overlay = $itemStack[$overlayIndex][SSViewer_Scope::ITEM_OVERLAY]; + } + } + $this->itemStack[] = [ $this->item, $this->itemIterator, @@ -264,10 +236,8 @@ public function obj($name, $arguments = [], $cache = false, $cacheName = null) /** * Gets the current object and resets the scope. - * - * @return object */ - public function self() + public function self(): ?ViewLayerData { $result = $this->getCurrentItem(); $this->resetLocalScope(); @@ -278,9 +248,13 @@ public function self() /** * Jump to the last item in the stack, called when a new item is added before a loop/with * - * @return SSViewer_Scope + * Store the current overlay (as it doesn't directly apply to the new scope + * that's being pushed). We want to store the overlay against the next item + * "up" in the stack (hence upIndex), rather than the current item, because + * SSViewer_Scope::obj() has already been called and pushed the new item to + * the stack by this point */ - public function pushScope() + public function pushScope(): static { $newLocalIndex = count($this->itemStack ?? []) - 1; @@ -294,16 +268,38 @@ public function pushScope() // once we enter a new global scope, we need to make sure we use a new one $this->itemIterator = $this->itemStack[$newLocalIndex][SSViewer_Scope::ITEM_ITERATOR] = null; + $upIndex = $this->getUpIndex() ?: 0; + + $itemStack = $this->getItemStack(); + $itemStack[$upIndex][SSViewer_Scope::ITEM_OVERLAY] = $this->overlay; + $this->setItemStack($itemStack); + + // Remove the overlay when we're changing to a new scope, as values in + // that scope take priority. The exceptions that set this flag are $Up + // and $Top as they require that the new scope inherits the overlay + if (!$this->preserveOverlay) { + $this->overlay = []; + } + return $this; } /** * Jump back to "previous" item in the stack, called after a loop/with block * - * @return SSViewer_Scope + * Now that we're going to jump up an item in the item stack, we need to + * restore the overlay that was previously stored against the next item "up" + * in the stack from the current one */ - public function popScope() + public function popScope(): static { + $upIndex = $this->getUpIndex(); + + if ($upIndex !== null) { + $itemStack = $this->getItemStack(); + $this->overlay = $itemStack[$upIndex][SSViewer_Scope::ITEM_OVERLAY]; + } + $this->localIndex = $this->popIndex; $this->resetLocalScope(); @@ -311,11 +307,10 @@ public function popScope() } /** - * Fast-forwards the current iterator to the next item - * - * @return mixed + * Fast-forwards the current iterator to the next item. + * @return bool True if there's an item, false if not. */ - public function next() + public function next(): bool { if (!$this->item) { return false; @@ -323,29 +318,18 @@ public function next() if (!$this->itemIterator) { // Note: it is important that getIterator() is called before count() as implemenations may rely on - // this to efficiency get both the number of records and an iterator (e.g. DataList does this) - - // Item may be an array or a regular IteratorAggregate - if (is_array($this->item)) { - $this->itemIterator = new ArrayIterator($this->item); - } elseif ($this->item instanceof Iterator) { - $this->itemIterator = $this->item; - } else { - $this->itemIterator = $this->item->getIterator(); + // this to efficiently get both the number of records and an iterator (e.g. DataList does this) + $this->itemIterator = $this->item->getIterator(); - // This will execute code in a generator up to the first yield. For example, this ensures that - // DataList::getIterator() is called before Datalist::count() - $this->itemIterator->rewind(); - } + // This will execute code in a generator up to the first yield. For example, this ensures that + // DataList::getIterator() is called before Datalist::count() which means we only run the query once + // instead of running a separate explicit count() query + $this->itemIterator->rewind(); - // If the item implements Countable, use that to fetch the count, otherwise we have to inspect the - // iterator and then rewind it. - if ($this->item instanceof Countable) { - $this->itemIteratorTotal = count($this->item); - } else { - $this->itemIteratorTotal = iterator_count($this->itemIterator); - $this->itemIterator->rewind(); - } + // Get the number of items in the iterator. + // Don't just use iterator_count because that results in running through the list + // which causes some iterators to no longer be iterable for some reason + $this->itemIteratorTotal = $this->item->getIteratorCount(); $this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR] = $this->itemIterator; $this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR_TOTAL] = $this->itemIteratorTotal; @@ -359,27 +343,90 @@ public function next() return false; } - return $this->itemIterator->key(); + return true; } /** - * @param string $name - * @param array $arguments - * @return mixed + * Get the value that will be directly rendered in the template. */ - public function __call($name, $arguments) + public function getOutputValue(string $name, array $arguments): string { - $on = $this->getCurrentItem(); - if ($on instanceof ViewableData && $name === 'XML_val') { - $retval = $on->XML_val(...$arguments); + $retval = $this->getObj($name, $arguments); + $this->resetLocalScope(); + return $retval === null ? '' : $retval->__toString(); + } + + /** + * Get the value to pass as an argument to a method. + */ + public function getValueAsArgument(string $name, array $arguments): mixed + { + $retval = null; + + if ($this->hasOverlay($name)) { + $retval = $this->getOverlay($name, $arguments, true); } else { - $retval = $on ? $on->$name(...$arguments) : null; + $on = $this->getCurrentItem(); + if ($on && isset($on->$name)) { + $retval = $on->getRawDataValue($name, $arguments); + } + + if ($retval === null) { + $retval = $this->getUnderlay($name, $arguments, true); + } } $this->resetLocalScope(); return $retval; } + /** + * Check if the current item in scope has a value for the named field. + */ + public function hasValue(string $name, array $arguments): bool + { + $retval = null; + $overlay = $this->getOverlay($name, $arguments); + if ($overlay && $overlay->hasDataValue()) { + $retval = true; + } + + if ($retval === null) { + $on = $this->getCurrentItem(); + if ($on) { + $retval = $on->hasDataValue($name, $arguments); + } + } + + if (!$retval) { + $underlay = $this->getUnderlay($name, $arguments); + $retval = $underlay && $underlay->hasDataValue(); + } + + $this->resetLocalScope(); + return $retval; + } + + /** + * Reset the local scope - restores saved state to the "global" item stack. Typically called after + * a lookup chain has been completed + */ + protected function resetLocalScope() + { + // Restore previous un-completed lookup chain if set + $previousLocalState = $this->localStack ? array_pop($this->localStack) : null; + array_splice($this->itemStack, $this->localIndex + 1, count($this->itemStack ?? []), $previousLocalState); + + list( + $this->item, + $this->itemIterator, + $this->itemIteratorTotal, + $this->popIndex, + $this->upIndex, + $this->currentIndex + ) = end($this->itemStack); + } + /** * @return array */ @@ -404,13 +451,170 @@ protected function getUpIndex() return $this->upIndex; } - private function convertScalarToDBField(bool|string|float|int $value): DBField + /** + * Evaluate a template override. Returns an array where the presence of + * a 'value' key indiciates whether an override was successfully found, + * as null is a valid override value + * + * @param string $property Name of override requested + * @param array $overrides List of overrides available + * @return array An array with a 'value' key if a value has been found, or empty if not + */ + protected function processTemplateOverride($property, $overrides) + { + if (!array_key_exists($property, $overrides)) { + return []; + } + + // Detect override type + $override = $overrides[$property]; + + // Late-evaluate this value + if (!is_string($override) && is_callable($override)) { + $override = $override(); + + // Late override may yet return null + if (!isset($override)) { + return []; + } + } + + return ['value' => $override]; + } + + /** + * Build cache of global properties + */ + protected function cacheGlobalProperties() + { + if (SSViewer_Scope::$globalProperties !== null) { + return; + } + + SSViewer_Scope::$globalProperties = SSViewer::getMethodsFromProvider( + TemplateGlobalProvider::class, + 'get_template_global_variables' + ); + } + + /** + * Build cache of global iterator properties + */ + protected function cacheIteratorProperties() + { + if (SSViewer_Scope::$iteratorProperties !== null) { + return; + } + + SSViewer_Scope::$iteratorProperties = SSViewer::getMethodsFromProvider( + TemplateIteratorProvider::class, + 'get_template_iterator_variables', + true // Call non-statically + ); + } + + protected function getObj(string $name, array $arguments): ?ViewLayerData + { + if ($this->hasOverlay($name)) { + return $this->getOverlay($name, $arguments); + } + + $on = $this->getCurrentItem(); + if ($on && isset($on->$name)) { + return $on->$name(...$arguments); + } + + return $this->getUnderlay($name, $arguments); + } + + protected function hasOverlay(string $property): bool + { + $result = $this->processTemplateOverride($property, $this->overlay); + return array_key_exists('value', $result); + } + + protected function getOverlay(string $property, array $args, bool $getRaw = false): mixed + { + $result = $this->processTemplateOverride($property, $this->overlay); + if (array_key_exists('value', $result)) { + return $this->getInjectedValue($result, $property, $args, $getRaw); + } + return null; + } + + protected function getUnderlay(string $property, array $args, bool $getRaw = false): mixed { - return match (gettype($value)) { - 'boolean' => DBBoolean::create()->setValue($value), - 'string' => DBText::create()->setValue($value), - 'double' => DBFloat::create()->setValue($value), - 'integer' => DBInt::create()->setValue($value), - }; + // Check for a presenter-specific override + $result = $this->processTemplateOverride($property, $this->underlay); + if (array_key_exists('value', $result)) { + return $this->getInjectedValue($result, $property, $args, $getRaw); + } + + // Then for iterator-specific overrides + if (array_key_exists($property, SSViewer_Scope::$iteratorProperties)) { + $source = SSViewer_Scope::$iteratorProperties[$property]; + /** @var TemplateIteratorProvider $implementor */ + $implementor = $source['implementor']; + if ($this->itemIterator) { + // Set the current iterator position and total (the object instance is the first item in + // the callable array) + $implementor->iteratorProperties( + $this->itemIterator->key(), + $this->itemIteratorTotal + ); + } else { + // If we don't actually have an iterator at the moment, act like a list of length 1 + $implementor->iteratorProperties(0, 1); + } + + return $this->getInjectedValue($source, $property, $args, $getRaw); + } + + // And finally for global overrides + if (array_key_exists($property, SSViewer_Scope::$globalProperties)) { + return $this->getInjectedValue( + SSViewer_Scope::$globalProperties[$property], + $property, + $args, + $getRaw + ); + } + + return null; + } + + protected function getInjectedValue( + array|TemplateGlobalProvider|TemplateIteratorProvider $source, + string $property, + array $params, + bool $getRaw = false + ) { + // Look up the value - either from a callable, or from a directly provided value + $value = null; + if (isset($source['callable'])) { + $value = $source['callable'](...$params); + } elseif (array_key_exists('value', $source)) { + $value = $source['value']; + } else { + throw new InvalidArgumentException( + "Injected property $property doesn't have a value or callable value source provided" + ); + } + + if ($value === null) { + return null; + } + + // TemplateGlobalProviders can provide an explicit service to cast to which works outside of the regular cast flow + if (!$getRaw && isset($source['casting'])) { + $castObject = Injector::inst()->create($source['casting'], $property); + if (!ClassInfo::hasMethod($castObject, 'setValue')) { + throw new LogicException('Explicit cast from template global provider must have a setValue method.'); + } + $castObject->setValue($value); + $value = $castObject; + } + + return $getRaw ? $value : ViewLayerData::create($value); } } diff --git a/src/View/TemplateEngine.php b/src/View/TemplateEngine.php new file mode 100644 index 00000000000..785130a1148 --- /dev/null +++ b/src/View/TemplateEngine.php @@ -0,0 +1,61 @@ +base; + } + /** * Given a theme identifier, determine the path from the root directory * @@ -162,101 +170,6 @@ public function getPath($identifier) return Path::normalise($modulePath . $subpath, true); } - /** - * Attempts to find possible candidate templates from a set of template - * names from modules, current theme directory and finally the application - * folder. - * - * The template names can be passed in as plain strings, or be in the - * format "type/name", where type is the type of template to search for - * (e.g. Includes, Layout). - * - * The results of this method will be cached for future use. - * - * @param string|array $template Template name, or template spec in array format with the keys - * 'type' (type string) and 'templates' (template hierarchy in order of precedence). - * If 'templates' is omitted then any other item in the array will be treated as the template - * list, or list of templates each in the array spec given. - * Templates with an .ss extension will be treated as file paths, and will bypass - * theme-coupled resolution. - * @param array $themes List of themes to use to resolve themes. Defaults to {@see SSViewer::get_themes()} - * @return string Absolute path to resolved template file, or null if not resolved. - * File location will be in the format themes//templates///.ss - * Note that type (e.g. 'Layout') is not the root level directory under 'templates'. - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it. - */ - public function findTemplate($template, $themes = null) - { - Deprecation::noticeWithNoReplacment('5.4.0', 'Will be removed without equivalent functionality to replace it.'); - if ($themes === null) { - $themes = SSViewer::get_themes(); - } - - // Look for a cached result for this data set - $cacheKey = md5(json_encode($template) . json_encode($themes)); - if ($this->getCache()->has($cacheKey)) { - return $this->getCache()->get($cacheKey); - } - - $type = ''; - if (is_array($template)) { - // Check if templates has type specified - if (array_key_exists('type', $template ?? [])) { - $type = $template['type']; - unset($template['type']); - } - // Templates are either nested in 'templates' or just the rest of the list - $templateList = array_key_exists('templates', $template ?? []) ? $template['templates'] : $template; - } else { - $templateList = [$template]; - } - - foreach ($templateList as $i => $template) { - // Check if passed list of templates in array format - if (is_array($template)) { - $path = $this->findTemplate($template, $themes); - if ($path) { - $this->getCache()->set($cacheKey, $path); - return $path; - } - continue; - } - - // If we have an .ss extension, this is a path, not a template name. We should - // pass in templates without extensions in order for template manifest to find - // files dynamically. - if (substr($template ?? '', -3) == '.ss' && file_exists($template ?? '')) { - $this->getCache()->set($cacheKey, $template); - return $template; - } - - // Check string template identifier - $template = str_replace('\\', '/', $template ?? ''); - $parts = explode('/', $template ?? ''); - - $tail = array_pop($parts); - $head = implode('/', $parts); - $themePaths = $this->getThemePaths($themes); - foreach ($themePaths as $themePath) { - // Join path - $pathParts = [ $this->base, $themePath, 'templates', $head, $type, $tail ]; - try { - $path = Path::join($pathParts) . '.ss'; - if (file_exists($path ?? '')) { - $this->getCache()->set($cacheKey, $path); - return $path; - } - } catch (InvalidArgumentException $e) { - // No-op - } - } - } - - // No template found - $this->getCache()->set($cacheKey, null); - return null; - } - /** * Resolve themed CSS path * diff --git a/src/View/ViewLayerData.php b/src/View/ViewLayerData.php new file mode 100644 index 00000000000..8a9146399a7 --- /dev/null +++ b/src/View/ViewLayerData.php @@ -0,0 +1,255 @@ +data + 'ClassName', + // Returns $this->data + 'Me', + ]; + + private object $data; + + /** + * @param mixed $source The source of the data + * @param string $name The name of the field the data comes from, if known + */ + public function __construct(mixed $data, mixed $source = null, string $name = '') + { + if ($data === null) { + throw new InvalidArgumentException('$data must not be null'); + } + if ($data instanceof ViewLayerData) { + $data = $data->data; + } else { + $source = $source instanceof ModelData ? $source : null; + $data = CastingService::singleton()->cast($data, $source, $name); + } + $this->data = $data; + } + + /** + * Needed so we can rewind in SSViewer_Scope::next() after getting itemIteratorTotal without throwing an exception. + */ + public function getIteratorCount(): int + { + $count = $this->getRawDataValue('count'); + if (is_numeric($count)) { + return (int) $count; + } + if (is_countable($this->data)) { + return count($this->data); + } + if (ClassInfo::hasMethod($this->data, 'getIterator')) { + return iterator_count($this->data->getIterator()); + } + return 0; + } + + public function getIterator(): Traversable + { + if (!is_iterable($this->data) && !ClassInfo::hasMethod($this->data, 'getIterator')) { + $type = get_class($this->data); + throw new BadMethodCallException("$type is not iterable."); + } + + $iterable = $this->data; + if (!is_iterable($iterable)) { + $iterable = $this->data->getIterator(); + } + $source = $this->data instanceof ModelData ? $this->data : null; + foreach ($iterable as $item) { + yield $item === null ? null : ViewLayerData::create($item, $source); + } + } + + /** + * Checks if a field is set, or if a getter or a method of that name exists. + * We need to check each of these, because we don't currently distinguish between a property, a getter, and a method + * which means if any of those exists we have to say the field is "set", otherwise template engines may skip fetching the data. + */ + public function __isset(string $name): bool + { + // Note we explicitly DO NOT call count() or exists() on the data here because that would + // require fetching the data prematurely which could cause performance issues in extreme cases + return in_array($name, ViewLayerData::META_DATA_NAMES) + || isset($this->data->$name) + || ClassInfo::hasMethod($this->data, "get$name") + || ClassInfo::hasMethod($this->data, $name); + } + + public function __get(string $name): ?ViewLayerData + { + $value = $this->getRawDataValue($name); + if ($value === null) { + return null; + } + $source = $this->data instanceof ModelData ? $this->data : null; + return ViewLayerData::create($value, $source, $name); + } + + public function __call(string $name, array $arguments = []): ?ViewLayerData + { + $value = $this->getRawDataValue($name, $arguments); + if ($value === null) { + return null; + } + $source = $this->data instanceof ModelData ? $this->data : null; + return ViewLayerData::create($value, $source, $name); + } + + public function __toString(): string + { + if (ClassInfo::hasMethod($this->data, 'forTemplate')) { + return $this->data->forTemplate(); + } + return (string) $this->data; + } + + /** + * Check if there is a truthy value or (for ModelData) if the data exists(). + * If $name is passed, we check for a value in the property/method with that name. Otherwise, + * check if the currently scoped data has a value. + */ + public function hasDataValue(?string $name = null, array $arguments = []): bool + { + if ($name) { + // Ask the model if it has a value for that field + if ($this->data instanceof ModelData) { + return $this->data->hasValue($name, $arguments); + } + // Check for ourselves if there's a value for that field + // This mimics what ModelData does, which provides consistency + $value = $this->getRawDataValue($name, $arguments); + if ($value === null) { + return false; + } + $source = $this->data instanceof ModelData ? $this->data : null; + return ViewLayerData::create($value, $source, $name)->hasDataValue(); + } + // Ask the model if it "exists" + if ($this->data instanceof ModelData) { + return $this->data->exists(); + } + // Mimics ModelData checks on lists + if (is_countable($this->data)) { + return count($this->data) > 0; + } + // Check for truthiness (which is effectively `return true` since data is an object) + // We do this to mimic ModelData->hasValue() for consistency + return (bool) $this->data; + } + + /** + * Get the raw value of some field/property/method on the data, without wrapping it in ViewLayerData. + */ + public function getRawDataValue(string $name, array $arguments = []): mixed + { + $data = $this->data; + if ($data instanceof ModelDataCustomised && $data->customisedHas($name)) { + $data = $data->getCustomisedModelData(); + } + + $value = $this->getValueFromData($data, $name, $arguments); + + return $value; + } + + private function getValueFromData(object $data, string $name, array $arguments): mixed + { + // Values from ModelData can be cached + if ($data instanceof ModelData) { + $cached = $data->objCacheGet($name, $arguments); + if ($cached !== null) { + return $cached; + } + } + + $value = null; + // Keep track of whether we've already fetched a value (allowing null to be the correct value) + $valueWasFetched = false; + + // Try calling a method even if we're fetching as a property + // This matches historical behaviour that a LOT of logic in core modules expects + $value = $this->callDataMethod($data, $name, $arguments, $valueWasFetched); + + // Try to get a property even if we aren't explicitly trying to call a method, if the method didn't exist. + // This matches historical behaviour and allows e.g. `$MyProperty(some-arg)` with a `getMyProperty($arg)` method. + if (!$valueWasFetched) { + // Try an explicit getter + // This matches the "magic" getter behaviour of ModelData across the board for consistent results + $getter = "get{$name}"; + $value = $this->callDataMethod($data, $getter, $arguments, $valueWasFetched); + if (!$valueWasFetched && isset($data->$name)) { + $value = $data->$name; + $valueWasFetched = true; + } + } + + // Caching for modeldata + if ($data instanceof ModelData) { + $data->objCacheSet($name, $arguments, $value); + } + + if ($value === null && in_array($name, ViewLayerData::META_DATA_NAMES)) { + $value = $this->getMetaData($data, $name); + } + + return $value; + } + + private function getMetaData(object $data, string $name): mixed + { + return match ($name) { + 'Me' => $data, + 'ClassName' => DBClassName::create()->setValue(get_class($data)), + default => null + }; + } + + private function callDataMethod(object $data, string $name, array $arguments, bool &$valueWasFetched = false): mixed + { + $hasDynamicMethods = method_exists($data, '__call'); + $hasMethod = ClassInfo::hasMethod($data, $name); + if ($hasMethod || $hasDynamicMethods) { + try { + $value = $data->$name(...$arguments); + $valueWasFetched = true; + return $value; + } catch (BadMethodCallException $e) { + // Only throw the exception if we weren't relying on __call + // It's common for __call to throw BadMethodCallException for methods that aren't "implemented" + // so we just want to return null in those cases. + if (!$hasDynamicMethods) { + throw $e; + } + } + } + return null; + } +} diff --git a/src/i18n/Messages/Symfony/FlushInvalidatedResource.php b/src/i18n/Messages/Symfony/FlushInvalidatedResource.php index 8ffa478f4c9..9b7b22c24a2 100644 --- a/src/i18n/Messages/Symfony/FlushInvalidatedResource.php +++ b/src/i18n/Messages/Symfony/FlushInvalidatedResource.php @@ -14,7 +14,6 @@ */ class FlushInvalidatedResource implements SelfCheckingResourceInterface, Flushable { - public function __toString() { return md5(__CLASS__); diff --git a/tests/php/Control/ControllerTest.php b/tests/php/Control/ControllerTest.php index d62bf4285be..598faedeef2 100644 --- a/tests/php/Control/ControllerTest.php +++ b/tests/php/Control/ControllerTest.php @@ -19,6 +19,8 @@ use SilverStripe\Security\Member; use SilverStripe\View\SSViewer; use PHPUnit\Framework\Attributes\DataProvider; +use SilverStripe\Control\Tests\ControllerTest\ControllerWithDummyEngine; +use SilverStripe\Control\Tests\ControllerTest\DummyTemplateEngine; class ControllerTest extends FunctionalTest { @@ -858,4 +860,12 @@ public function testSpecificHTTPMethods() $response = $this->post('HTTPMethodTestController', ['dummy' => 'example']); $this->assertEquals('Routed to postLegacyRoot', $response->getBody()); } + + public function testTemplateEngineUsed() + { + $controller = new ControllerWithDummyEngine(); + $this->assertSame('This is my controller', $controller->render()->getValue()); + $this->assertSame('This is my controller', $controller->renderWith('literally-any-template')->getValue()); + $this->assertInstanceOf(DummyTemplateEngine::class, $controller->getViewer('')->getTemplateEngine()); + } } diff --git a/tests/php/Control/ControllerTest/ControllerWithDummyEngine.php b/tests/php/Control/ControllerTest/ControllerWithDummyEngine.php new file mode 100644 index 00000000000..4f7ca61577c --- /dev/null +++ b/tests/php/Control/ControllerTest/ControllerWithDummyEngine.php @@ -0,0 +1,15 @@ +output; + } + + public function render(ViewLayerData $model, array $overlay = []): string + { + return $this->output; + } +} diff --git a/tests/php/Control/Email/EmailTest.php b/tests/php/Control/Email/EmailTest.php index 9a0c68e179d..140cedb9a7d 100644 --- a/tests/php/Control/Email/EmailTest.php +++ b/tests/php/Control/Email/EmailTest.php @@ -416,27 +416,14 @@ public function testDataWithModelData(): void public function testHTMLTemplate(): void { - // Find template on disk - $emailTemplate = ModuleResourceLoader::singleton()->resolveResource( - 'silverstripe/framework:templates/SilverStripe/Control/Email/Email.ss' - ); - $subClassTemplate = ModuleResourceLoader::singleton()->resolveResource( - 'silverstripe/framework:tests/php/Control/Email/EmailTest/templates/' - . str_replace('\\', '/', EmailSubClass::class) - . '.ss' - ); - $this->assertTrue($emailTemplate->exists()); - $this->assertTrue($subClassTemplate->exists()); - - // Check template is auto-found $email = new Email(); - $this->assertEquals($emailTemplate->getPath(), $email->getHTMLTemplate()); + $this->assertSame(SSViewer::get_templates_by_class(Email::class, '', Email::class), $email->getHTMLTemplate()); $email->setHTMLTemplate('MyTemplate'); $this->assertEquals('MyTemplate', $email->getHTMLTemplate()); - // Check subclass template is found + // Check subclass template $email2 = new EmailSubClass(); - $this->assertEquals($subClassTemplate->getPath(), $email2->getHTMLTemplate()); + $this->assertSame(SSViewer::get_templates_by_class(EmailSubClass::class, '', Email::class), $email2->getHTMLTemplate()); $email->setHTMLTemplate('MyTemplate'); $this->assertEquals('MyTemplate', $email->getHTMLTemplate()); } diff --git a/tests/php/Core/Manifest/ThemeResourceLoaderTest.php b/tests/php/Core/Manifest/ThemeResourceLoaderTest.php index 90f7fc61489..40007bbf958 100644 --- a/tests/php/Core/Manifest/ThemeResourceLoaderTest.php +++ b/tests/php/Core/Manifest/ThemeResourceLoaderTest.php @@ -2,7 +2,6 @@ namespace SilverStripe\Core\Tests\Manifest; -use Psr\SimpleCache\CacheInterface; use SilverStripe\Control\Director; use SilverStripe\Core\Manifest\ModuleLoader; use SilverStripe\View\ThemeResourceLoader; @@ -67,188 +66,6 @@ protected function tearDown(): void parent::tearDown(); } - /** - * Test that 'main' and 'Layout' templates are loaded from module - */ - public function testFindTemplatesInModule() - { - $this->assertEquals( - "$this->base/module/templates/Page.ss", - $this->loader->findTemplate('Page', ['$default']) - ); - - $this->assertEquals( - "$this->base/module/templates/Layout/Page.ss", - $this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default']) - ); - } - - public function testFindNestedThemeTemplates() - { - // Without including the theme this template cannot be found - $this->assertEquals(null, $this->loader->findTemplate('NestedThemePage', ['$default'])); - - // With a nested theme available then it is available - $this->assertEquals( - "{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss", - $this->loader->findTemplate( - 'NestedThemePage', - [ - 'silverstripe/module:subtheme', - '$default' - ] - ) - ); - - // Can also be found if excluding $default theme - $this->assertEquals( - "{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss", - $this->loader->findTemplate( - 'NestedThemePage', - [ - 'silverstripe/module:subtheme', - ] - ) - ); - } - - public function testFindTemplateByType() - { - // Test that "type" is respected properly - $this->assertEquals( - "{$this->base}/module/templates/MyNamespace/Layout/MyClass.ss", - $this->loader->findTemplate( - [ - [ - 'type' => 'Layout', - 'MyNamespace/NonExistantTemplate' - ], - [ - 'type' => 'Layout', - 'MyNamespace/MyClass' - ], - 'MyNamespace/MyClass' - ], - [ - 'silverstripe/module:subtheme', - 'theme', - '$default', - ] - ) - ); - - // Non-typed template can be found even if looking for typed theme at a lower priority - $this->assertEquals( - "{$this->base}/module/templates/MyNamespace/MyClass.ss", - $this->loader->findTemplate( - [ - [ - 'type' => 'Layout', - 'MyNamespace/NonExistantTemplate' - ], - 'MyNamespace/MyClass', - [ - 'type' => 'Layout', - 'MyNamespace/MyClass' - ] - ], - [ - 'silverstripe/module', - 'theme', - '$default', - ] - ) - ); - } - - public function testFindTemplatesByPath() - { - // Items given as full paths are returned directly - $this->assertEquals( - "$this->base/themes/theme/templates/Page.ss", - $this->loader->findTemplate("$this->base/themes/theme/templates/Page.ss", ['theme']) - ); - - $this->assertEquals( - "$this->base/themes/theme/templates/Page.ss", - $this->loader->findTemplate( - [ - "$this->base/themes/theme/templates/Page.ss", - "Page" - ], - ['theme'] - ) - ); - - // Ensure checks for file_exists - $this->assertEquals( - "$this->base/themes/theme/templates/Page.ss", - $this->loader->findTemplate( - [ - "$this->base/themes/theme/templates/NotAPage.ss", - "$this->base/themes/theme/templates/Page.ss", - ], - ['theme'] - ) - ); - } - - /** - * Test that 'main' and 'Layout' templates are loaded from set theme - */ - public function testFindTemplatesInTheme() - { - $this->assertEquals( - "$this->base/themes/theme/templates/Page.ss", - $this->loader->findTemplate('Page', ['theme']) - ); - - $this->assertEquals( - "$this->base/themes/theme/templates/Layout/Page.ss", - $this->loader->findTemplate(['type' => 'Layout', 'Page'], ['theme']) - ); - } - - /** - * Test that 'main' and 'Layout' templates are loaded from project without a set theme - */ - public function testFindTemplatesInApplication() - { - $templates = [ - $this->base . '/myproject/templates/Page.ss', - $this->base . '/myproject/templates/Layout/Page.ss' - ]; - $this->createTestTemplates($templates); - - $this->assertEquals( - "$this->base/myproject/templates/Page.ss", - $this->loader->findTemplate('Page', ['$default']) - ); - - $this->assertEquals( - "$this->base/myproject/templates/Layout/Page.ss", - $this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default']) - ); - - $this->removeTestTemplates($templates); - } - - /** - * Test that 'main' template is found in theme and 'Layout' is found in module - */ - public function testFindTemplatesMainThemeLayoutModule() - { - $this->assertEquals( - "$this->base/themes/theme/templates/CustomThemePage.ss", - $this->loader->findTemplate('CustomThemePage', ['theme', '$default']) - ); - - $this->assertEquals( - "$this->base/module/templates/Layout/CustomThemePage.ss", - $this->loader->findTemplate(['type' => 'Layout', 'CustomThemePage'], ['theme', '$default']) - ); - } - public function testFindThemedCSS() { $this->assertEquals( @@ -303,20 +120,6 @@ public function testFindThemedJavascript() ); } - protected function createTestTemplates($templates) - { - foreach ($templates as $template) { - file_put_contents($template ?? '', ''); - } - } - - protected function removeTestTemplates($templates) - { - foreach ($templates as $template) { - unlink($template ?? ''); - } - } - public static function providerTestGetPath() { return [ @@ -381,28 +184,4 @@ public function testGetPath($name, $path) { $this->assertEquals($path, $this->loader->getPath($name)); } - - public function testFindTemplateWithCacheMiss() - { - $mockCache = $this->createMock(CacheInterface::class); - $mockCache->expects($this->once())->method('has')->willReturn(false); - $mockCache->expects($this->never())->method('get'); - $mockCache->expects($this->once())->method('set'); - - $loader = new ThemeResourceLoader(); - $loader->setCache($mockCache); - $loader->findTemplate('Page', ['$default']); - } - - public function testFindTemplateWithCacheHit() - { - $mockCache = $this->createMock(CacheInterface::class); - $mockCache->expects($this->once())->method('has')->willReturn(true); - $mockCache->expects($this->never())->method('set'); - $mockCache->expects($this->once())->method('get')->willReturn('mock_template.ss'); - - $loader = new ThemeResourceLoader(); - $loader->setCache($mockCache); - $this->assertSame('mock_template.ss', $loader->findTemplate('Page', ['$default'])); - } } diff --git a/tests/php/Core/Manifest/fixtures/templatemanifest/module/Root.ss b/tests/php/Core/Manifest/fixtures/templatemanifest/myproject/templates/.gitkeep similarity index 100% rename from tests/php/Core/Manifest/fixtures/templatemanifest/module/Root.ss rename to tests/php/Core/Manifest/fixtures/templatemanifest/myproject/templates/.gitkeep diff --git a/tests/php/Forms/GridField/GridField_URLHandlerTest/TestComponent.php b/tests/php/Forms/GridField/GridField_URLHandlerTest/TestComponent.php index dea7ccc66fd..60adbcfd732 100644 --- a/tests/php/Forms/GridField/GridField_URLHandlerTest/TestComponent.php +++ b/tests/php/Forms/GridField/GridField_URLHandlerTest/TestComponent.php @@ -56,7 +56,7 @@ public function Link($action = null) public function showform(GridField $gridField, HTTPRequest $request) { $this->setRequest($request); - return "" . SSViewer::get_base_tag("") . "" . $this->Form($gridField, $request)->forTemplate(); + return "" . SSViewer::getBaseTag() . "" . $this->Form($gridField, $request)->forTemplate(); } /** diff --git a/tests/php/Forms/GridField/GridField_URLHandlerTest/TestComponent_ItemRequest.php b/tests/php/Forms/GridField/GridField_URLHandlerTest/TestComponent_ItemRequest.php index fc7774ef165..639c114d6d4 100644 --- a/tests/php/Forms/GridField/GridField_URLHandlerTest/TestComponent_ItemRequest.php +++ b/tests/php/Forms/GridField/GridField_URLHandlerTest/TestComponent_ItemRequest.php @@ -36,7 +36,7 @@ public function Link($action = null) public function showform() { - return "" . SSViewer::get_base_tag("") . "" . $this->Form()->forTemplate(); + return "" . SSViewer::getBaseTag() . "" . $this->Form()->forTemplate(); } public function Form() diff --git a/tests/php/Forms/TreeDropdownFieldTest.php b/tests/php/Forms/TreeDropdownFieldTest.php index 83e091f89e6..012b22b9a32 100644 --- a/tests/php/Forms/TreeDropdownFieldTest.php +++ b/tests/php/Forms/TreeDropdownFieldTest.php @@ -314,7 +314,7 @@ public function testTreeSearchUsingSubObject() $noResult = $parser->getBySelector($cssPath); $this->assertEmpty( $noResult, - $subObject2 . ' is not found' + get_class($subObject2) . ' is not found' ); } diff --git a/tests/php/Forms/TreeMultiselectFieldTest.php b/tests/php/Forms/TreeMultiselectFieldTest.php index 408ab4af861..2d07fdf10af 100644 --- a/tests/php/Forms/TreeMultiselectFieldTest.php +++ b/tests/php/Forms/TreeMultiselectFieldTest.php @@ -7,6 +7,8 @@ use SilverStripe\Forms\Form; use SilverStripe\Forms\FormTemplateHelper; use SilverStripe\Forms\TreeMultiselectField; +use SilverStripe\ORM\Tests\HierarchyTest\HierarchyOnSubclassTestObject; +use SilverStripe\ORM\Tests\HierarchyTest\HierarchyOnSubclassTestSubObject; use SilverStripe\ORM\Tests\HierarchyTest\TestObject; use SilverStripe\View\SSViewer; @@ -16,6 +18,8 @@ class TreeMultiselectFieldTest extends SapphireTest protected static $extra_dataobjects = [ TestObject::class, + HierarchyOnSubclassTestObject::class, + HierarchyOnSubclassTestSubObject::class, ]; protected $formId = 'TheFormID'; diff --git a/tests/php/Model/ModelDataTest.php b/tests/php/Model/ModelDataTest.php index 33f4b171d3a..9f233980f5a 100644 --- a/tests/php/Model/ModelDataTest.php +++ b/tests/php/Model/ModelDataTest.php @@ -12,6 +12,7 @@ use SilverStripe\Model\Tests\ModelDataTest\ModelDataTestObject; use SilverStripe\Model\ModelData; use PHPUnit\Framework\Attributes\DataProvider; +use SilverStripe\Model\Tests\ModelDataTest\TestModelData; /** * See {@link SSViewerTest->testCastingHelpers()} for more tests related to casting and ModelData behaviour, @@ -54,6 +55,18 @@ public function testCasting() $this->assertEquals($htmlString, $textField->obj('XML')->forTemplate()); } + public function testCastingValues() + { + $caster = new ModelDataTest\Castable(); + + $this->assertEquals('casted', $caster->obj('alwaysCasted')->forTemplate()); + $this->assertEquals('casted', $caster->obj('noCastingInformation')->forTemplate()); + + // Test automatic escaping is applied even to fields with no 'casting' + $this->assertEquals('casted', $caster->obj('unsafeXML')->forTemplate()); + $this->assertEquals('<foo>', $caster->obj('castedUnsafeXML')->forTemplate()); + } + public function testRequiresCasting() { $caster = new ModelDataTest\Castable(); @@ -78,18 +91,6 @@ public function testFailoverRequiresCasting() $this->assertInstanceOf(ModelDataTest\Caster::class, $caster->obj('noCastingInformation')); } - public function testCastingXMLVal() - { - $caster = new ModelDataTest\Castable(); - - $this->assertEquals('casted', $caster->XML_val('alwaysCasted')); - $this->assertEquals('casted', $caster->XML_val('noCastingInformation')); - - // Test automatic escaping is applied even to fields with no 'casting' - $this->assertEquals('casted', $caster->XML_val('unsafeXML')); - $this->assertEquals('<foo>', $caster->XML_val('castedUnsafeXML')); - } - public function testArrayCustomise() { $modelData = new ModelDataTest\Castable(); @@ -100,11 +101,11 @@ public function testArrayCustomise() ] ); - $this->assertEquals('test', $modelData->XML_val('test')); - $this->assertEquals('casted', $modelData->XML_val('alwaysCasted')); + $this->assertEquals('test', $modelData->obj('test')->forTemplate()); + $this->assertEquals('casted', $modelData->obj('alwaysCasted')->forTemplate()); - $this->assertEquals('overwritten', $newModelData->XML_val('test')); - $this->assertEquals('overwritten', $newModelData->XML_val('alwaysCasted')); + $this->assertEquals('overwritten', $newModelData->obj('test')->forTemplate()); + $this->assertEquals('overwritten', $newModelData->obj('alwaysCasted')->forTemplate()); $this->assertEquals('castable', $modelData->forTemplate()); $this->assertEquals('castable', $newModelData->forTemplate()); @@ -115,14 +116,14 @@ public function testObjectCustomise() $modelData = new ModelDataTest\Castable(); $newModelData = $modelData->customise(new ModelDataTest\RequiresCasting()); - $this->assertEquals('test', $modelData->XML_val('test')); - $this->assertEquals('casted', $modelData->XML_val('alwaysCasted')); + $this->assertEquals('test', $modelData->obj('test')->forTemplate()); + $this->assertEquals('casted', $modelData->obj('alwaysCasted')->forTemplate()); - $this->assertEquals('overwritten', $newModelData->XML_val('test')); - $this->assertEquals('casted', $newModelData->XML_val('alwaysCasted')); + $this->assertEquals('overwritten', $newModelData->obj('test')->forTemplate()); + $this->assertEquals('casted', $newModelData->obj('alwaysCasted')->forTemplate()); $this->assertEquals('castable', $modelData->forTemplate()); - $this->assertEquals('casted', $newModelData->forTemplate()); + $this->assertEquals('castable', $newModelData->forTemplate()); } public function testDefaultValueWrapping() @@ -139,25 +140,6 @@ public function testDefaultValueWrapping() $this->assertEquals('SomeTitleValue', $obj->forTemplate()); } - public function testCastingClass() - { - $expected = [ - //'NonExistant' => null, - 'Field' => 'CastingType', - 'Argument' => 'ArgumentType', - 'ArrayArgument' => 'ArrayArgumentType' - ]; - $obj = new ModelDataTest\CastingClass(); - - foreach ($expected as $field => $class) { - $this->assertEquals( - $class, - $obj->castingClass($field), - "castingClass() returns correct results for ::\$$field" - ); - } - } - public function testObjWithCachedStringValueReturnsValidObject() { $obj = new ModelDataTest\NoCastingInformation(); @@ -273,6 +255,114 @@ public function testIsAccessibleProperty() $this->assertTrue($output, 'Property should be accessible'); } + public static function provideObj(): array + { + return [ + 'returned value is caught' => [ + 'name' => 'justCallMethod', + 'args' => [], + 'expectRequested' => [ + [ + 'type' => 'method', + 'name' => 'justCallMethod', + 'args' => [], + ], + ], + 'expected' => 'This is a method value', + ], + 'getter is used' => [ + 'name' => 'ActualValue', + 'args' => [], + 'expectRequested' => [ + [ + 'type' => 'method', + 'name' => 'getActualValue', + 'args' => [], + ], + ], + 'expected' => 'this is the value', + ], + 'if no method exists, only property is fetched' => [ + 'name' => 'NoMethod', + 'args' => [], + 'expectRequested' => [ + [ + 'type' => 'property', + 'name' => 'NoMethod', + ], + ], + 'expected' => null, + ], + 'property value is caught' => [ + 'name' => 'ActualValueField', + 'args' => [], + 'expectRequested' => [ + [ + 'type' => 'property', + 'name' => 'ActualValueField', + ], + ], + 'expected' => 'the value is here', + ], + 'not set and no method' => [ + 'name' => 'NotSet', + 'args' => [], + 'expectRequested' => [], + 'expected' => null, + ], + 'args but no method' => [ + 'name' => 'SomeField', + 'args' => ['abc', 123], + 'expectRequested' => [ + [ + 'type' => 'property', + 'name' => 'SomeField', + ], + ], + 'expected' => null, + ], + 'method with args' => [ + 'name' => 'justCallMethod', + 'args' => ['abc', 123], + 'expectRequested' => [ + [ + 'type' => 'method', + 'name' => 'justCallMethod', + 'args' => ['abc', 123], + ], + ], + 'expected' => 'This is a method value', + ], + 'getter with args' => [ + 'name' => 'ActualValue', + 'args' => ['abc', 123], + 'expectRequested' => [ + [ + 'type' => 'method', + 'name' => 'getActualValue', + 'args' => ['abc', 123], + ], + ], + 'expected' => 'this is the value', + ], + ]; + } + + #[DataProvider('provideObj')] + public function testObj(string $name, array $args, array $expectRequested, ?string $expected): void + { + $fixture = new TestModelData(); + $value = $fixture->obj($name, $args); + $this->assertSame($expectRequested, $fixture->getRequested()); + $this->assertEquals($expected, ($value instanceof DBField) ? $value->getValue() : $value); + // Ensure value is being wrapped when not null + // Don't bother testing actual casting, there's some coverage for that in this class already + // but mostly it's tested in CastingServiceTest + if ($value !== null) { + $this->assertTrue(is_object($value)); + } + } + public function testDynamicData() { $obj = (object) ['SomeField' => [1, 2, 3]]; diff --git a/tests/php/Model/ModelDataTest/NotCached.php b/tests/php/Model/ModelDataTest/NotCached.php index 2b988824932..57678e6411e 100644 --- a/tests/php/Model/ModelDataTest/NotCached.php +++ b/tests/php/Model/ModelDataTest/NotCached.php @@ -9,7 +9,7 @@ class NotCached extends ModelData implements TestOnly { public $Test; - protected function objCacheGet($key) + public function objCacheGet(string $fieldName, array $arguments = []): mixed { // Disable caching return null; diff --git a/tests/php/Model/ModelDataTest/TestModelData.php b/tests/php/Model/ModelDataTest/TestModelData.php new file mode 100644 index 00000000000..bf3ae6f1893 --- /dev/null +++ b/tests/php/Model/ModelDataTest/TestModelData.php @@ -0,0 +1,59 @@ +requested[] = [ + 'type' => 'method', + 'name' => __FUNCTION__, + 'args' => func_get_args(), + ]; + return 'This is a method value'; + } + + public function getActualValue(): string + { + $this->requested[] = [ + 'type' => 'method', + 'name' => __FUNCTION__, + 'args' => func_get_args(), + ]; + return 'this is the value'; + } + + public function getField(string $name): ?string + { + $this->requested[] = [ + 'type' => 'property', + 'name' => $name, + ]; + if ($name === 'ActualValueField') { + return 'the value is here'; + } + return null; + } + + /** + * We need this so we always try to fetch a property. + */ + public function hasField(string $name): bool + { + return $name !== 'NotSet'; + } + + public function getRequested(): array + { + return $this->requested; + } +} diff --git a/tests/php/ORM/Filters/EndsWithFilterTest.php b/tests/php/ORM/Filters/EndsWithFilterTest.php index 40c69c0e209..907715d0767 100644 --- a/tests/php/ORM/Filters/EndsWithFilterTest.php +++ b/tests/php/ORM/Filters/EndsWithFilterTest.php @@ -197,20 +197,6 @@ public static function provideMatches() 'modifiers' => [], 'matches' => false, ], - // These will both evaluate to true because the __toString() method just returns the class name. - // We're testing this scenario because ArrayList might contain arbitrary values - [ - 'filterValue' => new ArrayData(['SomeField' => 'some value']), - 'matchValue' => new ArrayData(['SomeField' => 'some value']), - 'modifiers' => [], - 'matches' => true, - ], - [ - 'filterValue' => new ArrayData(['SomeField' => 'SoMe VaLuE']), - 'matchValue' => new ArrayData(['SomeField' => 'some value']), - 'modifiers' => [], - 'matches' => true, - ], // case insensitive [ 'filterValue' => 'somevalue', diff --git a/tests/php/ORM/Filters/PartialMatchFilterTest.php b/tests/php/ORM/Filters/PartialMatchFilterTest.php index 7d11ebe7c10..8a3d5fdaf9e 100644 --- a/tests/php/ORM/Filters/PartialMatchFilterTest.php +++ b/tests/php/ORM/Filters/PartialMatchFilterTest.php @@ -197,20 +197,6 @@ public static function provideMatches() 'modifiers' => [], 'matches' => false, ], - // These will both evaluate to true because the __toString() method just returns the class name. - // We're testing this scenario because ArrayList might contain arbitrary values - [ - 'filterValue' => new ArrayData(['SomeField' => 'some value']), - 'matchValue' => new ArrayData(['SomeField' => 'some value']), - 'modifiers' => [], - 'matches' => true, - ], - [ - 'filterValue' => new ArrayData(['SomeField' => 'SoMe VaLuE']), - 'matchValue' => new ArrayData(['SomeField' => 'some value']), - 'modifiers' => [], - 'matches' => true, - ], // case insensitive [ 'filterValue' => 'somevalue', diff --git a/tests/php/ORM/Filters/StartsWithFilterTest.php b/tests/php/ORM/Filters/StartsWithFilterTest.php index 32e2050ff83..66a2d8b16bd 100644 --- a/tests/php/ORM/Filters/StartsWithFilterTest.php +++ b/tests/php/ORM/Filters/StartsWithFilterTest.php @@ -197,20 +197,6 @@ public static function provideMatches() 'modifiers' => [], 'matches' => false, ], - // These will both evaluate to true because the __toString() method just returns the class name. - // We're testing this scenario because ArrayList might contain arbitrary values - [ - 'filterValue' => new ArrayData(['SomeField' => 'some value']), - 'matchValue' => new ArrayData(['SomeField' => 'some value']), - 'modifiers' => [], - 'matches' => true, - ], - [ - 'filterValue' => new ArrayData(['SomeField' => 'SoMe VaLuE']), - 'matchValue' => new ArrayData(['SomeField' => 'some value']), - 'modifiers' => [], - 'matches' => true, - ], // case insensitive [ 'filterValue' => 'somevalue', diff --git a/tests/php/ORM/LabelFieldTest.php b/tests/php/ORM/LabelFieldTest.php index 58f3b4edaf1..e1170ac612e 100644 --- a/tests/php/ORM/LabelFieldTest.php +++ b/tests/php/ORM/LabelFieldTest.php @@ -11,6 +11,6 @@ class LabelFieldTest extends SapphireTest public function testFieldHasNoNameAttribute() { $field = new LabelField('MyName', 'MyTitle'); - $this->assertEquals(trim($field->Field() ?? ''), ''); + $this->assertEquals('', trim($field->Field())); } } diff --git a/tests/php/View/CastingServiceTest.php b/tests/php/View/CastingServiceTest.php new file mode 100644 index 00000000000..455d34b3c06 --- /dev/null +++ b/tests/php/View/CastingServiceTest.php @@ -0,0 +1,206 @@ + null, + 'source' => null, + 'fieldName' => '', + 'expected' => null, + ], + [ + 'data' => new stdClass(), + 'source' => null, + 'fieldName' => '', + 'expected' => stdClass::class, + ], + [ + 'data' => new stdClass(), + 'source' => TestDataObject::class, + 'fieldName' => 'DateField', + 'expected' => stdClass::class, + ], + [ + 'data' => new DBText(), + 'source' => TestDataObject::class, + 'fieldName' => 'DateField', + 'expected' => stdClass::class, + ], + [ + 'data' => '2024-10-10', + 'source' => TestDataObject::class, + 'fieldName' => 'DateField', + 'expected' => DBDate::class, + ], + [ + 'data' => 'some value', + 'source' => TestDataObject::class, + 'fieldName' => 'HtmlField', + 'expected' => DBHTMLText::class, + ], + [ + 'data' => '12.35', + 'source' => TestDataObject::class, + 'fieldName' => 'OverrideCastingHelper', + 'expected' => DBCurrency::class, + ], + [ + 'data' => '10:17:36', + 'source' => TestDataObject::class, + 'fieldName' => 'TimeField', + 'expected' => DBTime::class, + ], + [ + 'data' => 123456, + 'source' => TestDataObject::class, + 'fieldName' => 'RandomField', + 'expected' => DBInt::class, + ], + [ + 'data' => 'some text', + 'source' => TestDataObject::class, + 'fieldName' => 'RandomField', + 'expected' => DBText::class, + ], + [ + 'data' => '12.35', + 'source' => null, + 'fieldName' => 'OverrideCastingHelper', + 'expected' => DBText::class, + ], + [ + 'data' => 123456, + 'source' => null, + 'fieldName' => 'RandomField', + 'expected' => DBInt::class, + ], + [ + 'data' => '10:17:36', + 'source' => null, + 'fieldName' => 'TimeField', + 'expected' => DBText::class, + ], + [ + 'data' => 'some text', + 'source' => null, + 'fieldName' => '', + 'expected' => DBText::class, + ], + [ + 'data' => true, + 'source' => null, + 'fieldName' => '', + 'expected' => DBBoolean::class, + ], + [ + 'data' => false, + 'source' => null, + 'fieldName' => '', + 'expected' => DBBoolean::class, + ], + [ + 'data' => 1.234, + 'source' => null, + 'fieldName' => '', + 'expected' => DBFloat::class, + ], + [ + 'data' => [], + 'source' => null, + 'fieldName' => '', + 'expected' => ArrayList::class, + ], + [ + 'data' => [1,2,3,4], + 'source' => null, + 'fieldName' => '', + 'expected' => ArrayList::class, + ], + [ + 'data' => ['one' => 1, 'two' => 2], + 'source' => null, + 'fieldName' => '', + 'expected' => ArrayData::class, + ], + [ + 'data' => ['one' => 1, 'two' => 2], + 'source' => TestDataObject::class, + 'fieldName' => 'AnyField', + 'expected' => ArrayData::class, + ], + [ + 'data' => ['one' => 1, 'two' => 2], + 'source' => TestDataObject::class, + 'fieldName' => 'ArrayAsText', + 'expected' => DBText::class, + ], + ]; + } + + #[DataProvider('provideCast')] + public function testCast(mixed $data, ?string $source, string $fieldName, ?string $expected): void + { + // Can't instantiate DataObject in a data provider + if (is_string($source)) { + $source = new $source(); + } + $service = new CastingService(); + $value = $service->cast($data, $source, $fieldName); + + // Check the cast object is the correct type + if ($expected === null) { + $this->assertNull($value); + } elseif (is_object($data)) { + $this->assertSame($data, $value); + } else { + $this->assertInstanceOf($expected, $value); + } + + // Check the value is retained + if ($value instanceof DBField && !is_object($data)) { + $this->assertSame($data, $value->getValue()); + } + if ($value instanceof ArrayData && !is_object($data)) { + $this->assertSame($data, $value->toMap()); + } + if ($value instanceof ArrayList && !is_object($data)) { + $this->assertSame($data, $value->toArray()); + } + } + + public function testCastStrict(): void + { + $service = new CastingService(); + $value = $service->cast(null, strict: true); + $this->assertInstanceOf(DBText::class, $value); + $this->assertNull($value->getValue()); + } +} diff --git a/tests/php/View/CastingServiceTest/TestDataObject.php b/tests/php/View/CastingServiceTest/TestDataObject.php new file mode 100644 index 00000000000..c136ea5b53c --- /dev/null +++ b/tests/php/View/CastingServiceTest/TestDataObject.php @@ -0,0 +1,30 @@ + 'HTMLText', + 'DateField' => 'Date', + ]; + + private static array $casting = [ + 'DateField' => 'Text', // won't override + 'TimeField' => 'Time', + 'ArrayAsText' => 'Text', + ]; + + public function castingHelper(string $field): ?string + { + if ($field === 'OverrideCastingHelper') { + return 'Currency'; + } + return parent::castingHelper($field); + } +} diff --git a/tests/php/View/ContentNegotiatorTest.php b/tests/php/View/ContentNegotiatorTest.php index 7465e55fa73..e8acfc6e4c4 100644 --- a/tests/php/View/ContentNegotiatorTest.php +++ b/tests/php/View/ContentNegotiatorTest.php @@ -6,31 +6,17 @@ use SilverStripe\Control\ContentNegotiator; use SilverStripe\Control\HTTPResponse; use SilverStripe\View\SSViewer; -use SilverStripe\View\Tests\SSViewerTest\TestFixture; class ContentNegotiatorTest extends SapphireTest { - - /** - * Small helper to render templates from strings - * Cloned from SSViewerTest - */ - private function render($templateString, $data = null) - { - $t = SSViewer::fromString($templateString); - if (!$data) { - $data = new TestFixture(); - } - return $t->process($data); - } - public function testXhtmltagReplacement() { - $tmpl1 = ' + $baseTag = SSViewer::getBaseTag(true); + $renderedOutput = ' - <% base_tag %> + ' . $baseTag . '