diff --git a/src/Control/RSS/RSSFeed_Entry.php b/src/Control/RSS/RSSFeed_Entry.php index 1ebaae7e7de..810f2589649 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,9 +58,9 @@ 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() + public function getTitle() { return $this->rssField($this->titleField); } @@ -68,9 +68,9 @@ 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() + public function getDescription() { $description = $this->rssField($this->descriptionField); @@ -85,9 +85,9 @@ 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() + public function getAuthor() { return $this->rssField($this->authorField); } @@ -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/CustomMethods.php b/src/Core/CustomMethods.php index 97f84f07d64..e6edc573894 100644 --- a/src/Core/CustomMethods.php +++ b/src/Core/CustomMethods.php @@ -36,6 +36,8 @@ trait CustomMethods */ protected static $built_in_methods = []; + protected array $extraMethodsForInstance = []; + /** * Attempts to locate and call a method dynamically added to a class at runtime if a default cannot be located * @@ -175,7 +177,7 @@ protected function getExtraMethodConfig($method) $this->defineMethods(); } - return self::class::$extra_methods[$lowerClass][strtolower($method)] ?? null; + return $this->extraMethodsForInstance[strtolower($method)] ?? self::class::$extra_methods[$lowerClass][strtolower($method)] ?? null; } /** @@ -190,6 +192,9 @@ public function allMethodNames($custom = false) // Query extra methods $lowerClass = strtolower(static::class); + if ($custom && !empty($this->extraMethodsForInstance)) { + $methods = array_merge($this->extraMethodsForInstance, $methods); + } if ($custom && isset(self::class::$extra_methods[$lowerClass])) { $methods = array_merge(self::class::$extra_methods[$lowerClass], $methods); } @@ -256,7 +261,7 @@ protected function findMethodsFrom($object) * @param string|int $index an index to use if the property is an array * @throws InvalidArgumentException */ - protected function addMethodsFrom($property, $index = null) + protected function addMethodsFrom($property, $index = null, bool $static = true) { $class = static::class; $object = ($index !== null) ? $this->{$property}[$index] : $this->$property; @@ -280,10 +285,18 @@ protected function addMethodsFrom($property, $index = null) // Merge with extra_methods $lowerClass = strtolower($class); - if (isset(self::class::$extra_methods[$lowerClass])) { - self::class::$extra_methods[$lowerClass] = array_merge(self::class::$extra_methods[$lowerClass], $newMethods); + if ($static) { + if (isset(self::class::$extra_methods[$lowerClass])) { + self::class::$extra_methods[$lowerClass] = array_merge(self::class::$extra_methods[$lowerClass], $newMethods); + } else { + self::class::$extra_methods[$lowerClass] = $newMethods; + } } else { - self::class::$extra_methods[$lowerClass] = $newMethods; + if (!empty($this->extraMethodsForInstance)) { + $this->extraMethodsForInstance = array_merge($this->extraMethodsForInstance, $newMethods); + } else { + $this->extraMethodsForInstance = $newMethods; + } } } @@ -293,7 +306,7 @@ protected function addMethodsFrom($property, $index = null) * @param string $property the property name * @param string|int $index an index to use if the property is an array */ - protected function removeMethodsFrom($property, $index = null) + protected function removeMethodsFrom($property, $index = null, bool $static = true) { $extension = ($index !== null) ? $this->{$property}[$index] : $this->$property; $class = static::class; @@ -310,12 +323,22 @@ protected function removeMethodsFrom($property, $index = null) } $methods = $this->findMethodsFrom($extension); - // Unset by key - self::class::$extra_methods[$lowerClass] = array_diff_key(self::class::$extra_methods[$lowerClass], $methods); + if ($static) { + // Unset by key + self::class::$extra_methods[$lowerClass] = array_diff_key(self::class::$extra_methods[$lowerClass], $methods); - // Clear empty list - if (empty(self::class::$extra_methods[$lowerClass])) { - unset(self::class::$extra_methods[$lowerClass]); + // Clear empty list + if (empty(self::class::$extra_methods[$lowerClass])) { + unset(self::class::$extra_methods[$lowerClass]); + } + } else { + // Unset by key + $this->extraMethodsForInstance = array_diff_key($this->extraMethodsForInstance, $methods); + + // Clear empty list + if (empty($this->extraMethodsForInstance)) { + unset($this->extraMethodsForInstance); + } } } 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/Forms/CheckboxSetField.php b/src/Forms/CheckboxSetField.php index 6f11db35799..6386564f187 100644 --- a/src/Forms/CheckboxSetField.php +++ b/src/Forms/CheckboxSetField.php @@ -46,13 +46,13 @@ class CheckboxSetField extends MultiSelectField * @param array $properties * @return DBHTMLText */ - public function Field($properties = []) + public function renderField($properties = []) { $properties = array_merge($properties, [ 'Options' => $this->getOptions() ]); - return FormField::Field($properties); + return FormField::renderField($properties); } /** diff --git a/src/Forms/ConfirmedPasswordField.php b/src/Forms/ConfirmedPasswordField.php index fa61dc91a38..d990a28fa9b 100644 --- a/src/Forms/ConfirmedPasswordField.php +++ b/src/Forms/ConfirmedPasswordField.php @@ -195,7 +195,7 @@ public function setTitle($title) * * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { // Build inner content $fieldContent = ''; @@ -209,7 +209,7 @@ public function Field($properties = []) } } - $fieldContent .= $field->FieldHolder(['AttributesHTML' => $this->getAttributesHTMLForChild($field)]); + $fieldContent .= $field->renderFieldHolder(['AttributesHTML' => $this->getAttributesHTMLForChild($field)]); } if (!$this->showOnClick) { diff --git a/src/Forms/CurrencyField_Disabled.php b/src/Forms/CurrencyField_Disabled.php index 4b26a7065d0..c5963b479a9 100644 --- a/src/Forms/CurrencyField_Disabled.php +++ b/src/Forms/CurrencyField_Disabled.php @@ -19,7 +19,7 @@ class CurrencyField_Disabled extends CurrencyField * @param array $properties * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { if ($this->value) { $val = Convert::raw2xml($this->value); diff --git a/src/Forms/CurrencyField_Readonly.php b/src/Forms/CurrencyField_Readonly.php index d9bb4037fcf..28280f18ed7 100644 --- a/src/Forms/CurrencyField_Readonly.php +++ b/src/Forms/CurrencyField_Readonly.php @@ -17,7 +17,7 @@ class CurrencyField_Readonly extends ReadonlyField * @param array $properties * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { $currencySymbol = DBCurrency::config()->get('currency_symbol'); if ($this->value) { diff --git a/src/Forms/DatalessField.php b/src/Forms/DatalessField.php index 0bddbc657a1..21d7a0fd1b4 100644 --- a/src/Forms/DatalessField.php +++ b/src/Forms/DatalessField.php @@ -42,9 +42,9 @@ public function getAttributes() * @param array $properties * @return DBHTMLText */ - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { - return $this->Field($properties); + return $this->renderField($properties); } /** @@ -54,9 +54,9 @@ public function FieldHolder($properties = []) * @param array $properties * @return DBHTMLText */ - public function SmallFieldHolder($properties = []) + public function renderSmallFieldHolder($properties = []) { - return $this->Field($properties); + return $this->renderField($properties); } /** diff --git a/src/Forms/DateField_Disabled.php b/src/Forms/DateField_Disabled.php index 6bdc64f882e..113ae0bb219 100644 --- a/src/Forms/DateField_Disabled.php +++ b/src/Forms/DateField_Disabled.php @@ -14,7 +14,7 @@ class DateField_Disabled extends DateField protected $disabled = true; - public function Field($properties = []) + public function renderField($properties = []) { // Default display value $displayValue = '(' . _t('SilverStripe\\Forms\\DateField.NOTSET', 'not set') . ')'; diff --git a/src/Forms/DropdownField.php b/src/Forms/DropdownField.php index ed5da300034..2dfba72b8c3 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() * ); * * @@ -128,7 +128,7 @@ public function getHasEmptyDefault() * @param array $properties * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { $options = []; @@ -141,6 +141,6 @@ public function Field($properties = []) 'Options' => new ArrayList($options) ]); - return parent::Field($properties); + return parent::renderField($properties); } } diff --git a/src/Forms/FieldGroup.php b/src/Forms/FieldGroup.php index 9a0d6c67588..1a14021a78c 100644 --- a/src/Forms/FieldGroup.php +++ b/src/Forms/FieldGroup.php @@ -9,8 +9,8 @@ * Lets you include a nested group of fields inside a template. * This control gives you more flexibility over form layout. * - * Note: the child fields within a field group aren't rendered using FieldHolder(). Instead, - * SmallFieldHolder() is called, which just prefixes $Field with a tag, if the Title is set. + * Note: the child fields within a field group aren't rendered using renderFieldHolder(). Instead, + * renderSmallFieldHolder() is called, which just prefixes $Field with a tag, if the Title is set. * * Usage * @@ -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/FieldList.php b/src/Forms/FieldList.php index d0dc54ac822..2e7cd0ec889 100644 --- a/src/Forms/FieldList.php +++ b/src/Forms/FieldList.php @@ -800,13 +800,13 @@ public function fieldPosition(string|FormField $field): int|false } /** - * Default template rendering of a FieldList will concatenate all FieldHolder values. + * Default template rendering of a FieldList will concatenate all renderFieldHolder values. */ public function forTemplate(): string { $output = ""; foreach ($this as $field) { - $output .= $field->FieldHolder(); + $output .= $field->renderFieldHolder(); } return $output; } diff --git a/src/Forms/FileField.php b/src/Forms/FileField.php index 8040d1fd594..7a3153b6ea4 100644 --- a/src/Forms/FileField.php +++ b/src/Forms/FileField.php @@ -79,13 +79,13 @@ public function __construct($name, $title = null, $value = null) * @param array $properties * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { $properties = array_merge($properties, [ 'MaxFileSize' => $this->getValidator()->getAllowedMaxFileSize() ]); - return parent::Field($properties); + return parent::renderField($properties); } public function getAttributes() diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 7ce206f8d57..9d39615d70a 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -58,9 +58,9 @@ * template when called through GET instead of POST. * * By appending to this URL, you can render individual form elements - * through the {@link FormField->FieldHolder()} method. + * through the {@link FormField->renderFieldHolder()} method. * For example, the "URLSegment" field in a standard CMS form would be - * accessible through "admin/EditForm/field/URLSegment/FieldHolder". + * accessible through "admin/EditForm/field/URLSegment/renderFieldHolder". */ class Form extends ModelData implements HasRequestHandler { diff --git a/src/Forms/FormAction.php b/src/Forms/FormAction.php index d709ce0a0fa..f34de2078d2 100644 --- a/src/Forms/FormAction.php +++ b/src/Forms/FormAction.php @@ -144,7 +144,7 @@ public function setFullAction($fullAction) * @param array $properties * @return DBHTMLText */ - public function Field($properties = []) + public function renderField($properties = []) { $properties = array_merge( $properties, @@ -155,16 +155,16 @@ public function Field($properties = []) ] ); - return parent::Field($properties); + return parent::renderField($properties); } /** * @param array $properties * @return DBHTMLText */ - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { - return $this->Field($properties); + return $this->renderField($properties); } public function Type() diff --git a/src/Forms/FormField.php b/src/Forms/FormField.php index 0d210436b0f..69206ef5760 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. @@ -260,9 +261,9 @@ class FormField extends RequestHandler protected $schemaData = []; private static $casting = [ - 'FieldHolder' => 'HTMLFragment', - 'SmallFieldHolder' => 'HTMLFragment', - 'Field' => 'HTMLFragment', + 'renderFieldHolder' => 'HTMLFragment', + 'renderSmallFieldHolder' => 'HTMLFragment', + 'renderField' => 'HTMLFragment', 'AttributesHTML' => 'HTMLFragment', // property $AttributesHTML version 'getAttributesHTML' => 'HTMLFragment', // method $getAttributesHTML($arg) version 'Value' => 'Text', @@ -458,7 +459,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 +470,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) { @@ -584,7 +587,7 @@ public function extraClass() // Allow custom styling of any element in the container based on validation errors, // e.g. red borders on input tags. // - // CSS class needs to be different from the one rendered through {@link FieldHolder()}. + // CSS class needs to be different from the one rendered through {@link renderFieldHolder()}. if ($this->getMessage()) { $classes[] = 'holder-' . $this->getMessageType(); } @@ -914,7 +917,7 @@ public function setSmallFieldHolderTemplate($smallFieldHolderTemplate) * @param array $properties * @return DBHTMLText */ - public function Field($properties = []) + public function renderField($properties = []) { $context = $this; @@ -949,7 +952,7 @@ public function Field($properties = []) * * @return DBHTMLText */ - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { $context = $this; @@ -969,7 +972,7 @@ public function FieldHolder($properties = []) * * @return string */ - public function SmallFieldHolder($properties = []) + public function renderSmallFieldHolder($properties = []) { $context = $this; @@ -1289,7 +1292,7 @@ public function debug(): string */ public function forTemplate(): string { - return $this->Field(); + return $this->renderField(); } /** @@ -1469,12 +1472,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/GridField.php b/src/Forms/GridField/GridField.php index 12b2522c087..9e3dba8a0b0 100644 --- a/src/Forms/GridField/GridField.php +++ b/src/Forms/GridField/GridField.php @@ -528,7 +528,7 @@ public function addAllStateToUrl(string $link): string * @param array $properties * @return string */ - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { $this->extend('onBeforeRenderHolder', $this, $properties); @@ -852,10 +852,10 @@ protected function newRowClasses($total, $index, $record) * @param array $properties * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { $this->extend('onBeforeRender', $this); - return $this->FieldHolder($properties); + return $this->renderFieldHolder($properties); } /** @@ -1160,7 +1160,7 @@ public function gridFieldAlterAction($data, $form, HTTPRequest $request) if ($this->getState()->Readonly === true) { $this->performDisabledTransformation(); } - return $this->FieldHolder(); + return $this->renderFieldHolder(); } return $form->forTemplate(); diff --git a/src/Forms/GridField/GridFieldDeleteAction.php b/src/Forms/GridField/GridFieldDeleteAction.php index dd9ca689eff..884d5c2f111 100644 --- a/src/Forms/GridField/GridFieldDeleteAction.php +++ b/src/Forms/GridField/GridFieldDeleteAction.php @@ -164,7 +164,7 @@ public function getColumnContent($gridField, $record, $columnName) $field = $this->getRemoveAction($gridField, $record, $columnName); if ($field) { - return $field->Field(); + return $field->renderField(); } return null; diff --git a/src/Forms/GridField/GridFieldExportButton.php b/src/Forms/GridField/GridFieldExportButton.php index 6b543b8c49e..0fb2361f314 100644 --- a/src/Forms/GridField/GridFieldExportButton.php +++ b/src/Forms/GridField/GridFieldExportButton.php @@ -80,7 +80,7 @@ public function getHTMLFragments($gridField) $button->addExtraClass('btn btn-secondary no-ajax font-icon-down-circled action_export'); $button->setForm($gridField->getForm()); return [ - $this->targetFragment => $button->Field(), + $this->targetFragment => $button->renderField(), ]; } diff --git a/src/Forms/GridField/GridFieldImportButton.php b/src/Forms/GridField/GridFieldImportButton.php index 4f0ba641ae5..a1e5e7b7d1a 100644 --- a/src/Forms/GridField/GridFieldImportButton.php +++ b/src/Forms/GridField/GridFieldImportButton.php @@ -86,7 +86,7 @@ public function getHTMLFragments($gridField) } return [ - $this->targetFragment => $button->Field() + $this->targetFragment => $button->renderField() ]; } diff --git a/src/Forms/GridField/GridFieldPrintButton.php b/src/Forms/GridField/GridFieldPrintButton.php index bebc63d65a6..6e892e17ee4 100644 --- a/src/Forms/GridField/GridFieldPrintButton.php +++ b/src/Forms/GridField/GridFieldPrintButton.php @@ -72,7 +72,7 @@ public function getHTMLFragments($gridField) $button->addExtraClass('font-icon-print grid-print-button btn btn-secondary'); return [ - $this->targetFragment => $button->Field(), + $this->targetFragment => $button->renderField(), ]; } diff --git a/src/Forms/GridField/GridState_Component.php b/src/Forms/GridField/GridState_Component.php index b1f67272f3b..5732a07528c 100644 --- a/src/Forms/GridField/GridState_Component.php +++ b/src/Forms/GridField/GridState_Component.php @@ -11,7 +11,7 @@ class GridState_Component extends AbstractGridFieldComponent implements GridFiel public function getHTMLFragments($gridField) { return [ - 'before' => $gridField->getState(false)->Field() + 'before' => $gridField->getState(false)->renderField() ]; } } diff --git a/src/Forms/HTMLEditor/HTMLEditorField.php b/src/Forms/HTMLEditor/HTMLEditorField.php index 90c3fad75c1..2e8e9d346d9 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.' ); @@ -170,11 +168,11 @@ public function performDisabledTransformation() return $this->performReadonlyTransformation(); } - public function Field($properties = []) + public function renderField($properties = []) { // Include requirements $this->getEditorConfig()->init(); - return parent::Field($properties); + return parent::renderField($properties); } public function getSchemaStateDefaults() @@ -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/HTMLReadonlyField.php b/src/Forms/HTMLReadonlyField.php index 64bb339e30f..5245fcd5384 100644 --- a/src/Forms/HTMLReadonlyField.php +++ b/src/Forms/HTMLReadonlyField.php @@ -21,7 +21,7 @@ class HTMLReadonlyField extends ReadonlyField */ protected $schemaComponent = 'HtmlReadonlyField'; - public function Field($properties = []) + public function renderField($properties = []) { return $this->renderWith($this->getTemplates()); } diff --git a/src/Forms/HiddenField.php b/src/Forms/HiddenField.php index 3c5ce588960..d18d61e5099 100644 --- a/src/Forms/HiddenField.php +++ b/src/Forms/HiddenField.php @@ -16,9 +16,9 @@ class HiddenField extends FormField * @param array $properties * @return string */ - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { - return $this->Field($properties); + return $this->renderField($properties); } /** @@ -41,8 +41,8 @@ public function IsHidden() return true; } - function SmallFieldHolder($properties = []) + function renderSmallFieldHolder($properties = []) { - return $this->FieldHolder($properties); + return $this->renderFieldHolder($properties); } } diff --git a/src/Forms/ListboxField.php b/src/Forms/ListboxField.php index 5ff18afff0c..6f2599bc45e 100644 --- a/src/Forms/ListboxField.php +++ b/src/Forms/ListboxField.php @@ -72,13 +72,13 @@ public function __construct($name, $title = '', $source = [], $value = null, $si * @param array $properties * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { $properties = array_merge($properties, [ 'Options' => $this->getOptions(), ]); - return FormField::Field($properties); + return FormField::renderField($properties); } /** diff --git a/src/Forms/LiteralField.php b/src/Forms/LiteralField.php index 0dda550c0c3..5edce17a4e2 100644 --- a/src/Forms/LiteralField.php +++ b/src/Forms/LiteralField.php @@ -49,7 +49,7 @@ public function __construct($name, $content) * * @return string */ - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { if ($this->content instanceof ModelData) { $context = $this->content; @@ -69,9 +69,9 @@ public function FieldHolder($properties = []) * * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { - return $this->FieldHolder($properties); + return $this->renderFieldHolder($properties); } /** @@ -130,7 +130,7 @@ public function performReadonlyTransformation() public function getSchemaStateDefaults() { $state = parent::getSchemaStateDefaults(); - $state['value'] = $this->FieldHolder(); + $state['value'] = $this->renderFieldHolder(); return $state; } diff --git a/src/Forms/LookupField.php b/src/Forms/LookupField.php index ac388fc394b..02af08cdde2 100644 --- a/src/Forms/LookupField.php +++ b/src/Forms/LookupField.php @@ -29,7 +29,7 @@ class LookupField extends MultiSelectField * * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { $source = ArrayLib::flatten($this->getSource()); $values = $this->getValueArray(); @@ -62,7 +62,7 @@ public function Field($properties = []) 'InputValue' => $inputValue ]); - return parent::Field($properties); + return parent::renderField($properties); } /** diff --git a/src/Forms/NullableField.php b/src/Forms/NullableField.php index 99669cd0ab8..0fd99e454c1 100644 --- a/src/Forms/NullableField.php +++ b/src/Forms/NullableField.php @@ -108,7 +108,7 @@ public function getIsNullId() * * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { if ($this->isReadonly()) { $nullableCheckbox = new CheckboxField_Readonly($this->getIsNullId()); @@ -120,8 +120,8 @@ public function Field($properties = []) return sprintf( '%s %s %s', - $this->valueField->Field(), - $nullableCheckbox->Field(), + $this->valueField->renderField(), + $nullableCheckbox->renderField(), $this->getIsNullLabel() ); } diff --git a/src/Forms/OptionsetField.php b/src/Forms/OptionsetField.php index 72655646c36..52222334198 100644 --- a/src/Forms/OptionsetField.php +++ b/src/Forms/OptionsetField.php @@ -116,7 +116,7 @@ protected function getOptionClass($value, $odd) } - public function Field($properties = []) + public function renderField($properties = []) { $options = []; $odd = false; @@ -131,7 +131,7 @@ public function Field($properties = []) 'Options' => new ArrayList($options) ]); - return FormField::Field($properties); + return FormField::renderField($properties); } /** diff --git a/src/Forms/PrintableTransformation_TabSet.php b/src/Forms/PrintableTransformation_TabSet.php index a457e7d0172..c61541cdeca 100644 --- a/src/Forms/PrintableTransformation_TabSet.php +++ b/src/Forms/PrintableTransformation_TabSet.php @@ -16,14 +16,14 @@ public function __construct($tabs) CompositeField::__construct($tabs); } - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { // This gives us support for sub-tabs. $tag = $this->getTabSet() ? 'h2>' : 'h1>'; $retVal = ''; foreach ($this->getChildren() as $tab) { $retVal .= "<$tag" . $tab->Title() . "$tag\n"; - $retVal .= $tab->FieldHolder(); + $retVal .= $tab->renderFieldHolder(); } return $retVal; } diff --git a/src/Forms/SearchableDropdownTrait.php b/src/Forms/SearchableDropdownTrait.php index fa751c49a2f..2fc38b8609d 100644 --- a/src/Forms/SearchableDropdownTrait.php +++ b/src/Forms/SearchableDropdownTrait.php @@ -265,7 +265,7 @@ public function getSource(): array return $this->getListMap($this->sourceList); } - public function Field($properties = []) + public function renderField($properties = []) { $context = $this; $this->extend('onBeforeRender', $context, $properties); diff --git a/src/Forms/SelectionGroup.php b/src/Forms/SelectionGroup.php index d814960f6bb..ead78a7c1d8 100644 --- a/src/Forms/SelectionGroup.php +++ b/src/Forms/SelectionGroup.php @@ -117,8 +117,8 @@ public function hasData() return true; } - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { - return parent::FieldHolder($properties); + return parent::renderFieldHolder($properties); } } diff --git a/src/Forms/SingleLookupField.php b/src/Forms/SingleLookupField.php index 5590574caa1..01d9a09a44f 100644 --- a/src/Forms/SingleLookupField.php +++ b/src/Forms/SingleLookupField.php @@ -113,7 +113,7 @@ public function getTemplate() * * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { $label = $this->valueToLabel(); if (!is_null($label)) { @@ -129,6 +129,6 @@ public function Field($properties = []) 'InputValue' => $inputValue ]); - return parent::Field($properties); + return parent::renderField($properties); } } diff --git a/src/Forms/TabSet.php b/src/Forms/TabSet.php index 47037193e7a..ec9ba47613c 100644 --- a/src/Forms/TabSet.php +++ b/src/Forms/TabSet.php @@ -129,7 +129,7 @@ public function setID($id) * @param array $properties * @return DBHTMLText|string */ - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { $obj = $properties ? $this->customise($properties) : $this; diff --git a/src/Forms/ToggleCompositeField.php b/src/Forms/ToggleCompositeField.php index 52e421e0c5c..a3de4edc9a0 100644 --- a/src/Forms/ToggleCompositeField.php +++ b/src/Forms/ToggleCompositeField.php @@ -42,7 +42,7 @@ public function __construct($name, $title, $children) * @param array $properties * @return string */ - public function FieldHolder($properties = []) + public function renderFieldHolder($properties = []) { $context = $this; diff --git a/src/Forms/TreeDropdownField.php b/src/Forms/TreeDropdownField.php index c503c591aa3..1019736ff36 100644 --- a/src/Forms/TreeDropdownField.php +++ b/src/Forms/TreeDropdownField.php @@ -870,14 +870,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/TreeDropdownField_Readonly.php b/src/Forms/TreeDropdownField_Readonly.php index d54b1736538..67e748de0db 100644 --- a/src/Forms/TreeDropdownField_Readonly.php +++ b/src/Forms/TreeDropdownField_Readonly.php @@ -6,7 +6,7 @@ class TreeDropdownField_Readonly extends TreeDropdownField { protected $readonly = true; - public function Field($properties = []) + public function renderField($properties = []) { $fieldName = $this->getTitleField(); if ($this->value) { @@ -20,6 +20,6 @@ public function Field($properties = []) $field = LookupField::create($this->name, $this->title, $source); $field->setValue($this->value); $field->setForm($this->form); - return $field->Field(); + return $field->renderField(); } } diff --git a/src/Forms/TreeMultiselectField.php b/src/Forms/TreeMultiselectField.php index a1362f24715..9d624c2355f 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; @@ -200,7 +200,7 @@ public function dataValue() * @param array $properties * @return DBHTMLText */ - public function Field($properties = []) + public function renderField($properties = []) { $value = ''; $titleArray = []; @@ -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()}); } @@ -239,7 +239,7 @@ public function Field($properties = []) 'Value' => $value ] ); - return FormField::Field($properties); + return FormField::renderField($properties); } /** diff --git a/src/Forms/TreeMultiselectField_Readonly.php b/src/Forms/TreeMultiselectField_Readonly.php index 65d23b17c16..ca979f948e9 100644 --- a/src/Forms/TreeMultiselectField_Readonly.php +++ b/src/Forms/TreeMultiselectField_Readonly.php @@ -7,7 +7,7 @@ class TreeMultiselectField_Readonly extends TreeMultiselectField protected $readonly = true; - public function Field($properties = []) + public function renderField($properties = []) { // Build list of titles $titleField = $this->getTitleField(); @@ -35,6 +35,6 @@ public function Field($properties = []) $valueField->setValue($itemIDsList); $valueField->setForm($this->form); - return $field->Field() . $valueField->Field(); + return $field->renderField() . $valueField->renderField(); } } diff --git a/src/Model/List/ListDecorator.php b/src/Model/List/ListDecorator.php index 6cfc963b425..fa3c43dae8b 100644 --- a/src/Model/List/ListDecorator.php +++ b/src/Model/List/ListDecorator.php @@ -56,7 +56,9 @@ public function getList(): SS_List&Sortable&Filterable&Limitable public function setList(SS_List&Sortable&Filterable&Limitable $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 04d5a1fc027..8eff38e2b3a 100644 --- a/src/Model/ModelData.php +++ b/src/Model/ModelData.php @@ -7,19 +7,22 @@ use LogicException; use ReflectionMethod; use ReflectionProperty; +use SilverStripe\Control\RSS\RSSFeed; +use SilverStripe\Control\RSS\RSSFeed_Entry; +use SilverStripe\Control\Tests\RSS\RSSFeedTest\ItemB; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Configurable; 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\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 Stringable; use UnexpectedValueException; /** @@ -29,7 +32,7 @@ * is provided and automatically escaped by ModelData. Any class that needs to be available to a view (controllers, * {@link DataObject}s, page controls) should inherit from this class. */ -class ModelData +class ModelData implements Stringable { use Extensible { defineMethods as extensibleDefineMethods; @@ -38,7 +41,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 ( @@ -47,16 +50,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 @@ -251,8 +256,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 */ @@ -262,7 +266,7 @@ public function defineMethods() throw new LogicException("ModelData::\$failover set to a non-object"); } if ($this->failover) { - $this->addMethodsFrom('failover'); + $this->addMethodsFrom('failover', static: false); if (isset($_REQUEST['debugfailover'])) { $class = static::class; @@ -305,12 +309,18 @@ public function exists(): bool return true; } + public function __toString(): string + { + return $this->forTemplate(); + } + /** - * Return the class name (though subclasses may return something else) + * 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 __toString(): string + public function forTemplate(): string { - return static::class; + return $this->renderWith($this->getViewerTemplates()); } public function getCustomisedObj(): ?ModelData @@ -326,14 +336,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. @@ -346,67 +352,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. - */ - public function castingClass(string $field): string - { - // Strip arguments - $spec = $this->castingHelper($field); - return trim(strtok($spec ?? '', '(') ?? ''); - } - - /** - * Return the string-format type for the given field. - * - * @return string 'xml'|'raw' - */ - public function escapeTypeForField(string $field): string - { - $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 ------------------------------------------------------------------------------------------- /** @@ -440,27 +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 - */ - protected function objCacheName($fieldName, $arguments) - { - return $arguments - ? $fieldName . ":" . var_export($arguments, true) - : $fieldName; - } - - /** - * Get a cached value from the field cache - * - * @param string $key Cache key - * @return mixed + * Get a cached value from the field cache for a field */ - 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]; } @@ -468,14 +406,11 @@ 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; } @@ -485,7 +420,7 @@ protected function objCacheSet($key, $value) * * @return $this */ - protected function objCacheClear() + public function objCacheClear() { $this->objCache = []; return $this; @@ -497,82 +432,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 { - $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 + $getter = "get{$fieldName}"; + $hasGetter = $this->hasMethod($getter) && $this->isAccessibleMethod($getter); + if ($hasGetter || $this->hasField($fieldName)) { + $hasObj = true; + if (!empty($arguments) && $hasGetter) { + $value = call_user_func_array([$this, $getter], $arguments); + } else { + // __get() uses the getter if there is one, so just rely on it + // to get the value correctly + $value = $this->$fieldName; + } + } elseif ($this->hasMethod($fieldName)) { + $hasObj = true; + $value = call_user_func_array([$this, $fieldName], $arguments ?: []); } - } - - // 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 && $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 - */ - public function cachedCall(string $fieldName, array $arguments = [], ?string $cacheName = null): object - { - return $this->obj($fieldName, $arguments, true, $cacheName); + return CastingService::singleton()->cast($value, $this, $fieldName, true); } /** @@ -677,4 +576,14 @@ public function Debug(): ModelData|string { return ModelDataDebugger::create($this); } + + /** + * Generate the cache name for a field + */ + private function objCacheName(string $fieldName, array $arguments = []): string + { + return empty($arguments) + ? $fieldName + : $fieldName . ":" . var_export($arguments, true); + } } diff --git a/src/Model/ModelDataCustomised.php b/src/Model/ModelDataCustomised.php index 6ae73be21ac..1ca66d6560a 100644 --- a/src/Model/ModelDataCustomised.php +++ b/src/Model/ModelDataCustomised.php @@ -49,17 +49,22 @@ public function __isset(string $property): bool return isset($this->customised->$property) || isset($this->original->$property) || parent::__isset($property); } + public function forTemplate(): string + { + 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( diff --git a/src/ORM/DataList.php b/src/ORM/DataList.php index d703d3b9050..e8d69f27f2e 100644 --- a/src/ORM/DataList.php +++ b/src/ORM/DataList.php @@ -19,6 +19,7 @@ use SilverStripe\Model\List\Map; use SilverStripe\Model\List\Sortable; use SilverStripe\Model\List\SS_List; +use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\Filters\SearchFilterable; /** @@ -1852,7 +1853,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 2b6bed1dae7..91325064565 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 @@ -3033,7 +3030,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) { @@ -3051,7 +3048,7 @@ public function castingHelper(string $field, bool $useFallback = true): ?string } } - return parent::castingHelper($field, $useFallback); + return parent::castingHelper($field); } /** @@ -3234,11 +3231,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(); @@ -3306,7 +3303,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)) { @@ -4399,7 +4396,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 d65a49d3767..ad53ad42e3c 100644 --- a/src/ORM/EagerLoadedList.php +++ b/src/ORM/EagerLoadedList.php @@ -171,7 +171,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/DBField.php b/src/ORM/FieldType/DBField.php index 38efb57583f..8d7254aa3ff 100644 --- a/src/ORM/FieldType/DBField.php +++ b/src/ORM/FieldType/DBField.php @@ -520,11 +520,6 @@ public function debug(): string DBG; } - public function __toString(): string - { - return (string)$this->forTemplate(); - } - public function getArrayValue() { return $this->arrayValue; 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/Relation.php b/src/ORM/Relation.php index 62b2b266cb2..93c63e961ff 100644 --- a/src/ORM/Relation.php +++ b/src/ORM/Relation.php @@ -45,9 +45,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 e01ff241e17..ab2780288bb 100644 --- a/src/ORM/UnsavedRelationList.php +++ b/src/ORM/UnsavedRelationList.php @@ -307,11 +307,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 cfcee6b786a..328c8ebf0ca 100644 --- a/src/Security/Member.php +++ b/src/Security/Member.php @@ -342,7 +342,7 @@ public function isLockedOut() { /** @var DBDatetime $lockedOutUntilObj */ $lockedOutUntilObj = $this->dbObject('LockedOutUntil'); - if ($lockedOutUntilObj->InFuture()) { + if ($lockedOutUntilObj?->InFuture()) { return true; } @@ -369,7 +369,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; @@ -429,7 +429,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..08c353e3b04 100644 --- a/src/Security/PermissionCheckboxSetField.php +++ b/src/Security/PermissionCheckboxSetField.php @@ -89,7 +89,7 @@ public function getHiddenPermissions() * @param array $properties * @return string */ - public function Field($properties = []) + public function renderField($properties = []) { $uninheritedCodes = []; $inheritedCodes = []; @@ -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..0d6f7510b7e --- /dev/null +++ b/src/View/CastingService.php @@ -0,0 +1,100 @@ +castingHelper($fieldName); + } + + // Cast to object if there's an explicit casting for this field + // Explicit casts take precedence over array casting + if ($service) { + $castObject = Injector::inst()->create($service, $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 + $service = $this->defaultService($data, $source, $fieldName); + $castObject = Injector::inst()->create($service, $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 defaultService(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->defaultService($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/SSTemplateParser.peg b/src/View/SSTemplateParser.peg index b893ef4ae5e..cc5c28d2e72 100644 --- a/src/View/SSTemplateParser.peg +++ b/src/View/SSTemplateParser.peg @@ -247,7 +247,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 +274,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) @@ -286,15 +286,17 @@ class SSTemplateParser extends Parser implements TemplateParser if (isset($sub['Call']['CallArguments']) && isset($sub['Call']['CallArguments']['php'])) { $arguments = $sub['Call']['CallArguments']['php']; - $res['php'] .= "->$method('$property', [$arguments], true)"; + $type = ViewLayerData::TYPE_METHOD; + $res['php'] .= "->$method('$property', [$arguments], '$type')"; } else { - $res['php'] .= "->$method('$property', [], true)"; + $type = ViewLayerData::TYPE_PROPERTY; + $res['php'] .= "->$method('$property', [], '$type')"; } } function Lookup_LookupStep(&$res, $sub) { - $this->Lookup_AddLookupStep($res, $sub, 'obj'); + $this->Lookup_AddLookupStep($res, $sub, 'scopeToIntermediateValue'); } function Lookup_LastLookupStep(&$res, $sub) @@ -357,7 +359,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 +394,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 +537,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'] ?? ''); } } @@ -567,7 +569,7 @@ class SSTemplateParser extends Parser implements TemplateParser } 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 + // Lookup_LastLookupStep and Argument_BareWord can produce hasValue instead of getOutputValue $res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? ''); } } @@ -697,7 +699,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'] ?? ''); } /*!* @@ -827,7 +829,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 +917,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; } } @@ -948,7 +950,7 @@ class SSTemplateParser extends Parser implements TemplateParser $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->getItem(), [' . + $res['php'] = '$val .= \\SilverStripe\\View\\SSViewer::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' . implode(',', $arguments)."], \$scope, true);\n"; if ($this->includeDebuggingComments) { // Add include filename comments on dev sites @@ -1037,7 +1039,8 @@ class SSTemplateParser extends Parser implements TemplateParser //loop without arguments loops on the current scope if ($res['ArgumentCount'] == 0) { - $on = '$scope->locally()->obj(\'Me\', [], true)'; + $type = ViewLayerData::TYPE_METHOD; + $on = "\$scope->locally()->scopeToIntermediateValue('Me', [], '$type')"; // @TODO use self instead or move $Me to scope explicitly } else { //loop in the normal way $arg = $res['Arguments'][0]; if ($arg['ArgumentMode'] == 'string') { @@ -1045,13 +1048,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 +1074,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 . @@ -1118,6 +1121,7 @@ class SSTemplateParser extends Parser implements TemplateParser /** * This is an open block handler, for the <% debug %> utility tag + * @TODO find out if this even works in CMS 5, and if so make sure it keeps working */ function OpenBlock_Handle_Debug(&$res) { diff --git a/src/View/SSTemplateParser.php b/src/View/SSTemplateParser.php index 4e48424898a..7731704e95e 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) @@ -777,15 +777,17 @@ function Lookup_AddLookupStep(&$res, $sub, $method) if (isset($sub['Call']['CallArguments']) && isset($sub['Call']['CallArguments']['php'])) { $arguments = $sub['Call']['CallArguments']['php']; - $res['php'] .= "->$method('$property', [$arguments], true)"; + $type = ViewLayerData::TYPE_METHOD; + $res['php'] .= "->$method('$property', [$arguments], '$type')"; } else { - $res['php'] .= "->$method('$property', [], true)"; + $type = ViewLayerData::TYPE_PROPERTY; + $res['php'] .= "->$method('$property', [], '$type')"; } } function Lookup_LookupStep(&$res, $sub) { - $this->Lookup_AddLookupStep($res, $sub, 'obj'); + $this->Lookup_AddLookupStep($res, $sub, 'scopeToIntermediateValue'); } function Lookup_LastLookupStep(&$res, $sub) @@ -1009,7 +1011,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 +1160,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 +1189,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 +1199,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 +1820,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 +1842,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); @@ -1887,7 +1889,7 @@ function PresenceCheck_Argument(&$res, $sub) } 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 + // Lookup_LastLookupStep and Argument_BareWord can produce hasValue instead of getOutputValue $res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? ''); } } @@ -2235,7 +2237,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 +2472,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 +2742,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 +3168,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 +3227,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; @@ -3587,7 +3589,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 +3794,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; } } @@ -3897,7 +3899,7 @@ function Include__finalise(&$res) $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->getItem(), [' . + $res['php'] = '$val .= \\SilverStripe\\View\\SSViewer::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 +4167,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); @@ -4265,7 +4267,8 @@ function ClosedBlock_Handle_Loop(&$res) //loop without arguments loops on the current scope if ($res['ArgumentCount'] == 0) { - $on = '$scope->locally()->obj(\'Me\', [], true)'; + $type = ViewLayerData::TYPE_METHOD; + $on = "\$scope->locally()->scopeToIntermediateValue('Me', [], '$type')"; // @TODO use self instead or move $Me to scope explicitly } else { //loop in the normal way $arg = $res['Arguments'][0]; if ($arg['ArgumentMode'] == 'string') { @@ -4273,13 +4276,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 +4302,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 . @@ -4403,6 +4406,7 @@ function OpenBlock__finalise(&$res) /** * This is an open block handler, for the <% debug %> utility tag + * @TODO find out if this even works in CMS 5, and if so make sure it keeps working */ function OpenBlock_Handle_Debug(&$res) { @@ -4575,7 +4579,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; } diff --git a/src/View/SSViewer.php b/src/View/SSViewer.php index 63f0edc344c..987ad5c4399 100644 --- a/src/View/SSViewer.php +++ b/src/View/SSViewer.php @@ -15,6 +15,7 @@ use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\Security\Permission; use InvalidArgumentException; +use RuntimeException; use SilverStripe\Model\ModelData; /** @@ -550,10 +551,10 @@ public function includeRequirements($incl = true) * 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 ViewLayerData $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 ModelData $inheritedScope The current scope of a parent template including a sub-template + * @param SSViewer_Scope $inheritedScope The current scope of a parent template including a sub-template * @return string The result of executing the template */ protected function includeGeneratedTemplate($cacheFile, $item, $overlay, $underlay, $inheritedScope = null) @@ -569,7 +570,7 @@ protected function includeGeneratedTemplate($cacheFile, $item, $overlay, $underl } $cache = $this->getPartialCacheStore(); - $scope = new SSViewer_DataPresenter($item, $overlay, $underlay, $inheritedScope); + $scope = new SSViewer_Scope($item, $overlay, $underlay, $inheritedScope); $val = ''; // Placeholder for values exposed to $cacheFile @@ -597,6 +598,7 @@ protected function includeGeneratedTemplate($cacheFile, $item, $overlay, $underl */ public function process($item, $arguments = null, $inheritedScope = null) { + $item = ViewLayerData::create($item); // Set hashlinks and temporarily modify global state $rewrite = $this->getRewriteHashLinks(); $origRewriteDefault = static::getRewriteHashLinksDefault(); @@ -606,6 +608,10 @@ public function process($item, $arguments = null, $inheritedScope = null) $template = $this->chosen; + if (!$template) { + throw new RuntimeException('No template to render'); + } + $cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . '.cache' . str_replace(['\\','/',':'], '.', Director::makeRelative(realpath($template ?? '')) ?? ''); $lastEdited = filemtime($template ?? ''); diff --git a/src/View/SSViewer_DataPresenter.php b/src/View/SSViewer_DataPresenter.php deleted file mode 100644 index 0729584a22e..00000000000 --- a/src/View/SSViewer_DataPresenter.php +++ /dev/null @@ -1,449 +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 index c7712a9f2b3..407c6343d94 100644 --- a/src/View/SSViewer_FromString.php +++ b/src/View/SSViewer_FromString.php @@ -50,6 +50,7 @@ public function __construct($content, TemplateParser $parser = null) */ public function process($item, $arguments = null, $scope = null) { + $item = ViewLayerData::create($item); $hash = sha1($this->content ?? ''); $cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . ".cache.$hash"; diff --git a/src/View/SSViewer_Scope.php b/src/View/SSViewer_Scope.php index 928b7b4a338..74c15f42746 100644 --- a/src/View/SSViewer_Scope.php +++ b/src/View/SSViewer_Scope.php @@ -4,12 +4,11 @@ use ArrayIterator; use Countable; +use InvalidArgumentException; use Iterator; -use SilverStripe\Model\List\ArrayList; -use SilverStripe\ORM\FieldType\DBBoolean; -use SilverStripe\ORM\FieldType\DBText; -use SilverStripe\ORM\FieldType\DBFloat; -use SilverStripe\ORM\FieldType\DBInt; +use LogicException; +use SilverStripe\Core\ClassInfo; +use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\FieldType\DBField; /** @@ -18,6 +17,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). * @@ -107,37 +110,73 @@ class SSViewer_Scope */ private $localIndex = 0; + /** + * List of global property providers + * + * @internal + * @var TemplateGlobalProvider[]|null + */ + private static $globalProperties = null; + + /** + * List of global iterator providers + * + * @internal + * @var TemplateIteratorProvider[]|null + */ + private static $iteratorProperties = null; + + /** + * Overlay variables. Take precedence over anything from the current scope + * + * @var array|null + */ + protected $overlay; + + /** + * Flag for whether overlay should be preserved when pushing a new scope + * + * @see SSViewer_Scope::pushScope() + * @var bool + */ + protected $preserveOverlay = false; + + /** + * Underlay variables. Concede precedence to overlay variables or anything from the current scope + * + * @var array + */ + protected $underlay; + /** * @var object $item * @var SSViewer_Scope $inheritedScope */ - public function __construct($item, SSViewer_Scope $inheritedScope = null) - { + public function __construct( + $item, + array $overlay = null, + array $underlay = null, + 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]; + + $this->overlay = $overlay ?: []; + $this->underlay = $underlay ?: []; + + $this->cacheGlobalProperties(); + $this->cacheIteratorProperties(); } /** - * Returns the current "active" item - * - * @return object + * Returns the current "current" item in scope */ - public function getItem() + public function getCurrentItem(): ?ViewLayerData { - $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); - } - - return $item; + return $this->itemIterator ? $this->itemIterator->current() : $this->item; } /** @@ -164,56 +203,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 - */ - public 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); - } - - /** - * @param string $name - * @param array $arguments - * @param bool $cache - * @param string $cacheName - * @return mixed + * Set scope to an intermediate value, which will be used for getting output later on. */ - public function getObj($name, $arguments = [], $cache = false, $cacheName = null) + public function scopeToIntermediateValue(string $name, array $arguments, string $type): static { - $on = $this->getItem(); - if ($on === null) { - return null; - } - return $on->obj($name, $arguments, $cache, $cacheName); - } + $overlayIndex = false; - /** - * @param string $name - * @param array $arguments - * @param bool $cache - * @param string $cacheName - * @return $this - */ - public function obj($name, $arguments = [], $cache = false, $cacheName = null) - { + // $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, @@ -224,6 +228,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, @@ -234,13 +240,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, $type); $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, @@ -254,12 +268,11 @@ public function obj($name, $arguments = [], $cache = false, $cacheName = null) /** * Gets the current object and resets the scope. - * - * @return object + * @TODO: Replace with $Me */ - public function self() + public function self(): ?ViewLayerData { - $result = $this->getItem(); + $result = $this->getCurrentItem(); $this->resetLocalScope(); return $result; @@ -268,9 +281,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; @@ -284,16 +301,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(); @@ -301,11 +340,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; @@ -349,23 +387,143 @@ 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 $type): string { - $on = $this->getItem(); - $retval = $on ? $on->$name(...$arguments) : null; + $retval = $this->getObj($name, $arguments, $type); + $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, string $type): mixed + { + $retval = null; + + if ($this->hasOverlay($name)) { + $retval = $this->getOverlay($name, $arguments, true); + } else { + $on = $this->getCurrentItem(); + if ($on && isset($on->$name)) { + $retval = $on->getRawDataValue($name, $type, $arguments); + } + + if ($retval === null) { + $retval = $this->getUnderlay($name, $arguments, true); + } + } + + // if ($retval instanceof DBField) { + // $retval = $retval->getValue(); // Workaround because we're still calling obj in ViewLayerData + // } + + $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 + { + // @TODO: look for ways to remove the need to call hasValue (e.g. using isset($this->getCurrentItem()->$name) and an equivalent for over/underlays) + $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; } + /** + * @var string $interfaceToQuery + * @var string $variableMethod + * @var boolean $createObject + * @return array + */ + protected function getPropertiesFromProvider($interfaceToQuery, $variableMethod, $createObject = false) + { + $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]; + } + + // 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; + } + + /** + * 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 */ @@ -390,13 +548,175 @@ 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) { - return match (gettype($value)) { - 'boolean' => DBBoolean::create()->setValue($value), - 'string' => DBText::create()->setValue($value), - 'double' => DBFloat::create()->setValue($value), - 'integer' => DBInt::create()->setValue($value), - }; + 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 = $this->getPropertiesFromProvider( + TemplateGlobalProvider::class, + 'get_template_global_variables' + ); + } + + /** + * Build cache of global iterator properties + */ + protected function cacheIteratorProperties() + { + if (SSViewer_Scope::$iteratorProperties !== null) { + return; + } + + SSViewer_Scope::$iteratorProperties = $this->getPropertiesFromProvider( + TemplateIteratorProvider::class, + 'get_template_iterator_variables', + true // Call non-statically + ); + } + + protected function getObj(string $name, array $arguments, string $type): ?ViewLayerData + { + if ($this->hasOverlay($name)) { + return $this->getOverlay($name, $arguments); + } + + // @TODO caching + $on = $this->getCurrentItem(); + if ($on && isset($on->$name)) { + if ($type === ViewLayerData::TYPE_METHOD) { + return $on->$name(...$arguments); + } + // property + return $on->$name; + } + + 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 + { + // 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/ViewLayerData.php b/src/View/ViewLayerData.php new file mode 100644 index 00000000000..85cc55d3572 --- /dev/null +++ b/src/View/ViewLayerData.php @@ -0,0 +1,195 @@ +data; + } else { + $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. + * @TODO see if we can remove the need for this + */ + public function count(): int + { + if (is_countable($this->data)) { + return count($this->data); + } + if (ClassInfo::hasMethod($this->data, 'getIterator')) { + return count($this->data->getIterator()); + } + if (ClassInfo::hasMethod($this->data, 'count')) { + return $this->data->count(); + } + if (isset($this->data->count)) { + return $this->data->count; + } + return 0; + } + + public function getIterator(): Traversable + { + if (!is_iterable($this->data) && !ClassInfo::hasMethod($this->data, 'getIterator')) { + $type = is_object($this->data) ? get_class($this->data) : gettype($this->data); + throw new BadMethodCallException("$type is not iterable."); + } + + $iterator = $this->data; + if (!is_iterable($iterator)) { + $iterator = $this->data->getIterator(); + } + $source = $this->data instanceof ModelData ? $this->data : null; + foreach ($iterator as $item) { + yield $item === null ? null : ViewLayerData::create($item, $source); + } + } + + public function __isset(string $name): bool + { + // Might be worth reintroducing the way ss template engine checks if lists/countables "exist" here, + // i.e. if ($this->data->__isset($name) && is_countable($this->data->{$name})) { return count($this->data->{$name}) > 0; } + // In worst-case scenarios that would result in lazy-loading a value when we don't need to, but we already do that with the current system. + + // The SS template system uses `ModelData::hasValue()` rather than isset(), but using that doesn't check for methods and we can't use + // method_exists on ViewLayerData because the method just simply DOESN'T exist.... so. Hmm. + // UPDATE: Added ClassInfo::hasMethod here to simulate what ModelData does... will still have to check if it works with twig + // Removing method_exists check in scope for now. + return isset($this->data->$name) || ClassInfo::hasMethod($this->data, $name); + } + + public function __get(string $name): ?ViewLayerData + { + $value = $this->getRawDataValue($name, ViewLayerData::TYPE_PROPERTY); + if ($value === null) { + return null; + } + $source = $this->data instanceof ModelData ? $this->data : null; + return ViewLayerData::create($value, $source, $name); // @TODO maybe not return this here, but wrap it again in the next layer? This may not play nicely with twig when passing values into args? + } + + public function __call(string $name, array $arguments = []): ?ViewLayerData + { + $value = $this->getRawDataValue($name, ViewLayerData::TYPE_METHOD, $arguments); + if ($value === null) { + return null; + } + $source = $this->data instanceof ModelData ? $this->data : null; + return ViewLayerData::create($value, $source, $name); // @TODO maybe not return this here, but wrap it again in the next layer? This may not play nicely with twig when passing values into args? + } + + public function __toString(): string + { + if ($this->data instanceof ModelData) { + return $this->data->forTemplate(); + } + return (string) $this->data; + } + + // @TODO We need this right now for the ss template engine, but need to check if + // we can rely on it, since twig won't be calling this at all + public function hasDataValue(?string $name = null, array $arguments = []): bool + { + if ($name) { + if ($this->data instanceof ModelData) { + return $this->data->hasValue($name, $arguments); + } + return isset($this->$name); + } + if ($this->data instanceof ModelData) { + return $this->data->exists(); + } + return (bool) $this->data; + } + + // @TODO We need this right now for the ss template engine, but need to check if + // we can rely on it, since twig won't be calling this at all + public function getRawDataValue(string $name, string $type, array $arguments = []): mixed + { + if ($type !== ViewLayerData::TYPE_METHOD && $type !== ViewLayerData::TYPE_PROPERTY) { + throw new InvalidArgumentException('$type must be one of the TYPE_* constant values'); + } + + // Values from ModelData can be cached + if ($this->data instanceof ModelData) { + $cached = $this->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) + $fetchedValue = false; + + // Try a getter before anything else + // This matches the "magic" getter behaviour of ModelData across the board for consistent results + $getter = "get{$name}"; + if (ClassInfo::hasMethod($this->data, $getter)) { + $value = $this->data->$getter(...$arguments); + $fetchedValue = true; + } + + // Only try to get a property if we aren't explicitly trying to call a method + if (!$fetchedValue && $type === ViewLayerData::TYPE_PROPERTY) { + if (isset($this->data->$name)) { + $value = $this->data->$name; + $fetchedValue = true; + } + } + + // Fall back on calling a method even if we're fetching as a property + if (!$fetchedValue) { + // Get value from the method, if there is one + $hasMethod = ClassInfo::hasMethod($this->data, $name); + $dynamicMethods = !$hasMethod && method_exists($this->data, '__call'); + if ($hasMethod || $dynamicMethods) { + try { + $value = $this->data->$name(...$arguments); + } 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 (!$dynamicMethods) { + throw $e; + } + } + } + } + + // Caching for modeldata + if ($this->data instanceof ModelData) { + $this->data->objCacheSet($name, $arguments, $value); + } + + return $value; + } +} diff --git a/templates/RightLabelledFieldHolder.ss b/templates/RightLabelledFieldHolder.ss index 574ab26d429..2572b0fe9ee 100644 --- a/templates/RightLabelledFieldHolder.ss +++ b/templates/RightLabelledFieldHolder.ss @@ -1,5 +1,5 @@ - $Field + $renderField $Title <% if $Message %>$Message<% end_if %> diff --git a/templates/SilverStripe/Forms/CheckboxField_holder.ss b/templates/SilverStripe/Forms/CheckboxField_holder.ss index 1114073b7d9..a490ab22fdf 100644 --- a/templates/SilverStripe/Forms/CheckboxField_holder.ss +++ b/templates/SilverStripe/Forms/CheckboxField_holder.ss @@ -1,5 +1,5 @@ - $Field + $renderField $Title<% if $RightTitle %> $RightTitle<% end_if %> <% if $Message %>$Message<% end_if %> <% if $Description %>$Description<% end_if %> diff --git a/templates/SilverStripe/Forms/CheckboxField_holder_small.ss b/templates/SilverStripe/Forms/CheckboxField_holder_small.ss index 5d5c19e7327..1c006dedf30 100644 --- a/templates/SilverStripe/Forms/CheckboxField_holder_small.ss +++ b/templates/SilverStripe/Forms/CheckboxField_holder_small.ss @@ -1,4 +1,4 @@ -$Field +$renderField <% if $Title %> for="$ID"<% end_if %>>$Title<% if $RightTitle %> $RightTitle<% end_if %> diff --git a/templates/SilverStripe/Forms/CompositeField.ss b/templates/SilverStripe/Forms/CompositeField.ss index 3a42cd04266..86ec3a36dd4 100644 --- a/templates/SilverStripe/Forms/CompositeField.ss +++ b/templates/SilverStripe/Forms/CompositeField.ss @@ -1,9 +1,9 @@ <% loop $FieldList %> <% if $Up.ColumnCount %> - $FieldHolder + $renderFieldHolder <% else %> - $FieldHolder + $renderFieldHolder <% end_if %> <% end_loop %> diff --git a/templates/SilverStripe/Forms/CompositeField_holder.ss b/templates/SilverStripe/Forms/CompositeField_holder.ss index 9cb6a0f0603..0e096e9659c 100644 --- a/templates/SilverStripe/Forms/CompositeField_holder.ss +++ b/templates/SilverStripe/Forms/CompositeField_holder.ss @@ -3,5 +3,5 @@ $Legend <% end_if %> - $Field + $renderField $Tag> diff --git a/templates/SilverStripe/Forms/CompositeField_holder_small.ss b/templates/SilverStripe/Forms/CompositeField_holder_small.ss index c7b433f1372..9e4f2cb2ad3 100644 --- a/templates/SilverStripe/Forms/CompositeField_holder_small.ss +++ b/templates/SilverStripe/Forms/CompositeField_holder_small.ss @@ -6,10 +6,10 @@ <% loop $FieldList %> <% if $Up.ColumnCount %> - $SmallFieldHolder + $renderSmallFieldHolder <% else %> - $SmallFieldHolder + $renderSmallFieldHolder <% end_if %> <% end_loop %> $Tag> diff --git a/templates/SilverStripe/Forms/FieldGroup.ss b/templates/SilverStripe/Forms/FieldGroup.ss index c9b6eb94ad9..b3805f9cf2a 100644 --- a/templates/SilverStripe/Forms/FieldGroup.ss +++ b/templates/SilverStripe/Forms/FieldGroup.ss @@ -1,7 +1,7 @@ id="$ID"<% end_if %>> <% loop $FieldList %> - $SmallFieldHolder + $renderSmallFieldHolder <% end_loop %> diff --git a/templates/SilverStripe/Forms/FieldGroup_DefaultFieldHolder.ss b/templates/SilverStripe/Forms/FieldGroup_DefaultFieldHolder.ss index 5a1558e69d9..f2211decb35 100644 --- a/templates/SilverStripe/Forms/FieldGroup_DefaultFieldHolder.ss +++ b/templates/SilverStripe/Forms/FieldGroup_DefaultFieldHolder.ss @@ -1,7 +1,7 @@ id="$ID"<% end_if %>> <% loop $FieldList %> - $FieldHolder + $renderFieldHolder <% end_loop %> diff --git a/templates/SilverStripe/Forms/FieldGroup_holder.ss b/templates/SilverStripe/Forms/FieldGroup_holder.ss index 19b45bb1689..98a799985fd 100644 --- a/templates/SilverStripe/Forms/FieldGroup_holder.ss +++ b/templates/SilverStripe/Forms/FieldGroup_holder.ss @@ -4,7 +4,7 @@ <% loop $FieldList %> - $SmallFieldHolder + $renderSmallFieldHolder <% end_loop %> diff --git a/templates/SilverStripe/Forms/FormField_holder.ss b/templates/SilverStripe/Forms/FormField_holder.ss index df878d9ac4f..767c1282c33 100644 --- a/templates/SilverStripe/Forms/FormField_holder.ss +++ b/templates/SilverStripe/Forms/FormField_holder.ss @@ -1,7 +1,7 @@ <% if $Title %>$Title<% end_if %> - $Field + $renderField <% if $RightTitle %>$RightTitle<% end_if %> <% if $Message %>$Message<% end_if %> diff --git a/templates/SilverStripe/Forms/FormField_holder_small.ss b/templates/SilverStripe/Forms/FormField_holder_small.ss index 15b3280d573..8a6e981e8d2 100644 --- a/templates/SilverStripe/Forms/FormField_holder_small.ss +++ b/templates/SilverStripe/Forms/FormField_holder_small.ss @@ -1,5 +1,5 @@ <% if $Title %>for="$ID"<% end_if %>>$Title<% end_if %> - $Field + $renderField <% if $RightTitle %>for="$ID"<% end_if %>>$RightTitle<% end_if %> diff --git a/templates/SilverStripe/Forms/GridField/GridFieldAddExistingAutocompleter.ss b/templates/SilverStripe/Forms/GridField/GridFieldAddExistingAutocompleter.ss index cdf9e1daba1..6b08ba0810d 100644 --- a/templates/SilverStripe/Forms/GridField/GridFieldAddExistingAutocompleter.ss +++ b/templates/SilverStripe/Forms/GridField/GridFieldAddExistingAutocompleter.ss @@ -1,5 +1,5 @@ <% loop $Fields %> - $Field + $renderField <% end_loop %> diff --git a/templates/SilverStripe/Forms/GridField/GridFieldFilterHeader_Row.ss b/templates/SilverStripe/Forms/GridField/GridFieldFilterHeader_Row.ss index bed1ebf5dd8..e8f63be314c 100644 --- a/templates/SilverStripe/Forms/GridField/GridFieldFilterHeader_Row.ss +++ b/templates/SilverStripe/Forms/GridField/GridFieldFilterHeader_Row.ss @@ -1,7 +1,7 @@ <% if $Fields %> <% loop $Fields %> - $Field + $renderField <% end_loop %> <% end_if %> diff --git a/templates/SilverStripe/Forms/GridField/GridFieldSortableHeader_Row.ss b/templates/SilverStripe/Forms/GridField/GridFieldSortableHeader_Row.ss index 2b12b728a8e..17242d8da33 100644 --- a/templates/SilverStripe/Forms/GridField/GridFieldSortableHeader_Row.ss +++ b/templates/SilverStripe/Forms/GridField/GridFieldSortableHeader_Row.ss @@ -1,5 +1,5 @@ <% loop $Fields %> - $Field + $renderField <% end_loop %> diff --git a/templates/SilverStripe/Forms/HTMLEditor/HTMLEditorField_Toolbar_viewfile.ss b/templates/SilverStripe/Forms/HTMLEditor/HTMLEditorField_Toolbar_viewfile.ss index b7661c84967..6def1325010 100644 --- a/templates/SilverStripe/Forms/HTMLEditor/HTMLEditorField_Toolbar_viewfile.ss +++ b/templates/SilverStripe/Forms/HTMLEditor/HTMLEditorField_Toolbar_viewfile.ss @@ -40,7 +40,7 @@ <% loop $Fields %> - $FieldHolder + $renderFieldHolder <% end_loop %> diff --git a/templates/SilverStripe/Forms/Includes/Form.ss b/templates/SilverStripe/Forms/Includes/Form.ss index caee5ad7685..08fd1809995 100644 --- a/templates/SilverStripe/Forms/Includes/Form.ss +++ b/templates/SilverStripe/Forms/Includes/Form.ss @@ -10,7 +10,7 @@ <% if $Legend %>$Legend<% end_if %> <% loop $Fields %> - $FieldHolder + $renderFieldHolder <% end_loop %> @@ -18,7 +18,7 @@ <% if $Actions %> <% loop $Actions %> - $Field + $renderField <% end_loop %> <% end_if %> diff --git a/templates/SilverStripe/Forms/OptionsetField_holder.ss b/templates/SilverStripe/Forms/OptionsetField_holder.ss index dd566be383f..a5476e0e6c0 100644 --- a/templates/SilverStripe/Forms/OptionsetField_holder.ss +++ b/templates/SilverStripe/Forms/OptionsetField_holder.ss @@ -1,7 +1,7 @@ <% if $Title %>$Title<% end_if %> - $Field + $renderField <% if $RightTitle %>$RightTitle<% end_if %> <% if $Message %>$Message<% end_if %> diff --git a/templates/SilverStripe/Security/MemberAuthenticator/Includes/CMSMemberLoginForm.ss b/templates/SilverStripe/Security/MemberAuthenticator/Includes/CMSMemberLoginForm.ss index 319a9bf500f..fbdc42a2cf9 100644 --- a/templates/SilverStripe/Security/MemberAuthenticator/Includes/CMSMemberLoginForm.ss +++ b/templates/SilverStripe/Security/MemberAuthenticator/Includes/CMSMemberLoginForm.ss @@ -4,7 +4,7 @@ <% if $Legend %>$Legend<% end_if %> <% loop $Fields %> - $FieldHolder + $renderFieldHolder <% end_loop %> @@ -12,7 +12,7 @@ <% if $Actions %> <% loop $Actions %> - $Field + $renderField <% end_loop %> <% end_if %> diff --git a/templates/forms/HtmlEditorField_holder_small.ss b/templates/forms/HtmlEditorField_holder_small.ss index 21eec8dc53d..de708d313f0 100644 --- a/templates/forms/HtmlEditorField_holder_small.ss +++ b/templates/forms/HtmlEditorField_holder_small.ss @@ -1,5 +1,5 @@ <% if $Title %>for="$ID"<% end_if %>>$Title<% end_if %> - $Field + $renderField <% if $RightTitle %>for="$ID"<% end_if %>>$RightTitle<% end_if %> 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/Model/ModelDataTest.php b/tests/php/Model/ModelDataTest.php index 33f4b171d3a..f3e910b774d 100644 --- a/tests/php/Model/ModelDataTest.php +++ b/tests/php/Model/ModelDataTest.php @@ -122,7 +122,7 @@ public function testObjectCustomise() $this->assertEquals('casted', $newModelData->XML_val('alwaysCasted')); $this->assertEquals('castable', $modelData->forTemplate()); - $this->assertEquals('casted', $newModelData->forTemplate()); + $this->assertEquals('castable', $newModelData->forTemplate()); } public function testDefaultValueWrapping() @@ -139,25 +139,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(); 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/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/View/SSViewerTest.php b/tests/php/View/SSViewerTest.php index d9de2385f30..881010f5ac3 100644 --- a/tests/php/View/SSViewerTest.php +++ b/tests/php/View/SSViewerTest.php @@ -360,8 +360,9 @@ public function testGlobalVariablesAreEscaped() 'zz', $this->render('$SSViewerTest_GlobalThatTakesArguments($SSViewerTest_GlobalHTMLFragment)') ); + // Don't escape value when passing into a method call $this->assertEquals( - 'z<div></div>z', + 'zz', $this->render('$SSViewerTest_GlobalThatTakesArguments($SSViewerTest_GlobalHTMLEscaped)') ); } @@ -1118,57 +1119,59 @@ public function testBaseTagGeneration() public function testIncludeWithArguments() { $this->assertEquals( - $this->render('<% include SSViewerTestIncludeWithArguments %>'), - '[out:Arg1][out:Arg2][out:Arg2.Count]' + '[out:Arg1][out:Arg2][out:Arg2.Count]', + $this->render('<% include SSViewerTestIncludeWithArguments %>') ); $this->assertEquals( - $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A %>'), - 'A[out:Arg2][out:Arg2.Count]' + 'A[out:Arg2][out:Arg2.Count]', + $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A %>') ); $this->assertEquals( - $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A, Arg2=B %>'), - 'AB' + 'AB', + $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A, Arg2=B %>') ); $this->assertEquals( - $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A Bare String, Arg2=B Bare String %>'), - 'A Bare StringB Bare String' + 'A Bare StringB Bare String', + $this->render('<% include SSViewerTestIncludeWithArguments Arg1=A Bare String, Arg2=B Bare String %>') ); $this->assertEquals( + 'ABar', $this->render( '<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=$B %>', new ArrayData(['B' => 'Bar']) - ), - 'ABar' + ) ); $this->assertEquals( + 'ABar', $this->render( '<% include SSViewerTestIncludeWithArguments Arg1="A" %>', new ArrayData(['Arg1' => 'Foo', 'Arg2' => 'Bar']) - ), - 'ABar' + ) ); $this->assertEquals( - $this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=0 %>'), - 'A0' + 'A0', + $this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=0 %>') ); $this->assertEquals( - $this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=false %>'), - 'A' + 'A', + $this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=false %>') ); $this->assertEquals( - $this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=null %>'), - 'A' + 'A', + // Note Arg2 is explicitly overridden with null + $this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=null %>') ); $this->assertEquals( + 'SomeArg - Foo - Bar - SomeArg', $this->render( '<% include SSViewerTestIncludeScopeInheritanceWithArgsInLoop Title="SomeArg" %>', new ArrayData( @@ -1179,19 +1182,19 @@ public function testIncludeWithArguments() ] )] ) - ), - 'SomeArg - Foo - Bar - SomeArg' + ) ); $this->assertEquals( + 'A - B - A', $this->render( '<% include SSViewerTestIncludeScopeInheritanceWithArgsInWith Title="A" %>', new ArrayData(['Item' => new ArrayData(['Title' =>'B'])]) - ), - 'A - B - A' + ) ); $this->assertEquals( + 'A - B - C - B - A', $this->render( '<% include SSViewerTestIncludeScopeInheritanceWithArgsInNestedWith Title="A" %>', new ArrayData( @@ -1202,11 +1205,11 @@ public function testIncludeWithArguments() ] )] ) - ), - 'A - B - C - B - A' + ) ); $this->assertEquals( + 'A - A - A', $this->render( '<% include SSViewerTestIncludeScopeInheritanceWithUpAndTop Title="A" %>', new ArrayData( @@ -1217,8 +1220,7 @@ public function testIncludeWithArguments() ] )] ) - ), - 'A - A - A' + ) ); $data = new ArrayData( @@ -2202,7 +2204,66 @@ public function testRequireCallInTemplateInclude() } } - public function testCallsWithArguments() + public static function provideCallsWithArguments(): array + { + return [ + [ + 'template' => '$Level.output(1)', + 'expected' => '1-1', + ], + [ + 'template' => '$Nest.Level.output($Set.First.Number)', + 'expected' => '2-1', + ], + [ + 'template' => '<% with $Set %>$Up.Level.output($First.Number)<% end_with %>', + 'expected' => '1-1', + ], + [ + 'template' => '<% with $Set %>$Top.Nest.Level.output($First.Number)<% end_with %>', + 'expected' => '2-1', + ], + [ + 'template' => '<% loop $Set %>$Up.Nest.Level.output($Number)<% end_loop %>', + 'expected' => '2-12-22-32-42-5', + ], + [ + 'template' => '<% loop $Set %>$Top.Level.output($Number)<% end_loop %>', + 'expected' => '1-11-21-31-41-5', + ], + [ + 'template' => '<% with $Nest %>$Level.output($Top.Set.First.Number)<% end_with %>', + 'expected' => '2-1', + ], + [ + 'template' => '<% with $Level %>$output($Up.Set.Last.Number)<% end_with %>', + 'expected' => '1-5', + ], + [ + 'template' => '<% with $Level.forWith($Set.Last.Number) %>$output("hi")<% end_with %>', + 'expected' => '5-hi', + ], + [ + 'template' => '<% loop $Level.forLoop($Set.First.Number) %>$Number<% end_loop %>', + 'expected' => '!0', + ], + [ + 'template' => '<% with $Nest %> + <% with $Level.forWith($Up.Set.First.Number) %>$output("hi")<% end_with %> + <% end_with %>', + 'expected' => '1-hi', + ], + [ + 'template' => '<% with $Nest %> + <% loop $Level.forLoop($Top.Set.Last.Number) %>$Number<% end_loop %> + <% end_with %>', + 'expected' => '!0!1!2!3!4', + ], + ]; + } + + #[DataProvider('provideCallsWithArguments')] + public function testCallsWithArguments(string $template, string $expected): void { $data = new ArrayData( [ @@ -2222,28 +2283,7 @@ public function testCallsWithArguments() ] ); - $tests = [ - '$Level.output(1)' => '1-1', - '$Nest.Level.output($Set.First.Number)' => '2-1', - '<% with $Set %>$Up.Level.output($First.Number)<% end_with %>' => '1-1', - '<% with $Set %>$Top.Nest.Level.output($First.Number)<% end_with %>' => '2-1', - '<% loop $Set %>$Up.Nest.Level.output($Number)<% end_loop %>' => '2-12-22-32-42-5', - '<% loop $Set %>$Top.Level.output($Number)<% end_loop %>' => '1-11-21-31-41-5', - '<% with $Nest %>$Level.output($Top.Set.First.Number)<% end_with %>' => '2-1', - '<% with $Level %>$output($Up.Set.Last.Number)<% end_with %>' => '1-5', - '<% with $Level.forWith($Set.Last.Number) %>$output("hi")<% end_with %>' => '5-hi', - '<% loop $Level.forLoop($Set.First.Number) %>$Number<% end_loop %>' => '!0', - '<% with $Nest %> - <% with $Level.forWith($Up.Set.First.Number) %>$output("hi")<% end_with %> - <% end_with %>' => '1-hi', - '<% with $Nest %> - <% loop $Level.forLoop($Top.Set.Last.Number) %>$Number<% end_loop %> - <% end_with %>' => '!0!1!2!3!4', - ]; - - foreach ($tests as $template => $expected) { - $this->assertEquals($expected, trim($this->render($template, $data) ?? '')); - } + $this->assertEquals($expected, trim($this->render($template, $data) ?? '')); } public function testRepeatedCallsAreCached() @@ -2360,7 +2400,7 @@ public function testPrimitivesConvertedToDBFields() public function testMe(): void { $myArrayData = new class extends ArrayData { - public function forTemplate() + public function forTemplate(): string { return ''; } diff --git a/tests/php/View/SSViewerTest/TestFixture.php b/tests/php/View/SSViewerTest/TestFixture.php index f0abb39bc69..bdb97fe3b1d 100644 --- a/tests/php/View/SSViewerTest/TestFixture.php +++ b/tests/php/View/SSViewerTest/TestFixture.php @@ -2,71 +2,84 @@ namespace SilverStripe\View\Tests\SSViewerTest; -use SilverStripe\Model\List\ArrayList; -use SilverStripe\Model\ModelData; +use ReflectionClass; +use SilverStripe\Dev\TestOnly; +use SilverStripe\View\SSViewer_Scope; +use Stringable; /** * A test fixture that will echo back the template item */ -class TestFixture extends ModelData +class TestFixture implements TestOnly, Stringable { - protected $name; + private ?string $name; public function __construct($name = null) { $this->name = $name; - parent::__construct(); } - private function argedName($fieldName, $arguments) + public function __call(string $name, array $arguments = []): static|array|null { - $childName = $this->name ? "$this->name.$fieldName" : $fieldName; - if ($arguments) { - return $childName . '(' . implode(',', $arguments) . ')'; - } else { - return $childName; + return $this->getValue($name, $arguments); + } + + public function __get(string $name): static|array|null + { + return $this->getValue($name); + } + + public function __isset(string $name): bool + { + if (preg_match('/NotSet/i', $name)) { + return false; } + $reflectionScope = new ReflectionClass(SSViewer_Scope::class); + $globalProperties = $reflectionScope->getStaticPropertyValue('globalProperties'); + if (array_key_exists($name, $globalProperties)) { + return false; + } + return true; } - public function obj( - string $fieldName, - array $arguments = [], - bool $cache = false, - ?string $cacheName = null - ): ?object { - $childName = $this->argedName($fieldName, $arguments); + public function __toString(): string + { + if (preg_match('/NotSet/i', $this->name ?? '')) { + return ''; + } + if (preg_match('/Raw/i', $this->name ?? '')) { + return $this->name ?? ''; + } + return '[out:' . $this->name . ']'; + } + + private function getValue(string $name, array $arguments = []): static|array|null + { + $childName = $this->argedName($name, $arguments); // Special field name Loop### to create a list - if (preg_match('/^Loop([0-9]+)$/', $fieldName ?? '', $matches)) { - $output = new ArrayList(); + if (preg_match('/^Loop([0-9]+)$/', $name ?? '', $matches)) { + $output = []; for ($i = 0; $i < $matches[1]; $i++) { - $output->push(new TestFixture($childName)); + $output[] = new TestFixture($childName); } return $output; - } else { - if (preg_match('/NotSet/i', $fieldName ?? '')) { - return new ModelData(); - } else { - return new TestFixture($childName); - } } - } - public function XML_val(string $fieldName, array $arguments = [], bool $cache = false): string - { - if (preg_match('/NotSet/i', $fieldName ?? '')) { - return ''; - } else { - if (preg_match('/Raw/i', $fieldName ?? '')) { - return $fieldName; - } else { - return '[out:' . $this->argedName($fieldName, $arguments) . ']'; - } + if (preg_match('/NotSet/i', $name)) { + return null; } + + return new TestFixture($childName); } - public function hasValue(string $fieldName, array $arguments = [], bool $cache = true): bool + private function argedName(string $fieldName, array $arguments): string { - return (bool)$this->XML_val($fieldName, $arguments); + $childName = $this->name ? "$this->name.$fieldName" : $fieldName; + if ($arguments) { + return $childName . '(' . implode(',', $arguments) . ')'; + } else { + return $childName; + } } } diff --git a/tests/php/i18n/i18nTestManifest.php b/tests/php/i18n/i18nTestManifest.php index d2fd62a133f..2a8bcfb9ec8 100644 --- a/tests/php/i18n/i18nTestManifest.php +++ b/tests/php/i18n/i18nTestManifest.php @@ -17,10 +17,10 @@ use SilverStripe\i18n\Tests\i18nTest\MySubObject; use SilverStripe\i18n\Tests\i18nTest\TestDataObject; use SilverStripe\View\SSViewer; -use SilverStripe\View\SSViewer_DataPresenter; use SilverStripe\View\ThemeResourceLoader; use SilverStripe\View\ThemeManifest; use SilverStripe\Model\ModelData; +use SilverStripe\View\SSViewer_Scope; use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\Translator; @@ -71,9 +71,9 @@ public static function getExtraDataObjects() public function setupManifest() { - // force SSViewer_DataPresenter to cache global template vars before we switch to the + // force SSViewer_Scope to cache global template vars before we switch to the // test-project class manifest (since it will lose visibility of core classes) - $presenter = new SSViewer_DataPresenter(new ModelData()); + $presenter = new SSViewer_Scope(new ModelData()); unset($presenter); // Switch to test manifest
* public static $casting = array ( @@ -47,16 +50,18 @@ class ModelData *
- $Field + $renderField $Title <% if $Message %>$Message<% end_if %>
[out:Arg1]
[out:Arg2]
[out:Arg2.Count]
A
B
A Bare String
B Bare String
Bar
0