Compare commits

..

6 Commits

Author SHA1 Message Date
Guy Sartorelli
eaac540c65
Merge d18c931ecf into 662ac9d1f9 2024-10-22 17:39:31 +13:00
Guy Sartorelli
662ac9d1f9
Merge pull request #11432 from creative-commoners/pulls/6/fix-definemethods
MNT Fix unit tests
2024-10-22 16:25:32 +13:00
Guy Sartorelli
d18c931ecf
API Refactor template layer into its own module
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
2024-10-22 16:15:39 +13:00
Steve Boyd
983e90b25e MNT Fix unit tests 2024-10-22 15:50:25 +13:00
Guy Sartorelli
8e08b1cf79
Merge branch '5' into 6
# Conflicts:
#	src/Forms/GridField/GridFieldDataColumns.php
#	src/Model/ModelData.php
#	src/View/SSViewer.php
#	src/View/SSViewer_DataPresenter.php
#	src/View/SSViewer_FromString.php
#	src/View/SSViewer_Scope.php
2024-10-22 13:04:29 +13:00
Guy Sartorelli
165f72fd22
API Deprecations for template layer (#11420) 2024-10-22 12:52:35 +13:00
16 changed files with 56 additions and 54 deletions

View File

@ -8,12 +8,11 @@ use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\View\Requirements;
use Stringable;
/**
* Represents a response returned by a controller.
*/
class HTTPResponse implements Stringable
class HTTPResponse
{
use Injectable;
@ -446,7 +445,7 @@ EOT
/**
* The HTTP response represented as a raw string
*/
public function __toString(): string
public function __toString()
{
$headers = [];
foreach ($this->getHeaders() as $header => $values) {

View File

@ -511,7 +511,7 @@ trait Extensible
// Setup all extension instances for this instance
$this->extension_instances = [];
foreach (ClassInfo::ancestry(static::class) as $class) {
if (in_array($class, self::$unextendable_classes)) {
if (in_array($class, self::class::$unextendable_classes)) {
continue;
}
$extensions = Config::inst()->get($class, 'extensions', Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES);

View File

@ -5,13 +5,12 @@ namespace SilverStripe\Core\Manifest;
use InvalidArgumentException;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Path;
use Stringable;
/**
* This object represents a single resource file attached to a module, and can be used
* as a reference to this to be later turned into either a URL or file path.
*/
class ModuleResource implements Stringable
class ModuleResource
{
/**
* @var Module
@ -116,7 +115,7 @@ class ModuleResource implements Stringable
/**
* Get relative path
*/
public function __toString(): string
public function __toString()
{
return $this->getRelativePath();
}

View File

@ -391,7 +391,7 @@ class Deprecation
}
// Getting a backtrace is slow, so we only do it if we need it
$backtrace = null;
$backtrace = [];
// Get the calling scope
if ($scope == Deprecation::SCOPE_METHOD) {

View File

@ -6,6 +6,7 @@ use SilverStripe\Core\Convert;
use InvalidArgumentException;
use LogicException;
use SilverStripe\Model\ModelData;
use SilverStripe\Dev\Deprecation;
/**
* @see GridField

View File

@ -5,7 +5,6 @@ namespace SilverStripe\Forms\GridField;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\HiddenField;
use SilverStripe\ORM\DataList;
use Stringable;
/**
* This class is a snapshot of the current status of a {@link GridField}.
@ -15,7 +14,7 @@ use Stringable;
*
* @see GridField
*/
class GridState extends HiddenField implements Stringable
class GridState extends HiddenField
{
/**
@ -130,7 +129,7 @@ class GridState extends HiddenField implements Stringable
return Convert::raw2att($this->Value());
}
public function __toString(): string
public function __toString()
{
return $this->Value();
}

View File

@ -2,15 +2,13 @@
namespace SilverStripe\Forms\GridField;
use Stringable;
/**
* Simple set of data, similar to stdClass, but without the notice-level
* errors.
*
* @see GridState
*/
class GridState_Data implements Stringable
class GridState_Data
{
/**
@ -96,7 +94,7 @@ class GridState_Data implements Stringable
unset($this->data[$name]);
}
public function __toString(): string
public function __toString()
{
if (!$this->data) {
return "";

View File

@ -18,7 +18,6 @@ use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Model\ArrayData;
use SilverStripe\View\CastingService;
use SilverStripe\View\SSViewer;
use Stringable;
use UnexpectedValueException;
/**
@ -28,7 +27,7 @@ use UnexpectedValueException;
* 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 implements Stringable
class ModelData
{
use Extensible {
defineMethods as extensibleDefineMethods;
@ -306,9 +305,12 @@ class ModelData implements Stringable
return true;
}
/**
* Return the class name (though subclasses may return something else)
*/
public function __toString(): string
{
return $this->forTemplate();
return static::class;
}
/**
@ -548,8 +550,9 @@ class ModelData implements Stringable
*/
private function objCacheName(string $fieldName, array $arguments = []): string
{
return empty($arguments)
$name = empty($arguments)
? $fieldName
: $fieldName . ":" . var_export($arguments, true);
return md5($name);
}
}

View File

@ -521,6 +521,11 @@ abstract class DBField extends ModelData implements DBIndexable
DBG;
}
public function __toString(): string
{
return (string)$this->forTemplate();
}
public function getArrayValue()
{
return $this->arrayValue;

View File

@ -6,13 +6,12 @@ use SilverStripe\Core\Convert;
use SilverStripe\ORM\Connect\Query;
use SilverStripe\ORM\DB;
use Exception;
use Stringable;
/**
* Abstract base class for an object representing an SQL query.
* The various parts of the SQL query can be manipulated individually.
*/
abstract class SQLExpression implements Stringable
abstract class SQLExpression
{
/**
@ -46,7 +45,7 @@ abstract class SQLExpression implements Stringable
/**
* Return the generated SQL string for this query
*/
public function __toString(): string
public function __toString()
{
try {
$sql = $this->sql($parameters);

View File

@ -21,33 +21,33 @@ class CastingService
/**
* Cast a value to the relevant object (usually a DBField instance) for use in the view layer.
*
* @param null|array|ModelData $source Where the data originates from. This is used both to check for casting helpers
* @param ModelData|array|null $source Where the data originates from. This is used both to check for casting helpers
* and to help set the value in cast DBField instances.
* @param bool $strict If true, an object will be returned even if $data is null.
*/
public function cast(mixed $data, null|array|ModelData $source = null, string $fieldName = '', bool $strict = false): ?object
public function cast(mixed $data, ModelData|array|null $source = null, string $fieldName = '', bool $strict = false): ?object
{
// null is null - we shouldn't cast it to an object, because that makes it harder
// for downstream checks to know there's "no value".
if (!$strict && $data === null) {
return null;
}
// Assume anything that's an object is intentionally using whatever class it's using
// and don't cast it.
if (is_object($data)) {
return $data;
}
$service = null;
// null is null - we shouldn't cast it to an object, because that makes it harder
// for downstream checks to know there's "no value".
if (!$strict && $data === null) {
return null;
}
$serviceKey = null;
if ($source instanceof ModelData) {
$service = $source->castingHelper($fieldName);
$serviceKey = $source->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 ($serviceKey) {
$castObject = Injector::inst()->create($serviceKey, $fieldName);
if (!ClassInfo::hasMethod($castObject, 'setValue')) {
throw new LogicException('Explicit casting service must have a setValue method.');
}
@ -61,8 +61,8 @@ class CastingService
}
// Fall back to default casting
$service = $this->defaultService($data, $source, $fieldName);
$castObject = Injector::inst()->create($service, $fieldName);
$serviceKey = $this->getDefaultServiceKey($data, $source, $fieldName);
$castObject = Injector::inst()->create($serviceKey, $fieldName);
if (!ClassInfo::hasMethod($castObject, 'setValue')) {
throw new LogicException('Default service must have a setValue method.');
}
@ -73,7 +73,7 @@ class CastingService
/**
* 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
private function getDefaultServiceKey(mixed $data, mixed $source = null, string $fieldName = ''): ?string
{
$default = null;
if ($source instanceof ModelData) {
@ -81,7 +81,7 @@ class CastingService
if ($default === null) {
$failover = $source->getFailover();
if ($failover) {
$default = $this->defaultService($data, $failover, $fieldName);
$default = $this->getDefaultServiceKey($data, $failover, $fieldName);
}
}
}

View File

@ -372,9 +372,8 @@ PHP;
if ($isXhtml) {
return "<base href=\"$base\" />";
} else {
return "<base href=\"$base\">";
}
return "<base href=\"$base\">";
}
/**

View File

@ -9,6 +9,7 @@ use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Core\Path;
use SilverStripe\Dev\Deprecation;
/**
* Handles finding templates from a stack of template manifest objects.

View File

@ -3,7 +3,6 @@
namespace SilverStripe\i18n\Messages\Symfony;
use SilverStripe\Core\Flushable;
use Stringable;
use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
/**
@ -13,9 +12,9 @@ use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
* @link https://media.giphy.com/media/fRRD3T37DeY6Y/giphy.gif for use case
* @see DirectoryResource
*/
class FlushInvalidatedResource implements SelfCheckingResourceInterface, Flushable, Stringable
class FlushInvalidatedResource implements SelfCheckingResourceInterface, Flushable
{
public function __toString(): string
public function __toString()
{
return md5(__CLASS__);
}

View File

@ -111,8 +111,8 @@ class DeprecationTest extends SapphireTest
'Will be removed without equivalent functionality to replace it.',
'Called from SilverStripe\Dev\Tests\DeprecationTest->testNoticeNoReplacement.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
$this->enableDeprecationNotices(true);
$ret = $this->myDeprecatedMethodNoReplacement();
$this->assertSame('abc', $ret);