Skip to content

Commit

Permalink
API Refactor template layer into its own module
Browse files Browse the repository at this point in the history
Includes the following large-scale changes:
- Impoved barrier between model and view layers
- Improved casting of scalar to relevant DBField types
- Improved capabilities for rendering arbitrary data in templates
  • Loading branch information
GuySartorelli committed Oct 2, 2024
1 parent 27708c7 commit 2b00a05
Show file tree
Hide file tree
Showing 92 changed files with 1,214 additions and 1,106 deletions.
16 changes: 8 additions & 8 deletions src/Control/RSS/RSSFeed_Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,19 +58,19 @@ 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);
}

/**
* 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);

Expand All @@ -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);
}
Expand All @@ -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)
{
Expand Down
45 changes: 34 additions & 11 deletions src/Core/CustomMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}

Expand All @@ -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;
Expand All @@ -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);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Dev/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Forms/CheckboxSetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/ConfirmedPasswordField.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function setTitle($title)
*
* @return string
*/
public function Field($properties = [])
public function renderField($properties = [])
{
// Build inner content
$fieldContent = '';
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/CurrencyField_Disabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/CurrencyField_Readonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/Forms/DatalessField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DateField_Disabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DateField_Disabled extends DateField

protected $disabled = true;

public function Field($properties = [])
public function renderField($properties = [])
{
// Default display value
$displayValue = '<i>(' . _t('SilverStripe\\Forms\\DateField.NOTSET', 'not set') . ')</i>';
Expand Down
6 changes: 3 additions & 3 deletions src/Forms/DropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* DropdownField::create(
* 'Country',
* 'Country',
* singleton(MyObject::class)->dbObject('Country')->enumValues()
* singleton(MyObject::class)->dbObject('Country')?->enumValues()
* );
* </code>
*
Expand Down Expand Up @@ -128,7 +128,7 @@ public function getHasEmptyDefault()
* @param array $properties
* @return string
*/
public function Field($properties = [])
public function renderField($properties = [])
{
$options = [];

Expand All @@ -141,6 +141,6 @@ public function Field($properties = [])
'Options' => new ArrayList($options)
]);

return parent::Field($properties);
return parent::renderField($properties);
}
}
6 changes: 3 additions & 3 deletions src/Forms/FieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <label> 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 <label> tag, if the Title is set.
*
* <b>Usage</b>
*
Expand Down Expand Up @@ -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 ?? '', ".");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/FieldList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
8 changes: 4 additions & 4 deletions src/Forms/FormAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 2b00a05

Please sign in to comment.