mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Compare commits
5 Commits
0f386039df
...
cd8090d247
Author | SHA1 | Date | |
---|---|---|---|
|
cd8090d247 | ||
|
6bb9a0b33d | ||
|
ebbd6427b2 | ||
|
f83f56eba1 | ||
|
f5ef850085 |
6
_config/view.yml
Normal file
6
_config/view.yml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
Name: view-config
|
||||||
|
---
|
||||||
|
SilverStripe\Core\Injector\Injector:
|
||||||
|
SilverStripe\View\TemplateEngine:
|
||||||
|
class: 'SilverStripe\View\SSTemplateEngine'
|
4
bin/sake
4
bin/sake
@ -2,6 +2,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use SilverStripe\Cli\Sake;
|
use SilverStripe\Cli\Sake;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
|
||||||
// Ensure that people can't access this from a web-server
|
// Ensure that people can't access this from a web-server
|
||||||
if (!in_array(PHP_SAPI, ['cli', 'cgi', 'cgi-fcgi'])) {
|
if (!in_array(PHP_SAPI, ['cli', 'cgi', 'cgi-fcgi'])) {
|
||||||
@ -11,5 +12,8 @@ if (!in_array(PHP_SAPI, ['cli', 'cgi', 'cgi-fcgi'])) {
|
|||||||
|
|
||||||
require_once __DIR__ . '/../src/includes/autoload.php';
|
require_once __DIR__ . '/../src/includes/autoload.php';
|
||||||
|
|
||||||
|
// CLI scripts must only use the primary database connection and not replicas
|
||||||
|
DB::setMustUsePrimary();
|
||||||
|
|
||||||
$sake = new Sake();
|
$sake = new Sake();
|
||||||
$sake->run();
|
$sake->run();
|
||||||
|
@ -225,7 +225,7 @@ class ContentNegotiator
|
|||||||
// Fix base tag
|
// Fix base tag
|
||||||
$content = preg_replace(
|
$content = preg_replace(
|
||||||
'/<base href="([^"]*)" \/>/',
|
'/<base href="([^"]*)" \/>/',
|
||||||
'<base href="$1"><!--[if lte IE 6]></base><![endif]-->',
|
'<base href="$1">',
|
||||||
$content ?? ''
|
$content ?? ''
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -3,11 +3,14 @@
|
|||||||
namespace SilverStripe\Control;
|
namespace SilverStripe\Control;
|
||||||
|
|
||||||
use SilverStripe\Core\ClassInfo;
|
use SilverStripe\Core\ClassInfo;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\Dev\Debug;
|
use SilverStripe\Dev\Debug;
|
||||||
|
use SilverStripe\Model\ModelData;
|
||||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
use SilverStripe\Security\Security;
|
use SilverStripe\Security\Security;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
|
use SilverStripe\View\TemplateEngine;
|
||||||
use SilverStripe\View\TemplateGlobalProvider;
|
use SilverStripe\View\TemplateGlobalProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -88,6 +91,8 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
|
|||||||
'handleIndex',
|
'handleIndex',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected ?TemplateEngine $templateEngine = null;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@ -401,7 +406,7 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
|
|||||||
$templates = array_unique(array_merge($actionTemplates, $classTemplates));
|
$templates = array_unique(array_merge($actionTemplates, $classTemplates));
|
||||||
}
|
}
|
||||||
|
|
||||||
return SSViewer::create($templates);
|
return SSViewer::create($templates, $this->getTemplateEngine());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -453,9 +458,10 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
|
|||||||
}
|
}
|
||||||
|
|
||||||
$class = static::class;
|
$class = static::class;
|
||||||
while ($class != 'SilverStripe\\Control\\RequestHandler') {
|
$engine = $this->getTemplateEngine();
|
||||||
|
while ($class !== RequestHandler::class) {
|
||||||
$templateName = strtok($class ?? '', '_') . '_' . $action;
|
$templateName = strtok($class ?? '', '_') . '_' . $action;
|
||||||
if (SSViewer::hasTemplate($templateName)) {
|
if ($engine->hasTemplate($templateName)) {
|
||||||
return $class;
|
return $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -487,17 +493,25 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
|
|||||||
$parentClass = get_parent_class($parentClass ?? '');
|
$parentClass = get_parent_class($parentClass ?? '');
|
||||||
}
|
}
|
||||||
|
|
||||||
return SSViewer::hasTemplate($templates);
|
$engine = $this->getTemplateEngine();
|
||||||
|
return $engine->hasTemplate($templates);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderWith($template, ModelData|array $customFields = []): DBHTMLText
|
||||||
|
{
|
||||||
|
// Ensure template engine is used, unless the viewer was already explicitly instantiated
|
||||||
|
if (!($template instanceof SSViewer)) {
|
||||||
|
$template = SSViewer::create($template, $this->getTemplateEngine());
|
||||||
|
}
|
||||||
|
return parent::renderWith($template, $customFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the current controller with the templates determined by {@link getViewer()}.
|
* Render the current controller with the templates determined by {@link getViewer()}.
|
||||||
*
|
*
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function render($params = null)
|
public function render($params = null): DBHTMLText
|
||||||
{
|
{
|
||||||
$template = $this->getViewer($this->getAction());
|
$template = $this->getViewer($this->getAction());
|
||||||
|
|
||||||
@ -737,4 +751,12 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
|
|||||||
'CurrentPage' => 'curr',
|
'CurrentPage' => 'curr',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getTemplateEngine(): TemplateEngine
|
||||||
|
{
|
||||||
|
if (!$this->templateEngine) {
|
||||||
|
$this->templateEngine = Injector::inst()->create(TemplateEngine::class);
|
||||||
|
}
|
||||||
|
return $this->templateEngine;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ use SilverStripe\Versioned\Versioned;
|
|||||||
use SilverStripe\View\Requirements;
|
use SilverStripe\View\Requirements;
|
||||||
use SilverStripe\View\Requirements_Backend;
|
use SilverStripe\View\Requirements_Backend;
|
||||||
use SilverStripe\View\TemplateGlobalProvider;
|
use SilverStripe\View\TemplateGlobalProvider;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Director is responsible for processing URLs, and providing environment information.
|
* Director is responsible for processing URLs, and providing environment information.
|
||||||
@ -84,6 +85,14 @@ class Director implements TemplateGlobalProvider
|
|||||||
*/
|
*/
|
||||||
private static $default_base_url = '`SS_BASE_URL`';
|
private static $default_base_url = '`SS_BASE_URL`';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of routing rule patterns that must only use the primary database and not a replica
|
||||||
|
*/
|
||||||
|
private static array $rule_patterns_must_use_primary_db = [
|
||||||
|
'dev',
|
||||||
|
'Security',
|
||||||
|
];
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -296,6 +305,18 @@ class Director implements TemplateGlobalProvider
|
|||||||
{
|
{
|
||||||
Injector::inst()->registerService($request, HTTPRequest::class);
|
Injector::inst()->registerService($request, HTTPRequest::class);
|
||||||
|
|
||||||
|
// Check if primary database must be used based on request rules
|
||||||
|
// Note this check must happend before the rules are processed as
|
||||||
|
// $shiftOnSuccess param is passed as true in `$request->match($pattern, true)` later on in
|
||||||
|
// this method, which modifies `$this->dirParts`, thus affecting `$request->match($rule)` directly below
|
||||||
|
$primaryDbOnlyRules = Director::config()->uninherited('rule_patterns_must_use_primary_db');
|
||||||
|
foreach ($primaryDbOnlyRules as $rule) {
|
||||||
|
if ($request->match($rule)) {
|
||||||
|
DB::setMustUsePrimary();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$rules = Director::config()->uninherited('rules');
|
$rules = Director::config()->uninherited('rules');
|
||||||
|
|
||||||
$this->extend('updateRules', $rules);
|
$this->extend('updateRules', $rules);
|
||||||
|
@ -398,16 +398,13 @@ class Email extends SymfonyEmail
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHTMLTemplate(): string
|
public function getHTMLTemplate(): string|array
|
||||||
{
|
{
|
||||||
if ($this->HTMLTemplate) {
|
if ($this->HTMLTemplate) {
|
||||||
return $this->HTMLTemplate;
|
return $this->HTMLTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ThemeResourceLoader::inst()->findTemplate(
|
return SSViewer::get_templates_by_class(static::class, '', Email::class);
|
||||||
SSViewer::get_templates_by_class(static::class, '', Email::class),
|
|
||||||
SSViewer::get_themes()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,11 +8,12 @@ use SilverStripe\Core\Convert;
|
|||||||
use SilverStripe\Core\Injector\Injectable;
|
use SilverStripe\Core\Injector\Injectable;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\View\Requirements;
|
use SilverStripe\View\Requirements;
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a response returned by a controller.
|
* Represents a response returned by a controller.
|
||||||
*/
|
*/
|
||||||
class HTTPResponse
|
class HTTPResponse implements Stringable
|
||||||
{
|
{
|
||||||
use Injectable;
|
use Injectable;
|
||||||
|
|
||||||
@ -444,10 +445,8 @@ EOT
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The HTTP response represented as a raw string
|
* The HTTP response represented as a raw string
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function __toString()
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
$headers = [];
|
$headers = [];
|
||||||
foreach ($this->getHeaders() as $header => $values) {
|
foreach ($this->getHeaders() as $header => $values) {
|
||||||
|
@ -47,7 +47,7 @@ class RSSFeed_Entry extends ModelData
|
|||||||
*/
|
*/
|
||||||
public function __construct($entry, $titleField, $descriptionField, $authorField)
|
public function __construct($entry, $titleField, $descriptionField, $authorField)
|
||||||
{
|
{
|
||||||
$this->failover = $entry;
|
$this->setFailover($entry);
|
||||||
$this->titleField = $titleField;
|
$this->titleField = $titleField;
|
||||||
$this->descriptionField = $descriptionField;
|
$this->descriptionField = $descriptionField;
|
||||||
$this->authorField = $authorField;
|
$this->authorField = $authorField;
|
||||||
@ -58,7 +58,7 @@ class RSSFeed_Entry extends ModelData
|
|||||||
/**
|
/**
|
||||||
* Get the description of this entry
|
* 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 Title()
|
||||||
{
|
{
|
||||||
@ -68,7 +68,7 @@ class RSSFeed_Entry extends ModelData
|
|||||||
/**
|
/**
|
||||||
* Get the description of this entry
|
* 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 Description()
|
||||||
{
|
{
|
||||||
@ -85,7 +85,7 @@ class RSSFeed_Entry extends ModelData
|
|||||||
/**
|
/**
|
||||||
* Get the author of this entry
|
* 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 Author()
|
||||||
{
|
{
|
||||||
@ -96,7 +96,7 @@ class RSSFeed_Entry extends ModelData
|
|||||||
* Return the safely casted field
|
* Return the safely casted field
|
||||||
*
|
*
|
||||||
* @param string $fieldName Name of field
|
* @param string $fieldName Name of field
|
||||||
* @return DBField
|
* @return DBField|null
|
||||||
*/
|
*/
|
||||||
public function rssField($fieldName)
|
public function rssField($fieldName)
|
||||||
{
|
{
|
||||||
|
@ -85,7 +85,7 @@ class ClassInfo implements Flushable
|
|||||||
public static function hasTable($tableName)
|
public static function hasTable($tableName)
|
||||||
{
|
{
|
||||||
$cache = ClassInfo::getCache();
|
$cache = ClassInfo::getCache();
|
||||||
$configData = serialize(DB::getConfig());
|
$configData = serialize(DB::getConfig(DB::CONN_PRIMARY));
|
||||||
$cacheKey = 'tableList_' . md5($configData);
|
$cacheKey = 'tableList_' . md5($configData);
|
||||||
$tableList = $cache->get($cacheKey) ?? [];
|
$tableList = $cache->get($cacheKey) ?? [];
|
||||||
if (empty($tableList) && DB::is_active()) {
|
if (empty($tableList) && DB::is_active()) {
|
||||||
|
@ -7,12 +7,14 @@ use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
|
|||||||
use SilverStripe\ORM\Connect\NullDatabase;
|
use SilverStripe\ORM\Connect\NullDatabase;
|
||||||
use SilverStripe\ORM\DB;
|
use SilverStripe\ORM\DB;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple Kernel container
|
* Simple Kernel container
|
||||||
*/
|
*/
|
||||||
class CoreKernel extends BaseKernel
|
class CoreKernel extends BaseKernel
|
||||||
{
|
{
|
||||||
|
|
||||||
protected bool $bootDatabase = true;
|
protected bool $bootDatabase = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,7 +40,7 @@ class CoreKernel extends BaseKernel
|
|||||||
$this->flush = $flush;
|
$this->flush = $flush;
|
||||||
|
|
||||||
if (!$this->bootDatabase) {
|
if (!$this->bootDatabase) {
|
||||||
DB::set_conn(new NullDatabase());
|
DB::set_conn(new NullDatabase(), DB::CONN_PRIMARY);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->bootPHP();
|
$this->bootPHP();
|
||||||
@ -73,7 +75,7 @@ class CoreKernel extends BaseKernel
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load default database configuration from the $database and $databaseConfig globals
|
* Load database configuration from the $database and $databaseConfig globals
|
||||||
*/
|
*/
|
||||||
protected function bootDatabaseGlobals()
|
protected function bootDatabaseGlobals()
|
||||||
{
|
{
|
||||||
@ -84,41 +86,62 @@ class CoreKernel extends BaseKernel
|
|||||||
global $databaseConfig;
|
global $databaseConfig;
|
||||||
global $database;
|
global $database;
|
||||||
|
|
||||||
// Case 1: $databaseConfig global exists. Merge $database in as needed
|
// Ensure global database config has prefix and suffix applied
|
||||||
if (!empty($databaseConfig)) {
|
if (!empty($databaseConfig) && !empty($database)) {
|
||||||
if (!empty($database)) {
|
|
||||||
$databaseConfig['database'] = $this->getDatabasePrefix() . $database . $this->getDatabaseSuffix();
|
$databaseConfig['database'] = $this->getDatabasePrefix() . $database . $this->getDatabaseSuffix();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only set it if its valid, otherwise ignore $databaseConfig entirely
|
// Set config for primary and any replicas
|
||||||
if (!empty($databaseConfig['database'])) {
|
for ($i = 0; $i <= DB::MAX_REPLICAS; $i++) {
|
||||||
DB::setConfig($databaseConfig);
|
if ($i === 0) {
|
||||||
|
$name = DB::CONN_PRIMARY;
|
||||||
|
} else {
|
||||||
|
$name = DB::getReplicaConfigKey($i);
|
||||||
|
if (!DB::hasConfig($name)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 1: $databaseConfig global exists
|
||||||
|
// Only set it if its valid, otherwise ignore $databaseConfig entirely
|
||||||
|
if (!empty($databaseConfig) && !empty($databaseConfig['database'])) {
|
||||||
|
DB::setConfig($databaseConfig, $name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Case 2: $database merged into existing config
|
// Case 2: $databaseConfig global does not exist
|
||||||
|
// Merge $database global into existing config
|
||||||
if (!empty($database)) {
|
if (!empty($database)) {
|
||||||
$existing = DB::getConfig();
|
$dbConfig = DB::getConfig($name);
|
||||||
$existing['database'] = $this->getDatabasePrefix() . $database . $this->getDatabaseSuffix();
|
$dbConfig['database'] = $this->getDatabasePrefix() . $database . $this->getDatabaseSuffix();
|
||||||
|
DB::setConfig($dbConfig, $name);
|
||||||
DB::setConfig($existing);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load default database configuration from environment variable
|
* Load database configuration from environment variables
|
||||||
*/
|
*/
|
||||||
protected function bootDatabaseEnvVars()
|
protected function bootDatabaseEnvVars()
|
||||||
{
|
{
|
||||||
if (!$this->bootDatabase) {
|
if (!$this->bootDatabase) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Set default database config
|
// Set primary database config
|
||||||
$databaseConfig = $this->getDatabaseConfig();
|
$databaseConfig = $this->getDatabaseConfig();
|
||||||
$databaseConfig['database'] = $this->getDatabaseName();
|
$databaseConfig['database'] = $this->getDatabaseName();
|
||||||
DB::setConfig($databaseConfig);
|
DB::setConfig($databaseConfig, DB::CONN_PRIMARY);
|
||||||
|
|
||||||
|
// Set database replicas config
|
||||||
|
for ($i = 1; $i <= DB::MAX_REPLICAS; $i++) {
|
||||||
|
$envKey = $this->getReplicaEnvKey('SS_DATABASE_SERVER', $i);
|
||||||
|
if (!Environment::hasEnv($envKey)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$replicaDatabaseConfig = $this->getDatabaseReplicaConfig($i);
|
||||||
|
$configKey = DB::getReplicaConfigKey($i);
|
||||||
|
DB::setConfig($replicaDatabaseConfig, $configKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -127,12 +150,72 @@ class CoreKernel extends BaseKernel
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getDatabaseConfig()
|
protected function getDatabaseConfig()
|
||||||
|
{
|
||||||
|
return $this->getSingleDataBaseConfig(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getDatabaseReplicaConfig(int $replica)
|
||||||
|
{
|
||||||
|
if ($replica <= 0) {
|
||||||
|
throw new InvalidArgumentException('Replica number must be greater than 0');
|
||||||
|
}
|
||||||
|
return $this->getSingleDataBaseConfig($replica);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a database key to a replica key
|
||||||
|
* e.g. SS_DATABASE_SERVER -> SS_DATABASE_SERVER_REPLICA_01
|
||||||
|
*
|
||||||
|
* @param string $key - The key to look up in the environment
|
||||||
|
* @param int $replica - Replica number
|
||||||
|
*/
|
||||||
|
private function getReplicaEnvKey(string $key, int $replica): string
|
||||||
|
{
|
||||||
|
if ($replica <= 0) {
|
||||||
|
throw new InvalidArgumentException('Replica number must be greater than 0');
|
||||||
|
}
|
||||||
|
// Do not allow replicas to define keys that could lead to unexpected behaviour if
|
||||||
|
// they do not match the primary database configuration
|
||||||
|
if (in_array($key, ['SS_DATABASE_CLASS', 'SS_DATABASE_NAME', 'SS_DATABASE_CHOOSE_NAME'])) {
|
||||||
|
return $key;
|
||||||
|
}
|
||||||
|
// Left pad replica number with a zeros to match the length of the maximum replica number
|
||||||
|
$len = strlen((string) DB::MAX_REPLICAS);
|
||||||
|
return $key . '_REPLICA_' . str_pad($replica, $len, '0', STR_PAD_LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads a single database configuration variable from the environment
|
||||||
|
* For replica databases, it will first attempt to find replica-specific configuration
|
||||||
|
* before falling back to the primary configuration.
|
||||||
|
*
|
||||||
|
* Replicate specific configuration has `_REPLICA_01` appended to the key
|
||||||
|
* where 01 is the replica number.
|
||||||
|
*
|
||||||
|
* @param string $key - The key to look up in the environment
|
||||||
|
* @param int $replica - Replica number. Passing 0 will return the primary database configuration
|
||||||
|
*/
|
||||||
|
private function getDatabaseConfigVariable(string $key, int $replica): string
|
||||||
|
{
|
||||||
|
if ($replica > 0) {
|
||||||
|
$key = $this->getReplicaEnvKey($key, $replica);
|
||||||
|
}
|
||||||
|
if (Environment::hasEnv($key)) {
|
||||||
|
return Environment::getEnv($key);
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $replica - Replica number. Passing 0 will return the primary database configuration
|
||||||
|
*/
|
||||||
|
private function getSingleDataBaseConfig(int $replica): array
|
||||||
{
|
{
|
||||||
$databaseConfig = [
|
$databaseConfig = [
|
||||||
"type" => Environment::getEnv('SS_DATABASE_CLASS') ?: 'MySQLDatabase',
|
"type" => $this->getDatabaseConfigVariable('SS_DATABASE_CLASS', $replica) ?: 'MySQLDatabase',
|
||||||
"server" => Environment::getEnv('SS_DATABASE_SERVER') ?: 'localhost',
|
"server" => $this->getDatabaseConfigVariable('SS_DATABASE_SERVER', $replica) ?: 'localhost',
|
||||||
"username" => Environment::getEnv('SS_DATABASE_USERNAME') ?: null,
|
"username" => $this->getDatabaseConfigVariable('SS_DATABASE_USERNAME', $replica) ?: null,
|
||||||
"password" => Environment::getEnv('SS_DATABASE_PASSWORD') ?: null,
|
"password" => $this->getDatabaseConfigVariable('SS_DATABASE_PASSWORD', $replica) ?: null,
|
||||||
];
|
];
|
||||||
|
|
||||||
// Only add SSL keys in the array if there is an actual value associated with them
|
// Only add SSL keys in the array if there is an actual value associated with them
|
||||||
@ -143,7 +226,7 @@ class CoreKernel extends BaseKernel
|
|||||||
'ssl_cipher' => 'SS_DATABASE_SSL_CIPHER',
|
'ssl_cipher' => 'SS_DATABASE_SSL_CIPHER',
|
||||||
];
|
];
|
||||||
foreach ($sslConf as $key => $envVar) {
|
foreach ($sslConf as $key => $envVar) {
|
||||||
$envValue = Environment::getEnv($envVar);
|
$envValue = $this->getDatabaseConfigVariable($envVar, $replica);
|
||||||
if ($envValue) {
|
if ($envValue) {
|
||||||
$databaseConfig[$key] = $envValue;
|
$databaseConfig[$key] = $envValue;
|
||||||
}
|
}
|
||||||
@ -159,25 +242,25 @@ class CoreKernel extends BaseKernel
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set the port if called for
|
// Set the port if called for
|
||||||
$dbPort = Environment::getEnv('SS_DATABASE_PORT');
|
$dbPort = $this->getDatabaseConfigVariable('SS_DATABASE_PORT', $replica);
|
||||||
if ($dbPort) {
|
if ($dbPort) {
|
||||||
$databaseConfig['port'] = $dbPort;
|
$databaseConfig['port'] = $dbPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the timezone if called for
|
// Set the timezone if called for
|
||||||
$dbTZ = Environment::getEnv('SS_DATABASE_TIMEZONE');
|
$dbTZ = $this->getDatabaseConfigVariable('SS_DATABASE_TIMEZONE', $replica);
|
||||||
if ($dbTZ) {
|
if ($dbTZ) {
|
||||||
$databaseConfig['timezone'] = $dbTZ;
|
$databaseConfig['timezone'] = $dbTZ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// For schema enabled drivers:
|
// For schema enabled drivers:
|
||||||
$dbSchema = Environment::getEnv('SS_DATABASE_SCHEMA');
|
$dbSchema = $this->getDatabaseConfigVariable('SS_DATABASE_SCHEMA', $replica);
|
||||||
if ($dbSchema) {
|
if ($dbSchema) {
|
||||||
$databaseConfig["schema"] = $dbSchema;
|
$databaseConfig["schema"] = $dbSchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
// For SQlite3 memory databases (mainly for testing purposes)
|
// For SQlite3 memory databases (mainly for testing purposes)
|
||||||
$dbMemory = Environment::getEnv('SS_DATABASE_MEMORY');
|
$dbMemory = $this->getDatabaseConfigVariable('SS_DATABASE_MEMORY', $replica);
|
||||||
if ($dbMemory) {
|
if ($dbMemory) {
|
||||||
$databaseConfig["memory"] = $dbMemory;
|
$databaseConfig["memory"] = $dbMemory;
|
||||||
}
|
}
|
||||||
@ -205,6 +288,7 @@ class CoreKernel extends BaseKernel
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get name of database
|
* Get name of database
|
||||||
|
* Note that any replicas must have the same database name as the primary database
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
@ -5,12 +5,13 @@ namespace SilverStripe\Core\Manifest;
|
|||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\Core\Path;
|
use SilverStripe\Core\Path;
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This object represents a single resource file attached to a module, and can be used
|
* 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.
|
* as a reference to this to be later turned into either a URL or file path.
|
||||||
*/
|
*/
|
||||||
class ModuleResource
|
class ModuleResource implements Stringable
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Module
|
* @var Module
|
||||||
@ -114,10 +115,8 @@ class ModuleResource
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get relative path
|
* Get relative path
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function __toString()
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
return $this->getRelativePath();
|
return $this->getRelativePath();
|
||||||
}
|
}
|
||||||
|
@ -149,11 +149,11 @@ class Backtrace
|
|||||||
if ($showArgs && isset($item['args'])) {
|
if ($showArgs && isset($item['args'])) {
|
||||||
$args = [];
|
$args = [];
|
||||||
foreach ($item['args'] as $arg) {
|
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);
|
$sarg = is_array($arg) ? 'Array' : strval($arg);
|
||||||
$args[] = (strlen($sarg ?? '') > $argCharLimit) ? substr($sarg, 0, $argCharLimit) . '...' : $sarg;
|
$args[] = (strlen($sarg ?? '') > $argCharLimit) ? substr($sarg, 0, $argCharLimit) . '...' : $sarg;
|
||||||
} else {
|
|
||||||
$args[] = get_class($arg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ use ReflectionClass;
|
|||||||
use SilverStripe\Dev\Exceptions\ExpectedErrorException;
|
use SilverStripe\Dev\Exceptions\ExpectedErrorException;
|
||||||
use SilverStripe\Dev\Exceptions\ExpectedNoticeException;
|
use SilverStripe\Dev\Exceptions\ExpectedNoticeException;
|
||||||
use SilverStripe\Dev\Exceptions\ExpectedWarningException;
|
use SilverStripe\Dev\Exceptions\ExpectedWarningException;
|
||||||
use SilverStripe\Dev\Exceptions\UnexpectedErrorException;
|
use SilverStripe\ORM\DB;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test case class for the Silverstripe framework.
|
* Test case class for the Silverstripe framework.
|
||||||
@ -434,6 +434,9 @@ abstract class SapphireTest extends TestCase implements TestOnly
|
|||||||
*/
|
*/
|
||||||
public static function setUpBeforeClass(): void
|
public static function setUpBeforeClass(): void
|
||||||
{
|
{
|
||||||
|
// Disallow the use of DB replicas in tests
|
||||||
|
DB::setMustUsePrimary();
|
||||||
|
|
||||||
// Start tests
|
// Start tests
|
||||||
static::start();
|
static::start();
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ namespace SilverStripe\Dev;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
use LogicException;
|
||||||
use SilverStripe\Control\Controller;
|
use SilverStripe\Control\Controller;
|
||||||
use SilverStripe\Control\Cookie_Backend;
|
use SilverStripe\Control\Cookie_Backend;
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
@ -214,7 +215,7 @@ class TestSession
|
|||||||
$formCrawler = $page->filterXPath("//form[@id='$formID']");
|
$formCrawler = $page->filterXPath("//form[@id='$formID']");
|
||||||
$form = $formCrawler->form();
|
$form = $formCrawler->form();
|
||||||
} catch (InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
user_error("TestSession::submitForm failed to find the form {$formID}");
|
throw new LogicException("TestSession::submitForm failed to find the form '{$formID}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($data as $fieldName => $value) {
|
foreach ($data as $fieldName => $value) {
|
||||||
@ -235,7 +236,7 @@ class TestSession
|
|||||||
if ($button) {
|
if ($button) {
|
||||||
$btnXpath = "//button[@name='$button'] | //input[@name='$button'][@type='button' or @type='submit']";
|
$btnXpath = "//button[@name='$button'] | //input[@name='$button'][@type='button' or @type='submit']";
|
||||||
if (!$formCrawler->children()->filterXPath($btnXpath)->count()) {
|
if (!$formCrawler->children()->filterXPath($btnXpath)->count()) {
|
||||||
throw new Exception("Can't find button '$button' to submit as part of test.");
|
throw new LogicException("Can't find button '$button' to submit as part of test.");
|
||||||
}
|
}
|
||||||
$values[$button] = true;
|
$values[$button] = true;
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ use SilverStripe\Model\ArrayData;
|
|||||||
* DropdownField::create(
|
* DropdownField::create(
|
||||||
* 'Country',
|
* 'Country',
|
||||||
* 'Country',
|
* 'Country',
|
||||||
* singleton(MyObject::class)->dbObject('Country')->enumValues()
|
* singleton(MyObject::class)->dbObject('Country')?->enumValues()
|
||||||
* );
|
* );
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
|
@ -154,7 +154,7 @@ class FieldGroup extends CompositeField
|
|||||||
/** @var FormField $subfield */
|
/** @var FormField $subfield */
|
||||||
$messages = [];
|
$messages = [];
|
||||||
foreach ($dataFields as $subfield) {
|
foreach ($dataFields as $subfield) {
|
||||||
$message = $subfield->obj('Message')->forTemplate();
|
$message = $subfield->obj('Message')?->forTemplate();
|
||||||
if ($message) {
|
if ($message) {
|
||||||
$messages[] = rtrim($message ?? '', ".");
|
$messages[] = rtrim($message ?? '', ".");
|
||||||
}
|
}
|
||||||
|
@ -899,10 +899,10 @@ class Form extends ModelData implements HasRequestHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the SS template that this form should use
|
* Set the template or template candidates that this form should use
|
||||||
* to render with. The default is "Form".
|
* to render with. The default is "Form".
|
||||||
*
|
*
|
||||||
* @param string|array $template The name of the template (without the .ss extension) or array form
|
* @param string|array $template The name of the template (without the file extension) or array of candidates
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setTemplate($template)
|
public function setTemplate($template)
|
||||||
|
@ -15,6 +15,7 @@ use SilverStripe\Core\Validation\ValidationResult;
|
|||||||
use SilverStripe\View\AttributesHTML;
|
use SilverStripe\View\AttributesHTML;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
use SilverStripe\Model\ModelData;
|
use SilverStripe\Model\ModelData;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a field in a form.
|
* Represents a field in a form.
|
||||||
@ -273,6 +274,8 @@ class FormField extends RequestHandler
|
|||||||
'Title' => 'Text',
|
'Title' => 'Text',
|
||||||
'RightTitle' => 'Text',
|
'RightTitle' => 'Text',
|
||||||
'Description' => 'HTMLFragment',
|
'Description' => 'HTMLFragment',
|
||||||
|
// This is an associative arrays, but we cast to Text so we can get a JSON string representation
|
||||||
|
'SchemaData' => 'Text',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -458,7 +461,7 @@ class FormField extends RequestHandler
|
|||||||
*
|
*
|
||||||
* By default, makes use of $this->dataValue()
|
* 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)
|
public function saveInto(DataObjectInterface $record)
|
||||||
{
|
{
|
||||||
@ -469,8 +472,10 @@ class FormField extends RequestHandler
|
|||||||
if (($pos = strrpos($this->name ?? '', '.')) !== false) {
|
if (($pos = strrpos($this->name ?? '', '.')) !== false) {
|
||||||
$relation = substr($this->name ?? '', 0, $pos);
|
$relation = substr($this->name ?? '', 0, $pos);
|
||||||
$fieldName = substr($this->name ?? '', $pos + 1);
|
$fieldName = substr($this->name ?? '', $pos + 1);
|
||||||
|
if ($record instanceof DataObject) {
|
||||||
$component = $record->relObject($relation);
|
$component = $record->relObject($relation);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($fieldName && $component) {
|
if ($fieldName && $component) {
|
||||||
$component->setCastedField($fieldName, $this->dataValue());
|
$component->setCastedField($fieldName, $this->dataValue());
|
||||||
@ -1027,7 +1032,7 @@ class FormField extends RequestHandler
|
|||||||
*/
|
*/
|
||||||
protected function _templates($customTemplate = null, $customTemplateSuffix = null)
|
protected function _templates($customTemplate = null, $customTemplateSuffix = null)
|
||||||
{
|
{
|
||||||
$templates = SSViewer::get_templates_by_class(static::class, $customTemplateSuffix, __CLASS__);
|
$templates = SSViewer::get_templates_by_class(static::class, $customTemplateSuffix ?? '', __CLASS__);
|
||||||
// Prefer any custom template
|
// Prefer any custom template
|
||||||
if ($customTemplate) {
|
if ($customTemplate) {
|
||||||
// Prioritise direct template
|
// Prioritise direct template
|
||||||
@ -1469,12 +1474,12 @@ class FormField extends RequestHandler
|
|||||||
'schemaType' => $this->getSchemaDataType(),
|
'schemaType' => $this->getSchemaDataType(),
|
||||||
'component' => $this->getSchemaComponent(),
|
'component' => $this->getSchemaComponent(),
|
||||||
'holderId' => $this->HolderID(),
|
'holderId' => $this->HolderID(),
|
||||||
'title' => $this->obj('Title')->getSchemaValue(),
|
'title' => $this->obj('Title')?->getSchemaValue(),
|
||||||
'source' => null,
|
'source' => null,
|
||||||
'extraClass' => $this->extraClass(),
|
'extraClass' => $this->extraClass(),
|
||||||
'description' => $this->obj('Description')->getSchemaValue(),
|
'description' => $this->obj('Description')?->getSchemaValue(),
|
||||||
'rightTitle' => $this->obj('RightTitle')->getSchemaValue(),
|
'rightTitle' => $this->obj('RightTitle')?->getSchemaValue(),
|
||||||
'leftTitle' => $this->obj('LeftTitle')->getSchemaValue(),
|
'leftTitle' => $this->obj('LeftTitle')?->getSchemaValue(),
|
||||||
'readOnly' => $this->isReadonly(),
|
'readOnly' => $this->isReadonly(),
|
||||||
'disabled' => $this->isDisabled(),
|
'disabled' => $this->isDisabled(),
|
||||||
'customValidationMessage' => $this->getCustomValidationMessage(),
|
'customValidationMessage' => $this->getCustomValidationMessage(),
|
||||||
|
@ -115,7 +115,7 @@ class FormScaffolder
|
|||||||
$fieldObject = $this
|
$fieldObject = $this
|
||||||
->obj
|
->obj
|
||||||
->dbObject($fieldName)
|
->dbObject($fieldName)
|
||||||
->scaffoldFormField(null, $this->getParamsArray());
|
?->scaffoldFormField(null, $this->getParamsArray());
|
||||||
}
|
}
|
||||||
// Allow fields to opt-out of scaffolding
|
// Allow fields to opt-out of scaffolding
|
||||||
if (!$fieldObject) {
|
if (!$fieldObject) {
|
||||||
@ -145,7 +145,7 @@ class FormScaffolder
|
|||||||
$fieldClass = $this->fieldClasses[$fieldName];
|
$fieldClass = $this->fieldClasses[$fieldName];
|
||||||
$hasOneField = new $fieldClass($fieldName);
|
$hasOneField = new $fieldClass($fieldName);
|
||||||
} else {
|
} else {
|
||||||
$hasOneField = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
|
$hasOneField = $this->obj->dbObject($fieldName)?->scaffoldFormField(null, $this->getParamsArray());
|
||||||
}
|
}
|
||||||
if (empty($hasOneField)) {
|
if (empty($hasOneField)) {
|
||||||
continue; // Allow fields to opt out of scaffolding
|
continue; // Allow fields to opt out of scaffolding
|
||||||
|
@ -17,6 +17,9 @@ use SilverStripe\Model\ArrayData;
|
|||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
use LogicException;
|
use LogicException;
|
||||||
use SilverStripe\Control\HTTPResponse_Exception;
|
use SilverStripe\Control\HTTPResponse_Exception;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\View\TemplateEngine;
|
||||||
|
use SilverStripe\View\ViewLayerData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is is responsible for adding objects to another object's has_many
|
* This class is is responsible for adding objects to another object's has_many
|
||||||
@ -283,12 +286,15 @@ class GridFieldAddExistingAutocompleter extends AbstractGridFieldComponent imple
|
|||||||
$json = [];
|
$json = [];
|
||||||
Config::nest();
|
Config::nest();
|
||||||
SSViewer::config()->set('source_file_comments', false);
|
SSViewer::config()->set('source_file_comments', false);
|
||||||
$viewer = SSViewer::fromString($this->resultsFormat);
|
|
||||||
|
$engine = Injector::inst()->create(TemplateEngine::class);
|
||||||
foreach ($results as $result) {
|
foreach ($results as $result) {
|
||||||
if (!$result->canView()) {
|
if (!$result->canView()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$title = Convert::html2raw($viewer->process($result));
|
$title = Convert::html2raw(
|
||||||
|
$engine->renderString($this->resultsFormat, ViewLayerData::create($result), cache: false)
|
||||||
|
);
|
||||||
$json[] = [
|
$json[] = [
|
||||||
'label' => $title,
|
'label' => $title,
|
||||||
'value' => $title,
|
'value' => $title,
|
||||||
|
@ -222,29 +222,6 @@ class GridFieldDataColumns extends AbstractGridFieldComponent implements GridFie
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Translate a Object.RelationName.ColumnName $columnName into the value that ColumnName returns
|
|
||||||
*
|
|
||||||
* @param ModelData $record
|
|
||||||
* @param string $columnName
|
|
||||||
* @return string|null - returns null if it could not found a value
|
|
||||||
*/
|
|
||||||
protected function getValueFromRelation($record, $columnName)
|
|
||||||
{
|
|
||||||
$fieldNameParts = explode('.', $columnName ?? '');
|
|
||||||
$tmpItem = clone($record);
|
|
||||||
for ($idx = 0; $idx < sizeof($fieldNameParts ?? []); $idx++) {
|
|
||||||
$methodName = $fieldNameParts[$idx];
|
|
||||||
// Last mmethod call from $columnName return what that method is returning
|
|
||||||
if ($idx == sizeof($fieldNameParts ?? []) - 1) {
|
|
||||||
return $tmpItem->XML_val($methodName);
|
|
||||||
}
|
|
||||||
// else get the object from this $methodName
|
|
||||||
$tmpItem = $tmpItem->$methodName();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Casts a field to a string which is safe to insert into HTML
|
* Casts a field to a string which is safe to insert into HTML
|
||||||
*
|
*
|
||||||
|
@ -49,6 +49,8 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
*/
|
*/
|
||||||
protected ?string $searchField = null;
|
protected ?string $searchField = null;
|
||||||
|
|
||||||
|
private string $placeHolderText = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
@ -245,6 +247,24 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the text to be used as a placeholder in the search field.
|
||||||
|
* If blank, the placeholder will be generated based on the class held in the GridField.
|
||||||
|
*/
|
||||||
|
public function getPlaceHolderText(): string
|
||||||
|
{
|
||||||
|
return $this->placeHolderText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the text to be used as a placeholder in the search field.
|
||||||
|
* If blank, this text will be generated based on the class held in the GridField.
|
||||||
|
*/
|
||||||
|
public function setPlaceHolderText(string $placeHolderText): static
|
||||||
|
{
|
||||||
|
$this->placeHolderText = $placeHolderText;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a search context based on the model class of the of the GridField
|
* Generate a search context based on the model class of the of the GridField
|
||||||
@ -318,7 +338,7 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
$schema = [
|
$schema = [
|
||||||
'formSchemaUrl' => $schemaUrl,
|
'formSchemaUrl' => $schemaUrl,
|
||||||
'name' => $searchField,
|
'name' => $searchField,
|
||||||
'placeholder' => _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $this->getTitle($gridField, $inst)]),
|
'placeholder' => $this->getPlaceHolder($inst),
|
||||||
'filters' => $filters ?: new \stdClass, // stdClass maps to empty json object '{}'
|
'filters' => $filters ?: new \stdClass, // stdClass maps to empty json object '{}'
|
||||||
'gridfield' => $gridField->getName(),
|
'gridfield' => $gridField->getName(),
|
||||||
'searchAction' => $searchAction->getAttribute('name'),
|
'searchAction' => $searchAction->getAttribute('name'),
|
||||||
@ -330,19 +350,6 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
return json_encode($schema);
|
return json_encode($schema);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTitle(GridField $gridField, object $inst): string
|
|
||||||
{
|
|
||||||
if ($gridField->Title) {
|
|
||||||
return $gridField->Title;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ClassInfo::hasMethod($inst, 'i18n_plural_name')) {
|
|
||||||
return $inst->i18n_plural_name();
|
|
||||||
}
|
|
||||||
|
|
||||||
return ClassInfo::shortName($inst);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the search form for the component
|
* Returns the search form for the component
|
||||||
*
|
*
|
||||||
@ -386,7 +393,7 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
$field->addExtraClass('stacked no-change-track');
|
$field->addExtraClass('stacked no-change-track');
|
||||||
}
|
}
|
||||||
|
|
||||||
$name = $this->getTitle($gridField, singleton($gridField->getModelClass()));
|
$name = $this->getTitle(singleton($gridField->getModelClass()));
|
||||||
|
|
||||||
$this->searchForm = $form = new Form(
|
$this->searchForm = $form = new Form(
|
||||||
$gridField,
|
$gridField,
|
||||||
@ -456,4 +463,32 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
|||||||
)
|
)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the text that will be used as a placeholder in the search field.
|
||||||
|
*
|
||||||
|
* @param object $obj An instance of the class that will be searched against.
|
||||||
|
* If getPlaceHolderText is empty, this object will be used to build the placeholder
|
||||||
|
* e.g. 'Search "My Data Object"'
|
||||||
|
*/
|
||||||
|
private function getPlaceHolder(object $obj): string
|
||||||
|
{
|
||||||
|
$placeholder = $this->getPlaceHolderText();
|
||||||
|
if (!empty($placeholder)) {
|
||||||
|
return $placeholder;
|
||||||
|
}
|
||||||
|
if ($obj) {
|
||||||
|
return _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $this->getTitle($obj)]);
|
||||||
|
}
|
||||||
|
return _t(__CLASS__ . '.Search_Default', 'Search');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTitle(object $inst): string
|
||||||
|
{
|
||||||
|
if (ClassInfo::hasMethod($inst, 'i18n_plural_name')) {
|
||||||
|
return $inst->i18n_plural_name();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ClassInfo::shortName($inst);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ namespace SilverStripe\Forms\GridField;
|
|||||||
use SilverStripe\Core\Convert;
|
use SilverStripe\Core\Convert;
|
||||||
use SilverStripe\Forms\HiddenField;
|
use SilverStripe\Forms\HiddenField;
|
||||||
use SilverStripe\ORM\DataList;
|
use SilverStripe\ORM\DataList;
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is a snapshot of the current status of a {@link GridField}.
|
* This class is a snapshot of the current status of a {@link GridField}.
|
||||||
@ -14,7 +15,7 @@ use SilverStripe\ORM\DataList;
|
|||||||
*
|
*
|
||||||
* @see GridField
|
* @see GridField
|
||||||
*/
|
*/
|
||||||
class GridState extends HiddenField
|
class GridState extends HiddenField implements Stringable
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,11 +130,7 @@ class GridState extends HiddenField
|
|||||||
return Convert::raw2att($this->Value());
|
return Convert::raw2att($this->Value());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function __toString(): string
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function __toString()
|
|
||||||
{
|
{
|
||||||
return $this->Value();
|
return $this->Value();
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,15 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Forms\GridField;
|
namespace SilverStripe\Forms\GridField;
|
||||||
|
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple set of data, similar to stdClass, but without the notice-level
|
* Simple set of data, similar to stdClass, but without the notice-level
|
||||||
* errors.
|
* errors.
|
||||||
*
|
*
|
||||||
* @see GridState
|
* @see GridState
|
||||||
*/
|
*/
|
||||||
class GridState_Data
|
class GridState_Data implements Stringable
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,7 +96,7 @@ class GridState_Data
|
|||||||
unset($this->data[$name]);
|
unset($this->data[$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
if (!$this->data) {
|
if (!$this->data) {
|
||||||
return "";
|
return "";
|
||||||
|
@ -5,9 +5,11 @@ namespace SilverStripe\Forms\HTMLEditor;
|
|||||||
use SilverStripe\Assets\Shortcodes\ImageShortcodeProvider;
|
use SilverStripe\Assets\Shortcodes\ImageShortcodeProvider;
|
||||||
use SilverStripe\Forms\FormField;
|
use SilverStripe\Forms\FormField;
|
||||||
use SilverStripe\Forms\TextareaField;
|
use SilverStripe\Forms\TextareaField;
|
||||||
use SilverStripe\ORM\DataObject;
|
|
||||||
use SilverStripe\ORM\DataObjectInterface;
|
use SilverStripe\ORM\DataObjectInterface;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use SilverStripe\Model\ModelData;
|
||||||
|
use SilverStripe\ORM\FieldType\DBField;
|
||||||
|
use SilverStripe\View\CastingService;
|
||||||
use SilverStripe\View\Parsers\HTMLValue;
|
use SilverStripe\View\Parsers\HTMLValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,13 +125,9 @@ class HTMLEditorField extends TextareaField
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param DataObject|DataObjectInterface $record
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function saveInto(DataObjectInterface $record)
|
public function saveInto(DataObjectInterface $record)
|
||||||
{
|
{
|
||||||
if ($record->hasField($this->name) && $record->escapeTypeForField($this->name) != 'xml') {
|
if (!$this->usesXmlFriendlyField($record)) {
|
||||||
throw new Exception(
|
throw new Exception(
|
||||||
'HTMLEditorField->saveInto(): This field should save into a HTMLText or HTMLVarchar field.'
|
'HTMLEditorField->saveInto(): This field should save into a HTMLText or HTMLVarchar field.'
|
||||||
);
|
);
|
||||||
@ -225,4 +223,15 @@ class HTMLEditorField extends TextareaField
|
|||||||
|
|
||||||
return $config;
|
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';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ use InvalidArgumentException;
|
|||||||
use SilverStripe\Assets\Folder;
|
use SilverStripe\Assets\Folder;
|
||||||
use SilverStripe\Control\HTTPRequest;
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Control\HTTPResponse;
|
use SilverStripe\Control\HTTPResponse;
|
||||||
|
use SilverStripe\Model\List\SS_List;
|
||||||
use SilverStripe\ORM\DataList;
|
use SilverStripe\ORM\DataList;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||||
@ -519,13 +520,20 @@ class TreeDropdownField extends FormField implements HasOneRelationFieldInterfac
|
|||||||
|
|
||||||
// Allow to pass values to be selected within the ajax request
|
// Allow to pass values to be selected within the ajax request
|
||||||
$value = $request->requestVar('forceValue') ?: $this->value;
|
$value = $request->requestVar('forceValue') ?: $this->value;
|
||||||
if ($value && ($values = preg_split('/,\s*/', $value ?? ''))) {
|
if ($value instanceof SS_List) {
|
||||||
|
$values = $value;
|
||||||
|
} elseif ($value) {
|
||||||
|
$values = preg_split('/,\s*/', $value ?? '');
|
||||||
|
} else {
|
||||||
|
$values = [];
|
||||||
|
}
|
||||||
|
if (!empty($values)) {
|
||||||
foreach ($values as $value) {
|
foreach ($values as $value) {
|
||||||
if (!$value || $value == 'unchanged') {
|
if (!$value || $value == 'unchanged') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$object = $this->objectForKey($value);
|
$object = is_object($value) ? $value : $this->objectForKey($value);
|
||||||
if (!$object) {
|
if (!$object) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -870,14 +878,14 @@ class TreeDropdownField extends FormField implements HasOneRelationFieldInterfac
|
|||||||
$ancestors = $record->getAncestors(true)->reverse();
|
$ancestors = $record->getAncestors(true)->reverse();
|
||||||
|
|
||||||
foreach ($ancestors as $parent) {
|
foreach ($ancestors as $parent) {
|
||||||
$title = $parent->obj($this->getTitleField())->getValue();
|
$title = $parent->obj($this->getTitleField())?->getValue();
|
||||||
$titlePath .= $title . '/';
|
$titlePath .= $title . '/';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$data['data']['valueObject'] = [
|
$data['data']['valueObject'] = [
|
||||||
'id' => $record->obj($this->getKeyField())->getValue(),
|
'id' => $record->obj($this->getKeyField())?->getValue(),
|
||||||
'title' => $record->obj($this->getTitleField())->getValue(),
|
'title' => $record->obj($this->getTitleField())?->getValue(),
|
||||||
'treetitle' => $record->obj($this->getLabelField())->getSchemaValue(),
|
'treetitle' => $record->obj($this->getLabelField())?->getSchemaValue(),
|
||||||
'titlePath' => $titlePath,
|
'titlePath' => $titlePath,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -92,10 +92,10 @@ class TreeMultiselectField extends TreeDropdownField
|
|||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
if ($item instanceof DataObject) {
|
if ($item instanceof DataObject) {
|
||||||
$values[] = [
|
$values[] = [
|
||||||
'id' => $item->obj($this->getKeyField())->getValue(),
|
'id' => $item->obj($this->getKeyField())?->getValue(),
|
||||||
'title' => $item->obj($this->getTitleField())->getValue(),
|
'title' => $item->obj($this->getTitleField())?->getValue(),
|
||||||
'parentid' => $item->ParentID,
|
'parentid' => $item->ParentID,
|
||||||
'treetitle' => $item->obj($this->getLabelField())->getSchemaValue(),
|
'treetitle' => $item->obj($this->getLabelField())?->getSchemaValue(),
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
$values[] = $item;
|
$values[] = $item;
|
||||||
@ -212,7 +212,7 @@ class TreeMultiselectField extends TreeDropdownField
|
|||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
$idArray[] = $item->ID;
|
$idArray[] = $item->ID;
|
||||||
$titleArray[] = ($item instanceof ModelData)
|
$titleArray[] = ($item instanceof ModelData)
|
||||||
? $item->obj($this->getLabelField())->forTemplate()
|
? $item->obj($this->getLabelField())?->forTemplate()
|
||||||
: Convert::raw2xml($item->{$this->getLabelField()});
|
: Convert::raw2xml($item->{$this->getLabelField()});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ namespace SilverStripe\Model;
|
|||||||
|
|
||||||
use SilverStripe\Core\ArrayLib;
|
use SilverStripe\Core\ArrayLib;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
use JsonSerializable;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,14 +17,9 @@ use stdClass;
|
|||||||
* ));
|
* ));
|
||||||
* </code>
|
* </code>
|
||||||
*/
|
*/
|
||||||
class ArrayData extends ModelData
|
class ArrayData extends ModelData implements JsonSerializable
|
||||||
{
|
{
|
||||||
|
protected array $array;
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
* @see ArrayData::_construct()
|
|
||||||
*/
|
|
||||||
protected $array;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param object|array $value An associative array, or an object with simple properties.
|
* @param object|array $value An associative array, or an object with simple properties.
|
||||||
@ -52,10 +48,8 @@ class ArrayData extends ModelData
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the source array
|
* Get the source array
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
public function toMap()
|
public function toMap(): array
|
||||||
{
|
{
|
||||||
return $this->array;
|
return $this->array;
|
||||||
}
|
}
|
||||||
@ -87,6 +81,7 @@ class ArrayData extends ModelData
|
|||||||
*/
|
*/
|
||||||
public function setField(string $fieldName, mixed $value): static
|
public function setField(string $fieldName, mixed $value): static
|
||||||
{
|
{
|
||||||
|
$this->objCacheClear();
|
||||||
$this->array[$fieldName] = $value;
|
$this->array[$fieldName] = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -102,6 +97,16 @@ class ArrayData extends ModelData
|
|||||||
return isset($this->array[$fieldName]);
|
return isset($this->array[$fieldName]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function exists(): bool
|
||||||
|
{
|
||||||
|
return !empty($this->array);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize(): array
|
||||||
|
{
|
||||||
|
return $this->array;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an associative array to a simple object
|
* Converts an associative array to a simple object
|
||||||
*
|
*
|
||||||
|
@ -56,7 +56,9 @@ abstract class ListDecorator extends ModelData implements SS_List, Sortable, Fil
|
|||||||
public function setList(SS_List&Sortable&Filterable&Limitable $list): ListDecorator
|
public function setList(SS_List&Sortable&Filterable&Limitable $list): ListDecorator
|
||||||
{
|
{
|
||||||
$this->list = $list;
|
$this->list = $list;
|
||||||
$this->failover = $this->list;
|
if ($list instanceof ModelData) {
|
||||||
|
$this->setFailover($list);
|
||||||
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Model;
|
namespace SilverStripe\Model;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use LogicException;
|
use LogicException;
|
||||||
use ReflectionMethod;
|
use ReflectionMethod;
|
||||||
@ -12,14 +11,14 @@ use SilverStripe\Core\Config\Configurable;
|
|||||||
use SilverStripe\Core\Convert;
|
use SilverStripe\Core\Convert;
|
||||||
use SilverStripe\Core\Extensible;
|
use SilverStripe\Core\Extensible;
|
||||||
use SilverStripe\Core\Injector\Injectable;
|
use SilverStripe\Core\Injector\Injectable;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
|
||||||
use SilverStripe\Dev\Debug;
|
use SilverStripe\Dev\Debug;
|
||||||
use SilverStripe\Core\ArrayLib;
|
use SilverStripe\Core\ArrayLib;
|
||||||
use SilverStripe\Model\List\ArrayList;
|
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
use SilverStripe\ORM\FieldType\DBField;
|
||||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||||
use SilverStripe\Model\ArrayData;
|
use SilverStripe\Model\ArrayData;
|
||||||
|
use SilverStripe\View\CastingService;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
|
use Stringable;
|
||||||
use UnexpectedValueException;
|
use UnexpectedValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,7 +28,7 @@ use UnexpectedValueException;
|
|||||||
* is provided and automatically escaped by ModelData. Any class that needs to be available to a view (controllers,
|
* 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.
|
* {@link DataObject}s, page controls) should inherit from this class.
|
||||||
*/
|
*/
|
||||||
class ModelData
|
class ModelData implements Stringable
|
||||||
{
|
{
|
||||||
use Extensible {
|
use Extensible {
|
||||||
defineMethods as extensibleDefineMethods;
|
defineMethods as extensibleDefineMethods;
|
||||||
@ -38,7 +37,7 @@ class ModelData
|
|||||||
use Configurable;
|
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:
|
||||||
*
|
*
|
||||||
* <code>
|
* <code>
|
||||||
* public static $casting = array (
|
* public static $casting = array (
|
||||||
@ -47,16 +46,18 @@ class ModelData
|
|||||||
* </code>
|
* </code>
|
||||||
*/
|
*/
|
||||||
private static array $casting = [
|
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.
|
* 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 ?string $default_cast = null;
|
||||||
|
|
||||||
private static array $casting_cache = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Acts as a PHP 8.2+ compliant replacement for dynamic properties
|
* Acts as a PHP 8.2+ compliant replacement for dynamic properties
|
||||||
@ -204,6 +205,7 @@ class ModelData
|
|||||||
|
|
||||||
public function setDynamicData(string $field, mixed $value): static
|
public function setDynamicData(string $field, mixed $value): static
|
||||||
{
|
{
|
||||||
|
$this->objCacheClear();
|
||||||
$this->dynamicData[$field] = $value;
|
$this->dynamicData[$field] = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -251,8 +253,7 @@ class ModelData
|
|||||||
// -----------------------------------------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add methods from the {@link ModelData::$failover} object, as well as wrapping any methods prefixed with an
|
* Add methods from the {@link ModelData::$failover} object
|
||||||
* underscore into a {@link ModelData::cachedCall()}.
|
|
||||||
*
|
*
|
||||||
* @throws LogicException
|
* @throws LogicException
|
||||||
*/
|
*/
|
||||||
@ -305,12 +306,18 @@ class ModelData
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the class name (though subclasses may return something else)
|
|
||||||
*/
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
return static::class;
|
return $this->forTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the HTML markup that represents this model when it is directly injected into a template (e.g. using $Me).
|
||||||
|
* By default this attempts to render the model using templates based on the class hierarchy.
|
||||||
|
*/
|
||||||
|
public function forTemplate(): string
|
||||||
|
{
|
||||||
|
return $this->renderWith($this->getViewerTemplates());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCustomisedObj(): ?ModelData
|
public function getCustomisedObj(): ?ModelData
|
||||||
@ -326,14 +333,10 @@ class ModelData
|
|||||||
// CASTING ---------------------------------------------------------------------------------------------------------
|
// 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.
|
* 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.
|
// Get casting if it has been configured.
|
||||||
// DB fields and PHP methods are all case insensitive so we normalise casing before checking.
|
// DB fields and PHP methods are all case insensitive so we normalise casing before checking.
|
||||||
@ -346,67 +349,15 @@ class ModelData
|
|||||||
// If no specific cast is declared, fall back to failover.
|
// If no specific cast is declared, fall back to failover.
|
||||||
$failover = $this->getFailover();
|
$failover = $this->getFailover();
|
||||||
if ($failover) {
|
if ($failover) {
|
||||||
$cast = $failover->castingHelper($field, $useFallback);
|
$cast = $failover->castingHelper($field);
|
||||||
if ($cast) {
|
if ($cast) {
|
||||||
return $cast;
|
return $cast;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($useFallback) {
|
|
||||||
return $this->defaultCastingHelper($field);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
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 -------------------------------------------------------------------------------------------
|
// TEMPLATE ACCESS LAYER -------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -417,9 +368,9 @@ class ModelData
|
|||||||
* - an SSViewer instance
|
* - an SSViewer instance
|
||||||
*
|
*
|
||||||
* @param string|array|SSViewer $template the template to render into
|
* @param string|array|SSViewer $template the template to render into
|
||||||
* @param ModelData|array|null $customFields fields to customise() the object with before rendering
|
* @param ModelData|array $customFields fields to customise() the object with before rendering
|
||||||
*/
|
*/
|
||||||
public function renderWith($template, ModelData|array|null $customFields = null): DBHTMLText
|
public function renderWith($template, ModelData|array $customFields = []): DBHTMLText
|
||||||
{
|
{
|
||||||
if (!is_object($template)) {
|
if (!is_object($template)) {
|
||||||
$template = SSViewer::create($template);
|
$template = SSViewer::create($template);
|
||||||
@ -429,9 +380,10 @@ class ModelData
|
|||||||
|
|
||||||
if ($customFields instanceof ModelData) {
|
if ($customFields instanceof ModelData) {
|
||||||
$data = $data->customise($customFields);
|
$data = $data->customise($customFields);
|
||||||
|
$customFields = [];
|
||||||
}
|
}
|
||||||
if ($template instanceof SSViewer) {
|
if ($template instanceof SSViewer) {
|
||||||
return $template->process($data, is_array($customFields) ? $customFields : null);
|
return $template->process($data, $customFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new UnexpectedValueException(
|
throw new UnexpectedValueException(
|
||||||
@ -440,27 +392,11 @@ class ModelData
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate the cache name for a field
|
* Get a cached value from the field cache for a field
|
||||||
*
|
|
||||||
* @param string $fieldName Name of field
|
|
||||||
* @param array $arguments List of optional arguments given
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
protected function objCacheName($fieldName, $arguments)
|
public function objCacheGet(string $fieldName, array $arguments = []): mixed
|
||||||
{
|
|
||||||
return $arguments
|
|
||||||
? $fieldName . ":" . var_export($arguments, true)
|
|
||||||
: $fieldName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a cached value from the field cache
|
|
||||||
*
|
|
||||||
* @param string $key Cache key
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
protected function objCacheGet($key)
|
|
||||||
{
|
{
|
||||||
|
$key = $this->objCacheName($fieldName, $arguments);
|
||||||
if (isset($this->objCache[$key])) {
|
if (isset($this->objCache[$key])) {
|
||||||
return $this->objCache[$key];
|
return $this->objCache[$key];
|
||||||
}
|
}
|
||||||
@ -468,24 +404,19 @@ class ModelData
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a value in the field cache
|
* Store a value in the field cache for a field
|
||||||
*
|
|
||||||
* @param string $key Cache key
|
|
||||||
* @param mixed $value
|
|
||||||
* @return $this
|
|
||||||
*/
|
*/
|
||||||
protected function objCacheSet($key, $value)
|
public function objCacheSet(string $fieldName, array $arguments, mixed $value): static
|
||||||
{
|
{
|
||||||
|
$key = $this->objCacheName($fieldName, $arguments);
|
||||||
$this->objCache[$key] = $value;
|
$this->objCache[$key] = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear object cache
|
* Clear object cache
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
*/
|
||||||
protected function objCacheClear()
|
public function objCacheClear(): static
|
||||||
{
|
{
|
||||||
$this->objCache = [];
|
$this->objCache = [];
|
||||||
return $this;
|
return $this;
|
||||||
@ -497,82 +428,46 @@ class ModelData
|
|||||||
*
|
*
|
||||||
* @return object|DBField|null The specific object representing the field, or null if there is no
|
* @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.
|
* 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(
|
public function obj(
|
||||||
string $fieldName,
|
string $fieldName,
|
||||||
array $arguments = [],
|
array $arguments = [],
|
||||||
bool $cache = false,
|
bool $cache = false
|
||||||
?string $cacheName = null
|
|
||||||
): ?object {
|
): ?object {
|
||||||
$hasObj = false;
|
|
||||||
if (!$cacheName && $cache) {
|
|
||||||
$cacheName = $this->objCacheName($fieldName, $arguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check pre-cached value
|
// Check pre-cached value
|
||||||
$value = $cache ? $this->objCacheGet($cacheName) : null;
|
$value = $cache ? $this->objCacheGet($fieldName, $arguments) : null;
|
||||||
if ($value !== null) {
|
if ($value === null) {
|
||||||
return $value;
|
$hasObj = false;
|
||||||
}
|
|
||||||
|
|
||||||
// Load value from record
|
// Load value from record
|
||||||
if ($this->hasMethod($fieldName)) {
|
if ($this->hasMethod($fieldName)) {
|
||||||
|
// Try methods first - there's a LOT of logic that assumes this will be checked first.
|
||||||
$hasObj = true;
|
$hasObj = true;
|
||||||
$value = call_user_func_array([$this, $fieldName], $arguments ?: []);
|
$value = call_user_func_array([$this, $fieldName], $arguments ?: []);
|
||||||
} else {
|
} else {
|
||||||
$hasObj = $this->hasField($fieldName) || ($this->hasMethod("get{$fieldName}") && $this->isAccessibleMethod("get{$fieldName}"));
|
$getter = "get{$fieldName}";
|
||||||
|
$hasGetter = $this->hasMethod($getter) && $this->isAccessibleMethod($getter);
|
||||||
|
// Try fields and getters if there was no method with that name.
|
||||||
|
$hasObj = $this->hasField($fieldName) || $hasGetter;
|
||||||
|
if ($hasGetter && !empty($arguments)) {
|
||||||
|
$value = $this->$getter(...$arguments);
|
||||||
|
} else {
|
||||||
$value = $this->$fieldName;
|
$value = $this->$fieldName;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Record in cache
|
||||||
|
if ($value !== null && $cache) {
|
||||||
|
$this->objCacheSet($fieldName, $arguments, $value);
|
||||||
|
}
|
||||||
|
|
||||||
// Return null early if there's no backing for this field
|
// 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.
|
// i.e. no poperty, no method, etc - it just doesn't exist on this model.
|
||||||
if (!$hasObj && $value === null) {
|
if (!$hasObj && $value === null) {
|
||||||
return 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap list arrays in ModelData so templates can handle them
|
return CastingService::singleton()->cast($value, $this, $fieldName, true);
|
||||||
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 ($cache) {
|
|
||||||
$this->objCacheSet($cacheName, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -588,36 +483,6 @@ class ModelData
|
|||||||
return (bool) $result;
|
return (bool) $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the string value of a field on this object that has been suitable escaped to be inserted directly into a
|
|
||||||
* template.
|
|
||||||
*/
|
|
||||||
public function XML_val(string $field, array $arguments = [], bool $cache = false): string
|
|
||||||
{
|
|
||||||
$result = $this->obj($field, $arguments, $cache);
|
|
||||||
if (!$result) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
// Might contain additional formatting over ->XML(). E.g. parse shortcodes, nl2br()
|
|
||||||
return $result->forTemplate();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get an array of XML-escaped values by field name
|
|
||||||
*
|
|
||||||
* @param array $fields an array of field names
|
|
||||||
*/
|
|
||||||
public function getXMLValues(array $fields): array
|
|
||||||
{
|
|
||||||
$result = [];
|
|
||||||
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
$result[$field] = $this->XML_val($field);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// UTILITY METHODS -------------------------------------------------------------------------------------------------
|
// UTILITY METHODS -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -677,4 +542,14 @@ class ModelData
|
|||||||
{
|
{
|
||||||
return ModelDataDebugger::create($this);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,17 +49,22 @@ class ModelDataCustomised extends ModelData
|
|||||||
return isset($this->customised->$property) || isset($this->original->$property) || parent::__isset($property);
|
return isset($this->customised->$property) || isset($this->original->$property) || parent::__isset($property);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function forTemplate(): string
|
||||||
|
{
|
||||||
|
return $this->original->forTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
public function hasMethod($method)
|
public function hasMethod($method)
|
||||||
{
|
{
|
||||||
return $this->customised->hasMethod($method) || $this->original->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)) {
|
if ($this->customisedHas($field)) {
|
||||||
return $this->customised->cachedCall($fieldName, $arguments, $cacheName);
|
return $this->customised->castingHelper($field);
|
||||||
}
|
}
|
||||||
return $this->original->cachedCall($fieldName, $arguments, $cacheName);
|
return $this->original->castingHelper($field);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function obj(
|
public function obj(
|
||||||
@ -74,10 +79,15 @@ class ModelDataCustomised extends ModelData
|
|||||||
return $this->original->obj($fieldName, $arguments, $cache, $cacheName);
|
return $this->original->obj($fieldName, $arguments, $cache, $cacheName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function customisedHas(string $fieldName): bool
|
public function customisedHas(string $fieldName): bool
|
||||||
{
|
{
|
||||||
return property_exists($this->customised, $fieldName) ||
|
return property_exists($this->customised, $fieldName) ||
|
||||||
$this->customised->hasField($fieldName) ||
|
$this->customised->hasField($fieldName) ||
|
||||||
$this->customised->hasMethod($fieldName);
|
$this->customised->hasMethod($fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCustomisedModelData(): ?ModelData
|
||||||
|
{
|
||||||
|
return $this->customised;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ class TempDatabase
|
|||||||
*
|
*
|
||||||
* @param string $name DB Connection name to use
|
* @param string $name DB Connection name to use
|
||||||
*/
|
*/
|
||||||
public function __construct($name = 'default')
|
public function __construct($name = DB::CONN_PRIMARY)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
}
|
}
|
||||||
|
233
src/ORM/DB.php
233
src/ORM/DB.php
@ -3,6 +3,7 @@
|
|||||||
namespace SilverStripe\ORM;
|
namespace SilverStripe\ORM;
|
||||||
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
use RunTimeException;
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
use SilverStripe\Control\HTTPRequest;
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
@ -21,6 +22,22 @@ use SilverStripe\ORM\Queries\SQLExpression;
|
|||||||
*/
|
*/
|
||||||
class DB
|
class DB
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* A dynamic connection that will use either a replica connection (if one is
|
||||||
|
* available and not forced to use the 'primary' connection), or the 'primary' connection
|
||||||
|
*/
|
||||||
|
public const CONN_DYNAMIC = 'dynamic';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The 'primary' connection name, which is the main database connection and is used for all write
|
||||||
|
* operations and for read operations when the 'dynamic' connection is forced to use the 'primary' connection
|
||||||
|
*/
|
||||||
|
public const CONN_PRIMARY = 'primary';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of replicas databases that can be configured
|
||||||
|
*/
|
||||||
|
public const MAX_REPLICAS = 99;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This constant was added in SilverStripe 2.4 to indicate that SQL-queries
|
* This constant was added in SilverStripe 2.4 to indicate that SQL-queries
|
||||||
@ -58,19 +75,47 @@ class DB
|
|||||||
*/
|
*/
|
||||||
protected static $configs = [];
|
protected static $configs = [];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The last SQL query run.
|
* The last SQL query run.
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public static $lastQuery;
|
public static $lastQuery;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the last connection used. This is only used for unit-testing purposes.
|
||||||
|
* @interal
|
||||||
|
*/
|
||||||
|
private static string $lastConnectionName = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal flag to keep track of when db connection was attempted.
|
* Internal flag to keep track of when db connection was attempted.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
*/
|
*/
|
||||||
private static $connection_attempted = false;
|
private static $connection_attempted = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only use the primary database connection for the rest of the current request
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static bool $mustUsePrimary = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used by DB::withPrimary() to count the number of times it has been called
|
||||||
|
* Uses an int instead of a bool to allow for nested calls
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static int $withPrimaryCount = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The key of the replica config to use for this request
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static string $replicaConfigKey = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the global database connection.
|
* Set the global database connection.
|
||||||
* Pass an object that's a subclass of SS_Database. This object will be used when {@link DB::query()}
|
* Pass an object that's a subclass of SS_Database. This object will be used when {@link DB::query()}
|
||||||
@ -78,11 +123,11 @@ class DB
|
|||||||
*
|
*
|
||||||
* @param Database $connection The connection object to set as the connection.
|
* @param Database $connection The connection object to set as the connection.
|
||||||
* @param string $name The name to give to this connection. If you omit this argument, the connection
|
* @param string $name The name to give to this connection. If you omit this argument, the connection
|
||||||
* will be the default one used by the ORM. However, you can store other named connections to
|
* will be the primary one used by the ORM. However, you can store other named connections to
|
||||||
* be accessed through DB::get_conn($name). This is useful when you have an application that
|
* be accessed through DB::get_conn($name). This is useful when you have an application that
|
||||||
* needs to connect to more than one database.
|
* needs to connect to more than one database.
|
||||||
*/
|
*/
|
||||||
public static function set_conn(Database $connection, $name = 'default')
|
public static function set_conn(Database $connection, $name)
|
||||||
{
|
{
|
||||||
DB::$connections[$name] = $connection;
|
DB::$connections[$name] = $connection;
|
||||||
}
|
}
|
||||||
@ -92,11 +137,17 @@ class DB
|
|||||||
*
|
*
|
||||||
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
||||||
* the default connection is returned.
|
* the default connection is returned.
|
||||||
* @return Database
|
* @return Database|null
|
||||||
*/
|
*/
|
||||||
public static function get_conn($name = 'default')
|
public static function get_conn($name = DB::CONN_DYNAMIC)
|
||||||
{
|
{
|
||||||
|
// Allow default to connect to replica if configured
|
||||||
|
if ($name === DB::CONN_DYNAMIC) {
|
||||||
|
$name = DB::getDynamicConnectionName();
|
||||||
|
}
|
||||||
|
|
||||||
if (isset(DB::$connections[$name])) {
|
if (isset(DB::$connections[$name])) {
|
||||||
|
DB::$lastConnectionName = $name;
|
||||||
return DB::$connections[$name];
|
return DB::$connections[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,14 +160,50 @@ class DB
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the primary database connection will be used if the database is used right now
|
||||||
|
*/
|
||||||
|
public static function willUsePrimary(): bool
|
||||||
|
{
|
||||||
|
return DB::$mustUsePrimary || DB::$withPrimaryCount > 0 || !DB::hasReplicaConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set to use the primary database connection for rest of the current request
|
||||||
|
* meaning that replia connections will no longer be used
|
||||||
|
*
|
||||||
|
* This intentioally does not have a parameter to set this back to false, as this it to prevent
|
||||||
|
* accidentally attempting writing to a replica, or reading from an out of date replica
|
||||||
|
* after a write
|
||||||
|
*/
|
||||||
|
public static function setMustUsePrimary(): void
|
||||||
|
{
|
||||||
|
DB::$mustUsePrimary = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Only use the primary database connection when calling $callback
|
||||||
|
* Use this when doing non-mutable queries on the primary database where querying
|
||||||
|
* an out of sync replica could cause issues
|
||||||
|
* There's no need to use this with mutable queries, or after calling a mutable query
|
||||||
|
* as the primary database connection will be automatically used
|
||||||
|
*/
|
||||||
|
public static function withPrimary(callable $callback): mixed
|
||||||
|
{
|
||||||
|
DB::$withPrimaryCount++;
|
||||||
|
$result = $callback();
|
||||||
|
DB::$withPrimaryCount--;
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the schema manager for the current database
|
* Retrieves the schema manager for the current database
|
||||||
*
|
*
|
||||||
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
* @param string $name An optional name given to a connection in the DB::setConn() call.
|
||||||
* the default connection is returned.
|
* If omitted, a dynamic connection is returned.
|
||||||
* @return DBSchemaManager
|
* @return DBSchemaManager|null
|
||||||
*/
|
*/
|
||||||
public static function get_schema($name = 'default')
|
public static function get_schema($name = DB::CONN_DYNAMIC)
|
||||||
{
|
{
|
||||||
$connection = DB::get_conn($name);
|
$connection = DB::get_conn($name);
|
||||||
if ($connection) {
|
if ($connection) {
|
||||||
@ -130,11 +217,11 @@ class DB
|
|||||||
*
|
*
|
||||||
* @param SQLExpression $expression The expression object to build from
|
* @param SQLExpression $expression The expression object to build from
|
||||||
* @param array $parameters Out parameter for the resulting query parameters
|
* @param array $parameters Out parameter for the resulting query parameters
|
||||||
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
* @param string $name An optional name given to a connection in the DB::setConn() call.
|
||||||
* the default connection is returned.
|
* If omitted, a dynamic connection is returned.
|
||||||
* @return string The resulting SQL as a string
|
* @return string|null The resulting SQL as a string
|
||||||
*/
|
*/
|
||||||
public static function build_sql(SQLExpression $expression, &$parameters, $name = 'default')
|
public static function build_sql(SQLExpression $expression, &$parameters, $name = DB::CONN_DYNAMIC)
|
||||||
{
|
{
|
||||||
$connection = DB::get_conn($name);
|
$connection = DB::get_conn($name);
|
||||||
if ($connection) {
|
if ($connection) {
|
||||||
@ -148,11 +235,11 @@ class DB
|
|||||||
/**
|
/**
|
||||||
* Retrieves the connector object for the current database
|
* Retrieves the connector object for the current database
|
||||||
*
|
*
|
||||||
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted,
|
* @param string $name An optional name given to a connection in the DB::setConn() call.
|
||||||
* the default connection is returned.
|
* If omitted, a dynamic connection is returned.
|
||||||
* @return DBConnector
|
* @return DBConnector|null
|
||||||
*/
|
*/
|
||||||
public static function get_connector($name = 'default')
|
public static function get_connector($name = DB::CONN_DYNAMIC)
|
||||||
{
|
{
|
||||||
$connection = DB::get_conn($name);
|
$connection = DB::get_conn($name);
|
||||||
if ($connection) {
|
if ($connection) {
|
||||||
@ -268,8 +355,13 @@ class DB
|
|||||||
* @param string $label identifier for the connection
|
* @param string $label identifier for the connection
|
||||||
* @return Database
|
* @return Database
|
||||||
*/
|
*/
|
||||||
public static function connect($databaseConfig, $label = 'default')
|
public static function connect($databaseConfig, $label = DB::CONN_DYNAMIC)
|
||||||
{
|
{
|
||||||
|
// Allow default to connect to replica if configured
|
||||||
|
if ($label === DB::CONN_DYNAMIC) {
|
||||||
|
$label = DB::getDynamicConnectionName();
|
||||||
|
}
|
||||||
|
|
||||||
// This is used by the "testsession" module to test up a test session using an alternative name
|
// This is used by the "testsession" module to test up a test session using an alternative name
|
||||||
if ($name = DB::get_alternative_database_name()) {
|
if ($name = DB::get_alternative_database_name()) {
|
||||||
$databaseConfig['database'] = $name;
|
$databaseConfig['database'] = $name;
|
||||||
@ -288,6 +380,7 @@ class DB
|
|||||||
$conn = Injector::inst()->create($dbClass);
|
$conn = Injector::inst()->create($dbClass);
|
||||||
DB::set_conn($conn, $label);
|
DB::set_conn($conn, $label);
|
||||||
$conn->connect($databaseConfig);
|
$conn->connect($databaseConfig);
|
||||||
|
DB::$lastConnectionName = $label;
|
||||||
|
|
||||||
return $conn;
|
return $conn;
|
||||||
}
|
}
|
||||||
@ -298,7 +391,7 @@ class DB
|
|||||||
* @param array $databaseConfig
|
* @param array $databaseConfig
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public static function setConfig($databaseConfig, $name = 'default')
|
public static function setConfig($databaseConfig, $name = DB::CONN_PRIMARY)
|
||||||
{
|
{
|
||||||
static::$configs[$name] = $databaseConfig;
|
static::$configs[$name] = $databaseConfig;
|
||||||
}
|
}
|
||||||
@ -309,13 +402,42 @@ class DB
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function getConfig($name = 'default')
|
public static function getConfig($name = DB::CONN_PRIMARY)
|
||||||
{
|
{
|
||||||
if (isset(static::$configs[$name])) {
|
if (static::hasConfig($name)) {
|
||||||
return static::$configs[$name];
|
return static::$configs[$name];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a named connection config exists
|
||||||
|
*/
|
||||||
|
public static function hasConfig($name = DB::CONN_PRIMARY): bool
|
||||||
|
{
|
||||||
|
return array_key_exists($name, static::$configs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a replica database configuration key
|
||||||
|
* e.g. replica_01
|
||||||
|
*/
|
||||||
|
public static function getReplicaConfigKey(int $replica): string
|
||||||
|
{
|
||||||
|
$len = strlen((string) DB::MAX_REPLICAS);
|
||||||
|
return 'replica_' . str_pad($replica, $len, '0', STR_PAD_LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if there are any replica configurations
|
||||||
|
*/
|
||||||
|
public static function hasReplicaConfig(): bool
|
||||||
|
{
|
||||||
|
$configKeys = array_keys(static::$configs);
|
||||||
|
return !empty(array_filter($configKeys, function (string $key) {
|
||||||
|
return (bool) preg_match('#^replica_[0-9]+$#', $key);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if a database connection has been attempted.
|
* Returns true if a database connection has been attempted.
|
||||||
* In particular, it lets the caller know if we're still so early in the execution pipeline that
|
* In particular, it lets the caller know if we're still so early in the execution pipeline that
|
||||||
@ -335,8 +457,8 @@ class DB
|
|||||||
public static function query($sql, $errorLevel = E_USER_ERROR)
|
public static function query($sql, $errorLevel = E_USER_ERROR)
|
||||||
{
|
{
|
||||||
DB::$lastQuery = $sql;
|
DB::$lastQuery = $sql;
|
||||||
|
$name = DB::getDynamicConnectionName($sql);
|
||||||
return DB::get_conn()->query($sql, $errorLevel);
|
return DB::get_conn($name)->query($sql, $errorLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -427,8 +549,8 @@ class DB
|
|||||||
public static function prepared_query($sql, $parameters, $errorLevel = E_USER_ERROR)
|
public static function prepared_query($sql, $parameters, $errorLevel = E_USER_ERROR)
|
||||||
{
|
{
|
||||||
DB::$lastQuery = $sql;
|
DB::$lastQuery = $sql;
|
||||||
|
$name = DB::getDynamicConnectionName($sql);
|
||||||
return DB::get_conn()->preparedQuery($sql, $parameters, $errorLevel);
|
return DB::get_conn($name)->preparedQuery($sql, $parameters, $errorLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -680,4 +802,63 @@ class DB
|
|||||||
{
|
{
|
||||||
DB::get_schema()->alterationMessage($message, $type);
|
DB::get_schema()->alterationMessage($message, $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the database connection to use for the given SQL query
|
||||||
|
* The 'dynamic' connection can be either the primary or a replica connection if configured
|
||||||
|
*/
|
||||||
|
private static function getDynamicConnectionName(string $sql = ''): string
|
||||||
|
{
|
||||||
|
if (DB::willUsePrimary()) {
|
||||||
|
return DB::CONN_PRIMARY;
|
||||||
|
}
|
||||||
|
if (DB::isMutableSql($sql)) {
|
||||||
|
DB::$mustUsePrimary = true;
|
||||||
|
return DB::CONN_PRIMARY;
|
||||||
|
}
|
||||||
|
if (DB::$replicaConfigKey) {
|
||||||
|
return DB::$replicaConfigKey;
|
||||||
|
}
|
||||||
|
$name = DB::getRandomReplicaConfigKey();
|
||||||
|
DB::$replicaConfigKey = $name;
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the given SQL query is mutable
|
||||||
|
*/
|
||||||
|
private static function isMutableSql(string $sql): bool
|
||||||
|
{
|
||||||
|
$dbClass = DB::getConfig(DB::CONN_PRIMARY)['type'];
|
||||||
|
// This must use getServiceSpec() and not Injector::get/create() followed by
|
||||||
|
// getConnector() as this can remove the dbConn from a different connection
|
||||||
|
// under edge case conditions
|
||||||
|
$dbSpec = Injector::inst()->getServiceSpec($dbClass);
|
||||||
|
$connectorService = $dbSpec['properties']['connector'];
|
||||||
|
$connector = Injector::inst()->convertServiceProperty($connectorService);
|
||||||
|
return $connector->isQueryMutable($sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a random replica database configuration key from the available replica configurations
|
||||||
|
* The replica choosen will be used for the rest of the request, unless the primary connection
|
||||||
|
* is forced
|
||||||
|
*/
|
||||||
|
private static function getRandomReplicaConfigKey(): string
|
||||||
|
{
|
||||||
|
$replicaNumbers = [];
|
||||||
|
for ($i = 1; $i <= DB::MAX_REPLICAS; $i++) {
|
||||||
|
$replicaKey = DB::getReplicaConfigKey($i);
|
||||||
|
if (!DB::hasConfig($replicaKey)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$replicaNumbers[] = $i;
|
||||||
|
}
|
||||||
|
if (count($replicaNumbers) === 0) {
|
||||||
|
throw new RunTimeException('No replica configurations found');
|
||||||
|
}
|
||||||
|
// Choose a random replica
|
||||||
|
$index = rand(0, count($replicaNumbers) - 1);
|
||||||
|
return DB::getReplicaConfigKey($replicaNumbers[$index]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ use SilverStripe\Model\List\Limitable;
|
|||||||
use SilverStripe\Model\List\Map;
|
use SilverStripe\Model\List\Map;
|
||||||
use SilverStripe\Model\List\Sortable;
|
use SilverStripe\Model\List\Sortable;
|
||||||
use SilverStripe\Model\List\SS_List;
|
use SilverStripe\Model\List\SS_List;
|
||||||
|
use SilverStripe\ORM\FieldType\DBField;
|
||||||
use SilverStripe\ORM\Filters\SearchFilterable;
|
use SilverStripe\ORM\Filters\SearchFilterable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -237,7 +238,7 @@ class DataList extends ModelData implements SS_List, Filterable, Sortable, Limit
|
|||||||
*/
|
*/
|
||||||
public function sql(&$parameters = [])
|
public function sql(&$parameters = [])
|
||||||
{
|
{
|
||||||
return $this->dataQuery->query()->sql($parameters);
|
return $this->dataQuery->sql($parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1040,7 +1041,7 @@ class DataList extends ModelData implements SS_List, Filterable, Sortable, Limit
|
|||||||
|
|
||||||
private function executeQuery(): Query
|
private function executeQuery(): Query
|
||||||
{
|
{
|
||||||
$query = $this->dataQuery->query()->execute();
|
$query = $this->dataQuery->execute();
|
||||||
$this->fetchEagerLoadRelations($query);
|
$this->fetchEagerLoadRelations($query);
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
@ -1852,7 +1853,7 @@ class DataList extends ModelData implements SS_List, Filterable, Sortable, Limit
|
|||||||
return $relation;
|
return $relation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dbObject($fieldName)
|
public function dbObject(string $fieldName): ?DBField
|
||||||
{
|
{
|
||||||
return singleton($this->dataClass)->dbObject($fieldName);
|
return singleton($this->dataClass)->dbObject($fieldName);
|
||||||
}
|
}
|
||||||
|
@ -104,9 +104,6 @@ use stdClass;
|
|||||||
* }
|
* }
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* 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 $ID ID of the DataObject, 0 if the DataObject doesn't exist in database.
|
||||||
* @property int $OldID ID of object, if deleted
|
* @property int $OldID ID of object, if deleted
|
||||||
* @property string $Title
|
* @property string $Title
|
||||||
@ -145,6 +142,14 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
|
|||||||
*/
|
*/
|
||||||
private static $default_classname = null;
|
private static $default_classname = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this DataObject class must only use the primary database and not a read-only replica
|
||||||
|
* Note that this will be only be enforced when using DataQuery::execute() or
|
||||||
|
* another method that uses calls DataQuery::execute() internally e.g. DataObject::get()
|
||||||
|
* This will not be enforced when using low-level ORM functionality to query data e.g. SQLSelect or DB::query()
|
||||||
|
*/
|
||||||
|
private static bool $must_use_primary_db = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Data stored in this objects database record. An array indexed by fieldname.
|
* Data stored in this objects database record. An array indexed by fieldname.
|
||||||
*
|
*
|
||||||
@ -1937,6 +1942,7 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
|
|||||||
string $eagerLoadRelation,
|
string $eagerLoadRelation,
|
||||||
EagerLoadedList|DataObject $eagerLoadedData
|
EagerLoadedList|DataObject $eagerLoadedData
|
||||||
): void {
|
): void {
|
||||||
|
$this->objCacheClear();
|
||||||
$this->eagerLoadedData[$eagerLoadRelation] = $eagerLoadedData;
|
$this->eagerLoadedData[$eagerLoadRelation] = $eagerLoadedData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3033,7 +3039,7 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function castingHelper(string $field, bool $useFallback = true): ?string
|
public function castingHelper(string $field): ?string
|
||||||
{
|
{
|
||||||
$fieldSpec = static::getSchema()->fieldSpec(static::class, $field);
|
$fieldSpec = static::getSchema()->fieldSpec(static::class, $field);
|
||||||
if ($fieldSpec) {
|
if ($fieldSpec) {
|
||||||
@ -3051,7 +3057,7 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::castingHelper($field, $useFallback);
|
return parent::castingHelper($field);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -3234,11 +3240,11 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
|
|||||||
* - it still returns an object even when the field has no value.
|
* - it still returns an object even when the field has no value.
|
||||||
* - it only matches fields and not methods
|
* - it only matches fields and not methods
|
||||||
* - it matches foreign keys generated by has_one relationships, eg, "ParentID"
|
* - 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
|
* Returns null if the field doesn't exist
|
||||||
* @return DBField The field as a DBField object
|
|
||||||
*/
|
*/
|
||||||
public function dbObject($fieldName)
|
public function dbObject(string $fieldName): ?DBField
|
||||||
{
|
{
|
||||||
// Check for field in DB
|
// Check for field in DB
|
||||||
$schema = static::getSchema();
|
$schema = static::getSchema();
|
||||||
@ -3306,7 +3312,7 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
|
|||||||
} elseif ($component instanceof Relation || $component instanceof DataList) {
|
} elseif ($component instanceof Relation || $component instanceof DataList) {
|
||||||
// $relation could either be a field (aggregate), or another relation
|
// $relation could either be a field (aggregate), or another relation
|
||||||
$singleton = DataObject::singleton($component->dataClass());
|
$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))) {
|
} elseif ($component instanceof DataObject && ($dbObject = $component->dbObject($relation))) {
|
||||||
$component = $dbObject;
|
$component = $dbObject;
|
||||||
} elseif ($component instanceof ModelData && $component->hasField($relation)) {
|
} elseif ($component instanceof ModelData && $component->hasField($relation)) {
|
||||||
@ -4399,7 +4405,7 @@ class DataObject extends ModelData implements DataObjectInterface, i18nEntityPro
|
|||||||
// has_one fields should not use dbObject to check if a value is given
|
// has_one fields should not use dbObject to check if a value is given
|
||||||
$hasOne = static::getSchema()->hasOneComponent(static::class, $field);
|
$hasOne = static::getSchema()->hasOneComponent(static::class, $field);
|
||||||
if (!$hasOne && ($obj = $this->dbObject($field))) {
|
if (!$hasOne && ($obj = $this->dbObject($field))) {
|
||||||
return $obj->exists();
|
return $obj && $obj->exists();
|
||||||
} else {
|
} else {
|
||||||
return parent::hasValue($field, $arguments, $cache);
|
return parent::hasValue($field, $arguments, $cache);
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ use SilverStripe\ORM\Connect\Query;
|
|||||||
use SilverStripe\ORM\Queries\SQLConditionGroup;
|
use SilverStripe\ORM\Queries\SQLConditionGroup;
|
||||||
use SilverStripe\ORM\Queries\SQLSelect;
|
use SilverStripe\ORM\Queries\SQLSelect;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An object representing a query of data from the DataObject's supporting database.
|
* An object representing a query of data from the DataObject's supporting database.
|
||||||
@ -449,7 +450,9 @@ class DataQuery
|
|||||||
*/
|
*/
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
return $this->getFinalisedQuery()->execute();
|
return $this->withCorrectDatabase(
|
||||||
|
fn() => $this->getFinalisedQuery()->execute()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -472,7 +475,9 @@ class DataQuery
|
|||||||
public function count()
|
public function count()
|
||||||
{
|
{
|
||||||
$quotedColumn = DataObject::getSchema()->sqlColumnForField($this->dataClass(), 'ID');
|
$quotedColumn = DataObject::getSchema()->sqlColumnForField($this->dataClass(), 'ID');
|
||||||
return $this->getFinalisedQuery()->count("DISTINCT {$quotedColumn}");
|
return $this->withCorrectDatabase(
|
||||||
|
fn() => $this->getFinalisedQuery()->count("DISTINCT {$quotedColumn}")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -501,7 +506,9 @@ class DataQuery
|
|||||||
|
|
||||||
// Wrap the whole thing in an "EXISTS"
|
// Wrap the whole thing in an "EXISTS"
|
||||||
$sql = 'SELECT CASE WHEN EXISTS(' . $statement->sql($params) . ') THEN 1 ELSE 0 END';
|
$sql = 'SELECT CASE WHEN EXISTS(' . $statement->sql($params) . ') THEN 1 ELSE 0 END';
|
||||||
$result = DB::prepared_query($sql, $params);
|
$result = $this->withCorrectDatabase(
|
||||||
|
fn() => DB::prepared_query($sql, $params)
|
||||||
|
);
|
||||||
$row = $result->record();
|
$row = $result->record();
|
||||||
$result = reset($row);
|
$result = reset($row);
|
||||||
|
|
||||||
@ -582,7 +589,9 @@ class DataQuery
|
|||||||
*/
|
*/
|
||||||
public function aggregate($expression)
|
public function aggregate($expression)
|
||||||
{
|
{
|
||||||
return $this->getFinalisedQuery()->aggregate($expression)->execute()->value();
|
return $this->withCorrectDatabase(
|
||||||
|
fn() => $this->getFinalisedQuery()->aggregate($expression)->execute()->value()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -593,7 +602,9 @@ class DataQuery
|
|||||||
*/
|
*/
|
||||||
public function firstRow()
|
public function firstRow()
|
||||||
{
|
{
|
||||||
return $this->getFinalisedQuery()->firstRow();
|
return $this->withCorrectDatabase(
|
||||||
|
fn() => $this->getFinalisedQuery()->firstRow()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -604,7 +615,9 @@ class DataQuery
|
|||||||
*/
|
*/
|
||||||
public function lastRow()
|
public function lastRow()
|
||||||
{
|
{
|
||||||
return $this->getFinalisedQuery()->lastRow();
|
return $this->withCorrectDatabase(
|
||||||
|
fn() => $this->getFinalisedQuery()->lastRow()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1344,7 +1357,9 @@ class DataQuery
|
|||||||
$query->selectField($fieldExpression, $field);
|
$query->selectField($fieldExpression, $field);
|
||||||
$this->ensureSelectContainsOrderbyColumns($query, $originalSelect);
|
$this->ensureSelectContainsOrderbyColumns($query, $originalSelect);
|
||||||
|
|
||||||
return $query->execute()->column($field);
|
return $this->withCorrectDatabase(
|
||||||
|
fn() => $query->execute()->column($field)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1495,4 +1510,16 @@ class DataQuery
|
|||||||
|
|
||||||
return $updated;
|
return $updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls a callback on either the primary database or a replica, with respect to the configured
|
||||||
|
* value of `must_use_primary_db` on the current dataClass
|
||||||
|
*/
|
||||||
|
private function withCorrectDatabase(callable $callback): mixed
|
||||||
|
{
|
||||||
|
if (Config::inst()->get($this->dataClass(), 'must_use_primary_db')) {
|
||||||
|
return DB::withPrimary($callback);
|
||||||
|
}
|
||||||
|
return $callback();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,7 +171,7 @@ class EagerLoadedList extends ModelData implements Relation, SS_List, Filterable
|
|||||||
return $this->dataClass;
|
return $this->dataClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dbObject($fieldName): ?DBField
|
public function dbObject(string $fieldName): ?DBField
|
||||||
{
|
{
|
||||||
return singleton($this->dataClass)->dbObject($fieldName);
|
return singleton($this->dataClass)->dbObject($fieldName);
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ abstract class DBComposite extends DBField
|
|||||||
foreach ($this->compositeDatabaseFields() as $field => $spec) {
|
foreach ($this->compositeDatabaseFields() as $field => $spec) {
|
||||||
// Write sub-manipulation
|
// Write sub-manipulation
|
||||||
$fieldObject = $this->dbObject($field);
|
$fieldObject = $this->dbObject($field);
|
||||||
$fieldObject->writeToManipulation($manipulation);
|
$fieldObject?->writeToManipulation($manipulation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ abstract class DBComposite extends DBField
|
|||||||
// By default all fields
|
// By default all fields
|
||||||
foreach ($this->compositeDatabaseFields() as $field => $spec) {
|
foreach ($this->compositeDatabaseFields() as $field => $spec) {
|
||||||
$fieldObject = $this->dbObject($field);
|
$fieldObject = $this->dbObject($field);
|
||||||
if (!$fieldObject->exists()) {
|
if (!$fieldObject?->exists()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -520,11 +520,6 @@ abstract class DBField extends ModelData implements DBIndexable
|
|||||||
DBG;
|
DBG;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
|
||||||
{
|
|
||||||
return (string)$this->forTemplate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getArrayValue()
|
public function getArrayValue()
|
||||||
{
|
{
|
||||||
return $this->arrayValue;
|
return $this->arrayValue;
|
||||||
|
@ -47,7 +47,7 @@ class DBVarchar extends DBString
|
|||||||
* can be useful if you want to have text fields with a length limit that
|
* can be useful if you want to have text fields with a length limit that
|
||||||
* is dictated by the DB field.
|
* 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
|
* @return int The size of the field
|
||||||
*/
|
*/
|
||||||
|
@ -339,7 +339,7 @@ abstract class SearchFilter
|
|||||||
|
|
||||||
/** @var DBField $dbField */
|
/** @var DBField $dbField */
|
||||||
$dbField = singleton($this->model)->dbObject($this->name);
|
$dbField = singleton($this->model)->dbObject($this->name);
|
||||||
$dbField->setValue($this->value);
|
$dbField?->setValue($this->value);
|
||||||
return $dbField->RAW();
|
return $dbField->RAW();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,12 +6,13 @@ use SilverStripe\Core\Convert;
|
|||||||
use SilverStripe\ORM\Connect\Query;
|
use SilverStripe\ORM\Connect\Query;
|
||||||
use SilverStripe\ORM\DB;
|
use SilverStripe\ORM\DB;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for an object representing an SQL query.
|
* Abstract base class for an object representing an SQL query.
|
||||||
* The various parts of the SQL query can be manipulated individually.
|
* The various parts of the SQL query can be manipulated individually.
|
||||||
*/
|
*/
|
||||||
abstract class SQLExpression
|
abstract class SQLExpression implements Stringable
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,10 +45,8 @@ abstract class SQLExpression
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the generated SQL string for this query
|
* Return the generated SQL string for this query
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function __toString()
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$sql = $this->sql($parameters);
|
$sql = $this->sql($parameters);
|
||||||
|
@ -45,9 +45,6 @@ interface Relation extends SS_List, Filterable, Sortable, Limitable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the DBField object that represents the given field on the related class.
|
* 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;
|
||||||
}
|
}
|
||||||
|
@ -307,11 +307,8 @@ class UnsavedRelationList extends ArrayList implements Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the DBField object that represents the given field on the related class.
|
* 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);
|
return DataObject::singleton($this->dataClass)->dbObject($fieldName);
|
||||||
}
|
}
|
||||||
|
@ -226,9 +226,6 @@ class PolyOutput extends Output
|
|||||||
{
|
{
|
||||||
$listInfo = $this->listTypeStack[array_key_last($this->listTypeStack)];
|
$listInfo = $this->listTypeStack[array_key_last($this->listTypeStack)];
|
||||||
$listType = $listInfo['type'];
|
$listType = $listInfo['type'];
|
||||||
if ($listType === PolyOutput::LIST_ORDERED) {
|
|
||||||
echo '';
|
|
||||||
}
|
|
||||||
if ($options === null) {
|
if ($options === null) {
|
||||||
$options = $listInfo['options'];
|
$options = $listInfo['options'];
|
||||||
}
|
}
|
||||||
|
@ -88,6 +88,8 @@ class Group extends DataObject
|
|||||||
|
|
||||||
private static $table_name = "Group";
|
private static $table_name = "Group";
|
||||||
|
|
||||||
|
private static bool $must_use_primary_db = true;
|
||||||
|
|
||||||
private static $indexes = [
|
private static $indexes = [
|
||||||
'Title' => true,
|
'Title' => true,
|
||||||
'Code' => true,
|
'Code' => true,
|
||||||
|
@ -50,6 +50,8 @@ class LoginAttempt extends DataObject
|
|||||||
|
|
||||||
private static $table_name = "LoginAttempt";
|
private static $table_name = "LoginAttempt";
|
||||||
|
|
||||||
|
private static bool $must_use_primary_db = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $includerelations Indicate if the labels returned include relation fields
|
* @param bool $includerelations Indicate if the labels returned include relation fields
|
||||||
* @return array
|
* @return array
|
||||||
|
@ -105,6 +105,8 @@ class Member extends DataObject
|
|||||||
|
|
||||||
private static $table_name = "Member";
|
private static $table_name = "Member";
|
||||||
|
|
||||||
|
private static bool $must_use_primary_db = true;
|
||||||
|
|
||||||
private static $default_sort = '"Surname", "FirstName"';
|
private static $default_sort = '"Surname", "FirstName"';
|
||||||
|
|
||||||
private static $indexes = [
|
private static $indexes = [
|
||||||
@ -343,7 +345,7 @@ class Member extends DataObject
|
|||||||
{
|
{
|
||||||
/** @var DBDatetime $lockedOutUntilObj */
|
/** @var DBDatetime $lockedOutUntilObj */
|
||||||
$lockedOutUntilObj = $this->dbObject('LockedOutUntil');
|
$lockedOutUntilObj = $this->dbObject('LockedOutUntil');
|
||||||
if ($lockedOutUntilObj->InFuture()) {
|
if ($lockedOutUntilObj?->InFuture()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -370,7 +372,7 @@ class Member extends DataObject
|
|||||||
/** @var DBDatetime $firstFailureDate */
|
/** @var DBDatetime $firstFailureDate */
|
||||||
$firstFailureDate = $attempts->first()->dbObject('Created');
|
$firstFailureDate = $attempts->first()->dbObject('Created');
|
||||||
$maxAgeSeconds = $this->config()->get('lock_out_delay_mins') * 60;
|
$maxAgeSeconds = $this->config()->get('lock_out_delay_mins') * 60;
|
||||||
$lockedOutUntil = $firstFailureDate->getTimestamp() + $maxAgeSeconds;
|
$lockedOutUntil = $firstFailureDate?->getTimestamp() + $maxAgeSeconds;
|
||||||
$now = DBDatetime::now()->getTimestamp();
|
$now = DBDatetime::now()->getTimestamp();
|
||||||
if ($now < $lockedOutUntil) {
|
if ($now < $lockedOutUntil) {
|
||||||
return true;
|
return true;
|
||||||
@ -426,7 +428,7 @@ class Member extends DataObject
|
|||||||
$currentValue = $this->PasswordExpiry;
|
$currentValue = $this->PasswordExpiry;
|
||||||
$currentDate = $this->dbObject('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
|
// Only alter future expiries - this way an admin could see how long ago a password expired still
|
||||||
$this->PasswordExpiry = DBDatetime::now()->Rfc2822();
|
$this->PasswordExpiry = DBDatetime::now()->Rfc2822();
|
||||||
} elseif (!$dataValue && $this->isPasswordExpired()) {
|
} elseif (!$dataValue && $this->isPasswordExpired()) {
|
||||||
|
@ -27,6 +27,8 @@ class MemberPassword extends DataObject
|
|||||||
|
|
||||||
private static $table_name = "MemberPassword";
|
private static $table_name = "MemberPassword";
|
||||||
|
|
||||||
|
private static bool $must_use_primary_db = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log a password change from the given member.
|
* Log a password change from the given member.
|
||||||
* Call MemberPassword::log($this) from within Member whenever the password is changed.
|
* Call MemberPassword::log($this) from within Member whenever the password is changed.
|
||||||
|
@ -46,6 +46,8 @@ class Permission extends DataObject implements TemplateGlobalProvider, Resettabl
|
|||||||
|
|
||||||
private static $table_name = "Permission";
|
private static $table_name = "Permission";
|
||||||
|
|
||||||
|
private static bool $must_use_primary_db = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the value to use for the "Type" field if a permission should be
|
* This is the value to use for the "Type" field if a permission should be
|
||||||
* granted.
|
* granted.
|
||||||
@ -233,7 +235,7 @@ class Permission extends DataObject implements TemplateGlobalProvider, Resettabl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Raw SQL for efficiency
|
// Raw SQL for efficiency
|
||||||
$permission = DB::prepared_query(
|
$permission = DB::withPrimary(fn() => DB::prepared_query(
|
||||||
"SELECT \"ID\"
|
"SELECT \"ID\"
|
||||||
FROM \"Permission\"
|
FROM \"Permission\"
|
||||||
WHERE (
|
WHERE (
|
||||||
@ -249,7 +251,7 @@ class Permission extends DataObject implements TemplateGlobalProvider, Resettabl
|
|||||||
$groupParams,
|
$groupParams,
|
||||||
$argParams
|
$argParams
|
||||||
)
|
)
|
||||||
)->value();
|
)->value());
|
||||||
|
|
||||||
if ($permission) {
|
if ($permission) {
|
||||||
return $permission;
|
return $permission;
|
||||||
@ -257,7 +259,7 @@ class Permission extends DataObject implements TemplateGlobalProvider, Resettabl
|
|||||||
|
|
||||||
// Strict checking disabled?
|
// Strict checking disabled?
|
||||||
if (!static::config()->strict_checking || !$strict) {
|
if (!static::config()->strict_checking || !$strict) {
|
||||||
$hasPermission = DB::prepared_query(
|
$hasPermission = DB::withPrimary(fn() => DB::prepared_query(
|
||||||
"SELECT COUNT(*)
|
"SELECT COUNT(*)
|
||||||
FROM \"Permission\"
|
FROM \"Permission\"
|
||||||
WHERE (
|
WHERE (
|
||||||
@ -265,7 +267,7 @@ class Permission extends DataObject implements TemplateGlobalProvider, Resettabl
|
|||||||
\"Type\" = ?
|
\"Type\" = ?
|
||||||
)",
|
)",
|
||||||
array_merge($codeParams, [Permission::GRANT_PERMISSION])
|
array_merge($codeParams, [Permission::GRANT_PERMISSION])
|
||||||
)->value();
|
)->value());
|
||||||
|
|
||||||
if (!$hasPermission) {
|
if (!$hasPermission) {
|
||||||
return false;
|
return false;
|
||||||
@ -288,7 +290,8 @@ class Permission extends DataObject implements TemplateGlobalProvider, Resettabl
|
|||||||
if ($groupList) {
|
if ($groupList) {
|
||||||
$groupCSV = implode(", ", $groupList);
|
$groupCSV = implode(", ", $groupList);
|
||||||
|
|
||||||
$allowed = array_unique(DB::query("
|
$allowed = array_unique(
|
||||||
|
DB::withPrimary(fn() => DB::query("
|
||||||
SELECT \"Code\"
|
SELECT \"Code\"
|
||||||
FROM \"Permission\"
|
FROM \"Permission\"
|
||||||
WHERE \"Type\" = " . Permission::GRANT_PERMISSION . " AND \"GroupID\" IN ($groupCSV)
|
WHERE \"Type\" = " . Permission::GRANT_PERMISSION . " AND \"GroupID\" IN ($groupCSV)
|
||||||
@ -300,13 +303,16 @@ class Permission extends DataObject implements TemplateGlobalProvider, Resettabl
|
|||||||
INNER JOIN \"PermissionRole\" PR ON PRC.\"RoleID\" = PR.\"ID\"
|
INNER JOIN \"PermissionRole\" PR ON PRC.\"RoleID\" = PR.\"ID\"
|
||||||
INNER JOIN \"Group_Roles\" GR ON GR.\"PermissionRoleID\" = PR.\"ID\"
|
INNER JOIN \"Group_Roles\" GR ON GR.\"PermissionRoleID\" = PR.\"ID\"
|
||||||
WHERE \"GroupID\" IN ($groupCSV)
|
WHERE \"GroupID\" IN ($groupCSV)
|
||||||
")->column() ?? []);
|
"))->column() ?? []
|
||||||
|
);
|
||||||
|
|
||||||
$denied = array_unique(DB::query("
|
$denied = array_unique(
|
||||||
|
DB::withPrimary(fn() => DB::query("
|
||||||
SELECT \"Code\"
|
SELECT \"Code\"
|
||||||
FROM \"Permission\"
|
FROM \"Permission\"
|
||||||
WHERE \"Type\" = " . Permission::DENY_PERMISSION . " AND \"GroupID\" IN ($groupCSV)
|
WHERE \"Type\" = " . Permission::DENY_PERMISSION . " AND \"GroupID\" IN ($groupCSV)
|
||||||
")->column() ?? []);
|
"))->column() ?? []
|
||||||
|
);
|
||||||
|
|
||||||
return array_diff($allowed ?? [], $denied);
|
return array_diff($allowed ?? [], $denied);
|
||||||
}
|
}
|
||||||
@ -584,7 +590,9 @@ class Permission extends DataObject implements TemplateGlobalProvider, Resettabl
|
|||||||
$flatCodeArray[] = $code;
|
$flatCodeArray[] = $code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$otherPerms = DB::query("SELECT DISTINCT \"Code\" From \"Permission\" WHERE \"Code\" != ''")->column();
|
$otherPerms = DB::withPrimary(
|
||||||
|
fn() => DB::query("SELECT DISTINCT \"Code\" From \"Permission\" WHERE \"Code\" != ''")->column()
|
||||||
|
);
|
||||||
|
|
||||||
if ($otherPerms) {
|
if ($otherPerms) {
|
||||||
foreach ($otherPerms as $otherPerm) {
|
foreach ($otherPerms as $otherPerm) {
|
||||||
|
@ -117,7 +117,7 @@ class PermissionCheckboxSetField extends FormField
|
|||||||
$uninheritedCodes[$permission->Code][] = _t(
|
$uninheritedCodes[$permission->Code][] = _t(
|
||||||
'SilverStripe\\Security\\PermissionCheckboxSetField.AssignedTo',
|
'SilverStripe\\Security\\PermissionCheckboxSetField.AssignedTo',
|
||||||
'assigned to "{title}"',
|
'assigned to "{title}"',
|
||||||
['title' => $record->dbObject('Title')->forTemplate()]
|
['title' => $record->dbObject('Title')?->forTemplate()]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ class PermissionCheckboxSetField extends FormField
|
|||||||
'SilverStripe\\Security\\PermissionCheckboxSetField.FromRole',
|
'SilverStripe\\Security\\PermissionCheckboxSetField.FromRole',
|
||||||
'inherited from role "{title}"',
|
'inherited from role "{title}"',
|
||||||
'A permission inherited from a certain permission role',
|
'A permission inherited from a certain permission role',
|
||||||
['title' => $role->dbObject('Title')->forTemplate()]
|
['title' => $role->dbObject('Title')?->forTemplate()]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,8 +159,8 @@ class PermissionCheckboxSetField extends FormField
|
|||||||
'inherited from role "{roletitle}" on group "{grouptitle}"',
|
'inherited from role "{roletitle}" on group "{grouptitle}"',
|
||||||
'A permission inherited from a role on a certain group',
|
'A permission inherited from a role on a certain group',
|
||||||
[
|
[
|
||||||
'roletitle' => $role->dbObject('Title')->forTemplate(),
|
'roletitle' => $role->dbObject('Title')?->forTemplate(),
|
||||||
'grouptitle' => $parent->dbObject('Title')->forTemplate()
|
'grouptitle' => $parent->dbObject('Title')?->forTemplate()
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -176,7 +176,7 @@ class PermissionCheckboxSetField extends FormField
|
|||||||
'SilverStripe\\Security\\PermissionCheckboxSetField.FromGroup',
|
'SilverStripe\\Security\\PermissionCheckboxSetField.FromGroup',
|
||||||
'inherited from group "{title}"',
|
'inherited from group "{title}"',
|
||||||
'A permission inherited from a certain group',
|
'A permission inherited from a certain group',
|
||||||
['title' => $parent->dbObject('Title')->forTemplate()]
|
['title' => $parent->dbObject('Title')?->forTemplate()]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,8 @@ class PermissionRole extends DataObject
|
|||||||
|
|
||||||
private static $table_name = "PermissionRole";
|
private static $table_name = "PermissionRole";
|
||||||
|
|
||||||
|
private static bool $must_use_primary_db = true;
|
||||||
|
|
||||||
private static $default_sort = '"Title"';
|
private static $default_sort = '"Title"';
|
||||||
|
|
||||||
private static $singular_name = 'Role';
|
private static $singular_name = 'Role';
|
||||||
|
@ -24,6 +24,8 @@ class PermissionRoleCode extends DataObject
|
|||||||
|
|
||||||
private static $table_name = "PermissionRoleCode";
|
private static $table_name = "PermissionRoleCode";
|
||||||
|
|
||||||
|
private static bool $must_use_primary_db = true;
|
||||||
|
|
||||||
private static $indexes = [
|
private static $indexes = [
|
||||||
"Code" => true,
|
"Code" => true,
|
||||||
];
|
];
|
||||||
|
@ -44,6 +44,8 @@ class RememberLoginHash extends DataObject
|
|||||||
|
|
||||||
private static $table_name = "RememberLoginHash";
|
private static $table_name = "RememberLoginHash";
|
||||||
|
|
||||||
|
private static bool $must_use_primary_db = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines if logging out on one device also clears existing login tokens
|
* Determines if logging out on one device also clears existing login tokens
|
||||||
* on all other devices owned by the member.
|
* on all other devices owned by the member.
|
||||||
|
@ -437,6 +437,12 @@ class Security extends Controller implements TemplateGlobalProvider
|
|||||||
*/
|
*/
|
||||||
public static function setCurrentUser($currentUser = null)
|
public static function setCurrentUser($currentUser = null)
|
||||||
{
|
{
|
||||||
|
// Always use the primary database and not a replica if a CMS user is logged in
|
||||||
|
// This is to ensure that when viewing content on the frontend it is always
|
||||||
|
// up to date i.e. not from an unsynced replica
|
||||||
|
if ($currentUser && Permission::checkMember($currentUser, 'CMS_ACCESS')) {
|
||||||
|
DB::setMustUsePrimary();
|
||||||
|
}
|
||||||
Security::$currentUser = $currentUser;
|
Security::$currentUser = $currentUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
100
src/View/CastingService.php
Normal file
100
src/View/CastingService.php
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View;
|
||||||
|
|
||||||
|
use LogicException;
|
||||||
|
use SilverStripe\Core\ClassInfo;
|
||||||
|
use SilverStripe\Core\Injector\Injectable;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\Model\ArrayData;
|
||||||
|
use SilverStripe\Model\List\ArrayList;
|
||||||
|
use SilverStripe\Model\ModelData;
|
||||||
|
use SilverStripe\ORM\FieldType\DBBoolean;
|
||||||
|
use SilverStripe\ORM\FieldType\DBFloat;
|
||||||
|
use SilverStripe\ORM\FieldType\DBInt;
|
||||||
|
use SilverStripe\ORM\FieldType\DBText;
|
||||||
|
|
||||||
|
class CastingService
|
||||||
|
{
|
||||||
|
use Injectable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* 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
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
if ($source instanceof ModelData) {
|
||||||
|
$service = $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 (!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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@ class SSViewerTestState implements TestState
|
|||||||
{
|
{
|
||||||
public function setUp(SapphireTest $test)
|
public function setUp(SapphireTest $test)
|
||||||
{
|
{
|
||||||
SSViewer::set_themes(null);
|
SSViewer::set_themes([]);
|
||||||
SSViewer::setRewriteHashLinksDefault(null);
|
SSViewer::setRewriteHashLinksDefault(null);
|
||||||
ContentNegotiator::setEnabled(null);
|
ContentNegotiator::setEnabled(null);
|
||||||
}
|
}
|
||||||
|
11
src/View/Exception/MissingTemplateException.php
Normal file
11
src/View/Exception/MissingTemplateException.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Exception;
|
||||||
|
|
||||||
|
use LogicException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception that indicates a template was not found when attemping to use a template engine
|
||||||
|
*/
|
||||||
|
class MissingTemplateException extends LogicException
|
||||||
|
{}
|
467
src/View/SSTemplateEngine.php
Normal file
467
src/View/SSTemplateEngine.php
Normal file
@ -0,0 +1,467 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
use SilverStripe\Control\Director;
|
||||||
|
use SilverStripe\Core\Config\Configurable;
|
||||||
|
use SilverStripe\Core\Flushable;
|
||||||
|
use SilverStripe\Core\Injector\Injectable;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\Core\Path;
|
||||||
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||||
|
use SilverStripe\Security\Permission;
|
||||||
|
use SilverStripe\View\Exception\MissingTemplateException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses template files with an *.ss file extension, or strings representing templates in that format.
|
||||||
|
*
|
||||||
|
* In addition to a full template in the templates/ folder, a template in
|
||||||
|
* templates/Content or templates/Layout will be rendered into `$Content` and
|
||||||
|
* `$Layout`, respectively.
|
||||||
|
*
|
||||||
|
* A single template can be parsed by multiple nested SSTemplateEngine instances
|
||||||
|
* through `$Layout`/`$Content` placeholders, as well as `<% include MyTemplateFile %>` template commands.
|
||||||
|
*
|
||||||
|
* <b>Caching</b>
|
||||||
|
*
|
||||||
|
* Compiled templates are cached, usually on the filesystem.
|
||||||
|
* If you put ?flush=1 on your URL, it will force the template to be recompiled.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class SSTemplateEngine implements TemplateEngine, Flushable
|
||||||
|
{
|
||||||
|
use Injectable;
|
||||||
|
use Configurable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default prepended cache key for partial caching
|
||||||
|
*/
|
||||||
|
private static string $global_key = '$CurrentReadingMode, $CurrentUser.ID';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of models being processed
|
||||||
|
*/
|
||||||
|
protected static array $topLevel = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static bool $template_cache_flushed = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static bool $cacheblock_cache_flushed = false;
|
||||||
|
|
||||||
|
private ?CacheInterface $partialCacheStore = null;
|
||||||
|
|
||||||
|
private ?TemplateParser $parser = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A template or pool of candidate templates to choose from.
|
||||||
|
*/
|
||||||
|
private string|array $templateCandidates = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Absolute path to chosen template file which will be used in the call to render()
|
||||||
|
*/
|
||||||
|
private ?string $chosen = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Templates to use when looking up 'Layout' or 'Content'
|
||||||
|
*/
|
||||||
|
private array $subTemplates = [];
|
||||||
|
|
||||||
|
public function __construct(string|array $templateCandidates = [])
|
||||||
|
{
|
||||||
|
if (!empty($templateCandidates)) {
|
||||||
|
$this->setTemplate($templateCandidates);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the given template, passing it the given data.
|
||||||
|
* Used by the <% include %> template tag to process included templates.
|
||||||
|
*
|
||||||
|
* @param array $overlay Associative array of fields (e.g. args into an include template) to inject into the
|
||||||
|
* template as properties. These override properties and methods with the same name from $data and from global
|
||||||
|
* template providers.
|
||||||
|
*/
|
||||||
|
public static function execute_template(array|string $template, ViewLayerData $data, array $overlay = [], ?SSViewer_Scope $scope = null): string
|
||||||
|
{
|
||||||
|
$engine = static::create($template);
|
||||||
|
return $engine->render($data, $overlay, $scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggered early in the request when someone requests a flush.
|
||||||
|
*/
|
||||||
|
public static function flush(): void
|
||||||
|
{
|
||||||
|
SSTemplateEngine::flushTemplateCache(true);
|
||||||
|
SSTemplateEngine::flushCacheBlockCache(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears all parsed template files in the cache folder.
|
||||||
|
*
|
||||||
|
* @param bool $force Set this to true to force a re-flush. If left to false, flushing
|
||||||
|
* will only be performed once a request.
|
||||||
|
*/
|
||||||
|
public static function flushTemplateCache(bool $force = false): void
|
||||||
|
{
|
||||||
|
if (!SSTemplateEngine::$template_cache_flushed || $force) {
|
||||||
|
$dir = dir(TEMP_PATH);
|
||||||
|
while (false !== ($file = $dir->read())) {
|
||||||
|
if (strstr($file ?? '', '.cache')) {
|
||||||
|
unlink(TEMP_PATH . DIRECTORY_SEPARATOR . $file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SSTemplateEngine::$template_cache_flushed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears all partial cache blocks.
|
||||||
|
*
|
||||||
|
* @param bool $force Set this to true to force a re-flush. If left to false, flushing
|
||||||
|
* will only be performed once a request.
|
||||||
|
*/
|
||||||
|
public static function flushCacheBlockCache(bool $force = false): void
|
||||||
|
{
|
||||||
|
if (!SSTemplateEngine::$cacheblock_cache_flushed || $force) {
|
||||||
|
$cache = Injector::inst()->get(CacheInterface::class . '.cacheblock');
|
||||||
|
$cache->clear();
|
||||||
|
SSTemplateEngine::$cacheblock_cache_flushed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasTemplate(array|string $templateCandidates): bool
|
||||||
|
{
|
||||||
|
return (bool) $this->findTemplate($templateCandidates);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true): string
|
||||||
|
{
|
||||||
|
$hash = sha1($template);
|
||||||
|
$cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . ".cache.$hash";
|
||||||
|
|
||||||
|
// Generate a file whether we're caching or not.
|
||||||
|
// This is an inefficiency that's required due to the way rendered templates get processed.
|
||||||
|
if (!file_exists($cacheFile ?? '') || isset($_GET['flush'])) {
|
||||||
|
$content = $this->parseTemplateContent($template, "string sha1=$hash");
|
||||||
|
$fh = fopen($cacheFile ?? '', 'w');
|
||||||
|
fwrite($fh, $content ?? '');
|
||||||
|
fclose($fh);
|
||||||
|
}
|
||||||
|
|
||||||
|
$output = $this->includeGeneratedTemplate($cacheFile, $model, $overlay, []);
|
||||||
|
|
||||||
|
if (!$cache) {
|
||||||
|
unlink($cacheFile ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(ViewLayerData $model, array $overlay = [], ?SSViewer_Scope $scope = null): string
|
||||||
|
{
|
||||||
|
SSTemplateEngine::$topLevel[] = $model;
|
||||||
|
$template = $this->chosen;
|
||||||
|
|
||||||
|
// If there's no template, throw an exception
|
||||||
|
if (!$template) {
|
||||||
|
if (empty($this->templateCandidates)) {
|
||||||
|
throw new MissingTemplateException(
|
||||||
|
'No template to render. '
|
||||||
|
. 'Try calling setTemplate() or passing template candidates into the constructor.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$message = 'None of the following templates could be found: ';
|
||||||
|
$message .= print_r($this->templateCandidates, true);
|
||||||
|
$themes = SSViewer::get_themes();
|
||||||
|
if (!$themes) {
|
||||||
|
$message .= ' (no theme in use)';
|
||||||
|
} else {
|
||||||
|
$message .= ' in themes "' . print_r($themes, true) . '"';
|
||||||
|
}
|
||||||
|
throw new MissingTemplateException($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
$cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . '.cache'
|
||||||
|
. str_replace(['\\','/',':'], '.', Director::makeRelative(realpath($template ?? '')) ?? '');
|
||||||
|
$lastEdited = filemtime($template ?? '');
|
||||||
|
|
||||||
|
if (!file_exists($cacheFile ?? '') || filemtime($cacheFile ?? '') < $lastEdited) {
|
||||||
|
$content = file_get_contents($template ?? '');
|
||||||
|
$content = $this->parseTemplateContent($content, $template);
|
||||||
|
|
||||||
|
$fh = fopen($cacheFile ?? '', 'w');
|
||||||
|
fwrite($fh, $content ?? '');
|
||||||
|
fclose($fh);
|
||||||
|
}
|
||||||
|
|
||||||
|
$underlay = ['I18NNamespace' => basename($template ?? '')];
|
||||||
|
|
||||||
|
// Makes the rendered sub-templates available on the parent model,
|
||||||
|
// through $Content and $Layout placeholders.
|
||||||
|
foreach (['Content', 'Layout'] as $subtemplate) {
|
||||||
|
// Detect sub-template to use
|
||||||
|
$sub = $this->getSubtemplateFor($subtemplate);
|
||||||
|
if (!$sub) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create lazy-evaluated underlay for this subtemplate
|
||||||
|
$underlay[$subtemplate] = function () use ($model, $overlay, $sub) {
|
||||||
|
$subtemplateViewer = clone $this;
|
||||||
|
// Select the right template and render if the template exists
|
||||||
|
$subtemplateViewer->setTemplate($sub);
|
||||||
|
// If there's no template for that underlay, just don't render anything.
|
||||||
|
// This mirrors how SSViewer_Scope handles null values.
|
||||||
|
if (!$subtemplateViewer->chosen) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Render and wrap in DBHTMLText so it doesn't get escaped
|
||||||
|
return DBHTMLText::create()->setValue($subtemplateViewer->render($model, $overlay));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
$output = $this->includeGeneratedTemplate($cacheFile, $model, $overlay, $underlay, $scope);
|
||||||
|
|
||||||
|
array_pop(SSTemplateEngine::$topLevel);
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTemplate(string|array $templateCandidates): static
|
||||||
|
{
|
||||||
|
$this->templateCandidates = $templateCandidates;
|
||||||
|
$this->chosen = $this->findTemplate($templateCandidates);
|
||||||
|
$this->subTemplates = [];
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the template parser that will be used in template generation
|
||||||
|
*/
|
||||||
|
public function setParser(TemplateParser $parser): static
|
||||||
|
{
|
||||||
|
$this->parser = $parser;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the parser that is set for template generation
|
||||||
|
*/
|
||||||
|
public function getParser(): TemplateParser
|
||||||
|
{
|
||||||
|
if (!$this->parser) {
|
||||||
|
$this->setParser(Injector::inst()->get(SSTemplateParser::class));
|
||||||
|
}
|
||||||
|
return $this->parser;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the cache object to use when storing / retrieving partial cache blocks.
|
||||||
|
*/
|
||||||
|
public function setPartialCacheStore(CacheInterface $cache): static
|
||||||
|
{
|
||||||
|
$this->partialCacheStore = $cache;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the cache object to use when storing / retrieving partial cache blocks.
|
||||||
|
*/
|
||||||
|
public function getPartialCacheStore(): CacheInterface
|
||||||
|
{
|
||||||
|
if (!$this->partialCacheStore) {
|
||||||
|
$this->partialCacheStore = Injector::inst()->get(CacheInterface::class . '.cacheblock');
|
||||||
|
}
|
||||||
|
return $this->partialCacheStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An internal utility function to set up variables in preparation for including a compiled
|
||||||
|
* template, then do the include
|
||||||
|
*
|
||||||
|
* @param string $cacheFile The path to the file that contains the template compiled to PHP
|
||||||
|
* @param ViewLayerData $model The model to use as the root scope for the template
|
||||||
|
* @param array $overlay Any variables to layer on top of the scope
|
||||||
|
* @param array $underlay Any variables to layer underneath the scope
|
||||||
|
* @param SSViewer_Scope|null $inheritedScope The current scope of a parent template including a sub-template
|
||||||
|
*/
|
||||||
|
protected function includeGeneratedTemplate(
|
||||||
|
string $cacheFile,
|
||||||
|
ViewLayerData $model,
|
||||||
|
array $overlay,
|
||||||
|
array $underlay,
|
||||||
|
?SSViewer_Scope $inheritedScope = null
|
||||||
|
): string {
|
||||||
|
if (isset($_GET['showtemplate']) && $_GET['showtemplate'] && Permission::check('ADMIN')) {
|
||||||
|
$lines = file($cacheFile ?? '');
|
||||||
|
echo "<h2>Template: $cacheFile</h2>";
|
||||||
|
echo '<pre>';
|
||||||
|
foreach ($lines as $num => $line) {
|
||||||
|
echo str_pad($num+1, 5) . htmlentities($line, ENT_COMPAT, 'UTF-8');
|
||||||
|
}
|
||||||
|
echo '</pre>';
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache = $this->getPartialCacheStore();
|
||||||
|
$scope = new SSViewer_Scope($model, $overlay, $underlay, $inheritedScope);
|
||||||
|
$val = '';
|
||||||
|
|
||||||
|
// Placeholder for values exposed to $cacheFile
|
||||||
|
[$cache, $scope, $val];
|
||||||
|
include($cacheFile);
|
||||||
|
|
||||||
|
return $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the appropriate template to use for the named sub-template, or null if none are appropriate
|
||||||
|
*/
|
||||||
|
protected function getSubtemplateFor(string $subtemplate): ?array
|
||||||
|
{
|
||||||
|
// Get explicit subtemplate name
|
||||||
|
if (isset($this->subTemplates[$subtemplate])) {
|
||||||
|
return $this->subTemplates[$subtemplate];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't apply sub-templates if type is already specified (e.g. 'Includes')
|
||||||
|
if (isset($this->templateCandidates['type'])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter out any other typed templates as we can only add, not change type
|
||||||
|
$templates = array_filter(
|
||||||
|
(array) $this->templateCandidates,
|
||||||
|
function ($template) {
|
||||||
|
return !isset($template['type']);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (empty($templates)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set type to subtemplate
|
||||||
|
$templates['type'] = $subtemplate;
|
||||||
|
return $templates;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse given template contents
|
||||||
|
*
|
||||||
|
* @param string $content The template contents
|
||||||
|
* @param string $template The template file name
|
||||||
|
*/
|
||||||
|
protected function parseTemplateContent(string $content, string $template = ""): string
|
||||||
|
{
|
||||||
|
return $this->getParser()->compileString(
|
||||||
|
$content,
|
||||||
|
$template,
|
||||||
|
Director::isDev() && SSViewer::config()->uninherited('source_file_comments')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to find possible candidate templates from a set of template
|
||||||
|
* names from modules, current theme directory and finally the application
|
||||||
|
* folder.
|
||||||
|
*
|
||||||
|
* The template names can be passed in as plain strings, or be in the
|
||||||
|
* format "type/name", where type is the type of template to search for
|
||||||
|
* (e.g. Includes, Layout).
|
||||||
|
*
|
||||||
|
* The results of this method will be cached for future use.
|
||||||
|
*
|
||||||
|
* @param string|array $template Template name, or template spec in array format with the keys
|
||||||
|
* 'type' (type string) and 'templates' (template hierarchy in order of precedence).
|
||||||
|
* If 'templates' is omitted then any other item in the array will be treated as the template
|
||||||
|
* list, or list of templates each in the array spec given.
|
||||||
|
* Templates with an .ss extension will be treated as file paths, and will bypass
|
||||||
|
* theme-coupled resolution.
|
||||||
|
* @param array $themes List of themes to use to resolve themes. Defaults to {@see SSViewer::get_themes()}
|
||||||
|
* @return string Absolute path to resolved template file, or null if not resolved.
|
||||||
|
* File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss
|
||||||
|
* Note that type (e.g. 'Layout') is not the root level directory under 'templates'.
|
||||||
|
* Returns null if no template was found.
|
||||||
|
*/
|
||||||
|
private function findTemplate(string|array $template, array $themes = []): ?string
|
||||||
|
{
|
||||||
|
if (empty($themes)) {
|
||||||
|
$themes = SSViewer::get_themes();
|
||||||
|
}
|
||||||
|
|
||||||
|
$cacheAdapter = ThemeResourceLoader::inst()->getCache();
|
||||||
|
$cacheKey = 'findTemplate_' . md5(json_encode($template) . json_encode($themes));
|
||||||
|
|
||||||
|
// Look for a cached result for this data set
|
||||||
|
if ($cacheAdapter->has($cacheKey)) {
|
||||||
|
return $cacheAdapter->get($cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
$type = '';
|
||||||
|
if (is_array($template)) {
|
||||||
|
// Check if templates has type specified
|
||||||
|
if (array_key_exists('type', $template ?? [])) {
|
||||||
|
$type = $template['type'];
|
||||||
|
unset($template['type']);
|
||||||
|
}
|
||||||
|
// Templates are either nested in 'templates' or just the rest of the list
|
||||||
|
$templateList = array_key_exists('templates', $template ?? []) ? $template['templates'] : $template;
|
||||||
|
} else {
|
||||||
|
$templateList = [$template];
|
||||||
|
}
|
||||||
|
|
||||||
|
$themePaths = ThemeResourceLoader::inst()->getThemePaths($themes);
|
||||||
|
$baseDir = ThemeResourceLoader::inst()->getBase();
|
||||||
|
foreach ($templateList as $i => $template) {
|
||||||
|
// Check if passed list of templates in array format
|
||||||
|
if (is_array($template)) {
|
||||||
|
$path = $this->findTemplate($template, $themes);
|
||||||
|
if ($path) {
|
||||||
|
$cacheAdapter->set($cacheKey, $path);
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have an .ss extension, this is a path, not a template name. We should
|
||||||
|
// pass in templates without extensions in order for template manifest to find
|
||||||
|
// files dynamically.
|
||||||
|
if (substr($template ?? '', -3) == '.ss' && file_exists($template ?? '')) {
|
||||||
|
$cacheAdapter->set($cacheKey, $template);
|
||||||
|
return $template;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check string template identifier
|
||||||
|
$template = str_replace('\\', '/', $template ?? '');
|
||||||
|
$parts = explode('/', $template ?? '');
|
||||||
|
|
||||||
|
$tail = array_pop($parts);
|
||||||
|
$head = implode('/', $parts);
|
||||||
|
foreach ($themePaths as $themePath) {
|
||||||
|
// Join path
|
||||||
|
$pathParts = [ $baseDir, $themePath, 'templates', $head, $type, $tail ];
|
||||||
|
try {
|
||||||
|
$path = Path::join($pathParts) . '.ss';
|
||||||
|
if (file_exists($path ?? '')) {
|
||||||
|
$cacheAdapter->set($cacheKey, $path);
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
} catch (InvalidArgumentException $e) {
|
||||||
|
// No-op
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// No template found
|
||||||
|
$cacheAdapter->set($cacheKey, null);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -16,15 +16,6 @@ this is: framework/src/View):
|
|||||||
|
|
||||||
See the php-peg docs for more information on the parser format, and how to convert this file into SSTemplateParser.php
|
See the php-peg docs for more information on the parser format, and how to convert this file into SSTemplateParser.php
|
||||||
|
|
||||||
TODO:
|
|
||||||
Template comments - <%-- --%>
|
|
||||||
$Iteration
|
|
||||||
Partial cache blocks
|
|
||||||
i18n - we dont support then deprecated _t() or sprintf(_t()) methods; or the new <% t %> block yet
|
|
||||||
Add with and loop blocks
|
|
||||||
Add Up and Top
|
|
||||||
More error detection?
|
|
||||||
|
|
||||||
This comment will not appear in the output
|
This comment will not appear in the output
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -247,7 +238,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
}
|
}
|
||||||
|
|
||||||
$res['php'] .= ($sub['ArgumentMode'] == 'default') ? $sub['string_php'] :
|
$res['php'] .= ($sub['ArgumentMode'] == 'default') ? $sub['string_php'] :
|
||||||
str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '');
|
str_replace('$$FINAL', 'getValueAsArgument', $sub['php'] ?? '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!*
|
/*!*
|
||||||
@ -274,8 +265,8 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The basic generated PHP of LookupStep and LastLookupStep is the same, except that LookupStep calls 'obj' to
|
* 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 (XML_val, hasValue, obj)
|
* get the next ModelData in the sequence, and LastLookupStep calls different methods (getOutputValue, hasValue, scopeToIntermediateValue)
|
||||||
* depending on the context the lookup is used in.
|
* depending on the context the lookup is used in.
|
||||||
*/
|
*/
|
||||||
function Lookup_AddLookupStep(&$res, $sub, $method)
|
function Lookup_AddLookupStep(&$res, $sub, $method)
|
||||||
@ -286,15 +277,17 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
|
|
||||||
if (isset($sub['Call']['CallArguments']) && isset($sub['Call']['CallArguments']['php'])) {
|
if (isset($sub['Call']['CallArguments']) && isset($sub['Call']['CallArguments']['php'])) {
|
||||||
$arguments = $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 {
|
} else {
|
||||||
$res['php'] .= "->$method('$property', [], true)";
|
$type = ViewLayerData::TYPE_PROPERTY;
|
||||||
|
$res['php'] .= "->$method('$property', [], '$type')";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Lookup_LookupStep(&$res, $sub)
|
function Lookup_LookupStep(&$res, $sub)
|
||||||
{
|
{
|
||||||
$this->Lookup_AddLookupStep($res, $sub, 'obj');
|
$this->Lookup_AddLookupStep($res, $sub, 'scopeToIntermediateValue');
|
||||||
}
|
}
|
||||||
|
|
||||||
function Lookup_LastLookupStep(&$res, $sub)
|
function Lookup_LastLookupStep(&$res, $sub)
|
||||||
@ -357,7 +350,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
|
|
||||||
function InjectionVariables_Argument(&$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)
|
function InjectionVariables__finalise(&$res)
|
||||||
@ -392,7 +385,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
*/
|
*/
|
||||||
function Injection_STR(&$res, $sub)
|
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 +528,10 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
if (!empty($res['php'])) {
|
if (!empty($res['php'])) {
|
||||||
$res['php'] .= $sub['string_php'];
|
$res['php'] .= $sub['string_php'];
|
||||||
} else {
|
} else {
|
||||||
$res['php'] = str_replace('$$FINAL', 'XML_val', $sub['lookup_php'] ?? '');
|
$res['php'] = str_replace('$$FINAL', 'getOutputValue', $sub['lookup_php'] ?? '');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '');
|
$res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -566,8 +559,6 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
$res['php'] .= '((bool)'.$sub['php'].')';
|
$res['php'] .= '((bool)'.$sub['php'].')';
|
||||||
} else {
|
} else {
|
||||||
$php = ($sub['ArgumentMode'] == 'default' ? $sub['lookup_php'] : $sub['php']);
|
$php = ($sub['ArgumentMode'] == 'default' ? $sub['lookup_php'] : $sub['php']);
|
||||||
// TODO: kinda hacky - maybe we need a way to pass state down the parse chain so
|
|
||||||
// Lookup_LastLookupStep and Argument_BareWord can produce hasValue instead of XML_val
|
|
||||||
$res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? '');
|
$res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -697,7 +688,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
$res['php'] = '';
|
$res['php'] = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '');
|
$res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!*
|
/*!*
|
||||||
@ -779,7 +770,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
// the passed cache key, the block index, and the sha hash of the template.
|
// the passed cache key, the block index, and the sha hash of the template.
|
||||||
$res['php'] .= '$keyExpression = function() use ($scope, $cache) {' . PHP_EOL;
|
$res['php'] .= '$keyExpression = function() use ($scope, $cache) {' . PHP_EOL;
|
||||||
$res['php'] .= '$val = \'\';' . PHP_EOL;
|
$res['php'] .= '$val = \'\';' . PHP_EOL;
|
||||||
if ($globalKey = SSViewer::config()->get('global_key')) {
|
if ($globalKey = SSTemplateEngine::config()->get('global_key')) {
|
||||||
// Embed the code necessary to evaluate the globalKey directly into the template,
|
// Embed the code necessary to evaluate the globalKey directly into the template,
|
||||||
// so that SSTemplateParser only needs to be called during template regeneration.
|
// so that SSTemplateParser only needs to be called during template regeneration.
|
||||||
// Warning: If the global key is changed, it's necessary to flush the template cache.
|
// Warning: If the global key is changed, it's necessary to flush the template cache.
|
||||||
@ -827,7 +818,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
{
|
{
|
||||||
$entity = $sub['String']['text'];
|
$entity = $sub['String']['text'];
|
||||||
if (strpos($entity ?? '', '.') === false) {
|
if (strpos($entity ?? '', '.') === false) {
|
||||||
$res['php'] .= "\$scope->XML_val('I18NNamespace').'.$entity'";
|
$res['php'] .= "\$scope->getOutputValue('I18NNamespace').'.$entity'";
|
||||||
} else {
|
} else {
|
||||||
$res['php'] .= "'$entity'";
|
$res['php'] .= "'$entity'";
|
||||||
}
|
}
|
||||||
@ -915,7 +906,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$res['php'] .= str_replace('$$FINAL', 'obj', $sub['php'] ?? '') . '->self()';
|
$res['php'] .= str_replace('$$FINAL', 'scopeToIntermediateValue', $sub['php'] ?? '') . '->self()';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -947,8 +938,8 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
$template = $res['template'];
|
$template = $res['template'];
|
||||||
$arguments = $res['arguments'];
|
$arguments = $res['arguments'];
|
||||||
|
|
||||||
// Note: 'type' here is important to disable subTemplates in SSViewer::getSubtemplateFor()
|
// Note: 'type' here is important to disable subTemplates in SSTemplateEngine::getSubtemplateFor()
|
||||||
$res['php'] = '$val .= \\SilverStripe\\View\\SSViewer::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getItem(), [' .
|
$res['php'] = '$val .= \\SilverStripe\\View\\SSTemplateEngine::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' .
|
||||||
implode(',', $arguments)."], \$scope, true);\n";
|
implode(',', $arguments)."], \$scope, true);\n";
|
||||||
|
|
||||||
if ($this->includeDebuggingComments) { // Add include filename comments on dev sites
|
if ($this->includeDebuggingComments) { // Add include filename comments on dev sites
|
||||||
@ -1037,7 +1028,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
|
|
||||||
// loop without arguments loops on the current scope
|
// loop without arguments loops on the current scope
|
||||||
if ($res['ArgumentCount'] == 0) {
|
if ($res['ArgumentCount'] == 0) {
|
||||||
$on = '$scope->locally()->obj(\'Me\', [], true)';
|
$on = '$scope->locally()->self()';
|
||||||
} else { //loop in the normal way
|
} else { //loop in the normal way
|
||||||
$arg = $res['Arguments'][0];
|
$arg = $res['Arguments'][0];
|
||||||
if ($arg['ArgumentMode'] == 'string') {
|
if ($arg['ArgumentMode'] == 'string') {
|
||||||
@ -1045,13 +1036,13 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
}
|
}
|
||||||
$on = str_replace(
|
$on = str_replace(
|
||||||
'$$FINAL',
|
'$$FINAL',
|
||||||
'obj',
|
'scopeToIntermediateValue',
|
||||||
($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']
|
($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
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 .
|
$res['Template']['php'] . PHP_EOL .
|
||||||
'}; $scope->popScope(); ';
|
'}; $scope->popScope(); ';
|
||||||
}
|
}
|
||||||
@ -1071,7 +1062,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
throw new SSTemplateParseException('Control block cant take string as argument.', $this);
|
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
|
return
|
||||||
$on . '; $scope->pushScope();' . PHP_EOL .
|
$on . '; $scope->pushScope();' . PHP_EOL .
|
||||||
$res['Template']['php'] . PHP_EOL .
|
$res['Template']['php'] . PHP_EOL .
|
||||||
@ -1116,27 +1107,6 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This is an open block handler, for the <% debug %> utility tag
|
|
||||||
*/
|
|
||||||
function OpenBlock_Handle_Debug(&$res)
|
|
||||||
{
|
|
||||||
if ($res['ArgumentCount'] == 0) {
|
|
||||||
return '$scope->debug();';
|
|
||||||
} elseif ($res['ArgumentCount'] == 1) {
|
|
||||||
$arg = $res['Arguments'][0];
|
|
||||||
|
|
||||||
if ($arg['ArgumentMode'] == 'string') {
|
|
||||||
return 'Debug::show('.$arg['php'].');';
|
|
||||||
}
|
|
||||||
|
|
||||||
$php = ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php'];
|
|
||||||
return '$val .= Debug::show('.str_replace('FINALGET!', 'cachedCall', $php ?? '').');';
|
|
||||||
} else {
|
|
||||||
throw new SSTemplateParseException('Debug takes 0 or 1 argument only.', $this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an open block handler, for the <% base_tag %> tag
|
* This is an open block handler, for the <% base_tag %> tag
|
||||||
*/
|
*/
|
||||||
@ -1145,7 +1115,9 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
if ($res['ArgumentCount'] != 0) {
|
if ($res['ArgumentCount'] != 0) {
|
||||||
throw new SSTemplateParseException('Base_tag takes no arguments', $this);
|
throw new SSTemplateParseException('Base_tag takes no arguments', $this);
|
||||||
}
|
}
|
||||||
return '$val .= \\SilverStripe\\View\\SSViewer::get_base_tag($val);';
|
$code = '$isXhtml = preg_match(\'/<!DOCTYPE[^>]+xhtml/i\', $val);';
|
||||||
|
$code .= PHP_EOL . '$val .= \\SilverStripe\\View\\SSViewer::getBaseTag($isXhtml);';
|
||||||
|
return $code;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1297,9 +1269,9 @@ EOC;
|
|||||||
* @param string $templateName The name of the template, normally the filename the template source was loaded from
|
* @param string $templateName The name of the template, normally the filename the template source was loaded from
|
||||||
* @param bool $includeDebuggingComments True is debugging comments should be included in the output
|
* @param bool $includeDebuggingComments True is debugging comments should be included in the output
|
||||||
* @param bool $topTemplate True if this is a top template, false if it's just a template
|
* @param bool $topTemplate True if this is a top template, false if it's just a template
|
||||||
* @return mixed|string The php that, when executed (via include or exec) will behave as per the template source
|
* @return string The php that, when executed (via include or exec) will behave as per the template source
|
||||||
*/
|
*/
|
||||||
public function compileString($string, $templateName = "", $includeDebuggingComments = false, $topTemplate = true)
|
public function compileString(string $string, string $templateName = "", bool $includeDebuggingComments = false, bool $topTemplate = true): string
|
||||||
{
|
{
|
||||||
if (!trim($string ?? '')) {
|
if (!trim($string ?? '')) {
|
||||||
$code = '';
|
$code = '';
|
||||||
@ -1308,8 +1280,7 @@ EOC;
|
|||||||
|
|
||||||
$this->includeDebuggingComments = $includeDebuggingComments;
|
$this->includeDebuggingComments = $includeDebuggingComments;
|
||||||
|
|
||||||
// Ignore UTF8 BOM at beginning of string. TODO: Confirm this is needed, make sure SSViewer handles UTF
|
// Ignore UTF8 BOM at beginning of string.
|
||||||
// (and other encodings) properly
|
|
||||||
if (substr($string ?? '', 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
|
if (substr($string ?? '', 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
|
||||||
$this->pos = 3;
|
$this->pos = 3;
|
||||||
}
|
}
|
||||||
@ -1341,7 +1312,7 @@ EOC;
|
|||||||
* @param string $templateName
|
* @param string $templateName
|
||||||
* @return string $code
|
* @return string $code
|
||||||
*/
|
*/
|
||||||
protected function includeDebuggingComments($code, $templateName)
|
protected function includeDebuggingComments(string $code, string $templateName): string
|
||||||
{
|
{
|
||||||
// If this template contains a doctype, put it right after it,
|
// If this template contains a doctype, put it right after it,
|
||||||
// if not, put it after the <html> tag to avoid IE glitches
|
// if not, put it after the <html> tag to avoid IE glitches
|
||||||
@ -1375,11 +1346,10 @@ EOC;
|
|||||||
* Compiles some file that contains template source code, and returns the php code that will execute as per that
|
* Compiles some file that contains template source code, and returns the php code that will execute as per that
|
||||||
* source
|
* source
|
||||||
*
|
*
|
||||||
* @static
|
* @param string $template - A file path that contains template source code
|
||||||
* @param $template - A file path that contains template source code
|
* @return string - The php that, when executed (via include or exec) will behave as per the template source
|
||||||
* @return mixed|string - The php that, when executed (via include or exec) will behave as per the template source
|
|
||||||
*/
|
*/
|
||||||
public function compileFile($template)
|
public function compileFile(string $template): string
|
||||||
{
|
{
|
||||||
return $this->compileString(file_get_contents($template ?? ''), $template);
|
return $this->compileString(file_get_contents($template ?? ''), $template);
|
||||||
}
|
}
|
||||||
|
@ -572,7 +572,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
}
|
}
|
||||||
|
|
||||||
$res['php'] .= ($sub['ArgumentMode'] == 'default') ? $sub['string_php'] :
|
$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? > ")" )? */
|
/* Call: Method:Word ( "(" < :CallArguments? > ")" )? */
|
||||||
@ -765,8 +765,8 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The basic generated PHP of LookupStep and LastLookupStep is the same, except that LookupStep calls 'obj' to
|
* 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 (XML_val, hasValue, obj)
|
* get the next ModelData in the sequence, and LastLookupStep calls different methods (getOutputValue, hasValue, scopeToIntermediateValue)
|
||||||
* depending on the context the lookup is used in.
|
* depending on the context the lookup is used in.
|
||||||
*/
|
*/
|
||||||
function Lookup_AddLookupStep(&$res, $sub, $method)
|
function Lookup_AddLookupStep(&$res, $sub, $method)
|
||||||
@ -777,15 +777,17 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
|
|
||||||
if (isset($sub['Call']['CallArguments']) && isset($sub['Call']['CallArguments']['php'])) {
|
if (isset($sub['Call']['CallArguments']) && isset($sub['Call']['CallArguments']['php'])) {
|
||||||
$arguments = $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 {
|
} else {
|
||||||
$res['php'] .= "->$method('$property', [], true)";
|
$type = ViewLayerData::TYPE_PROPERTY;
|
||||||
|
$res['php'] .= "->$method('$property', [], '$type')";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Lookup_LookupStep(&$res, $sub)
|
function Lookup_LookupStep(&$res, $sub)
|
||||||
{
|
{
|
||||||
$this->Lookup_AddLookupStep($res, $sub, 'obj');
|
$this->Lookup_AddLookupStep($res, $sub, 'scopeToIntermediateValue');
|
||||||
}
|
}
|
||||||
|
|
||||||
function Lookup_LastLookupStep(&$res, $sub)
|
function Lookup_LastLookupStep(&$res, $sub)
|
||||||
@ -1009,7 +1011,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
|
|
||||||
function InjectionVariables_Argument(&$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)
|
function InjectionVariables__finalise(&$res)
|
||||||
@ -1158,7 +1160,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
|
|
||||||
function Injection_STR(&$res, $sub)
|
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 */
|
/* DollarMarkedLookup: SimpleInjection */
|
||||||
@ -1818,10 +1820,10 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
if (!empty($res['php'])) {
|
if (!empty($res['php'])) {
|
||||||
$res['php'] .= $sub['string_php'];
|
$res['php'] .= $sub['string_php'];
|
||||||
} else {
|
} else {
|
||||||
$res['php'] = str_replace('$$FINAL', 'XML_val', $sub['lookup_php'] ?? '');
|
$res['php'] = str_replace('$$FINAL', 'getOutputValue', $sub['lookup_php'] ?? '');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '');
|
$res['php'] .= str_replace('$$FINAL', 'getOutputValue', $sub['php'] ?? '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1886,8 +1888,6 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
$res['php'] .= '((bool)'.$sub['php'].')';
|
$res['php'] .= '((bool)'.$sub['php'].')';
|
||||||
} else {
|
} else {
|
||||||
$php = ($sub['ArgumentMode'] == 'default' ? $sub['lookup_php'] : $sub['php']);
|
$php = ($sub['ArgumentMode'] == 'default' ? $sub['lookup_php'] : $sub['php']);
|
||||||
// TODO: kinda hacky - maybe we need a way to pass state down the parse chain so
|
|
||||||
// Lookup_LastLookupStep and Argument_BareWord can produce hasValue instead of XML_val
|
|
||||||
$res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? '');
|
$res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2470,7 +2470,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
$res['php'] = '';
|
$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 |
|
/* CacheBlockTemplate: (Comment | Translate | If | Require | OldI18NTag | Include | ClosedBlock |
|
||||||
@ -3428,7 +3428,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
// the passed cache key, the block index, and the sha hash of the template.
|
// the passed cache key, the block index, and the sha hash of the template.
|
||||||
$res['php'] .= '$keyExpression = function() use ($scope, $cache) {' . PHP_EOL;
|
$res['php'] .= '$keyExpression = function() use ($scope, $cache) {' . PHP_EOL;
|
||||||
$res['php'] .= '$val = \'\';' . PHP_EOL;
|
$res['php'] .= '$val = \'\';' . PHP_EOL;
|
||||||
if ($globalKey = SSViewer::config()->get('global_key')) {
|
if ($globalKey = SSTemplateEngine::config()->get('global_key')) {
|
||||||
// Embed the code necessary to evaluate the globalKey directly into the template,
|
// Embed the code necessary to evaluate the globalKey directly into the template,
|
||||||
// so that SSTemplateParser only needs to be called during template regeneration.
|
// so that SSTemplateParser only needs to be called during template regeneration.
|
||||||
// Warning: If the global key is changed, it's necessary to flush the template cache.
|
// Warning: If the global key is changed, it's necessary to flush the template cache.
|
||||||
@ -3587,7 +3587,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
{
|
{
|
||||||
$entity = $sub['String']['text'];
|
$entity = $sub['String']['text'];
|
||||||
if (strpos($entity ?? '', '.') === false) {
|
if (strpos($entity ?? '', '.') === false) {
|
||||||
$res['php'] .= "\$scope->XML_val('I18NNamespace').'.$entity'";
|
$res['php'] .= "\$scope->getOutputValue('I18NNamespace').'.$entity'";
|
||||||
} else {
|
} else {
|
||||||
$res['php'] .= "'$entity'";
|
$res['php'] .= "'$entity'";
|
||||||
}
|
}
|
||||||
@ -3792,7 +3792,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$res['php'] .= str_replace('$$FINAL', 'obj', $sub['php'] ?? '') . '->self()';
|
$res['php'] .= str_replace('$$FINAL', 'scopeToIntermediateValue', $sub['php'] ?? '') . '->self()';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3896,8 +3896,8 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
$template = $res['template'];
|
$template = $res['template'];
|
||||||
$arguments = $res['arguments'];
|
$arguments = $res['arguments'];
|
||||||
|
|
||||||
// Note: 'type' here is important to disable subTemplates in SSViewer::getSubtemplateFor()
|
// Note: 'type' here is important to disable subTemplates in SSTemplateEngine::getSubtemplateFor()
|
||||||
$res['php'] = '$val .= \\SilverStripe\\View\\SSViewer::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getItem(), [' .
|
$res['php'] = '$val .= \\SilverStripe\\View\\SSTemplateEngine::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' .
|
||||||
implode(',', $arguments)."], \$scope, true);\n";
|
implode(',', $arguments)."], \$scope, true);\n";
|
||||||
|
|
||||||
if ($this->includeDebuggingComments) { // Add include filename comments on dev sites
|
if ($this->includeDebuggingComments) { // Add include filename comments on dev sites
|
||||||
@ -4265,7 +4265,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
|
|
||||||
// loop without arguments loops on the current scope
|
// loop without arguments loops on the current scope
|
||||||
if ($res['ArgumentCount'] == 0) {
|
if ($res['ArgumentCount'] == 0) {
|
||||||
$on = '$scope->locally()->obj(\'Me\', [], true)';
|
$on = '$scope->locally()->self()';
|
||||||
} else { //loop in the normal way
|
} else { //loop in the normal way
|
||||||
$arg = $res['Arguments'][0];
|
$arg = $res['Arguments'][0];
|
||||||
if ($arg['ArgumentMode'] == 'string') {
|
if ($arg['ArgumentMode'] == 'string') {
|
||||||
@ -4273,13 +4273,13 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
}
|
}
|
||||||
$on = str_replace(
|
$on = str_replace(
|
||||||
'$$FINAL',
|
'$$FINAL',
|
||||||
'obj',
|
'scopeToIntermediateValue',
|
||||||
($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']
|
($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
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 .
|
$res['Template']['php'] . PHP_EOL .
|
||||||
'}; $scope->popScope(); ';
|
'}; $scope->popScope(); ';
|
||||||
}
|
}
|
||||||
@ -4299,7 +4299,7 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
throw new SSTemplateParseException('Control block cant take string as argument.', $this);
|
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
|
return
|
||||||
$on . '; $scope->pushScope();' . PHP_EOL .
|
$on . '; $scope->pushScope();' . PHP_EOL .
|
||||||
$res['Template']['php'] . PHP_EOL .
|
$res['Template']['php'] . PHP_EOL .
|
||||||
@ -4401,27 +4401,6 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This is an open block handler, for the <% debug %> utility tag
|
|
||||||
*/
|
|
||||||
function OpenBlock_Handle_Debug(&$res)
|
|
||||||
{
|
|
||||||
if ($res['ArgumentCount'] == 0) {
|
|
||||||
return '$scope->debug();';
|
|
||||||
} elseif ($res['ArgumentCount'] == 1) {
|
|
||||||
$arg = $res['Arguments'][0];
|
|
||||||
|
|
||||||
if ($arg['ArgumentMode'] == 'string') {
|
|
||||||
return 'Debug::show('.$arg['php'].');';
|
|
||||||
}
|
|
||||||
|
|
||||||
$php = ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php'];
|
|
||||||
return '$val .= Debug::show('.str_replace('FINALGET!', 'cachedCall', $php ?? '').');';
|
|
||||||
} else {
|
|
||||||
throw new SSTemplateParseException('Debug takes 0 or 1 argument only.', $this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an open block handler, for the <% base_tag %> tag
|
* This is an open block handler, for the <% base_tag %> tag
|
||||||
*/
|
*/
|
||||||
@ -4430,7 +4409,9 @@ class SSTemplateParser extends Parser implements TemplateParser
|
|||||||
if ($res['ArgumentCount'] != 0) {
|
if ($res['ArgumentCount'] != 0) {
|
||||||
throw new SSTemplateParseException('Base_tag takes no arguments', $this);
|
throw new SSTemplateParseException('Base_tag takes no arguments', $this);
|
||||||
}
|
}
|
||||||
return '$val .= \\SilverStripe\\View\\SSViewer::get_base_tag($val);';
|
$code = '$isXhtml = preg_match(\'/<!DOCTYPE[^>]+xhtml/i\', $val);';
|
||||||
|
$code .= PHP_EOL . '$val .= \\SilverStripe\\View\\SSViewer::getBaseTag($isXhtml);';
|
||||||
|
return $code;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -5321,9 +5302,9 @@ EOC;
|
|||||||
* @param string $templateName The name of the template, normally the filename the template source was loaded from
|
* @param string $templateName The name of the template, normally the filename the template source was loaded from
|
||||||
* @param bool $includeDebuggingComments True is debugging comments should be included in the output
|
* @param bool $includeDebuggingComments True is debugging comments should be included in the output
|
||||||
* @param bool $topTemplate True if this is a top template, false if it's just a template
|
* @param bool $topTemplate True if this is a top template, false if it's just a template
|
||||||
* @return mixed|string The php that, when executed (via include or exec) will behave as per the template source
|
* @return string The php that, when executed (via include or exec) will behave as per the template source
|
||||||
*/
|
*/
|
||||||
public function compileString($string, $templateName = "", $includeDebuggingComments = false, $topTemplate = true)
|
public function compileString(string $string, string $templateName = "", bool $includeDebuggingComments = false, bool $topTemplate = true): string
|
||||||
{
|
{
|
||||||
if (!trim($string ?? '')) {
|
if (!trim($string ?? '')) {
|
||||||
$code = '';
|
$code = '';
|
||||||
@ -5332,8 +5313,7 @@ EOC;
|
|||||||
|
|
||||||
$this->includeDebuggingComments = $includeDebuggingComments;
|
$this->includeDebuggingComments = $includeDebuggingComments;
|
||||||
|
|
||||||
// Ignore UTF8 BOM at beginning of string. TODO: Confirm this is needed, make sure SSViewer handles UTF
|
// Ignore UTF8 BOM at beginning of string.
|
||||||
// (and other encodings) properly
|
|
||||||
if (substr($string ?? '', 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
|
if (substr($string ?? '', 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
|
||||||
$this->pos = 3;
|
$this->pos = 3;
|
||||||
}
|
}
|
||||||
@ -5365,7 +5345,7 @@ EOC;
|
|||||||
* @param string $templateName
|
* @param string $templateName
|
||||||
* @return string $code
|
* @return string $code
|
||||||
*/
|
*/
|
||||||
protected function includeDebuggingComments($code, $templateName)
|
protected function includeDebuggingComments(string $code, string $templateName): string
|
||||||
{
|
{
|
||||||
// If this template contains a doctype, put it right after it,
|
// If this template contains a doctype, put it right after it,
|
||||||
// if not, put it after the <html> tag to avoid IE glitches
|
// if not, put it after the <html> tag to avoid IE glitches
|
||||||
@ -5399,11 +5379,10 @@ EOC;
|
|||||||
* Compiles some file that contains template source code, and returns the php code that will execute as per that
|
* Compiles some file that contains template source code, and returns the php code that will execute as per that
|
||||||
* source
|
* source
|
||||||
*
|
*
|
||||||
* @static
|
* @param string $template - A file path that contains template source code
|
||||||
* @param $template - A file path that contains template source code
|
* @return string - The php that, when executed (via include or exec) will behave as per the template source
|
||||||
* @return mixed|string - The php that, when executed (via include or exec) will behave as per the template source
|
|
||||||
*/
|
*/
|
||||||
public function compileFile($template)
|
public function compileFile(string $template): string
|
||||||
{
|
{
|
||||||
return $this->compileString(file_get_contents($template ?? ''), $template);
|
return $this->compileString(file_get_contents($template ?? ''), $template);
|
||||||
}
|
}
|
||||||
|
@ -5,41 +5,20 @@ namespace SilverStripe\View;
|
|||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Core\Config\Configurable;
|
use SilverStripe\Core\Config\Configurable;
|
||||||
use SilverStripe\Core\ClassInfo;
|
use SilverStripe\Core\ClassInfo;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
|
||||||
use SilverStripe\Core\Convert;
|
use SilverStripe\Core\Convert;
|
||||||
use SilverStripe\Core\Flushable;
|
|
||||||
use SilverStripe\Core\Injector\Injector;
|
|
||||||
use SilverStripe\Core\Injector\Injectable;
|
use SilverStripe\Core\Injector\Injectable;
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
use SilverStripe\ORM\FieldType\DBField;
|
||||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||||
use SilverStripe\Security\Permission;
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use SilverStripe\Model\ModelData;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a template file with an *.ss file extension.
|
* Class that manages themes and interacts with TemplateEngine classes to render templates.
|
||||||
*
|
*
|
||||||
* In addition to a full template in the templates/ folder, a template in
|
* Ensures rendered templates are normalised, e.g have appropriate resources from the Requirements API.
|
||||||
* templates/Content or templates/Layout will be rendered into $Content and
|
|
||||||
* $Layout, respectively.
|
|
||||||
*
|
|
||||||
* A single template can be parsed by multiple nested {@link SSViewer} instances
|
|
||||||
* through $Layout/$Content placeholders, as well as <% include MyTemplateFile %> template commands.
|
|
||||||
*
|
|
||||||
* <b>Themes</b>
|
|
||||||
*
|
|
||||||
* See http://doc.silverstripe.org/themes and http://doc.silverstripe.org/themes:developing
|
|
||||||
*
|
|
||||||
* <b>Caching</b>
|
|
||||||
*
|
|
||||||
* Compiled templates are cached via {@link Cache}, usually on the filesystem.
|
|
||||||
* If you put ?flush=1 on your URL, it will force the template to be recompiled.
|
|
||||||
*
|
|
||||||
* @see http://doc.silverstripe.org/themes
|
|
||||||
* @see http://doc.silverstripe.org/themes:developing
|
|
||||||
*/
|
*/
|
||||||
class SSViewer implements Flushable
|
class SSViewer
|
||||||
{
|
{
|
||||||
use Configurable;
|
use Configurable;
|
||||||
use Injectable;
|
use Injectable;
|
||||||
@ -57,18 +36,8 @@ class SSViewer implements Flushable
|
|||||||
/**
|
/**
|
||||||
* A list (highest priority first) of themes to use
|
* A list (highest priority first) of themes to use
|
||||||
* Only used when {@link $theme_enabled} is set to TRUE.
|
* Only used when {@link $theme_enabled} is set to TRUE.
|
||||||
*
|
|
||||||
* @config
|
|
||||||
* @var string
|
|
||||||
*/
|
*/
|
||||||
private static $themes = [];
|
private static array $themes = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* Overridden value of $themes config
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $current_themes = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use the theme. Set to FALSE in order to disable themes,
|
* Use the theme. Set to FALSE in order to disable themes,
|
||||||
@ -76,103 +45,46 @@ class SSViewer implements Flushable
|
|||||||
* such as an administrative interface separate from the website theme.
|
* such as an administrative interface separate from the website theme.
|
||||||
* It retains the theme settings to be re-enabled, for example when a website content
|
* It retains the theme settings to be re-enabled, for example when a website content
|
||||||
* needs to be rendered from within this administrative interface.
|
* needs to be rendered from within this administrative interface.
|
||||||
*
|
|
||||||
* @config
|
|
||||||
* @var bool
|
|
||||||
*/
|
*/
|
||||||
private static $theme_enabled = true;
|
private static bool $theme_enabled = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default prepended cache key for partial caching
|
* If true, rendered templates will include comments indicating which template file was used.
|
||||||
*
|
* May not be supported for some rendering engines.
|
||||||
* @config
|
|
||||||
* @var string
|
|
||||||
*/
|
*/
|
||||||
private static $global_key = '$CurrentReadingMode, $CurrentUser.ID';
|
private static bool $source_file_comments = false;
|
||||||
|
|
||||||
/**
|
|
||||||
* @config
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
private static $source_file_comments = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set if hash links should be rewritten
|
* Set if hash links should be rewritten
|
||||||
*
|
|
||||||
* @config
|
|
||||||
* @var bool
|
|
||||||
*/
|
*/
|
||||||
private static $rewrite_hash_links = true;
|
private static bool $rewrite_hash_links = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overridden value of $themes config
|
||||||
|
*/
|
||||||
|
protected static array $current_themes = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overridden value of rewrite_hash_links config
|
* Overridden value of rewrite_hash_links config
|
||||||
*
|
*
|
||||||
* @var bool
|
* Can be set to "php" to rewrite hash links with PHP executable code.
|
||||||
*/
|
*/
|
||||||
protected static $current_rewrite_hash_links = null;
|
protected static null|bool|string $current_rewrite_hash_links = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instance variable to disable rewrite_hash_links (overrides global default)
|
* Instance variable to disable rewrite_hash_links (overrides global default)
|
||||||
* Leave null to use global state.
|
* Leave null to use global state.
|
||||||
*
|
*
|
||||||
* @var bool|null
|
* Can be set to "php" to rewrite hash links with PHP executable code.
|
||||||
*/
|
*/
|
||||||
protected $rewriteHashlinks = null;
|
protected null|bool|string $rewriteHashlinks = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* Determines whether resources from the Requirements API are included in a processed result.
|
||||||
* @ignore
|
|
||||||
*/
|
*/
|
||||||
private static $template_cache_flushed = false;
|
protected bool $includeRequirements = true;
|
||||||
|
|
||||||
/**
|
private TemplateEngine $templateEngine;
|
||||||
* @internal
|
|
||||||
* @ignore
|
|
||||||
*/
|
|
||||||
private static $cacheblock_cache_flushed = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List of items being processed
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $topLevel = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List of templates to select from
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $templates = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Absolute path to chosen template file
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $chosen = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Templates to use when looking up 'Layout' or 'Content'
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $subTemplates = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected $includeRequirements = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var TemplateParser
|
|
||||||
*/
|
|
||||||
protected $parser;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var CacheInterface
|
|
||||||
*/
|
|
||||||
protected $partialCacheStore = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|array $templates If passed as a string with .ss extension, used as the "main" template.
|
* @param string|array $templates If passed as a string with .ss extension, used as the "main" template.
|
||||||
@ -181,86 +93,41 @@ class SSViewer implements Flushable
|
|||||||
* <code>
|
* <code>
|
||||||
* array('MySpecificPage', 'MyPage', 'Page')
|
* array('MySpecificPage', 'MyPage', 'Page')
|
||||||
* </code>
|
* </code>
|
||||||
* @param TemplateParser $parser
|
|
||||||
*/
|
*/
|
||||||
public function __construct($templates, TemplateParser $parser = null)
|
public function __construct(string|array $templates, ?TemplateEngine $templateEngine = null)
|
||||||
{
|
{
|
||||||
if ($parser) {
|
if ($templateEngine) {
|
||||||
$this->setParser($parser);
|
$templateEngine->setTemplate($templates);
|
||||||
}
|
|
||||||
|
|
||||||
$this->setTemplate($templates);
|
|
||||||
|
|
||||||
if (!$this->chosen) {
|
|
||||||
$message = 'None of the following templates could be found: ';
|
|
||||||
$message .= print_r($templates, true);
|
|
||||||
|
|
||||||
$themes = SSViewer::get_themes();
|
|
||||||
if (!$themes) {
|
|
||||||
$message .= ' (no theme in use)';
|
|
||||||
} else {
|
} else {
|
||||||
$message .= ' in themes "' . print_r($themes, true) . '"';
|
$templateEngine = Injector::inst()->create(TemplateEngine::class, $templates);
|
||||||
}
|
}
|
||||||
|
$this->setTemplateEngine($templateEngine);
|
||||||
user_error($message ?? '', E_USER_WARNING);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Triggered early in the request when someone requests a flush.
|
|
||||||
*/
|
|
||||||
public static function flush()
|
|
||||||
{
|
|
||||||
SSViewer::flush_template_cache(true);
|
|
||||||
SSViewer::flush_cacheblock_cache(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a template from a string instead of a .ss file
|
|
||||||
*
|
|
||||||
* @param string $content The template content
|
|
||||||
* @param bool|void $cacheTemplate Whether or not to cache the template from string
|
|
||||||
* @return SSViewer
|
|
||||||
*/
|
|
||||||
public static function fromString($content, $cacheTemplate = null)
|
|
||||||
{
|
|
||||||
$viewer = SSViewer_FromString::create($content);
|
|
||||||
if ($cacheTemplate !== null) {
|
|
||||||
$viewer->setCacheTemplate($cacheTemplate);
|
|
||||||
}
|
|
||||||
return $viewer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assign the list of active themes to apply.
|
* Assign the list of active themes to apply.
|
||||||
* If default themes should be included add $default as the last entry.
|
* If default themes should be included add $default as the last entry.
|
||||||
*
|
|
||||||
* @param array $themes
|
|
||||||
*/
|
*/
|
||||||
public static function set_themes($themes = [])
|
public static function set_themes(array $themes): void
|
||||||
{
|
{
|
||||||
static::$current_themes = $themes;
|
static::$current_themes = $themes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add to the list of active themes to apply
|
* Add to the list of active themes to apply
|
||||||
*
|
|
||||||
* @param array $themes
|
|
||||||
*/
|
*/
|
||||||
public static function add_themes($themes = [])
|
public static function add_themes(array $themes)
|
||||||
{
|
{
|
||||||
$currentThemes = SSViewer::get_themes();
|
$currentThemes = SSViewer::get_themes();
|
||||||
$finalThemes = array_merge($themes, $currentThemes);
|
$finalThemes = array_merge($themes, $currentThemes);
|
||||||
// array_values is used to ensure sequential array keys as array_unique can leave gaps
|
// array_values is used to ensure sequential array keys as array_unique can leave gaps
|
||||||
static::set_themes(array_values(array_unique($finalThemes ?? [])));
|
static::set_themes(array_values(array_unique($finalThemes)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of active themes
|
* Get the list of active themes
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
public static function get_themes()
|
public static function get_themes(): array
|
||||||
{
|
{
|
||||||
$default = [SSViewer::PUBLIC_THEME, SSViewer::DEFAULT_THEME];
|
$default = [SSViewer::PUBLIC_THEME, SSViewer::DEFAULT_THEME];
|
||||||
|
|
||||||
@ -270,7 +137,7 @@ class SSViewer implements Flushable
|
|||||||
|
|
||||||
// Explicit list is assigned
|
// Explicit list is assigned
|
||||||
$themes = static::$current_themes;
|
$themes = static::$current_themes;
|
||||||
if (!isset($themes)) {
|
if (empty($themes)) {
|
||||||
$themes = SSViewer::config()->uninherited('themes');
|
$themes = SSViewer::config()->uninherited('themes');
|
||||||
}
|
}
|
||||||
if ($themes) {
|
if ($themes) {
|
||||||
@ -282,23 +149,26 @@ class SSViewer implements Flushable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Traverses the given the given class context looking for candidate template names
|
* Traverses the given the given class context looking for candidate template names
|
||||||
* which match each item in the class hierarchy. The resulting list of template candidates
|
* which match each item in the class hierarchy.
|
||||||
* may or may not exist, but you can invoke {@see SSViewer::chooseTemplate} on any list
|
*
|
||||||
* to determine the best candidate based on the current themes.
|
* This method does NOT check the filesystem, so the resulting list of template candidates
|
||||||
|
* may or may not exist - but you can pass these template candidates into the SSViewer
|
||||||
|
* constructor or into a TemplateEngine.
|
||||||
|
*
|
||||||
|
* If you really need know if a template file exists, you can call hasTemplate() on a TemplateEngine.
|
||||||
*
|
*
|
||||||
* @param string|object $classOrObject Valid class name, or object
|
* @param string|object $classOrObject Valid class name, or object
|
||||||
* @param string $suffix
|
|
||||||
* @param string $baseClass Class to halt ancestry search at
|
* @param string $baseClass Class to halt ancestry search at
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
public static function get_templates_by_class($classOrObject, $suffix = '', $baseClass = null)
|
public static function get_templates_by_class(
|
||||||
{
|
string|object $classOrObject,
|
||||||
|
string $suffix = '',
|
||||||
|
?string $baseClass = null
|
||||||
|
): array {
|
||||||
// Figure out the class name from the supplied context.
|
// Figure out the class name from the supplied context.
|
||||||
if (!is_object($classOrObject) && !(
|
if (is_string($classOrObject) && !class_exists($classOrObject ?? '')) {
|
||||||
is_string($classOrObject) && class_exists($classOrObject ?? '')
|
|
||||||
)) {
|
|
||||||
throw new InvalidArgumentException(
|
throw new InvalidArgumentException(
|
||||||
'SSViewer::get_templates_by_class() expects a valid class name as its first parameter.'
|
'SSViewer::get_templates_by_class() expects a valid class name or instantiated object as its first parameter.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,7 +184,7 @@ class SSViewer implements Flushable
|
|||||||
$templates[] = $matches['name'] . $suffix;
|
$templates[] = $matches['name'] . $suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($baseClass && $class == $baseClass) {
|
if ($baseClass && $class === $baseClass) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -323,26 +193,66 @@ class SSViewer implements Flushable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current item being processed
|
* Get an associative array of names to information about callable template provider methods.
|
||||||
*
|
*
|
||||||
* @return ModelData
|
* @var boolean $createObject If true, methods will be called on instantiated objects rather than statically on the class.
|
||||||
*/
|
*/
|
||||||
public static function topLevel()
|
public static function getMethodsFromProvider(string $providerInterface, $methodName, bool $createObject = false): array
|
||||||
{
|
{
|
||||||
if (SSViewer::$topLevel) {
|
$implementors = ClassInfo::implementorsOf($providerInterface);
|
||||||
return SSViewer::$topLevel[sizeof(SSViewer::$topLevel)-1];
|
if ($implementors) {
|
||||||
|
foreach ($implementors as $implementor) {
|
||||||
|
// Create a new instance of the object for method calls
|
||||||
|
if ($createObject) {
|
||||||
|
$implementor = new $implementor();
|
||||||
|
$exposedVariables = $implementor->$methodName();
|
||||||
|
} else {
|
||||||
|
$exposedVariables = $implementor::$methodName();
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the template engine used to render templates for this viewer
|
||||||
|
*/
|
||||||
|
public function getTemplateEngine(): TemplateEngine
|
||||||
|
{
|
||||||
|
return $this->templateEngine;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if rewrite hash links are enabled on this instance
|
* Check if rewrite hash links are enabled on this instance
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
public function getRewriteHashLinks()
|
public function getRewriteHashLinks(): null|bool|string
|
||||||
{
|
{
|
||||||
if (isset($this->rewriteHashlinks)) {
|
if ($this->rewriteHashlinks !== null) {
|
||||||
return $this->rewriteHashlinks;
|
return $this->rewriteHashlinks;
|
||||||
}
|
}
|
||||||
return static::getRewriteHashLinksDefault();
|
return static::getRewriteHashLinksDefault();
|
||||||
@ -350,11 +260,8 @@ class SSViewer implements Flushable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set if hash links are rewritten for this instance
|
* Set if hash links are rewritten for this instance
|
||||||
*
|
|
||||||
* @param bool $rewrite
|
|
||||||
* @return $this
|
|
||||||
*/
|
*/
|
||||||
public function setRewriteHashLinks($rewrite)
|
public function setRewriteHashLinks(null|bool|string $rewrite): static
|
||||||
{
|
{
|
||||||
$this->rewriteHashlinks = $rewrite;
|
$this->rewriteHashlinks = $rewrite;
|
||||||
return $this;
|
return $this;
|
||||||
@ -362,13 +269,11 @@ class SSViewer implements Flushable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get default value for rewrite hash links for all modules
|
* Get default value for rewrite hash links for all modules
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
public static function getRewriteHashLinksDefault()
|
public static function getRewriteHashLinksDefault(): null|bool|string
|
||||||
{
|
{
|
||||||
// Check if config overridden
|
// Check if config overridden
|
||||||
if (isset(static::$current_rewrite_hash_links)) {
|
if (static::$current_rewrite_hash_links !== null) {
|
||||||
return static::$current_rewrite_hash_links;
|
return static::$current_rewrite_hash_links;
|
||||||
}
|
}
|
||||||
return Config::inst()->get(static::class, 'rewrite_hash_links');
|
return Config::inst()->get(static::class, 'rewrite_hash_links');
|
||||||
@ -376,209 +281,29 @@ class SSViewer implements Flushable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set default rewrite hash links
|
* Set default rewrite hash links
|
||||||
*
|
|
||||||
* @param bool $rewrite
|
|
||||||
*/
|
*/
|
||||||
public static function setRewriteHashLinksDefault($rewrite)
|
public static function setRewriteHashLinksDefault(null|bool|string $rewrite)
|
||||||
{
|
{
|
||||||
static::$current_rewrite_hash_links = $rewrite;
|
static::$current_rewrite_hash_links = $rewrite;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string|array $templates
|
|
||||||
*/
|
|
||||||
public function setTemplate($templates)
|
|
||||||
{
|
|
||||||
$this->templates = $templates;
|
|
||||||
$this->chosen = $this->chooseTemplate($templates);
|
|
||||||
$this->subTemplates = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find the template to use for a given list
|
|
||||||
*
|
|
||||||
* @param array|string $templates
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function chooseTemplate($templates)
|
|
||||||
{
|
|
||||||
return ThemeResourceLoader::inst()->findTemplate($templates, SSViewer::get_themes());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the template parser that will be used in template generation
|
|
||||||
*
|
|
||||||
* @param TemplateParser $parser
|
|
||||||
*/
|
|
||||||
public function setParser(TemplateParser $parser)
|
|
||||||
{
|
|
||||||
$this->parser = $parser;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the parser that is set for template generation
|
|
||||||
*
|
|
||||||
* @return TemplateParser
|
|
||||||
*/
|
|
||||||
public function getParser()
|
|
||||||
{
|
|
||||||
if (!$this->parser) {
|
|
||||||
$this->setParser(Injector::inst()->get('SilverStripe\\View\\SSTemplateParser'));
|
|
||||||
}
|
|
||||||
return $this->parser;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if at least one of the listed templates exists.
|
|
||||||
*
|
|
||||||
* @param array|string $templates
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function hasTemplate($templates)
|
|
||||||
{
|
|
||||||
return (bool)ThemeResourceLoader::inst()->findTemplate($templates, SSViewer::get_themes());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Call this to disable rewriting of <a href="#xxx"> links. This is useful in Ajax applications.
|
* Call this to disable rewriting of <a href="#xxx"> links. This is useful in Ajax applications.
|
||||||
* It returns the SSViewer objects, so that you can call new SSViewer("X")->dontRewriteHashlinks()->process();
|
* It returns the SSViewer objects, so that you can call new SSViewer("X")->dontRewriteHashlinks()->process();
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
*/
|
||||||
public function dontRewriteHashlinks()
|
public function dontRewriteHashlinks(): static
|
||||||
{
|
{
|
||||||
return $this->setRewriteHashLinks(false);
|
return $this->setRewriteHashLinks(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function exists()
|
|
||||||
{
|
|
||||||
return $this->chosen;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $identifier A template name without '.ss' extension or path
|
|
||||||
* @param string $type The template type, either "main", "Includes" or "Layout"
|
|
||||||
* @return string Full system path to a template file
|
|
||||||
*/
|
|
||||||
public static function getTemplateFileByType($identifier, $type = null)
|
|
||||||
{
|
|
||||||
return ThemeResourceLoader::inst()->findTemplate(['type' => $type, $identifier], SSViewer::get_themes());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clears all parsed template files in the cache folder.
|
|
||||||
*
|
|
||||||
* Can only be called once per request (there may be multiple SSViewer instances).
|
|
||||||
*
|
|
||||||
* @param bool $force Set this to true to force a re-flush. If left to false, flushing
|
|
||||||
* may only be performed once a request.
|
|
||||||
*/
|
|
||||||
public static function flush_template_cache($force = false)
|
|
||||||
{
|
|
||||||
if (!SSViewer::$template_cache_flushed || $force) {
|
|
||||||
$dir = dir(TEMP_PATH);
|
|
||||||
while (false !== ($file = $dir->read())) {
|
|
||||||
if (strstr($file ?? '', '.cache')) {
|
|
||||||
unlink(TEMP_PATH . DIRECTORY_SEPARATOR . $file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SSViewer::$template_cache_flushed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clears all partial cache blocks.
|
|
||||||
*
|
|
||||||
* Can only be called once per request (there may be multiple SSViewer instances).
|
|
||||||
*
|
|
||||||
* @param bool $force Set this to true to force a re-flush. If left to false, flushing
|
|
||||||
* may only be performed once a request.
|
|
||||||
*/
|
|
||||||
public static function flush_cacheblock_cache($force = false)
|
|
||||||
{
|
|
||||||
if (!SSViewer::$cacheblock_cache_flushed || $force) {
|
|
||||||
$cache = Injector::inst()->get(CacheInterface::class . '.cacheblock');
|
|
||||||
$cache->clear();
|
|
||||||
|
|
||||||
|
|
||||||
SSViewer::$cacheblock_cache_flushed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the cache object to use when storing / retrieving partial cache blocks.
|
|
||||||
*
|
|
||||||
* @param CacheInterface $cache
|
|
||||||
*/
|
|
||||||
public function setPartialCacheStore($cache)
|
|
||||||
{
|
|
||||||
$this->partialCacheStore = $cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the cache object to use when storing / retrieving partial cache blocks.
|
|
||||||
*
|
|
||||||
* @return CacheInterface
|
|
||||||
*/
|
|
||||||
public function getPartialCacheStore()
|
|
||||||
{
|
|
||||||
if ($this->partialCacheStore) {
|
|
||||||
return $this->partialCacheStore;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Injector::inst()->get(CacheInterface::class . '.cacheblock');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flag whether to include the requirements in this response.
|
* Flag whether to include the requirements in this response.
|
||||||
*
|
|
||||||
* @param bool $incl
|
|
||||||
*/
|
*/
|
||||||
public function includeRequirements($incl = true)
|
public function includeRequirements(bool $incl = true)
|
||||||
{
|
{
|
||||||
$this->includeRequirements = $incl;
|
$this->includeRequirements = $incl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* An internal utility function to set up variables in preparation for including a compiled
|
|
||||||
* template, then do the include
|
|
||||||
*
|
|
||||||
* Effectively this is the common code that both SSViewer#process and SSViewer_FromString#process call
|
|
||||||
*
|
|
||||||
* @param string $cacheFile The path to the file that contains the template compiled to PHP
|
|
||||||
* @param ModelData $item The item to use as the root scope for the template
|
|
||||||
* @param array $overlay Any variables to layer on top of the scope
|
|
||||||
* @param array $underlay Any variables to layer underneath the scope
|
|
||||||
* @param ModelData $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)
|
|
||||||
{
|
|
||||||
if (isset($_GET['showtemplate']) && $_GET['showtemplate'] && Permission::check('ADMIN')) {
|
|
||||||
$lines = file($cacheFile ?? '');
|
|
||||||
echo "<h2>Template: $cacheFile</h2>";
|
|
||||||
echo "<pre>";
|
|
||||||
foreach ($lines as $num => $line) {
|
|
||||||
echo str_pad($num+1, 5) . htmlentities($line, ENT_COMPAT, 'UTF-8');
|
|
||||||
}
|
|
||||||
echo "</pre>";
|
|
||||||
}
|
|
||||||
|
|
||||||
$cache = $this->getPartialCacheStore();
|
|
||||||
$scope = new SSViewer_DataPresenter($item, $overlay, $underlay, $inheritedScope);
|
|
||||||
$val = '';
|
|
||||||
|
|
||||||
// Placeholder for values exposed to $cacheFile
|
|
||||||
[$cache, $scope, $val];
|
|
||||||
include($cacheFile);
|
|
||||||
|
|
||||||
return $val;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The process() method handles the "meat" of the template processing.
|
* The process() method handles the "meat" of the template processing.
|
||||||
*
|
*
|
||||||
@ -590,70 +315,25 @@ class SSViewer implements Flushable
|
|||||||
*
|
*
|
||||||
* Note: You can call this method indirectly by {@link ModelData->renderWith()}.
|
* Note: You can call this method indirectly by {@link ModelData->renderWith()}.
|
||||||
*
|
*
|
||||||
* @param ModelData $item
|
* @param array $overlay Associative array of fields for use in the template.
|
||||||
* @param array|null $arguments Arguments to an included template
|
* These will override properties and methods with the same name from $data and from global
|
||||||
* @param ModelData $inheritedScope The current scope of a parent template including a sub-template
|
* template providers.
|
||||||
* @return DBHTMLText Parsed template output.
|
|
||||||
*/
|
*/
|
||||||
public function process($item, $arguments = null, $inheritedScope = null)
|
public function process(mixed $item, array $overlay = []): DBHTMLText
|
||||||
{
|
{
|
||||||
|
$item = ViewLayerData::create($item);
|
||||||
// Set hashlinks and temporarily modify global state
|
// Set hashlinks and temporarily modify global state
|
||||||
$rewrite = $this->getRewriteHashLinks();
|
$rewrite = $this->getRewriteHashLinks();
|
||||||
$origRewriteDefault = static::getRewriteHashLinksDefault();
|
$origRewriteDefault = static::getRewriteHashLinksDefault();
|
||||||
static::setRewriteHashLinksDefault($rewrite);
|
static::setRewriteHashLinksDefault($rewrite);
|
||||||
|
|
||||||
SSViewer::$topLevel[] = $item;
|
// Actually render the template
|
||||||
|
$output = $this->getTemplateEngine()->render($item, $overlay);
|
||||||
$template = $this->chosen;
|
|
||||||
|
|
||||||
$cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . '.cache'
|
|
||||||
. str_replace(['\\','/',':'], '.', Director::makeRelative(realpath($template ?? '')) ?? '');
|
|
||||||
$lastEdited = filemtime($template ?? '');
|
|
||||||
|
|
||||||
if (!file_exists($cacheFile ?? '') || filemtime($cacheFile ?? '') < $lastEdited) {
|
|
||||||
$content = file_get_contents($template ?? '');
|
|
||||||
$content = $this->parseTemplateContent($content, $template);
|
|
||||||
|
|
||||||
$fh = fopen($cacheFile ?? '', 'w');
|
|
||||||
fwrite($fh, $content ?? '');
|
|
||||||
fclose($fh);
|
|
||||||
}
|
|
||||||
|
|
||||||
$underlay = ['I18NNamespace' => basename($template ?? '')];
|
|
||||||
|
|
||||||
// Makes the rendered sub-templates available on the parent item,
|
|
||||||
// through $Content and $Layout placeholders.
|
|
||||||
foreach (['Content', 'Layout'] as $subtemplate) {
|
|
||||||
// Detect sub-template to use
|
|
||||||
$sub = $this->getSubtemplateFor($subtemplate);
|
|
||||||
if (!$sub) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create lazy-evaluated underlay for this subtemplate
|
|
||||||
$underlay[$subtemplate] = function () use ($item, $arguments, $sub) {
|
|
||||||
$subtemplateViewer = clone $this;
|
|
||||||
// Disable requirements - this will be handled by the parent template
|
|
||||||
$subtemplateViewer->includeRequirements(false);
|
|
||||||
// Select the right template
|
|
||||||
$subtemplateViewer->setTemplate($sub);
|
|
||||||
|
|
||||||
// Render if available
|
|
||||||
if ($subtemplateViewer->exists()) {
|
|
||||||
return $subtemplateViewer->process($item, $arguments);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
$output = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, $underlay, $inheritedScope);
|
|
||||||
|
|
||||||
if ($this->includeRequirements) {
|
if ($this->includeRequirements) {
|
||||||
$output = Requirements::includeInHTML($output);
|
$output = Requirements::includeInHTML($output);
|
||||||
}
|
}
|
||||||
|
|
||||||
array_pop(SSViewer::$topLevel);
|
|
||||||
|
|
||||||
// If we have our crazy base tag, then fix # links referencing the current page.
|
// If we have our crazy base tag, then fix # links referencing the current page.
|
||||||
if ($rewrite) {
|
if ($rewrite) {
|
||||||
if (strpos($output ?? '', '<base') !== false) {
|
if (strpos($output ?? '', '<base') !== false) {
|
||||||
@ -669,6 +349,8 @@ PHP;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrap the HTML in a `DBHTMLText`. We use `HTMLFragment` here because shortcodes should
|
||||||
|
// already have been processed, so this avoids unnecessarily trying to process them again
|
||||||
/** @var DBHTMLText $html */
|
/** @var DBHTMLText $html */
|
||||||
$html = DBField::create_field('HTMLFragment', $output);
|
$html = DBField::create_field('HTMLFragment', $output);
|
||||||
|
|
||||||
@ -677,163 +359,31 @@ PHP;
|
|||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the appropriate template to use for the named sub-template, or null if none are appropriate
|
|
||||||
*
|
|
||||||
* @param string $subtemplate Sub-template to use
|
|
||||||
*
|
|
||||||
* @return array|null
|
|
||||||
*/
|
|
||||||
protected function getSubtemplateFor($subtemplate)
|
|
||||||
{
|
|
||||||
// Get explicit subtemplate name
|
|
||||||
if (isset($this->subTemplates[$subtemplate])) {
|
|
||||||
return $this->subTemplates[$subtemplate];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't apply sub-templates if type is already specified (e.g. 'Includes')
|
|
||||||
if (isset($this->templates['type'])) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter out any other typed templates as we can only add, not change type
|
|
||||||
$templates = array_filter(
|
|
||||||
(array)$this->templates,
|
|
||||||
function ($template) {
|
|
||||||
return !isset($template['type']);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (empty($templates)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set type to subtemplate
|
|
||||||
$templates['type'] = $subtemplate;
|
|
||||||
return $templates;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the given template, passing it the given data.
|
|
||||||
* Used by the <% include %> template tag to process templates.
|
|
||||||
*
|
|
||||||
* @param string $template Template name
|
|
||||||
* @param mixed $data Data context
|
|
||||||
* @param array $arguments Additional arguments
|
|
||||||
* @param Object $scope
|
|
||||||
* @param bool $globalRequirements
|
|
||||||
*
|
|
||||||
* @return string Evaluated result
|
|
||||||
*/
|
|
||||||
public static function execute_template($template, $data, $arguments = null, $scope = null, $globalRequirements = false)
|
|
||||||
{
|
|
||||||
$v = SSViewer::create($template);
|
|
||||||
|
|
||||||
if ($globalRequirements) {
|
|
||||||
$v->includeRequirements(false);
|
|
||||||
} else {
|
|
||||||
//nest a requirements backend for our template rendering
|
|
||||||
$origBackend = Requirements::backend();
|
|
||||||
Requirements::set_backend(Requirements_Backend::create());
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return $v->process($data, $arguments, $scope);
|
|
||||||
} finally {
|
|
||||||
if (!$globalRequirements) {
|
|
||||||
Requirements::set_backend($origBackend);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the evaluated string, passing it the given data.
|
|
||||||
* Used by partial caching to evaluate custom cache keys expressed using
|
|
||||||
* template expressions
|
|
||||||
*
|
|
||||||
* @param string $content Input string
|
|
||||||
* @param mixed $data Data context
|
|
||||||
* @param array $arguments Additional arguments
|
|
||||||
* @param bool $globalRequirements
|
|
||||||
*
|
|
||||||
* @return string Evaluated result
|
|
||||||
*/
|
|
||||||
public static function execute_string($content, $data, $arguments = null, $globalRequirements = false)
|
|
||||||
{
|
|
||||||
$v = SSViewer::fromString($content);
|
|
||||||
|
|
||||||
if ($globalRequirements) {
|
|
||||||
$v->includeRequirements(false);
|
|
||||||
} else {
|
|
||||||
//nest a requirements backend for our template rendering
|
|
||||||
$origBackend = Requirements::backend();
|
|
||||||
Requirements::set_backend(Requirements_Backend::create());
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return $v->process($data, $arguments);
|
|
||||||
} finally {
|
|
||||||
if (!$globalRequirements) {
|
|
||||||
Requirements::set_backend($origBackend);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse given template contents
|
|
||||||
*
|
|
||||||
* @param string $content The template contents
|
|
||||||
* @param string $template The template file name
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function parseTemplateContent($content, $template = "")
|
|
||||||
{
|
|
||||||
return $this->getParser()->compileString(
|
|
||||||
$content,
|
|
||||||
$template,
|
|
||||||
Director::isDev() && SSViewer::config()->uninherited('source_file_comments')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the filenames of the template that will be rendered. It is a map that may contain
|
|
||||||
* 'Content' & 'Layout', and will have to contain 'main'
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function templates()
|
|
||||||
{
|
|
||||||
return array_merge(['main' => $this->chosen], $this->subTemplates);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $type "Layout" or "main"
|
|
||||||
* @param string $file Full system path to the template file
|
|
||||||
*/
|
|
||||||
public function setTemplateFile($type, $file)
|
|
||||||
{
|
|
||||||
if (!$type || $type == 'main') {
|
|
||||||
$this->chosen = $file;
|
|
||||||
} else {
|
|
||||||
$this->subTemplates[$type] = $file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an appropriate base tag for the given template.
|
* Return an appropriate base tag for the given template.
|
||||||
* It will be closed on an XHTML document, and unclosed on an HTML document.
|
* It will be closed on an XHTML document, and unclosed on an HTML document.
|
||||||
*
|
*
|
||||||
* @param string $contentGeneratedSoFar The content of the template generated so far; it should contain
|
* @param bool $isXhtml Whether the DOCTYPE is xhtml or not.
|
||||||
* the DOCTYPE declaration.
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public static function get_base_tag($contentGeneratedSoFar)
|
public static function getBaseTag(bool $isXhtml = false): string
|
||||||
{
|
{
|
||||||
// Base href should always have a trailing slash
|
// Base href should always have a trailing slash
|
||||||
$base = rtrim(Director::absoluteBaseURL(), '/') . '/';
|
$base = rtrim(Director::absoluteBaseURL(), '/') . '/';
|
||||||
|
|
||||||
// Is the document XHTML?
|
if ($isXhtml) {
|
||||||
if (preg_match('/<!DOCTYPE[^>]+xhtml/i', $contentGeneratedSoFar ?? '')) {
|
|
||||||
return "<base href=\"$base\" />";
|
return "<base href=\"$base\" />";
|
||||||
} else {
|
} else {
|
||||||
return "<base href=\"$base\"><!--[if lte IE 6]></base><![endif]-->";
|
return "<base href=\"$base\">";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the engine used to render templates for this viewer.
|
||||||
|
* Note that this is intentionally not public to avoid the engine being set after instantiation.
|
||||||
|
*/
|
||||||
|
protected function setTemplateEngine(TemplateEngine $engine): static
|
||||||
|
{
|
||||||
|
$this->templateEngine = $engine;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,449 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace SilverStripe\View;
|
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use SilverStripe\Core\ClassInfo;
|
|
||||||
use SilverStripe\Model\ModelData;
|
|
||||||
use SilverStripe\Model\List\ArrayList;
|
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This extends SSViewer_Scope to mix 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).
|
|
||||||
*
|
|
||||||
* It's separate from SSViewer_Scope to keep that fairly complex code as clean as possible.
|
|
||||||
*/
|
|
||||||
class SSViewer_DataPresenter extends SSViewer_Scope
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* 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_DataPresenter::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 array $overlay
|
|
||||||
* @var array $underlay
|
|
||||||
* @var SSViewer_Scope $inheritedScope
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
$item,
|
|
||||||
array $overlay = null,
|
|
||||||
array $underlay = null,
|
|
||||||
SSViewer_Scope $inheritedScope = null
|
|
||||||
) {
|
|
||||||
parent::__construct($item, $inheritedScope);
|
|
||||||
|
|
||||||
$this->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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,95 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace SilverStripe\View;
|
|
||||||
|
|
||||||
use SilverStripe\Core\Config\Config;
|
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Special SSViewer that will process a template passed as a string, rather than a filename.
|
|
||||||
*/
|
|
||||||
class SSViewer_FromString extends SSViewer
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The global template caching behaviour if no instance override is specified
|
|
||||||
*
|
|
||||||
* @config
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
private static $cache_template = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The template to use
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $content;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicates whether templates should be cached
|
|
||||||
*
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected $cacheTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $content
|
|
||||||
* @param TemplateParser $parser
|
|
||||||
*/
|
|
||||||
public function __construct($content, TemplateParser $parser = null)
|
|
||||||
{
|
|
||||||
if ($parser) {
|
|
||||||
$this->setParser($parser);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->content = $content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function process($item, $arguments = null, $scope = null)
|
|
||||||
{
|
|
||||||
$hash = sha1($this->content ?? '');
|
|
||||||
$cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . ".cache.$hash";
|
|
||||||
|
|
||||||
if (!file_exists($cacheFile ?? '') || isset($_GET['flush'])) {
|
|
||||||
$content = $this->parseTemplateContent($this->content, "string sha1=$hash");
|
|
||||||
$fh = fopen($cacheFile ?? '', 'w');
|
|
||||||
fwrite($fh, $content ?? '');
|
|
||||||
fclose($fh);
|
|
||||||
}
|
|
||||||
|
|
||||||
$val = $this->includeGeneratedTemplate($cacheFile, $item, $arguments, null, $scope);
|
|
||||||
|
|
||||||
if ($this->cacheTemplate !== null) {
|
|
||||||
$cacheTemplate = $this->cacheTemplate;
|
|
||||||
} else {
|
|
||||||
$cacheTemplate = static::config()->get('cache_template');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$cacheTemplate) {
|
|
||||||
unlink($cacheFile ?? '');
|
|
||||||
}
|
|
||||||
|
|
||||||
$html = DBField::create_field('HTMLFragment', $val);
|
|
||||||
|
|
||||||
return $html;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param boolean $cacheTemplate
|
|
||||||
*/
|
|
||||||
public function setCacheTemplate($cacheTemplate)
|
|
||||||
{
|
|
||||||
$this->cacheTemplate = (bool)$cacheTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function getCacheTemplate()
|
|
||||||
{
|
|
||||||
return $this->cacheTemplate;
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,15 +2,11 @@
|
|||||||
|
|
||||||
namespace SilverStripe\View;
|
namespace SilverStripe\View;
|
||||||
|
|
||||||
use ArrayIterator;
|
use InvalidArgumentException;
|
||||||
use Countable;
|
|
||||||
use Iterator;
|
use Iterator;
|
||||||
use SilverStripe\Model\List\ArrayList;
|
use LogicException;
|
||||||
use SilverStripe\ORM\FieldType\DBBoolean;
|
use SilverStripe\Core\ClassInfo;
|
||||||
use SilverStripe\ORM\FieldType\DBText;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\ORM\FieldType\DBFloat;
|
|
||||||
use SilverStripe\ORM\FieldType\DBInt;
|
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This tracks the current scope for an SSViewer instance. It has three goals:
|
* This tracks the current scope for an SSViewer instance. It has three goals:
|
||||||
@ -18,6 +14,10 @@ use SilverStripe\ORM\FieldType\DBField;
|
|||||||
* - Track Up and Top
|
* - Track Up and Top
|
||||||
* - (As a side effect) Inject data that needs to be available globally (used to live in ModelData)
|
* - (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
|
* 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).
|
* for each step, we use indexes into the itemStack (which already has to exist).
|
||||||
*
|
*
|
||||||
@ -44,100 +44,107 @@ class SSViewer_Scope
|
|||||||
/**
|
/**
|
||||||
* The stack of previous items ("scopes") - an indexed array of: item, item iterator, item iterator total,
|
* The stack of previous items ("scopes") - an indexed array of: item, item iterator, item iterator total,
|
||||||
* pop index, up index, current index & parent overlay
|
* pop index, up index, current index & parent overlay
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
*/
|
||||||
private $itemStack = [];
|
private array $itemStack = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current "global" item (the one any lookup starts from)
|
* The current "global" item (the one any lookup starts from)
|
||||||
*
|
|
||||||
* @var object
|
|
||||||
*/
|
*/
|
||||||
protected $item;
|
protected ?ViewLayerData $item;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If we're looping over the current "global" item, here's the iterator that tracks with item we're up to
|
* If we're looping over the current "global" item, here's the iterator that tracks with item we're up to
|
||||||
*
|
|
||||||
* @var Iterator
|
|
||||||
*/
|
*/
|
||||||
protected $itemIterator;
|
protected ?Iterator $itemIterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Total number of items in the iterator
|
* Total number of items in the iterator
|
||||||
*
|
|
||||||
* @var int
|
|
||||||
*/
|
*/
|
||||||
protected $itemIteratorTotal;
|
protected int $itemIteratorTotal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pointer into the item stack for the item that will become the active scope on the next pop call
|
* A pointer into the item stack for the item that will become the active scope on the next pop call
|
||||||
*
|
|
||||||
* @var int
|
|
||||||
*/
|
*/
|
||||||
private $popIndex;
|
private ?int $popIndex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pointer into the item stack for which item is "up" from this one
|
* A pointer into the item stack for which item is "up" from this one
|
||||||
*
|
|
||||||
* @var int
|
|
||||||
*/
|
*/
|
||||||
private $upIndex;
|
private ?int $upIndex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pointer into the item stack for which the active item (or null if not in stack yet)
|
* A pointer into the item stack for which the active item (or null if not in stack yet)
|
||||||
*
|
|
||||||
* @var int
|
|
||||||
*/
|
*/
|
||||||
private $currentIndex;
|
private int $currentIndex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A store of copies of the main item stack, so it's preserved during a lookup from local scope
|
* A store of copies of the main item stack, so it's preserved during a lookup from local scope
|
||||||
* (which may push/pop items to/from the main item stack)
|
* (which may push/pop items to/from the main item stack)
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
*/
|
||||||
private $localStack = [];
|
private array $localStack = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The index of the current item in the main item stack, so we know where to restore the scope
|
* The index of the current item in the main item stack, so we know where to restore the scope
|
||||||
* stored in $localStack.
|
* stored in $localStack.
|
||||||
*
|
|
||||||
* @var int
|
|
||||||
*/
|
*/
|
||||||
private $localIndex = 0;
|
private int $localIndex = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var object $item
|
* List of global property providers
|
||||||
* @var SSViewer_Scope $inheritedScope
|
*
|
||||||
|
* @internal
|
||||||
|
* @var TemplateGlobalProvider[]|null
|
||||||
*/
|
*/
|
||||||
public function __construct($item, SSViewer_Scope $inheritedScope = 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
|
||||||
|
*/
|
||||||
|
protected array $overlay;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flag for whether overlay should be preserved when pushing a new scope
|
||||||
|
*/
|
||||||
|
protected bool $preserveOverlay = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Underlay variables. Concede precedence to overlay variables or anything from the current scope
|
||||||
|
*/
|
||||||
|
protected array $underlay;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
?ViewLayerData $item,
|
||||||
|
array $overlay = [],
|
||||||
|
array $underlay = [],
|
||||||
|
?SSViewer_Scope $inheritedScope = null
|
||||||
|
) {
|
||||||
$this->item = $item;
|
$this->item = $item;
|
||||||
|
|
||||||
$this->itemIterator = ($inheritedScope) ? $inheritedScope->itemIterator : null;
|
$this->itemIterator = ($inheritedScope) ? $inheritedScope->itemIterator : null;
|
||||||
$this->itemIteratorTotal = ($inheritedScope) ? $inheritedScope->itemIteratorTotal : 0;
|
$this->itemIteratorTotal = ($inheritedScope) ? $inheritedScope->itemIteratorTotal : 0;
|
||||||
$this->itemStack[] = [$this->item, $this->itemIterator, $this->itemIteratorTotal, null, null, 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
|
* Returns the current "current" item in scope
|
||||||
*
|
|
||||||
* @return object
|
|
||||||
*/
|
*/
|
||||||
public function getItem()
|
public function getCurrentItem(): ?ViewLayerData
|
||||||
{
|
{
|
||||||
$item = $this->itemIterator ? $this->itemIterator->current() : $this->item;
|
return $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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -164,56 +171,21 @@ class SSViewer_Scope
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the local scope - restores saved state to the "global" item stack. Typically called after
|
* Set scope to an intermediate value, which will be used for getting output later on.
|
||||||
* a lookup chain has been completed
|
|
||||||
*/
|
*/
|
||||||
public function resetLocalScope()
|
public function scopeToIntermediateValue(string $name, array $arguments, string $type): static
|
||||||
{
|
{
|
||||||
// Restore previous un-completed lookup chain if set
|
$overlayIndex = false;
|
||||||
$previousLocalState = $this->localStack ? array_pop($this->localStack) : null;
|
|
||||||
array_splice($this->itemStack, $this->localIndex + 1, count($this->itemStack ?? []), $previousLocalState);
|
|
||||||
|
|
||||||
list(
|
// $Up and $Top need to restore the overlay from the parent and top-level scope respectively.
|
||||||
$this->item,
|
|
||||||
$this->itemIterator,
|
|
||||||
$this->itemIteratorTotal,
|
|
||||||
$this->popIndex,
|
|
||||||
$this->upIndex,
|
|
||||||
$this->currentIndex
|
|
||||||
) = end($this->itemStack);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param array $arguments
|
|
||||||
* @param bool $cache
|
|
||||||
* @param string $cacheName
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getObj($name, $arguments = [], $cache = false, $cacheName = null)
|
|
||||||
{
|
|
||||||
$on = $this->getItem();
|
|
||||||
if ($on === null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return $on->obj($name, $arguments, $cache, $cacheName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param array $arguments
|
|
||||||
* @param bool $cache
|
|
||||||
* @param string $cacheName
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function obj($name, $arguments = [], $cache = false, $cacheName = null)
|
|
||||||
{
|
|
||||||
switch ($name) {
|
switch ($name) {
|
||||||
case 'Up':
|
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');
|
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(
|
list(
|
||||||
$this->item,
|
$this->item,
|
||||||
$this->itemIterator,
|
$this->itemIterator,
|
||||||
@ -224,6 +196,8 @@ class SSViewer_Scope
|
|||||||
) = $this->itemStack[$this->upIndex];
|
) = $this->itemStack[$this->upIndex];
|
||||||
break;
|
break;
|
||||||
case 'Top':
|
case 'Top':
|
||||||
|
$overlayIndex = 0; // Top-level scope
|
||||||
|
$this->preserveOverlay = true; // Preserve overlay
|
||||||
list(
|
list(
|
||||||
$this->item,
|
$this->item,
|
||||||
$this->itemIterator,
|
$this->itemIterator,
|
||||||
@ -234,13 +208,21 @@ class SSViewer_Scope
|
|||||||
) = $this->itemStack[0];
|
) = $this->itemStack[0];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$this->item = $this->getObj($name, $arguments, $cache, $cacheName);
|
$this->preserveOverlay = false;
|
||||||
|
$this->item = $this->getObj($name, $arguments, $type);
|
||||||
$this->itemIterator = null;
|
$this->itemIterator = null;
|
||||||
$this->upIndex = $this->currentIndex ? $this->currentIndex : count($this->itemStack) - 1;
|
$this->upIndex = $this->currentIndex ? $this->currentIndex : count($this->itemStack) - 1;
|
||||||
$this->currentIndex = count($this->itemStack);
|
$this->currentIndex = count($this->itemStack);
|
||||||
break;
|
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->itemStack[] = [
|
||||||
$this->item,
|
$this->item,
|
||||||
$this->itemIterator,
|
$this->itemIterator,
|
||||||
@ -254,12 +236,10 @@ class SSViewer_Scope
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current object and resets the scope.
|
* Gets the current object and resets the scope.
|
||||||
*
|
|
||||||
* @return object
|
|
||||||
*/
|
*/
|
||||||
public function self()
|
public function self(): ?ViewLayerData
|
||||||
{
|
{
|
||||||
$result = $this->getItem();
|
$result = $this->getCurrentItem();
|
||||||
$this->resetLocalScope();
|
$this->resetLocalScope();
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
@ -268,9 +248,13 @@ class SSViewer_Scope
|
|||||||
/**
|
/**
|
||||||
* Jump to the last item in the stack, called when a new item is added before a loop/with
|
* 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;
|
$newLocalIndex = count($this->itemStack ?? []) - 1;
|
||||||
|
|
||||||
@ -284,16 +268,38 @@ class SSViewer_Scope
|
|||||||
// once we enter a new global scope, we need to make sure we use a new one
|
// 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;
|
$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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Jump back to "previous" item in the stack, called after a loop/with block
|
* 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->localIndex = $this->popIndex;
|
||||||
$this->resetLocalScope();
|
$this->resetLocalScope();
|
||||||
|
|
||||||
@ -301,11 +307,10 @@ class SSViewer_Scope
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fast-forwards the current iterator to the next item
|
* Fast-forwards the current iterator to the next item.
|
||||||
*
|
* @return bool True if there's an item, false if not.
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public function next()
|
public function next(): bool
|
||||||
{
|
{
|
||||||
if (!$this->item) {
|
if (!$this->item) {
|
||||||
return false;
|
return false;
|
||||||
@ -313,29 +318,18 @@ class SSViewer_Scope
|
|||||||
|
|
||||||
if (!$this->itemIterator) {
|
if (!$this->itemIterator) {
|
||||||
// Note: it is important that getIterator() is called before count() as implemenations may rely on
|
// Note: it is important that getIterator() is called before count() as implemenations may rely on
|
||||||
// this to efficiency get both the number of records and an iterator (e.g. DataList does this)
|
// this to efficiently get both the number of records and an iterator (e.g. DataList does this)
|
||||||
|
|
||||||
// Item may be an array or a regular IteratorAggregate
|
|
||||||
if (is_array($this->item)) {
|
|
||||||
$this->itemIterator = new ArrayIterator($this->item);
|
|
||||||
} elseif ($this->item instanceof Iterator) {
|
|
||||||
$this->itemIterator = $this->item;
|
|
||||||
} else {
|
|
||||||
$this->itemIterator = $this->item->getIterator();
|
$this->itemIterator = $this->item->getIterator();
|
||||||
|
|
||||||
// This will execute code in a generator up to the first yield. For example, this ensures that
|
// This will execute code in a generator up to the first yield. For example, this ensures that
|
||||||
// DataList::getIterator() is called before Datalist::count()
|
// DataList::getIterator() is called before Datalist::count() which means we only run the query once
|
||||||
|
// instead of running a separate explicit count() query
|
||||||
$this->itemIterator->rewind();
|
$this->itemIterator->rewind();
|
||||||
}
|
|
||||||
|
|
||||||
// If the item implements Countable, use that to fetch the count, otherwise we have to inspect the
|
// Get the number of items in the iterator.
|
||||||
// iterator and then rewind it.
|
// Don't just use iterator_count because that results in running through the list
|
||||||
if ($this->item instanceof Countable) {
|
// which causes some iterators to no longer be iterable for some reason
|
||||||
$this->itemIteratorTotal = count($this->item);
|
$this->itemIteratorTotal = $this->item->getIteratorCount();
|
||||||
} else {
|
|
||||||
$this->itemIteratorTotal = iterator_count($this->itemIterator);
|
|
||||||
$this->itemIterator->rewind();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR] = $this->itemIterator;
|
$this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR] = $this->itemIterator;
|
||||||
$this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR_TOTAL] = $this->itemIteratorTotal;
|
$this->itemStack[$this->localIndex][SSViewer_Scope::ITEM_ITERATOR_TOTAL] = $this->itemIteratorTotal;
|
||||||
@ -349,23 +343,90 @@ class SSViewer_Scope
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->itemIterator->key();
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* Get the value that will be directly rendered in the template.
|
||||||
* @param array $arguments
|
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public function __call($name, $arguments)
|
public function getOutputValue(string $name, array $arguments, string $type): string
|
||||||
{
|
{
|
||||||
$on = $this->getItem();
|
$retval = $this->getObj($name, $arguments, $type);
|
||||||
$retval = $on ? $on->$name(...$arguments) : null;
|
$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, $arguments, $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($retval === null) {
|
||||||
|
$retval = $this->getUnderlay($name, $arguments, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->resetLocalScope();
|
$this->resetLocalScope();
|
||||||
return $retval;
|
return $retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the current item in scope has a value for the named field.
|
||||||
|
*/
|
||||||
|
public function hasValue(string $name, array $arguments, string $type): bool
|
||||||
|
{
|
||||||
|
$retval = null;
|
||||||
|
$overlay = $this->getOverlay($name, $arguments);
|
||||||
|
if ($overlay && $overlay->hasDataValue()) {
|
||||||
|
$retval = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($retval === null) {
|
||||||
|
$on = $this->getCurrentItem();
|
||||||
|
if ($on) {
|
||||||
|
$retval = $on->hasDataValue($name, $arguments, $type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$retval) {
|
||||||
|
$underlay = $this->getUnderlay($name, $arguments);
|
||||||
|
$retval = $underlay && $underlay->hasDataValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->resetLocalScope();
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the local scope - restores saved state to the "global" item stack. Typically called after
|
||||||
|
* a lookup chain has been completed
|
||||||
|
*/
|
||||||
|
protected function resetLocalScope()
|
||||||
|
{
|
||||||
|
// Restore previous un-completed lookup chain if set
|
||||||
|
$previousLocalState = $this->localStack ? array_pop($this->localStack) : null;
|
||||||
|
array_splice($this->itemStack, $this->localIndex + 1, count($this->itemStack ?? []), $previousLocalState);
|
||||||
|
|
||||||
|
list(
|
||||||
|
$this->item,
|
||||||
|
$this->itemIterator,
|
||||||
|
$this->itemIteratorTotal,
|
||||||
|
$this->popIndex,
|
||||||
|
$this->upIndex,
|
||||||
|
$this->currentIndex
|
||||||
|
) = end($this->itemStack);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@ -390,13 +451,174 @@ class SSViewer_Scope
|
|||||||
return $this->upIndex;
|
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)) {
|
if (!array_key_exists($property, $overrides)) {
|
||||||
'boolean' => DBBoolean::create()->setValue($value),
|
return [];
|
||||||
'string' => DBText::create()->setValue($value),
|
}
|
||||||
'double' => DBFloat::create()->setValue($value),
|
|
||||||
'integer' => DBInt::create()->setValue($value),
|
// Detect override type
|
||||||
};
|
$override = $overrides[$property];
|
||||||
|
|
||||||
|
// Late-evaluate this value
|
||||||
|
if (!is_string($override) && is_callable($override)) {
|
||||||
|
$override = $override();
|
||||||
|
|
||||||
|
// Late override may yet return null
|
||||||
|
if (!isset($override)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ['value' => $override];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build cache of global properties
|
||||||
|
*/
|
||||||
|
protected function cacheGlobalProperties()
|
||||||
|
{
|
||||||
|
if (SSViewer_Scope::$globalProperties !== null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SSViewer_Scope::$globalProperties = SSViewer::getMethodsFromProvider(
|
||||||
|
TemplateGlobalProvider::class,
|
||||||
|
'get_template_global_variables'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build cache of global iterator properties
|
||||||
|
*/
|
||||||
|
protected function cacheIteratorProperties()
|
||||||
|
{
|
||||||
|
if (SSViewer_Scope::$iteratorProperties !== null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SSViewer_Scope::$iteratorProperties = SSViewer::getMethodsFromProvider(
|
||||||
|
TemplateIteratorProvider::class,
|
||||||
|
'get_template_iterator_variables',
|
||||||
|
true // Call non-statically
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getObj(string $name, array $arguments, string $type): ?ViewLayerData
|
||||||
|
{
|
||||||
|
if ($this->hasOverlay($name)) {
|
||||||
|
return $this->getOverlay($name, $arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
61
src/View/TemplateEngine.php
Normal file
61
src/View/TemplateEngine.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View;
|
||||||
|
|
||||||
|
use SilverStripe\View\Exception\MissingTemplateException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for template rendering engines such as twig or ss templates.
|
||||||
|
*/
|
||||||
|
interface TemplateEngine
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Instantiate a TemplateEngine
|
||||||
|
*
|
||||||
|
* @param string|array $templateCandidates A template or pool of candidate templates to choose from.
|
||||||
|
* The template engine will check the currently set themes from SSViewer for template files it can handle
|
||||||
|
* from the candidates.
|
||||||
|
*/
|
||||||
|
public function __construct(string|array $templateCandidates = []);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the template which will be used in the call to render()
|
||||||
|
*
|
||||||
|
* @param string|array $templateCandidates A template or pool of candidate templates to choose from.
|
||||||
|
* The template engine will check the currently set themes from SSViewer for template files it can handle
|
||||||
|
* from the candidates.
|
||||||
|
*/
|
||||||
|
public function setTemplate(string|array $templateCandidates): static;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if there is a template amongst the template candidates that this rendering engine can use.
|
||||||
|
*/
|
||||||
|
public function hasTemplate(string|array $templateCandidates): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the template string.
|
||||||
|
*
|
||||||
|
* Doesn't include normalisation such as inserting js/css from Requirements API - that's handled by SSViewer.
|
||||||
|
*
|
||||||
|
* @param ViewLayerData $model The model to get data from when rendering the template.
|
||||||
|
* @param array $overlay Associative array of fields (e.g. args into an include template) to inject into the
|
||||||
|
* template as properties. These override properties and methods with the same name from $data and from global
|
||||||
|
* template providers.
|
||||||
|
*/
|
||||||
|
public function renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the template which was selected during instantiation or which was set via setTemplate().
|
||||||
|
*
|
||||||
|
* Doesn't include normalisation such as inserting js/css from Requirements API - that's handled by SSViewer.
|
||||||
|
*
|
||||||
|
* @param ViewLayerData $model The model to get data from when rendering the template.
|
||||||
|
* @param array $overlay Associative array of fields (e.g. args into an include template) to inject into the
|
||||||
|
* template as properties. These override properties and methods with the same name from $data and from global
|
||||||
|
* template providers.
|
||||||
|
*
|
||||||
|
* @throws MissingTemplateException if no template file has been set, or there was no valid template file found from the
|
||||||
|
* template candidates
|
||||||
|
*/
|
||||||
|
public function render(ViewLayerData $model, array $overlay = []): string;
|
||||||
|
}
|
@ -16,5 +16,5 @@ interface TemplateParser
|
|||||||
* @param bool $includeDebuggingComments True is debugging comments should be included in the output
|
* @param bool $includeDebuggingComments True is debugging comments should be included in the output
|
||||||
* @return string The php that, when executed (via include or exec) will behave as per the template source
|
* @return string The php that, when executed (via include or exec) will behave as per the template source
|
||||||
*/
|
*/
|
||||||
public function compileString($string, $templateName = "", $includeDebuggingComments = false);
|
public function compileString(string $string, string $templateName = "", bool $includeDebuggingComments = false): string;
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,14 @@ class ThemeResourceLoader implements Flushable, TemplateGlobalProvider
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the base path of the application according to the resource loader
|
||||||
|
*/
|
||||||
|
public function getBase(): string
|
||||||
|
{
|
||||||
|
return $this->base;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a theme identifier, determine the path from the root directory
|
* Given a theme identifier, determine the path from the root directory
|
||||||
*
|
*
|
||||||
@ -161,99 +169,6 @@ class ThemeResourceLoader implements Flushable, TemplateGlobalProvider
|
|||||||
return Path::normalise($modulePath . $subpath, true);
|
return Path::normalise($modulePath . $subpath, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Attempts to find possible candidate templates from a set of template
|
|
||||||
* names from modules, current theme directory and finally the application
|
|
||||||
* folder.
|
|
||||||
*
|
|
||||||
* The template names can be passed in as plain strings, or be in the
|
|
||||||
* format "type/name", where type is the type of template to search for
|
|
||||||
* (e.g. Includes, Layout).
|
|
||||||
*
|
|
||||||
* The results of this method will be cached for future use.
|
|
||||||
*
|
|
||||||
* @param string|array $template Template name, or template spec in array format with the keys
|
|
||||||
* 'type' (type string) and 'templates' (template hierarchy in order of precedence).
|
|
||||||
* If 'templates' is omitted then any other item in the array will be treated as the template
|
|
||||||
* list, or list of templates each in the array spec given.
|
|
||||||
* Templates with an .ss extension will be treated as file paths, and will bypass
|
|
||||||
* theme-coupled resolution.
|
|
||||||
* @param array $themes List of themes to use to resolve themes. Defaults to {@see SSViewer::get_themes()}
|
|
||||||
* @return string Absolute path to resolved template file, or null if not resolved.
|
|
||||||
* File location will be in the format themes/<theme>/templates/<directories>/<type>/<basename>.ss
|
|
||||||
* Note that type (e.g. 'Layout') is not the root level directory under 'templates'.
|
|
||||||
*/
|
|
||||||
public function findTemplate($template, $themes = null)
|
|
||||||
{
|
|
||||||
if ($themes === null) {
|
|
||||||
$themes = SSViewer::get_themes();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look for a cached result for this data set
|
|
||||||
$cacheKey = md5(json_encode($template) . json_encode($themes));
|
|
||||||
if ($this->getCache()->has($cacheKey)) {
|
|
||||||
return $this->getCache()->get($cacheKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
$type = '';
|
|
||||||
if (is_array($template)) {
|
|
||||||
// Check if templates has type specified
|
|
||||||
if (array_key_exists('type', $template ?? [])) {
|
|
||||||
$type = $template['type'];
|
|
||||||
unset($template['type']);
|
|
||||||
}
|
|
||||||
// Templates are either nested in 'templates' or just the rest of the list
|
|
||||||
$templateList = array_key_exists('templates', $template ?? []) ? $template['templates'] : $template;
|
|
||||||
} else {
|
|
||||||
$templateList = [$template];
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($templateList as $i => $template) {
|
|
||||||
// Check if passed list of templates in array format
|
|
||||||
if (is_array($template)) {
|
|
||||||
$path = $this->findTemplate($template, $themes);
|
|
||||||
if ($path) {
|
|
||||||
$this->getCache()->set($cacheKey, $path);
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have an .ss extension, this is a path, not a template name. We should
|
|
||||||
// pass in templates without extensions in order for template manifest to find
|
|
||||||
// files dynamically.
|
|
||||||
if (substr($template ?? '', -3) == '.ss' && file_exists($template ?? '')) {
|
|
||||||
$this->getCache()->set($cacheKey, $template);
|
|
||||||
return $template;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check string template identifier
|
|
||||||
$template = str_replace('\\', '/', $template ?? '');
|
|
||||||
$parts = explode('/', $template ?? '');
|
|
||||||
|
|
||||||
$tail = array_pop($parts);
|
|
||||||
$head = implode('/', $parts);
|
|
||||||
$themePaths = $this->getThemePaths($themes);
|
|
||||||
foreach ($themePaths as $themePath) {
|
|
||||||
// Join path
|
|
||||||
$pathParts = [ $this->base, $themePath, 'templates', $head, $type, $tail ];
|
|
||||||
try {
|
|
||||||
$path = Path::join($pathParts) . '.ss';
|
|
||||||
if (file_exists($path ?? '')) {
|
|
||||||
$this->getCache()->set($cacheKey, $path);
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
// No-op
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// No template found
|
|
||||||
$this->getCache()->set($cacheKey, null);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve themed CSS path
|
* Resolve themed CSS path
|
||||||
*
|
*
|
||||||
|
254
src/View/ViewLayerData.php
Normal file
254
src/View/ViewLayerData.php
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View;
|
||||||
|
|
||||||
|
use BadMethodCallException;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use IteratorAggregate;
|
||||||
|
use SilverStripe\Core\ClassInfo;
|
||||||
|
use SilverStripe\Core\Injector\Injectable;
|
||||||
|
use SilverStripe\Model\ModelData;
|
||||||
|
use SilverStripe\Model\ModelDataCustomised;
|
||||||
|
use SilverStripe\ORM\FieldType\DBClassName;
|
||||||
|
use Stringable;
|
||||||
|
use Traversable;
|
||||||
|
|
||||||
|
class ViewLayerData implements IteratorAggregate, Stringable
|
||||||
|
{
|
||||||
|
use Injectable;
|
||||||
|
|
||||||
|
public const TYPE_PROPERTY = 'property';
|
||||||
|
|
||||||
|
public const TYPE_METHOD = 'method';
|
||||||
|
|
||||||
|
public const TYPE_ANY = 'any';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Special variable names that can be used to get metadata about values
|
||||||
|
*/
|
||||||
|
public const META_DATA_NAMES = [
|
||||||
|
// Gets a DBClassName with the class name of $this->data
|
||||||
|
'ClassName',
|
||||||
|
// Returns $this->data
|
||||||
|
'Me',
|
||||||
|
];
|
||||||
|
|
||||||
|
private object $data;
|
||||||
|
|
||||||
|
public function __construct(mixed $data, mixed $source = null, string $name = '')
|
||||||
|
{
|
||||||
|
if ($data === null) {
|
||||||
|
throw new InvalidArgumentException('$data must not be null');
|
||||||
|
}
|
||||||
|
if ($data instanceof ViewLayerData) {
|
||||||
|
$data = $data->data;
|
||||||
|
} else {
|
||||||
|
$source = $source instanceof ModelData ? $source : null;
|
||||||
|
$data = CastingService::singleton()->cast($data, $source, $name);
|
||||||
|
}
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Needed so we can rewind in SSViewer_Scope::next() after getting itemIteratorTotal without throwing an exception.
|
||||||
|
*/
|
||||||
|
public function getIteratorCount(): int
|
||||||
|
{
|
||||||
|
$count = $this->getRawDataValue('count');
|
||||||
|
if (is_numeric($count)) {
|
||||||
|
return $count;
|
||||||
|
}
|
||||||
|
if (is_countable($this->data)) {
|
||||||
|
return count($this->data);
|
||||||
|
}
|
||||||
|
if (ClassInfo::hasMethod($this->data, 'getIterator')) {
|
||||||
|
return iterator_count($this->data->getIterator());
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getIterator(): Traversable
|
||||||
|
{
|
||||||
|
if (!is_iterable($this->data) && !ClassInfo::hasMethod($this->data, 'getIterator')) {
|
||||||
|
$type = get_class($this->data);
|
||||||
|
throw new BadMethodCallException("$type is not iterable.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a field is set, or if a getter or a method of that name exists.
|
||||||
|
* We need to check each of these, because we don't currently distinguish between a property, a getter, and a method
|
||||||
|
* which means if any of those exists we have to say the field is "set", otherwise template engines may skip fetching the data.
|
||||||
|
*/
|
||||||
|
public function __isset(string $name): bool
|
||||||
|
{
|
||||||
|
// Note we explicitly DO NOT call count() or exists() on the data here because that would
|
||||||
|
// require fetching the data prematurely which could cause performance issues in extreme cases
|
||||||
|
return in_array($name, ViewLayerData::META_DATA_NAMES)
|
||||||
|
|| isset($this->data->$name)
|
||||||
|
|| ClassInfo::hasMethod($this->data, "get$name")
|
||||||
|
|| ClassInfo::hasMethod($this->data, $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __get(string $name): ?ViewLayerData
|
||||||
|
{
|
||||||
|
$value = $this->getRawDataValue($name, type: ViewLayerData::TYPE_PROPERTY);
|
||||||
|
if ($value === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$source = $this->data instanceof ModelData ? $this->data : null;
|
||||||
|
return ViewLayerData::create($value, $source, $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __call(string $name, array $arguments = []): ?ViewLayerData
|
||||||
|
{
|
||||||
|
$value = $this->getRawDataValue($name, $arguments, ViewLayerData::TYPE_METHOD);
|
||||||
|
if ($value === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$source = $this->data instanceof ModelData ? $this->data : null;
|
||||||
|
return ViewLayerData::create($value, $source, $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
if (ClassInfo::hasMethod($this->data, 'forTemplate')) {
|
||||||
|
return $this->data->forTemplate();
|
||||||
|
}
|
||||||
|
return (string) $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if there is a truthy value or (for ModelData) if the data exists().
|
||||||
|
*/
|
||||||
|
public function hasDataValue(?string $name = null, array $arguments = [], string $type = ViewLayerData::TYPE_ANY): bool
|
||||||
|
{
|
||||||
|
if ($name) {
|
||||||
|
// Ask the model if it has a value for that field
|
||||||
|
if ($this->data instanceof ModelData) {
|
||||||
|
return $this->data->hasValue($name, $arguments);
|
||||||
|
}
|
||||||
|
// Check for ourselves if there's a value for that field
|
||||||
|
// This mimics what ModelData does, which provides consistency
|
||||||
|
$value = $this->getRawDataValue($name, $arguments, $type);
|
||||||
|
if ($value === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$source = $this->data instanceof ModelData ? $this->data : null;
|
||||||
|
return ViewLayerData::create($value, $source, $name)->hasDataValue();
|
||||||
|
}
|
||||||
|
// Ask the model if it "exists"
|
||||||
|
if ($this->data instanceof ModelData) {
|
||||||
|
return $this->data->exists();
|
||||||
|
}
|
||||||
|
// Mimics ModelData checks on lists
|
||||||
|
if (is_countable($this->data)) {
|
||||||
|
return count($this->data) > 0;
|
||||||
|
}
|
||||||
|
// Check for truthiness (which is effectively `return true` since data is an object)
|
||||||
|
// We do this to mimic ModelData->hasValue() for consistency
|
||||||
|
return (bool) $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the raw value of some field/property/method on the data, without wrapping it in ViewLayerData.
|
||||||
|
*/
|
||||||
|
public function getRawDataValue(string $name, array $arguments = [], string $type = ViewLayerData::TYPE_ANY): mixed
|
||||||
|
{
|
||||||
|
if ($type !== ViewLayerData::TYPE_ANY && $type !== ViewLayerData::TYPE_METHOD && $type !== ViewLayerData::TYPE_PROPERTY) {
|
||||||
|
throw new InvalidArgumentException('$type must be one of the TYPE_* constant values');
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->data;
|
||||||
|
if ($data instanceof ModelDataCustomised && $data->customisedHas($name)) {
|
||||||
|
$data = $data->getCustomisedModelData();
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't currently use the $type, but could in a future enhancement if we find that distinction useful.
|
||||||
|
$value = $this->getValueFromData($data, $name, $arguments);
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getValueFromData(object $data, string $name, array $arguments): mixed
|
||||||
|
{
|
||||||
|
// Values from ModelData can be cached
|
||||||
|
if ($data instanceof ModelData) {
|
||||||
|
$cached = $data->objCacheGet($name, $arguments);
|
||||||
|
if ($cached !== null) {
|
||||||
|
return $cached;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = null;
|
||||||
|
// Keep track of whether we've already fetched a value (allowing null to be the correct value)
|
||||||
|
$fetchedValue = false;
|
||||||
|
|
||||||
|
// Try calling a method even if we're fetching as a property
|
||||||
|
// This matches historical behaviour that a LOT of logic in core modules expects
|
||||||
|
$value = $this->callDataMethod($data, $name, $arguments, $fetchedValue);
|
||||||
|
|
||||||
|
// Try to get a property even if we aren't explicitly trying to call a method, if the method didn't exist.
|
||||||
|
// This matches historical behaviour and allows e.g. `$MyProperty(some-arg)` with a `getMyProperty($arg)` method.
|
||||||
|
if (!$fetchedValue) {
|
||||||
|
// Try an explicit getter
|
||||||
|
// This matches the "magic" getter behaviour of ModelData across the board for consistent results
|
||||||
|
$getter = "get{$name}";
|
||||||
|
$value = $this->callDataMethod($data, $getter, $arguments, $fetchedValue);
|
||||||
|
if (!$fetchedValue && isset($data->$name)) {
|
||||||
|
$value = $data->$name;
|
||||||
|
$fetchedValue = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Caching for modeldata
|
||||||
|
if ($data instanceof ModelData) {
|
||||||
|
$data->objCacheSet($name, $arguments, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($value === null && in_array($name, ViewLayerData::META_DATA_NAMES)) {
|
||||||
|
$value = $this->getMetaData($data, $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getMetaData(object $data, string $name): mixed
|
||||||
|
{
|
||||||
|
return match ($name) {
|
||||||
|
'Me' => $data,
|
||||||
|
'ClassName' => DBClassName::create()->setValue(get_class($data)),
|
||||||
|
default => null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private function callDataMethod(object $data, string $name, array $arguments, bool &$fetchedValue = false): mixed
|
||||||
|
{
|
||||||
|
$hasDynamicMethods = method_exists($data, '__call');
|
||||||
|
$hasMethod = ClassInfo::hasMethod($data, $name);
|
||||||
|
if ($hasMethod || $hasDynamicMethods) {
|
||||||
|
try {
|
||||||
|
$value = $data->$name(...$arguments);
|
||||||
|
$fetchedValue = true;
|
||||||
|
return $value;
|
||||||
|
} catch (BadMethodCallException $e) {
|
||||||
|
// Only throw the exception if we weren't relying on __call
|
||||||
|
// It's common for __call to throw BadMethodCallException for methods that aren't "implemented"
|
||||||
|
// so we just want to return null in those cases.
|
||||||
|
if (!$hasDynamicMethods) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace SilverStripe\i18n\Messages\Symfony;
|
namespace SilverStripe\i18n\Messages\Symfony;
|
||||||
|
|
||||||
use SilverStripe\Core\Flushable;
|
use SilverStripe\Core\Flushable;
|
||||||
|
use Stringable;
|
||||||
use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
|
use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,10 +13,9 @@ use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
|
|||||||
* @link https://media.giphy.com/media/fRRD3T37DeY6Y/giphy.gif for use case
|
* @link https://media.giphy.com/media/fRRD3T37DeY6Y/giphy.gif for use case
|
||||||
* @see DirectoryResource
|
* @see DirectoryResource
|
||||||
*/
|
*/
|
||||||
class FlushInvalidatedResource implements SelfCheckingResourceInterface, Flushable
|
class FlushInvalidatedResource implements SelfCheckingResourceInterface, Flushable, Stringable
|
||||||
{
|
{
|
||||||
|
public function __toString(): string
|
||||||
public function __toString()
|
|
||||||
{
|
{
|
||||||
return md5(__CLASS__);
|
return md5(__CLASS__);
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ use SilverStripe\Dev\FunctionalTest;
|
|||||||
use SilverStripe\Security\Member;
|
use SilverStripe\Security\Member;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
use PHPUnit\Framework\Attributes\DataProvider;
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use SilverStripe\Control\Tests\ControllerTest\ControllerWithDummyEngine;
|
||||||
|
use SilverStripe\Control\Tests\ControllerTest\DummyTemplateEngine;
|
||||||
|
|
||||||
class ControllerTest extends FunctionalTest
|
class ControllerTest extends FunctionalTest
|
||||||
{
|
{
|
||||||
@ -858,4 +860,12 @@ class ControllerTest extends FunctionalTest
|
|||||||
$response = $this->post('HTTPMethodTestController', ['dummy' => 'example']);
|
$response = $this->post('HTTPMethodTestController', ['dummy' => 'example']);
|
||||||
$this->assertEquals('Routed to postLegacyRoot', $response->getBody());
|
$this->assertEquals('Routed to postLegacyRoot', $response->getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTemplateEngineUsed()
|
||||||
|
{
|
||||||
|
$controller = new ControllerWithDummyEngine();
|
||||||
|
$this->assertSame('This is my controller', $controller->render()->getValue());
|
||||||
|
$this->assertSame('This is my controller', $controller->renderWith('literally-any-template')->getValue());
|
||||||
|
$this->assertInstanceOf(DummyTemplateEngine::class, $controller->getViewer('')->getTemplateEngine());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Control\Tests\ControllerTest;
|
||||||
|
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\View\TemplateEngine;
|
||||||
|
|
||||||
|
class ControllerWithDummyEngine extends Controller implements TestOnly
|
||||||
|
{
|
||||||
|
protected function getTemplateEngine(): TemplateEngine
|
||||||
|
{
|
||||||
|
return new DummyTemplateEngine();
|
||||||
|
}
|
||||||
|
}
|
40
tests/php/Control/ControllerTest/DummyTemplateEngine.php
Normal file
40
tests/php/Control/ControllerTest/DummyTemplateEngine.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Control\Tests\ControllerTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\View\TemplateEngine;
|
||||||
|
use SilverStripe\View\ViewLayerData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A dummy template renderer that doesn't actually render any templates.
|
||||||
|
*/
|
||||||
|
class DummyTemplateEngine implements TemplateEngine, TestOnly
|
||||||
|
{
|
||||||
|
private string $output = 'This is my controller';
|
||||||
|
|
||||||
|
public function __construct(string|array $templateCandidates = [])
|
||||||
|
{
|
||||||
|
// no-op
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTemplate(string|array $templateCandidates): static
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasTemplate(string|array $templateCandidates): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true): string
|
||||||
|
{
|
||||||
|
return $this->output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(ViewLayerData $model, array $overlay = []): string
|
||||||
|
{
|
||||||
|
return $this->output;
|
||||||
|
}
|
||||||
|
}
|
@ -416,27 +416,14 @@ class EmailTest extends SapphireTest
|
|||||||
|
|
||||||
public function testHTMLTemplate(): void
|
public function testHTMLTemplate(): void
|
||||||
{
|
{
|
||||||
// Find template on disk
|
|
||||||
$emailTemplate = ModuleResourceLoader::singleton()->resolveResource(
|
|
||||||
'silverstripe/framework:templates/SilverStripe/Control/Email/Email.ss'
|
|
||||||
);
|
|
||||||
$subClassTemplate = ModuleResourceLoader::singleton()->resolveResource(
|
|
||||||
'silverstripe/framework:tests/php/Control/Email/EmailTest/templates/'
|
|
||||||
. str_replace('\\', '/', EmailSubClass::class)
|
|
||||||
. '.ss'
|
|
||||||
);
|
|
||||||
$this->assertTrue($emailTemplate->exists());
|
|
||||||
$this->assertTrue($subClassTemplate->exists());
|
|
||||||
|
|
||||||
// Check template is auto-found
|
|
||||||
$email = new Email();
|
$email = new Email();
|
||||||
$this->assertEquals($emailTemplate->getPath(), $email->getHTMLTemplate());
|
$this->assertSame(SSViewer::get_templates_by_class(Email::class, '', Email::class), $email->getHTMLTemplate());
|
||||||
$email->setHTMLTemplate('MyTemplate');
|
$email->setHTMLTemplate('MyTemplate');
|
||||||
$this->assertEquals('MyTemplate', $email->getHTMLTemplate());
|
$this->assertEquals('MyTemplate', $email->getHTMLTemplate());
|
||||||
|
|
||||||
// Check subclass template is found
|
// Check subclass template
|
||||||
$email2 = new EmailSubClass();
|
$email2 = new EmailSubClass();
|
||||||
$this->assertEquals($subClassTemplate->getPath(), $email2->getHTMLTemplate());
|
$this->assertSame(SSViewer::get_templates_by_class(EmailSubClass::class, '', Email::class), $email2->getHTMLTemplate());
|
||||||
$email->setHTMLTemplate('MyTemplate');
|
$email->setHTMLTemplate('MyTemplate');
|
||||||
$this->assertEquals('MyTemplate', $email->getHTMLTemplate());
|
$this->assertEquals('MyTemplate', $email->getHTMLTemplate());
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,10 @@ use SilverStripe\Core\Injector\Injector;
|
|||||||
use SilverStripe\Core\Injector\InjectorLoader;
|
use SilverStripe\Core\Injector\InjectorLoader;
|
||||||
use SilverStripe\Core\Kernel;
|
use SilverStripe\Core\Kernel;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Core\Environment;
|
||||||
|
use ReflectionClass;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
use ReflectionObject;
|
||||||
|
|
||||||
class KernelTest extends SapphireTest
|
class KernelTest extends SapphireTest
|
||||||
{
|
{
|
||||||
@ -81,4 +85,32 @@ class KernelTest extends SapphireTest
|
|||||||
|
|
||||||
$kernel->getConfigLoader()->getManifest();
|
$kernel->getConfigLoader()->getManifest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testReplicaDatabaseVarsLoaded()
|
||||||
|
{
|
||||||
|
// Set environment variables for a fake replica database
|
||||||
|
Environment::setEnv('SS_DATABASE_SERVER_REPLICA_01', 'the-moon');
|
||||||
|
Environment::setEnv('SS_DATABASE_USERNAME_REPLICA_01', 'alien');
|
||||||
|
Environment::setEnv('SS_DATABASE_PASSWORD_REPLICA_01', 'hi_people');
|
||||||
|
// Get the CoreKernel
|
||||||
|
/** @var Kernel $kernel */
|
||||||
|
$kernel = Injector::inst()->get(Kernel::class);
|
||||||
|
/** @var CoreKernel $coreKernel */
|
||||||
|
$coreKernel = $kernel->nest();
|
||||||
|
$this->assertTrue(is_a($coreKernel, CoreKernel::class));
|
||||||
|
// Boot the database environment variables
|
||||||
|
$reflector = new ReflectionObject($coreKernel);
|
||||||
|
$method = $reflector->getMethod('bootDatabaseEnvVars');
|
||||||
|
$method->setAccessible(true);
|
||||||
|
$method->invoke($coreKernel);
|
||||||
|
// Assert DB config was updated
|
||||||
|
$default = DB::getConfig(DB::CONN_PRIMARY);
|
||||||
|
$configs = (new ReflectionClass(DB::class))->getStaticPropertyValue('configs');
|
||||||
|
$this->assertSame([
|
||||||
|
'type' => $default['type'],
|
||||||
|
'server' => 'the-moon',
|
||||||
|
'username' => 'alien',
|
||||||
|
'password' => 'hi_people',
|
||||||
|
], $configs['replica_01']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Core\Tests\Manifest;
|
namespace SilverStripe\Core\Tests\Manifest;
|
||||||
|
|
||||||
use Psr\SimpleCache\CacheInterface;
|
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
use SilverStripe\Core\Manifest\ModuleLoader;
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
||||||
use SilverStripe\View\ThemeResourceLoader;
|
use SilverStripe\View\ThemeResourceLoader;
|
||||||
@ -67,188 +66,6 @@ class ThemeResourceLoaderTest extends SapphireTest
|
|||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Test that 'main' and 'Layout' templates are loaded from module
|
|
||||||
*/
|
|
||||||
public function testFindTemplatesInModule()
|
|
||||||
{
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/module/templates/Page.ss",
|
|
||||||
$this->loader->findTemplate('Page', ['$default'])
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/module/templates/Layout/Page.ss",
|
|
||||||
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default'])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFindNestedThemeTemplates()
|
|
||||||
{
|
|
||||||
// Without including the theme this template cannot be found
|
|
||||||
$this->assertEquals(null, $this->loader->findTemplate('NestedThemePage', ['$default']));
|
|
||||||
|
|
||||||
// With a nested theme available then it is available
|
|
||||||
$this->assertEquals(
|
|
||||||
"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss",
|
|
||||||
$this->loader->findTemplate(
|
|
||||||
'NestedThemePage',
|
|
||||||
[
|
|
||||||
'silverstripe/module:subtheme',
|
|
||||||
'$default'
|
|
||||||
]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Can also be found if excluding $default theme
|
|
||||||
$this->assertEquals(
|
|
||||||
"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss",
|
|
||||||
$this->loader->findTemplate(
|
|
||||||
'NestedThemePage',
|
|
||||||
[
|
|
||||||
'silverstripe/module:subtheme',
|
|
||||||
]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFindTemplateByType()
|
|
||||||
{
|
|
||||||
// Test that "type" is respected properly
|
|
||||||
$this->assertEquals(
|
|
||||||
"{$this->base}/module/templates/MyNamespace/Layout/MyClass.ss",
|
|
||||||
$this->loader->findTemplate(
|
|
||||||
[
|
|
||||||
[
|
|
||||||
'type' => 'Layout',
|
|
||||||
'MyNamespace/NonExistantTemplate'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'type' => 'Layout',
|
|
||||||
'MyNamespace/MyClass'
|
|
||||||
],
|
|
||||||
'MyNamespace/MyClass'
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'silverstripe/module:subtheme',
|
|
||||||
'theme',
|
|
||||||
'$default',
|
|
||||||
]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Non-typed template can be found even if looking for typed theme at a lower priority
|
|
||||||
$this->assertEquals(
|
|
||||||
"{$this->base}/module/templates/MyNamespace/MyClass.ss",
|
|
||||||
$this->loader->findTemplate(
|
|
||||||
[
|
|
||||||
[
|
|
||||||
'type' => 'Layout',
|
|
||||||
'MyNamespace/NonExistantTemplate'
|
|
||||||
],
|
|
||||||
'MyNamespace/MyClass',
|
|
||||||
[
|
|
||||||
'type' => 'Layout',
|
|
||||||
'MyNamespace/MyClass'
|
|
||||||
]
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'silverstripe/module',
|
|
||||||
'theme',
|
|
||||||
'$default',
|
|
||||||
]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFindTemplatesByPath()
|
|
||||||
{
|
|
||||||
// Items given as full paths are returned directly
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/themes/theme/templates/Page.ss",
|
|
||||||
$this->loader->findTemplate("$this->base/themes/theme/templates/Page.ss", ['theme'])
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/themes/theme/templates/Page.ss",
|
|
||||||
$this->loader->findTemplate(
|
|
||||||
[
|
|
||||||
"$this->base/themes/theme/templates/Page.ss",
|
|
||||||
"Page"
|
|
||||||
],
|
|
||||||
['theme']
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Ensure checks for file_exists
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/themes/theme/templates/Page.ss",
|
|
||||||
$this->loader->findTemplate(
|
|
||||||
[
|
|
||||||
"$this->base/themes/theme/templates/NotAPage.ss",
|
|
||||||
"$this->base/themes/theme/templates/Page.ss",
|
|
||||||
],
|
|
||||||
['theme']
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test that 'main' and 'Layout' templates are loaded from set theme
|
|
||||||
*/
|
|
||||||
public function testFindTemplatesInTheme()
|
|
||||||
{
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/themes/theme/templates/Page.ss",
|
|
||||||
$this->loader->findTemplate('Page', ['theme'])
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/themes/theme/templates/Layout/Page.ss",
|
|
||||||
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['theme'])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test that 'main' and 'Layout' templates are loaded from project without a set theme
|
|
||||||
*/
|
|
||||||
public function testFindTemplatesInApplication()
|
|
||||||
{
|
|
||||||
$templates = [
|
|
||||||
$this->base . '/myproject/templates/Page.ss',
|
|
||||||
$this->base . '/myproject/templates/Layout/Page.ss'
|
|
||||||
];
|
|
||||||
$this->createTestTemplates($templates);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/myproject/templates/Page.ss",
|
|
||||||
$this->loader->findTemplate('Page', ['$default'])
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/myproject/templates/Layout/Page.ss",
|
|
||||||
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default'])
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->removeTestTemplates($templates);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test that 'main' template is found in theme and 'Layout' is found in module
|
|
||||||
*/
|
|
||||||
public function testFindTemplatesMainThemeLayoutModule()
|
|
||||||
{
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/themes/theme/templates/CustomThemePage.ss",
|
|
||||||
$this->loader->findTemplate('CustomThemePage', ['theme', '$default'])
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"$this->base/module/templates/Layout/CustomThemePage.ss",
|
|
||||||
$this->loader->findTemplate(['type' => 'Layout', 'CustomThemePage'], ['theme', '$default'])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFindThemedCSS()
|
public function testFindThemedCSS()
|
||||||
{
|
{
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
@ -303,20 +120,6 @@ class ThemeResourceLoaderTest extends SapphireTest
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createTestTemplates($templates)
|
|
||||||
{
|
|
||||||
foreach ($templates as $template) {
|
|
||||||
file_put_contents($template ?? '', '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function removeTestTemplates($templates)
|
|
||||||
{
|
|
||||||
foreach ($templates as $template) {
|
|
||||||
unlink($template ?? '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function providerTestGetPath()
|
public static function providerTestGetPath()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@ -381,28 +184,4 @@ class ThemeResourceLoaderTest extends SapphireTest
|
|||||||
{
|
{
|
||||||
$this->assertEquals($path, $this->loader->getPath($name));
|
$this->assertEquals($path, $this->loader->getPath($name));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFindTemplateWithCacheMiss()
|
|
||||||
{
|
|
||||||
$mockCache = $this->createMock(CacheInterface::class);
|
|
||||||
$mockCache->expects($this->once())->method('has')->willReturn(false);
|
|
||||||
$mockCache->expects($this->never())->method('get');
|
|
||||||
$mockCache->expects($this->once())->method('set');
|
|
||||||
|
|
||||||
$loader = new ThemeResourceLoader();
|
|
||||||
$loader->setCache($mockCache);
|
|
||||||
$loader->findTemplate('Page', ['$default']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testFindTemplateWithCacheHit()
|
|
||||||
{
|
|
||||||
$mockCache = $this->createMock(CacheInterface::class);
|
|
||||||
$mockCache->expects($this->once())->method('has')->willReturn(true);
|
|
||||||
$mockCache->expects($this->never())->method('set');
|
|
||||||
$mockCache->expects($this->once())->method('get')->willReturn('mock_template.ss');
|
|
||||||
|
|
||||||
$loader = new ThemeResourceLoader();
|
|
||||||
$loader->setCache($mockCache);
|
|
||||||
$this->assertSame('mock_template.ss', $loader->findTemplate('Page', ['$default']));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace SilverStripe\Forms\Tests\GridField;
|
namespace SilverStripe\Forms\Tests\GridField;
|
||||||
|
|
||||||
use LogicException;
|
use LogicException;
|
||||||
|
use ReflectionMethod;
|
||||||
use SilverStripe\Control\HTTPRequest;
|
use SilverStripe\Control\HTTPRequest;
|
||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
@ -117,6 +118,29 @@ class GridFieldFilterHeaderTest extends SapphireTest
|
|||||||
$this->assertEquals('testfield', $searchSchema->gridfield);
|
$this->assertEquals('testfield', $searchSchema->gridfield);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the private method that returns the placeholder for the search field
|
||||||
|
*/
|
||||||
|
public function testGetPlaceHolder()
|
||||||
|
{
|
||||||
|
$gridField = new GridField('test');
|
||||||
|
$filterHeader = new GridFieldFilterHeader();
|
||||||
|
$reflectionGetPlaceHolder = new ReflectionMethod($filterHeader, 'getPlaceHolder');
|
||||||
|
$reflectionGetPlaceHolder->setAccessible(true);
|
||||||
|
|
||||||
|
// No explicit placeholder or model i18n_plural_name method
|
||||||
|
$this->assertSame('Search "ArrayData"', $reflectionGetPlaceHolder->invoke($filterHeader, new ArrayData()));
|
||||||
|
|
||||||
|
// No explicit placeholder, but model has i18n_plural_name method
|
||||||
|
$model = new DataObject();
|
||||||
|
$this->assertSame('Search "' . $model->i18n_plural_name() . '"', $reflectionGetPlaceHolder->invoke($filterHeader, $model));
|
||||||
|
|
||||||
|
// Explicit placeholder is set, which overrides both of the above cases
|
||||||
|
$filterHeader->setPlaceHolderText('This is the text');
|
||||||
|
$this->assertSame('This is the text', $reflectionGetPlaceHolder->invoke($filterHeader, $model));
|
||||||
|
$this->assertSame('This is the text', $reflectionGetPlaceHolder->invoke($filterHeader, new ArrayData()));
|
||||||
|
}
|
||||||
|
|
||||||
public function testHandleActionReset()
|
public function testHandleActionReset()
|
||||||
{
|
{
|
||||||
// Init Grid state with some pre-existing filters
|
// Init Grid state with some pre-existing filters
|
||||||
|
@ -56,7 +56,7 @@ class TestComponent extends RequestHandler implements GridField_URLHandler
|
|||||||
public function showform(GridField $gridField, HTTPRequest $request)
|
public function showform(GridField $gridField, HTTPRequest $request)
|
||||||
{
|
{
|
||||||
$this->setRequest($request);
|
$this->setRequest($request);
|
||||||
return "<head>" . SSViewer::get_base_tag("") . "</head>" . $this->Form($gridField, $request)->forTemplate();
|
return "<head>" . SSViewer::getBaseTag() . "</head>" . $this->Form($gridField, $request)->forTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,7 +36,7 @@ class TestComponent_ItemRequest extends RequestHandler
|
|||||||
|
|
||||||
public function showform()
|
public function showform()
|
||||||
{
|
{
|
||||||
return "<head>" . SSViewer::get_base_tag("") . "</head>" . $this->Form()->forTemplate();
|
return "<head>" . SSViewer::getBaseTag() . "</head>" . $this->Form()->forTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Form()
|
public function Form()
|
||||||
|
@ -314,7 +314,7 @@ class TreeDropdownFieldTest extends SapphireTest
|
|||||||
$noResult = $parser->getBySelector($cssPath);
|
$noResult = $parser->getBySelector($cssPath);
|
||||||
$this->assertEmpty(
|
$this->assertEmpty(
|
||||||
$noResult,
|
$noResult,
|
||||||
$subObject2 . ' is not found'
|
get_class($subObject2) . ' is not found'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ use SilverStripe\Dev\SapphireTest;
|
|||||||
use SilverStripe\Forms\Form;
|
use SilverStripe\Forms\Form;
|
||||||
use SilverStripe\Forms\FormTemplateHelper;
|
use SilverStripe\Forms\FormTemplateHelper;
|
||||||
use SilverStripe\Forms\TreeMultiselectField;
|
use SilverStripe\Forms\TreeMultiselectField;
|
||||||
|
use SilverStripe\ORM\Tests\HierarchyTest\HierarchyOnSubclassTestObject;
|
||||||
|
use SilverStripe\ORM\Tests\HierarchyTest\HierarchyOnSubclassTestSubObject;
|
||||||
use SilverStripe\ORM\Tests\HierarchyTest\TestObject;
|
use SilverStripe\ORM\Tests\HierarchyTest\TestObject;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
|
|
||||||
@ -16,6 +18,8 @@ class TreeMultiselectFieldTest extends SapphireTest
|
|||||||
|
|
||||||
protected static $extra_dataobjects = [
|
protected static $extra_dataobjects = [
|
||||||
TestObject::class,
|
TestObject::class,
|
||||||
|
HierarchyOnSubclassTestObject::class,
|
||||||
|
HierarchyOnSubclassTestSubObject::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $formId = 'TheFormID';
|
protected $formId = 'TheFormID';
|
||||||
|
@ -12,6 +12,7 @@ use SilverStripe\Model\Tests\ModelDataTest\ModelDataTestExtension;
|
|||||||
use SilverStripe\Model\Tests\ModelDataTest\ModelDataTestObject;
|
use SilverStripe\Model\Tests\ModelDataTest\ModelDataTestObject;
|
||||||
use SilverStripe\Model\ModelData;
|
use SilverStripe\Model\ModelData;
|
||||||
use PHPUnit\Framework\Attributes\DataProvider;
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use SilverStripe\Model\Tests\ModelDataTest\TestModelData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See {@link SSViewerTest->testCastingHelpers()} for more tests related to casting and ModelData behaviour,
|
* See {@link SSViewerTest->testCastingHelpers()} for more tests related to casting and ModelData behaviour,
|
||||||
@ -54,6 +55,18 @@ class ModelDataTest extends SapphireTest
|
|||||||
$this->assertEquals($htmlString, $textField->obj('XML')->forTemplate());
|
$this->assertEquals($htmlString, $textField->obj('XML')->forTemplate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCastingValues()
|
||||||
|
{
|
||||||
|
$caster = new ModelDataTest\Castable();
|
||||||
|
|
||||||
|
$this->assertEquals('casted', $caster->obj('alwaysCasted')->forTemplate());
|
||||||
|
$this->assertEquals('casted', $caster->obj('noCastingInformation')->forTemplate());
|
||||||
|
|
||||||
|
// Test automatic escaping is applied even to fields with no 'casting'
|
||||||
|
$this->assertEquals('casted', $caster->obj('unsafeXML')->forTemplate());
|
||||||
|
$this->assertEquals('<foo>', $caster->obj('castedUnsafeXML')->forTemplate());
|
||||||
|
}
|
||||||
|
|
||||||
public function testRequiresCasting()
|
public function testRequiresCasting()
|
||||||
{
|
{
|
||||||
$caster = new ModelDataTest\Castable();
|
$caster = new ModelDataTest\Castable();
|
||||||
@ -78,18 +91,6 @@ class ModelDataTest extends SapphireTest
|
|||||||
$this->assertInstanceOf(ModelDataTest\Caster::class, $caster->obj('noCastingInformation'));
|
$this->assertInstanceOf(ModelDataTest\Caster::class, $caster->obj('noCastingInformation'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCastingXMLVal()
|
|
||||||
{
|
|
||||||
$caster = new ModelDataTest\Castable();
|
|
||||||
|
|
||||||
$this->assertEquals('casted', $caster->XML_val('alwaysCasted'));
|
|
||||||
$this->assertEquals('casted', $caster->XML_val('noCastingInformation'));
|
|
||||||
|
|
||||||
// Test automatic escaping is applied even to fields with no 'casting'
|
|
||||||
$this->assertEquals('casted', $caster->XML_val('unsafeXML'));
|
|
||||||
$this->assertEquals('<foo>', $caster->XML_val('castedUnsafeXML'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testArrayCustomise()
|
public function testArrayCustomise()
|
||||||
{
|
{
|
||||||
$modelData = new ModelDataTest\Castable();
|
$modelData = new ModelDataTest\Castable();
|
||||||
@ -100,11 +101,11 @@ class ModelDataTest extends SapphireTest
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('test', $modelData->XML_val('test'));
|
$this->assertEquals('test', $modelData->obj('test')->forTemplate());
|
||||||
$this->assertEquals('casted', $modelData->XML_val('alwaysCasted'));
|
$this->assertEquals('casted', $modelData->obj('alwaysCasted')->forTemplate());
|
||||||
|
|
||||||
$this->assertEquals('overwritten', $newModelData->XML_val('test'));
|
$this->assertEquals('overwritten', $newModelData->obj('test')->forTemplate());
|
||||||
$this->assertEquals('overwritten', $newModelData->XML_val('alwaysCasted'));
|
$this->assertEquals('overwritten', $newModelData->obj('alwaysCasted')->forTemplate());
|
||||||
|
|
||||||
$this->assertEquals('castable', $modelData->forTemplate());
|
$this->assertEquals('castable', $modelData->forTemplate());
|
||||||
$this->assertEquals('castable', $newModelData->forTemplate());
|
$this->assertEquals('castable', $newModelData->forTemplate());
|
||||||
@ -115,14 +116,14 @@ class ModelDataTest extends SapphireTest
|
|||||||
$modelData = new ModelDataTest\Castable();
|
$modelData = new ModelDataTest\Castable();
|
||||||
$newModelData = $modelData->customise(new ModelDataTest\RequiresCasting());
|
$newModelData = $modelData->customise(new ModelDataTest\RequiresCasting());
|
||||||
|
|
||||||
$this->assertEquals('test', $modelData->XML_val('test'));
|
$this->assertEquals('test', $modelData->obj('test')->forTemplate());
|
||||||
$this->assertEquals('casted', $modelData->XML_val('alwaysCasted'));
|
$this->assertEquals('casted', $modelData->obj('alwaysCasted')->forTemplate());
|
||||||
|
|
||||||
$this->assertEquals('overwritten', $newModelData->XML_val('test'));
|
$this->assertEquals('overwritten', $newModelData->obj('test')->forTemplate());
|
||||||
$this->assertEquals('casted', $newModelData->XML_val('alwaysCasted'));
|
$this->assertEquals('casted', $newModelData->obj('alwaysCasted')->forTemplate());
|
||||||
|
|
||||||
$this->assertEquals('castable', $modelData->forTemplate());
|
$this->assertEquals('castable', $modelData->forTemplate());
|
||||||
$this->assertEquals('casted', $newModelData->forTemplate());
|
$this->assertEquals('castable', $newModelData->forTemplate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDefaultValueWrapping()
|
public function testDefaultValueWrapping()
|
||||||
@ -139,25 +140,6 @@ class ModelDataTest extends SapphireTest
|
|||||||
$this->assertEquals('SomeTitleValue', $obj->forTemplate());
|
$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()
|
public function testObjWithCachedStringValueReturnsValidObject()
|
||||||
{
|
{
|
||||||
$obj = new ModelDataTest\NoCastingInformation();
|
$obj = new ModelDataTest\NoCastingInformation();
|
||||||
@ -273,6 +255,114 @@ class ModelDataTest extends SapphireTest
|
|||||||
$this->assertTrue($output, 'Property should be accessible');
|
$this->assertTrue($output, 'Property should be accessible');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function provideObj(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'returned value is caught' => [
|
||||||
|
'name' => 'justCallMethod',
|
||||||
|
'args' => [],
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'justCallMethod',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => 'This is a method value',
|
||||||
|
],
|
||||||
|
'getter is used' => [
|
||||||
|
'name' => 'ActualValue',
|
||||||
|
'args' => [],
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getActualValue',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => 'this is the value',
|
||||||
|
],
|
||||||
|
'if no method exists, only property is fetched' => [
|
||||||
|
'name' => 'NoMethod',
|
||||||
|
'args' => [],
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'NoMethod',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
'property value is caught' => [
|
||||||
|
'name' => 'ActualValueField',
|
||||||
|
'args' => [],
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'ActualValueField',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => 'the value is here',
|
||||||
|
],
|
||||||
|
'not set and no method' => [
|
||||||
|
'name' => 'NotSet',
|
||||||
|
'args' => [],
|
||||||
|
'expectRequested' => [],
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
'args but no method' => [
|
||||||
|
'name' => 'SomeField',
|
||||||
|
'args' => ['abc', 123],
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'SomeField',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
'method with args' => [
|
||||||
|
'name' => 'justCallMethod',
|
||||||
|
'args' => ['abc', 123],
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'justCallMethod',
|
||||||
|
'args' => ['abc', 123],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => 'This is a method value',
|
||||||
|
],
|
||||||
|
'getter with args' => [
|
||||||
|
'name' => 'ActualValue',
|
||||||
|
'args' => ['abc', 123],
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getActualValue',
|
||||||
|
'args' => ['abc', 123],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => 'this is the value',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideObj')]
|
||||||
|
public function testObj(string $name, array $args, array $expectRequested, ?string $expected): void
|
||||||
|
{
|
||||||
|
$fixture = new TestModelData();
|
||||||
|
$value = $fixture->obj($name, $args);
|
||||||
|
$this->assertSame($expectRequested, $fixture->getRequested());
|
||||||
|
$this->assertEquals($expected, ($value instanceof DBField) ? $value->getValue() : $value);
|
||||||
|
// Ensure value is being wrapped when not null
|
||||||
|
// Don't bother testing actual casting, there's some coverage for that in this class already
|
||||||
|
// but mostly it's tested in CastingServiceTest
|
||||||
|
if ($value !== null) {
|
||||||
|
$this->assertTrue(is_object($value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function testDynamicData()
|
public function testDynamicData()
|
||||||
{
|
{
|
||||||
$obj = (object) ['SomeField' => [1, 2, 3]];
|
$obj = (object) ['SomeField' => [1, 2, 3]];
|
||||||
|
@ -9,7 +9,7 @@ class NotCached extends ModelData implements TestOnly
|
|||||||
{
|
{
|
||||||
public $Test;
|
public $Test;
|
||||||
|
|
||||||
protected function objCacheGet($key)
|
public function objCacheGet(string $fieldName, array $arguments = []): mixed
|
||||||
{
|
{
|
||||||
// Disable caching
|
// Disable caching
|
||||||
return null;
|
return null;
|
||||||
|
59
tests/php/Model/ModelDataTest/TestModelData.php
Normal file
59
tests/php/Model/ModelDataTest/TestModelData.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Model\Tests\ModelDataTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\Model\ModelData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A model that captures information about what's being fetched on it for some methods
|
||||||
|
*/
|
||||||
|
class TestModelData extends ModelData implements TestOnly
|
||||||
|
{
|
||||||
|
private array $requested = [];
|
||||||
|
|
||||||
|
public function justCallMethod(): string
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => __FUNCTION__,
|
||||||
|
'args' => func_get_args(),
|
||||||
|
];
|
||||||
|
return 'This is a method value';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getActualValue(): string
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => __FUNCTION__,
|
||||||
|
'args' => func_get_args(),
|
||||||
|
];
|
||||||
|
return 'this is the value';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getField(string $name): ?string
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => $name,
|
||||||
|
];
|
||||||
|
if ($name === 'ActualValueField') {
|
||||||
|
return 'the value is here';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We need this so we always try to fetch a property.
|
||||||
|
*/
|
||||||
|
public function hasField(string $name): bool
|
||||||
|
{
|
||||||
|
return $name !== 'NotSet';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRequested(): array
|
||||||
|
{
|
||||||
|
return $this->requested;
|
||||||
|
}
|
||||||
|
}
|
260
tests/php/ORM/DBReplicaTest.php
Normal file
260
tests/php/ORM/DBReplicaTest.php
Normal file
@ -0,0 +1,260 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\ORM\Tests;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
use SilverStripe\Dev\FunctionalTest;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
use SilverStripe\Control\Director;
|
||||||
|
use SilverStripe\Security\Security;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
|
use SilverStripe\ORM\Tests\DBReplicaTest\TestController;
|
||||||
|
use SilverStripe\ORM\Tests\DBReplicaTest\TestObject;
|
||||||
|
use SilverStripe\Security\Group;
|
||||||
|
use SilverStripe\Security\Member;
|
||||||
|
use SilverStripe\ORM\DataQuery;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
|
||||||
|
class DBReplicaTest extends FunctionalTest
|
||||||
|
{
|
||||||
|
protected static $extra_dataobjects = [
|
||||||
|
TestObject::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
protected static $fixture_file = 'DBReplicaTest.yml';
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->setupConfigsAndConnections(true);
|
||||||
|
// Set DB:$mustUsePrimary to true to allow using replicas
|
||||||
|
// This is disabled by default in SapphireTest::setUpBeforeClass()
|
||||||
|
// Also reset mustUsePrimary after using mutable sql to create yml fixtures
|
||||||
|
// and also because by default an ADMIN user is logged in when using fixtures in SapphireTest::setUp()
|
||||||
|
// and also prevent tests from affecting subsequent tests
|
||||||
|
(new ReflectionClass(DB::class))->setStaticPropertyValue('mustUsePrimary', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
$this->setupConfigsAndConnections(false);
|
||||||
|
// Reset DB:$mustUsePrimary to true which is the default set by SapphireTest::setUpBeforeClass()
|
||||||
|
(new ReflectionClass(DB::class))->setStaticPropertyValue('mustUsePrimary', true);
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUsesReplica(): void
|
||||||
|
{
|
||||||
|
// Assert uses replica by default
|
||||||
|
TestObject::get()->count();
|
||||||
|
$this->assertSame('replica_01', $this->getLastConnectionName());
|
||||||
|
// Assert uses primary when using withPrimary()
|
||||||
|
DB::withPrimary(fn() => TestObject::get()->count());
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
// Assert that withPrimary() was only temporary
|
||||||
|
TestObject::get()->count();
|
||||||
|
$this->assertSame('replica_01', $this->getLastConnectionName());
|
||||||
|
// Assert DB::setMustUsePrimary() forces primary from now on
|
||||||
|
DB::setMustUsePrimary();
|
||||||
|
TestObject::get()->count();
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMutableSql(): void
|
||||||
|
{
|
||||||
|
// Assert that using mutable sql in an ORM method with a dataclass uses primary
|
||||||
|
TestObject::create(['Title' => 'testing'])->write();
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
// Assert that now all subsequent queries use primary
|
||||||
|
TestObject::get()->count();
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMutableSqlDbQuery(): void
|
||||||
|
{
|
||||||
|
// Assert that using mutable sql in DB::query() uses primary
|
||||||
|
DB::query('INSERT INTO "DBReplicaTest_TestObject" ("Title") VALUES (\'testing\')');
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
// Assert that now all subsequent queries use primary
|
||||||
|
TestObject::get()->count();
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMutableSqlDbPreparedQuery(): void
|
||||||
|
{
|
||||||
|
// Assert that using mutable sql in DB::prepared_query() uses primary
|
||||||
|
DB::prepared_query('INSERT INTO "DBReplicaTest_TestObject" ("Title") VALUES (?)', ['testing']);
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
// Assert that now all subsequent queries use primary
|
||||||
|
TestObject::get()->count();
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideSetCurrentUser')]
|
||||||
|
public function testSetCurrentUser(string $firstName, string $expected): void
|
||||||
|
{
|
||||||
|
$member = Member::get()->find('FirstName', $firstName);
|
||||||
|
Security::setCurrentUser($member);
|
||||||
|
TestObject::get()->count();
|
||||||
|
$this->assertSame($expected, $this->getLastConnectionName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDataObjectMustUsePrimaryDb(): void
|
||||||
|
{
|
||||||
|
// Assert that DataList::getIterator() respect DataObject.must_use_primary_db
|
||||||
|
foreach (TestObject::get() as $object) {
|
||||||
|
$object->Title = 'test2';
|
||||||
|
}
|
||||||
|
$this->assertSame('replica_01', $this->getLastConnectionName());
|
||||||
|
foreach (Group::get() as $group) {
|
||||||
|
$group->Title = 'test2';
|
||||||
|
}
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName());
|
||||||
|
// Assert that DataQuery methods without params respect DataObject.must_use_primary_db
|
||||||
|
$methods = [
|
||||||
|
'count',
|
||||||
|
'exists',
|
||||||
|
'firstRow',
|
||||||
|
'lastRow'
|
||||||
|
];
|
||||||
|
foreach ($methods as $method) {
|
||||||
|
(new DataQuery(TestObject::class))->$method();
|
||||||
|
$this->assertSame('replica_01', $this->getLastConnectionName(), "method is $method");
|
||||||
|
(new DataQuery(Group::class))->$method();
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName(), "method is $method");
|
||||||
|
}
|
||||||
|
// Assert that DataQuery methods with a param respect DataObject.must_use_primary_db
|
||||||
|
$methods = [
|
||||||
|
'max',
|
||||||
|
'min',
|
||||||
|
'avg',
|
||||||
|
'sum',
|
||||||
|
'column',
|
||||||
|
];
|
||||||
|
foreach ($methods as $method) {
|
||||||
|
(new DataQuery(TestObject::class))->$method('ID');
|
||||||
|
$this->assertSame('replica_01', $this->getLastConnectionName(), "method is $method");
|
||||||
|
(new DataQuery(Group::class))->$method('ID');
|
||||||
|
$this->assertSame(DB::CONN_PRIMARY, $this->getLastConnectionName(), "method is $method");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideSetCurrentUser(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'non_cms_user' => [
|
||||||
|
'firstName' => 'random',
|
||||||
|
'expected' => 'replica_01'
|
||||||
|
],
|
||||||
|
'cms_user' => [
|
||||||
|
'firstName' => 'cmsuser',
|
||||||
|
'expected' => DB::CONN_PRIMARY
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideRoutes(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'normal_route' => [
|
||||||
|
'path' => 'test',
|
||||||
|
'expected' => 'replica_01'
|
||||||
|
],
|
||||||
|
'security_route' => [
|
||||||
|
'path' => 'Security/login',
|
||||||
|
'expected' => DB::CONN_PRIMARY
|
||||||
|
],
|
||||||
|
'dev_route' => [
|
||||||
|
'path' => 'dev/tasks',
|
||||||
|
'expected' => DB::CONN_PRIMARY
|
||||||
|
],
|
||||||
|
'dev_in_path_but_not_dev_route' => [
|
||||||
|
'path' => 'test/dev',
|
||||||
|
'expected' => 'replica_01'
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideRoutes')]
|
||||||
|
public function testRoutes(string $path, string $expected): void
|
||||||
|
{
|
||||||
|
// Create a custom rule to test our controller that should default to using a replica
|
||||||
|
$rules = Config::inst()->get(Director::class, 'rules');
|
||||||
|
$rules['test'] = TestController::class;
|
||||||
|
// Ensure that routes staring with '$' are at the bottom of the assoc array index and don't override
|
||||||
|
// our new 'test' route
|
||||||
|
uksort($rules, fn($a, $b) => str_starts_with($a, '$') ? 1 : (str_starts_with($b, '$') ? -1 : 0));
|
||||||
|
$this->get($path);
|
||||||
|
$this->assertSame($expected, $this->getLastConnectionName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideHasReplicaConfig(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'no_replica' => [
|
||||||
|
'includeReplica' => false,
|
||||||
|
'expected' => false
|
||||||
|
],
|
||||||
|
'with_replica' => [
|
||||||
|
'includeReplica' => true,
|
||||||
|
'expected' => true
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideHasReplicaConfig')]
|
||||||
|
public function testHasReplicaConfig(bool $includeReplica, bool $expected): void
|
||||||
|
{
|
||||||
|
$this->assertTrue(DB::hasReplicaConfig());
|
||||||
|
$primaryConfig = DB::getConfig(DB::CONN_PRIMARY);
|
||||||
|
$config = [DB::CONN_PRIMARY => $primaryConfig];
|
||||||
|
if ($includeReplica) {
|
||||||
|
$config['replica_01'] = $primaryConfig;
|
||||||
|
}
|
||||||
|
(new ReflectionClass(DB::class))->setStaticPropertyValue('configs', $config);
|
||||||
|
$this->assertSame($expected, DB::hasReplicaConfig());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHasConfig(): void
|
||||||
|
{
|
||||||
|
$this->assertFalse(DB::hasConfig('lorem'));
|
||||||
|
DB::setConfig(['type' => 'lorem'], 'lorem');
|
||||||
|
$this->assertTrue(DB::hasConfig('lorem'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetReplicaConfigKey(): void
|
||||||
|
{
|
||||||
|
$this->assertSame('replica_03', DB::getReplicaConfigKey(3));
|
||||||
|
$this->assertSame('replica_58', DB::getReplicaConfigKey(58));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Using reflection, set DB::configs and DB::connections with a fake a replica connection
|
||||||
|
* that points to the same connection as the primary connection.
|
||||||
|
*/
|
||||||
|
private function setupConfigsAndConnections($includeReplica = true): void
|
||||||
|
{
|
||||||
|
$reflector = new ReflectionClass(DB::class);
|
||||||
|
$primaryConfig = DB::getConfig(DB::CONN_PRIMARY);
|
||||||
|
$configs = [DB::CONN_PRIMARY => $primaryConfig];
|
||||||
|
if ($includeReplica) {
|
||||||
|
$configs['replica_01'] = $primaryConfig;
|
||||||
|
}
|
||||||
|
$reflector->setStaticPropertyValue('configs', $configs);
|
||||||
|
// Create connections
|
||||||
|
$primaryConnection = DB::get_conn(DB::CONN_PRIMARY);
|
||||||
|
$connections = [DB::CONN_PRIMARY => $primaryConnection];
|
||||||
|
if ($includeReplica) {
|
||||||
|
$connections['replica_01'] = $primaryConnection;
|
||||||
|
}
|
||||||
|
$reflector->setStaticPropertyValue('connections', $connections);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the last connection name used by the DB class. This shows if a replica was used.
|
||||||
|
*/
|
||||||
|
private function getLastConnectionName(): string
|
||||||
|
{
|
||||||
|
return (new ReflectionClass(DB::class))->getStaticPropertyValue('lastConnectionName');
|
||||||
|
}
|
||||||
|
}
|
18
tests/php/ORM/DBReplicaTest.yml
Normal file
18
tests/php/ORM/DBReplicaTest.yml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
SilverStripe\ORM\Tests\DBReplicaTest\TestObject:
|
||||||
|
test:
|
||||||
|
Title: 'test'
|
||||||
|
SilverStripe\Security\Permission:
|
||||||
|
test:
|
||||||
|
Code: 'CMS_ACCESS_Something'
|
||||||
|
SilverStripe\Security\Group:
|
||||||
|
test:
|
||||||
|
Title: 'test'
|
||||||
|
Permissions:
|
||||||
|
- =>SilverStripe\Security\Permission.test
|
||||||
|
SilverStripe\Security\Member:
|
||||||
|
cmsuser:
|
||||||
|
FirstName: 'CmsUser'
|
||||||
|
Groups:
|
||||||
|
- =>SilverStripe\Security\Group.test
|
||||||
|
random:
|
||||||
|
FirstName: 'Random'
|
18
tests/php/ORM/DBReplicaTest/TestController.php
Normal file
18
tests/php/ORM/DBReplicaTest/TestController.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\ORM\Tests\DBReplicaTest;
|
||||||
|
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class TestController extends Controller implements TestOnly
|
||||||
|
{
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
// Make a call to the database
|
||||||
|
TestObject::get()->count();
|
||||||
|
$response = $this->getResponse();
|
||||||
|
$response->setBody('DB_REPLICA_TEST_CONTROLLER');
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
16
tests/php/ORM/DBReplicaTest/TestObject.php
Normal file
16
tests/php/ORM/DBReplicaTest/TestObject.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\ORM\Tests\DBReplicaTest;
|
||||||
|
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
|
class TestObject extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $db = [
|
||||||
|
'Title' => 'Varchar',
|
||||||
|
];
|
||||||
|
|
||||||
|
private static $table_name = 'DBReplicaTest_TestObject';
|
||||||
|
}
|
@ -197,20 +197,6 @@ class EndsWithFilterTest extends SapphireTest
|
|||||||
'modifiers' => [],
|
'modifiers' => [],
|
||||||
'matches' => false,
|
'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
|
// case insensitive
|
||||||
[
|
[
|
||||||
'filterValue' => 'somevalue',
|
'filterValue' => 'somevalue',
|
||||||
|
@ -197,20 +197,6 @@ class PartialMatchFilterTest extends SapphireTest
|
|||||||
'modifiers' => [],
|
'modifiers' => [],
|
||||||
'matches' => false,
|
'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
|
// case insensitive
|
||||||
[
|
[
|
||||||
'filterValue' => 'somevalue',
|
'filterValue' => 'somevalue',
|
||||||
|
@ -197,20 +197,6 @@ class StartsWithFilterTest extends SapphireTest
|
|||||||
'modifiers' => [],
|
'modifiers' => [],
|
||||||
'matches' => false,
|
'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
|
// case insensitive
|
||||||
[
|
[
|
||||||
'filterValue' => 'somevalue',
|
'filterValue' => 'somevalue',
|
||||||
|
@ -11,6 +11,6 @@ class LabelFieldTest extends SapphireTest
|
|||||||
public function testFieldHasNoNameAttribute()
|
public function testFieldHasNoNameAttribute()
|
||||||
{
|
{
|
||||||
$field = new LabelField('MyName', 'MyTitle');
|
$field = new LabelField('MyName', 'MyTitle');
|
||||||
$this->assertEquals(trim($field->Field() ?? ''), '<label id="MyName" class="readonly">MyTitle</label>');
|
$this->assertEquals('<label id="MyName" class="readonly">MyTitle</label>', trim($field->Field()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ class MySQLiConnectorTest extends SapphireTest implements TestOnly
|
|||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$config = DB::getConfig();
|
$config = DB::getConfig(DB::CONN_PRIMARY);
|
||||||
|
|
||||||
if (strtolower(substr($config['type'] ?? '', 0, 5)) !== 'mysql') {
|
if (strtolower(substr($config['type'] ?? '', 0, 5)) !== 'mysql') {
|
||||||
$this->markTestSkipped("The test only relevant for MySQL - but $config[type] is in use");
|
$this->markTestSkipped("The test only relevant for MySQL - but $config[type] is in use");
|
||||||
|
206
tests/php/View/CastingServiceTest.php
Normal file
206
tests/php/View/CastingServiceTest.php
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Model\ArrayData;
|
||||||
|
use SilverStripe\Model\List\ArrayList;
|
||||||
|
use SilverStripe\ORM\FieldType\DBBoolean;
|
||||||
|
use SilverStripe\ORM\FieldType\DBCurrency;
|
||||||
|
use SilverStripe\ORM\FieldType\DBDate;
|
||||||
|
use SilverStripe\ORM\FieldType\DBField;
|
||||||
|
use SilverStripe\ORM\FieldType\DBFloat;
|
||||||
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||||
|
use SilverStripe\ORM\FieldType\DBInt;
|
||||||
|
use SilverStripe\ORM\FieldType\DBText;
|
||||||
|
use SilverStripe\ORM\FieldType\DBTime;
|
||||||
|
use SilverStripe\View\CastingService;
|
||||||
|
use SilverStripe\View\Tests\CastingServiceTest\TestDataObject;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
class CastingServiceTest extends SapphireTest
|
||||||
|
{
|
||||||
|
// protected static $extra_dataobjects = [
|
||||||
|
// TestDataObject::class,
|
||||||
|
// ];
|
||||||
|
|
||||||
|
protected $usesDatabase = false;
|
||||||
|
|
||||||
|
public static function provideCast(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'data' => null,
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => new stdClass(),
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => stdClass::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => new stdClass(),
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'DateField',
|
||||||
|
'expected' => stdClass::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => new DBText(),
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'DateField',
|
||||||
|
'expected' => stdClass::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => '2024-10-10',
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'DateField',
|
||||||
|
'expected' => DBDate::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => 'some value',
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'HtmlField',
|
||||||
|
'expected' => DBHTMLText::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => '12.35',
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'OverrideCastingHelper',
|
||||||
|
'expected' => DBCurrency::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => '10:17:36',
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'TimeField',
|
||||||
|
'expected' => DBTime::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => 123456,
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'RandomField',
|
||||||
|
'expected' => DBInt::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => '<body>some text</body>',
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'RandomField',
|
||||||
|
'expected' => DBText::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => '12.35',
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => 'OverrideCastingHelper',
|
||||||
|
'expected' => DBText::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => 123456,
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => 'RandomField',
|
||||||
|
'expected' => DBInt::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => '10:17:36',
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => 'TimeField',
|
||||||
|
'expected' => DBText::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => '<body>some text</body>',
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => DBText::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => true,
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => DBBoolean::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => false,
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => DBBoolean::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => 1.234,
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => DBFloat::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => [],
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => ArrayList::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => [1,2,3,4],
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => ArrayList::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['one' => 1, 'two' => 2],
|
||||||
|
'source' => null,
|
||||||
|
'fieldName' => '',
|
||||||
|
'expected' => ArrayData::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['one' => 1, 'two' => 2],
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'AnyField',
|
||||||
|
'expected' => ArrayData::class,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['one' => 1, 'two' => 2],
|
||||||
|
'source' => TestDataObject::class,
|
||||||
|
'fieldName' => 'ArrayAsText',
|
||||||
|
'expected' => DBText::class,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideCast')]
|
||||||
|
public function testCast(mixed $data, ?string $source, string $fieldName, ?string $expected): void
|
||||||
|
{
|
||||||
|
// Can't instantiate DataObject in a data provider
|
||||||
|
if (is_string($source)) {
|
||||||
|
$source = new $source();
|
||||||
|
}
|
||||||
|
$service = new CastingService();
|
||||||
|
$value = $service->cast($data, $source, $fieldName);
|
||||||
|
|
||||||
|
// Check the cast object is the correct type
|
||||||
|
if ($expected === null) {
|
||||||
|
$this->assertNull($value);
|
||||||
|
} elseif (is_object($data)) {
|
||||||
|
$this->assertSame($data, $value);
|
||||||
|
} else {
|
||||||
|
$this->assertInstanceOf($expected, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the value is retained
|
||||||
|
if ($value instanceof DBField && !is_object($data)) {
|
||||||
|
$this->assertSame($data, $value->getValue());
|
||||||
|
}
|
||||||
|
if ($value instanceof ArrayData && !is_object($data)) {
|
||||||
|
$this->assertSame($data, $value->toMap());
|
||||||
|
}
|
||||||
|
if ($value instanceof ArrayList && !is_object($data)) {
|
||||||
|
$this->assertSame($data, $value->toArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCastStrict(): void
|
||||||
|
{
|
||||||
|
$service = new CastingService();
|
||||||
|
$value = $service->cast(null, strict: true);
|
||||||
|
$this->assertInstanceOf(DBText::class, $value);
|
||||||
|
$this->assertNull($value->getValue());
|
||||||
|
}
|
||||||
|
}
|
30
tests/php/View/CastingServiceTest/TestDataObject.php
Normal file
30
tests/php/View/CastingServiceTest/TestDataObject.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\CastingServiceTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
|
class TestDataObject extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static string $table_name = 'CastingServiceTest_TestDataObject';
|
||||||
|
|
||||||
|
private static array $db = [
|
||||||
|
'HtmlField' => 'HTMLText',
|
||||||
|
'DateField' => 'Date',
|
||||||
|
];
|
||||||
|
|
||||||
|
private static array $casting = [
|
||||||
|
'DateField' => 'Text', // won't override
|
||||||
|
'TimeField' => 'Time',
|
||||||
|
'ArrayAsText' => 'Text',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function castingHelper(string $field): ?string
|
||||||
|
{
|
||||||
|
if ($field === 'OverrideCastingHelper') {
|
||||||
|
return 'Currency';
|
||||||
|
}
|
||||||
|
return parent::castingHelper($field);
|
||||||
|
}
|
||||||
|
}
|
@ -6,31 +6,17 @@ use SilverStripe\Dev\SapphireTest;
|
|||||||
use SilverStripe\Control\ContentNegotiator;
|
use SilverStripe\Control\ContentNegotiator;
|
||||||
use SilverStripe\Control\HTTPResponse;
|
use SilverStripe\Control\HTTPResponse;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
use SilverStripe\View\Tests\SSViewerTest\TestFixture;
|
|
||||||
|
|
||||||
class ContentNegotiatorTest extends SapphireTest
|
class ContentNegotiatorTest extends SapphireTest
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* Small helper to render templates from strings
|
|
||||||
* Cloned from SSViewerTest
|
|
||||||
*/
|
|
||||||
private function render($templateString, $data = null)
|
|
||||||
{
|
|
||||||
$t = SSViewer::fromString($templateString);
|
|
||||||
if (!$data) {
|
|
||||||
$data = new TestFixture();
|
|
||||||
}
|
|
||||||
return $t->process($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testXhtmltagReplacement()
|
public function testXhtmltagReplacement()
|
||||||
{
|
{
|
||||||
$tmpl1 = '<?xml version="1.0" encoding="UTF-8"?>
|
$baseTag = SSViewer::getBaseTag(true);
|
||||||
|
$renderedOutput = '<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
|
||||||
. ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
. ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
<html>
|
<html>
|
||||||
<head><% base_tag %></head>
|
<head>' . $baseTag . '</head>
|
||||||
<body>
|
<body>
|
||||||
<form action="#">
|
<form action="#">
|
||||||
<select>
|
<select>
|
||||||
@ -53,8 +39,7 @@ class ContentNegotiatorTest extends SapphireTest
|
|||||||
|
|
||||||
// Check that the content negotiator converts to the equally legal formats
|
// Check that the content negotiator converts to the equally legal formats
|
||||||
$negotiator = new ContentNegotiator();
|
$negotiator = new ContentNegotiator();
|
||||||
|
$response = new HTTPResponse($renderedOutput);
|
||||||
$response = new HTTPResponse($this->render($tmpl1));
|
|
||||||
$negotiator->xhtml($response);
|
$negotiator->xhtml($response);
|
||||||
|
|
||||||
////////////////////////
|
////////////////////////
|
||||||
|
@ -3,8 +3,9 @@
|
|||||||
namespace SilverStripe\View\Tests\Embed;
|
namespace SilverStripe\View\Tests\Embed;
|
||||||
|
|
||||||
use Psr\Http\Message\UriInterface;
|
use Psr\Http\Message\UriInterface;
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
class MockUri implements UriInterface
|
class MockUri implements UriInterface, Stringable
|
||||||
{
|
{
|
||||||
private string $scheme;
|
private string $scheme;
|
||||||
private string $host;
|
private string $host;
|
||||||
@ -91,7 +92,7 @@ class MockUri implements UriInterface
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
$query = $this->getQuery();
|
$query = $this->getQuery();
|
||||||
return sprintf(
|
return sprintf(
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace SilverStripe\View\Tests;
|
namespace SilverStripe\View\Tests;
|
||||||
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
use SilverStripe\Core\Injector\Injector;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
@ -14,13 +13,12 @@ use SilverStripe\View\Requirements_Backend;
|
|||||||
use SilverStripe\Core\Manifest\ResourceURLGenerator;
|
use SilverStripe\Core\Manifest\ResourceURLGenerator;
|
||||||
use SilverStripe\Control\SimpleResourceURLGenerator;
|
use SilverStripe\Control\SimpleResourceURLGenerator;
|
||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Dev\Deprecation;
|
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
use SilverStripe\View\ThemeResourceLoader;
|
use SilverStripe\View\ThemeResourceLoader;
|
||||||
|
use Symfony\Component\Filesystem\Path;
|
||||||
|
|
||||||
class RequirementsTest extends SapphireTest
|
class RequirementsTest extends SapphireTest
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ThemeResourceLoader
|
* @var ThemeResourceLoader
|
||||||
*/
|
*/
|
||||||
@ -31,9 +29,8 @@ class RequirementsTest extends SapphireTest
|
|||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
Director::config()->set('alternate_base_folder', __DIR__ . '/SSViewerTest');
|
Director::config()->set('alternate_base_folder', __DIR__ . '/RequirementsTest');
|
||||||
Director::config()->set('alternate_base_url', 'http://www.mysite.com/basedir/');
|
Director::config()->set('alternate_base_url', 'http://www.mysite.com/basedir/');
|
||||||
Director::config()->set('alternate_public_dir', 'public'); // Enforce public dir
|
|
||||||
// Add public as a theme in itself
|
// Add public as a theme in itself
|
||||||
SSViewer::set_themes([SSViewer::PUBLIC_THEME, SSViewer::DEFAULT_THEME]);
|
SSViewer::set_themes([SSViewer::PUBLIC_THEME, SSViewer::DEFAULT_THEME]);
|
||||||
TestAssetStore::activate('RequirementsTest'); // Set backend root to /RequirementsTest
|
TestAssetStore::activate('RequirementsTest'); // Set backend root to /RequirementsTest
|
||||||
@ -959,12 +956,12 @@ class RequirementsTest extends SapphireTest
|
|||||||
|
|
||||||
public function testConditionalTemplateRequire()
|
public function testConditionalTemplateRequire()
|
||||||
{
|
{
|
||||||
// Set /SSViewerTest and /SSViewerTest/public as themes
|
// Set /RequirementsTest and /RequirementsTest/public as themes
|
||||||
SSViewer::set_themes([
|
SSViewer::set_themes([
|
||||||
'/',
|
'/',
|
||||||
SSViewer::PUBLIC_THEME
|
SSViewer::PUBLIC_THEME
|
||||||
]);
|
]);
|
||||||
ThemeResourceLoader::set_instance(new ThemeResourceLoader(__DIR__ . '/SSViewerTest'));
|
ThemeResourceLoader::set_instance(new ThemeResourceLoader(__DIR__ . '/RequirementsTest'));
|
||||||
|
|
||||||
/** @var Requirements_Backend $backend */
|
/** @var Requirements_Backend $backend */
|
||||||
$backend = Injector::inst()->create(Requirements_Backend::class);
|
$backend = Injector::inst()->create(Requirements_Backend::class);
|
||||||
@ -1501,4 +1498,35 @@ EOS
|
|||||||
'Head Tag is correctly not displaying original write'
|
'Head Tag is correctly not displaying original write'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRequirementsCombine()
|
||||||
|
{
|
||||||
|
// Need to reset base folder for this test, which requires also resetting the asset store.
|
||||||
|
Director::config()->remove('alternate_base_folder');
|
||||||
|
TestAssetStore::reset();
|
||||||
|
TestAssetStore::activate('RequirementsTest');
|
||||||
|
/** @var Requirements_Backend $testBackend */
|
||||||
|
$testBackend = Injector::inst()->create(Requirements_Backend::class);
|
||||||
|
$testBackend->setSuffixRequirements(false);
|
||||||
|
$testBackend->setCombinedFilesEnabled(true);
|
||||||
|
|
||||||
|
//$combinedTestFilePath = BASE_PATH . '/' . $testBackend->getCombinedFilesFolder() . '/testRequirementsCombine.js';
|
||||||
|
|
||||||
|
$jsFile = Path::makeAbsolute($this->getCurrentRelativePath() . '/RequirementsTest/javascript/bad.js', BASE_PATH);
|
||||||
|
$jsFileContents = file_get_contents($jsFile);
|
||||||
|
$testBackend->combineFiles('testRequirementsCombine.js', [$jsFile]);
|
||||||
|
|
||||||
|
// secondly, make sure that requirements is generated, even though minification failed
|
||||||
|
$testBackend->processCombinedFiles();
|
||||||
|
$js = array_keys($testBackend->getJavascript() ?? []);
|
||||||
|
$combinedTestFilePath = Path::join(Director::publicFolder(), reset($js));
|
||||||
|
$this->assertStringContainsString('_combinedfiles/testRequirementsCombine-4c0e97a.js', $combinedTestFilePath);
|
||||||
|
|
||||||
|
// and make sure the combined content matches the input content, i.e. no loss of functionality
|
||||||
|
if (!file_exists($combinedTestFilePath ?? '')) {
|
||||||
|
$this->fail('No combined file was created at expected path: ' . $combinedTestFilePath);
|
||||||
|
}
|
||||||
|
$combinedTestFileContents = file_get_contents($combinedTestFilePath ?? '');
|
||||||
|
$this->assertStringContainsString($jsFileContents, $combinedTestFileContents);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,18 +9,19 @@ use SilverStripe\Versioned\Versioned;
|
|||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
|
use SilverStripe\View\SSTemplateEngine;
|
||||||
use SilverStripe\View\SSTemplateParseException;
|
use SilverStripe\View\SSTemplateParseException;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\ViewLayerData;
|
||||||
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||||
use Symfony\Component\Cache\Adapter\NullAdapter;
|
use Symfony\Component\Cache\Adapter\NullAdapter;
|
||||||
use Symfony\Component\Cache\Psr16Cache;
|
use Symfony\Component\Cache\Psr16Cache;
|
||||||
|
|
||||||
// Not actually a data object, we just want a ModelData object that's just for us
|
// Not actually a data object, we just want a ModelData object that's just for us
|
||||||
|
|
||||||
class SSViewerCacheBlockTest extends SapphireTest
|
class SSTemplateEngineCacheBlockTest extends SapphireTest
|
||||||
{
|
{
|
||||||
protected static $extra_dataobjects = [
|
protected static $extra_dataobjects = [
|
||||||
SSViewerCacheBlockTest\TestModel::class
|
SSTemplateEngineCacheBlockTest\TestModel::class
|
||||||
];
|
];
|
||||||
|
|
||||||
public static function getExtraDataObjects()
|
public static function getExtraDataObjects()
|
||||||
@ -29,19 +30,19 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
|
|
||||||
// Add extra classes if versioning is enabled
|
// Add extra classes if versioning is enabled
|
||||||
if (class_exists(Versioned::class)) {
|
if (class_exists(Versioned::class)) {
|
||||||
$classes[] = SSViewerCacheBlockTest\VersionedModel::class;
|
$classes[] = SSTemplateEngineCacheBlockTest\VersionedModel::class;
|
||||||
}
|
}
|
||||||
return $classes;
|
return $classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var SSViewerCacheBlockTest\TestModel
|
* @var SSTemplateEngineCacheBlockTest\TestModel
|
||||||
*/
|
*/
|
||||||
protected $data = null;
|
protected $data = null;
|
||||||
|
|
||||||
protected function _reset($cacheOn = true)
|
protected function _reset($cacheOn = true)
|
||||||
{
|
{
|
||||||
$this->data = new SSViewerCacheBlockTest\TestModel();
|
$this->data = new SSTemplateEngineCacheBlockTest\TestModel();
|
||||||
|
|
||||||
$cache = null;
|
$cache = null;
|
||||||
if ($cacheOn) {
|
if ($cacheOn) {
|
||||||
@ -64,7 +65,8 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
$data = $this->data->customise($data);
|
$data = $this->data->customise($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return SSViewer::execute_string($template, $data);
|
$engine = new SSTemplateEngine();
|
||||||
|
return $engine->renderString($template, new ViewLayerData($data));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testParsing()
|
public function testParsing()
|
||||||
@ -74,52 +76,52 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
|
|
||||||
// Make sure an empty cached block parses
|
// Make sure an empty cached block parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %><% end_cached %>'), '');
|
$this->assertEquals('', $this->_runtemplate('<% cached %><% end_cached %>'));
|
||||||
|
|
||||||
// Make sure an empty cacheblock block parses
|
// Make sure an empty cacheblock block parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals($this->_runtemplate('<% cacheblock %><% end_cacheblock %>'), '');
|
$this->assertEquals('', $this->_runtemplate('<% cacheblock %><% end_cacheblock %>'));
|
||||||
|
|
||||||
// Make sure an empty uncached block parses
|
// Make sure an empty uncached block parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals($this->_runtemplate('<% uncached %><% end_uncached %>'), '');
|
$this->assertEquals('', $this->_runtemplate('<% uncached %><% end_uncached %>'));
|
||||||
|
|
||||||
// ** Argument checks **
|
// ** Argument checks **
|
||||||
|
|
||||||
// Make sure a simple cacheblock parses
|
// Make sure a simple cacheblock parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %>Yay<% end_cached %>'), 'Yay');
|
$this->assertEquals('Yay', $this->_runtemplate('<% cached %>Yay<% end_cached %>'));
|
||||||
|
|
||||||
// Make sure a moderately complicated cacheblock parses
|
// Make sure a moderately complicated cacheblock parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals($this->_runtemplate('<% cached \'block\', Foo, "jumping" %>Yay<% end_cached %>'), 'Yay');
|
$this->assertEquals('Yay', $this->_runtemplate('<% cached \'block\', Foo, "jumping" %>Yay<% end_cached %>'));
|
||||||
|
|
||||||
// Make sure a complicated cacheblock parses
|
// Make sure a complicated cacheblock parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
'Yay',
|
||||||
$this->_runtemplate(
|
$this->_runtemplate(
|
||||||
'<% cached \'block\', Foo, Test.Test(4).Test(jumping).Foo %>Yay<% end_cached %>'
|
'<% cached \'block\', Foo, Test.Test(4).Test(jumping).Foo %>Yay<% end_cached %>'
|
||||||
),
|
)
|
||||||
'Yay'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// ** Conditional Checks **
|
// ** Conditional Checks **
|
||||||
|
|
||||||
// Make sure a cacheblock with a simple conditional parses
|
// Make sure a cacheblock with a simple conditional parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals($this->_runtemplate('<% cached if true %>Yay<% end_cached %>'), 'Yay');
|
$this->assertEquals('Yay', $this->_runtemplate('<% cached if true %>Yay<% end_cached %>'));
|
||||||
|
|
||||||
// Make sure a cacheblock with a complex conditional parses
|
// Make sure a cacheblock with a complex conditional parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals($this->_runtemplate('<% cached if Test.Test(yank).Foo %>Yay<% end_cached %>'), 'Yay');
|
$this->assertEquals('Yay', $this->_runtemplate('<% cached if Test.Test(yank).Foo %>Yay<% end_cached %>'));
|
||||||
|
|
||||||
// Make sure a cacheblock with a complex conditional and arguments parses
|
// Make sure a cacheblock with a complex conditional and arguments parses
|
||||||
$this->_reset();
|
$this->_reset();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
'Yay',
|
||||||
$this->_runtemplate(
|
$this->_runtemplate(
|
||||||
'<% cached Foo, Test.Test(4).Test(jumping).Foo if Test.Test(yank).Foo %>Yay<% end_cached %>'
|
'<% cached Foo, Test.Test(4).Test(jumping).Foo if Test.Test(yank).Foo %>Yay<% end_cached %>'
|
||||||
),
|
)
|
||||||
'Yay'
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,14 +133,14 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
// First, run twice without caching, to prove we get two different values
|
// First, run twice without caching, to prove we get two different values
|
||||||
$this->_reset(false);
|
$this->_reset(false);
|
||||||
|
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 1]), '1');
|
$this->assertEquals('1', $this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 1]));
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 2]), '2');
|
$this->assertEquals('2', $this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 2]));
|
||||||
|
|
||||||
// Then twice with caching, should get same result each time
|
// Then twice with caching, should get same result each time
|
||||||
$this->_reset(true);
|
$this->_reset(true);
|
||||||
|
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 1]), '1');
|
$this->assertEquals('1', $this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 1]));
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 2]), '1');
|
$this->assertEquals('1', $this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -150,17 +152,17 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
$this->_reset(true);
|
$this->_reset(true);
|
||||||
|
|
||||||
// Generate cached value for foo = 1
|
// Generate cached value for foo = 1
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 1]), '1');
|
$this->assertEquals('1', $this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 1]));
|
||||||
|
|
||||||
// Test without flush
|
// Test without flush
|
||||||
Injector::inst()->get(Kernel::class)->boot();
|
Injector::inst()->get(Kernel::class)->boot();
|
||||||
Director::test('/');
|
Director::test('/');
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 3]), '1');
|
$this->assertEquals('1', $this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 3]));
|
||||||
|
|
||||||
// Test with flush
|
// Test with flush
|
||||||
Injector::inst()->get(Kernel::class)->boot(true);
|
Injector::inst()->get(Kernel::class)->boot(true);
|
||||||
Director::test('/?flush=1');
|
Director::test('/?flush=1');
|
||||||
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 2]), '2');
|
$this->assertEquals('2', $this->_runtemplate('<% cached %>$Foo<% end_cached %>', ['Foo' => 2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testVersionedCache()
|
public function testVersionedCache()
|
||||||
@ -173,29 +175,29 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
// Run without caching in stage to prove data is uncached
|
// Run without caching in stage to prove data is uncached
|
||||||
$this->_reset(false);
|
$this->_reset(false);
|
||||||
Versioned::set_stage(Versioned::DRAFT);
|
Versioned::set_stage(Versioned::DRAFT);
|
||||||
$data = new SSViewerCacheBlockTest\VersionedModel();
|
$data = new SSTemplateEngineCacheBlockTest\VersionedModel();
|
||||||
$data->setEntropy('default');
|
$data->setEntropy('default');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'default Stage.Stage',
|
'default Stage.Stage',
|
||||||
SSViewer::execute_string('<% cached %>$Inspect<% end_cached %>', $data)
|
$this->_runtemplate('<% cached %>$Inspect<% end_cached %>', $data)
|
||||||
);
|
);
|
||||||
$data = new SSViewerCacheBlockTest\VersionedModel();
|
$data = new SSTemplateEngineCacheBlockTest\VersionedModel();
|
||||||
$data->setEntropy('first');
|
$data->setEntropy('first');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'first Stage.Stage',
|
'first Stage.Stage',
|
||||||
SSViewer::execute_string('<% cached %>$Inspect<% end_cached %>', $data)
|
$this->_runtemplate('<% cached %>$Inspect<% end_cached %>', $data)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Run without caching in live to prove data is uncached
|
// Run without caching in live to prove data is uncached
|
||||||
$this->_reset(false);
|
$this->_reset(false);
|
||||||
Versioned::set_stage(Versioned::LIVE);
|
Versioned::set_stage(Versioned::LIVE);
|
||||||
$data = new SSViewerCacheBlockTest\VersionedModel();
|
$data = new SSTemplateEngineCacheBlockTest\VersionedModel();
|
||||||
$data->setEntropy('default');
|
$data->setEntropy('default');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'default Stage.Live',
|
'default Stage.Live',
|
||||||
$this->_runtemplate('<% cached %>$Inspect<% end_cached %>', $data)
|
$this->_runtemplate('<% cached %>$Inspect<% end_cached %>', $data)
|
||||||
);
|
);
|
||||||
$data = new SSViewerCacheBlockTest\VersionedModel();
|
$data = new SSTemplateEngineCacheBlockTest\VersionedModel();
|
||||||
$data->setEntropy('first');
|
$data->setEntropy('first');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'first Stage.Live',
|
'first Stage.Live',
|
||||||
@ -207,13 +209,13 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
// within them
|
// within them
|
||||||
$this->_reset(true);
|
$this->_reset(true);
|
||||||
Versioned::set_stage(Versioned::DRAFT);
|
Versioned::set_stage(Versioned::DRAFT);
|
||||||
$data = new SSViewerCacheBlockTest\VersionedModel();
|
$data = new SSTemplateEngineCacheBlockTest\VersionedModel();
|
||||||
$data->setEntropy('default');
|
$data->setEntropy('default');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'default Stage.Stage',
|
'default Stage.Stage',
|
||||||
$this->_runtemplate('<% cached %>$Inspect<% end_cached %>', $data)
|
$this->_runtemplate('<% cached %>$Inspect<% end_cached %>', $data)
|
||||||
);
|
);
|
||||||
$data = new SSViewerCacheBlockTest\VersionedModel();
|
$data = new SSTemplateEngineCacheBlockTest\VersionedModel();
|
||||||
$data->setEntropy('first');
|
$data->setEntropy('first');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'default Stage.Stage', // entropy should be ignored due to caching
|
'default Stage.Stage', // entropy should be ignored due to caching
|
||||||
@ -221,13 +223,13 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
);
|
);
|
||||||
|
|
||||||
Versioned::set_stage(Versioned::LIVE);
|
Versioned::set_stage(Versioned::LIVE);
|
||||||
$data = new SSViewerCacheBlockTest\VersionedModel();
|
$data = new SSTemplateEngineCacheBlockTest\VersionedModel();
|
||||||
$data->setEntropy('first');
|
$data->setEntropy('first');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'first Stage.Live', // First hit in live, so display current entropy
|
'first Stage.Live', // First hit in live, so display current entropy
|
||||||
$this->_runtemplate('<% cached %>$Inspect<% end_cached %>', $data)
|
$this->_runtemplate('<% cached %>$Inspect<% end_cached %>', $data)
|
||||||
);
|
);
|
||||||
$data = new SSViewerCacheBlockTest\VersionedModel();
|
$data = new SSTemplateEngineCacheBlockTest\VersionedModel();
|
||||||
$data->setEntropy('second');
|
$data->setEntropy('second');
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
'first Stage.Live', // entropy should be ignored due to caching
|
'first Stage.Live', // entropy should be ignored due to caching
|
||||||
@ -245,48 +247,48 @@ class SSViewerCacheBlockTest extends SapphireTest
|
|||||||
// First, run twice with caching
|
// First, run twice with caching
|
||||||
$this->_reset(true);
|
$this->_reset(true);
|
||||||
|
|
||||||
$this->assertEquals($this->_runtemplate('<% cached if True %>$Foo<% end_cached %>', ['Foo' => 1]), '1');
|
$this->assertEquals('1', $this->_runtemplate('<% cached if True %>$Foo<% end_cached %>', ['Foo' => 1]));
|
||||||
$this->assertEquals($this->_runtemplate('<% cached if True %>$Foo<% end_cached %>', ['Foo' => 2]), '1');
|
$this->assertEquals('1', $this->_runtemplate('<% cached if True %>$Foo<% end_cached %>', ['Foo' => 2]));
|
||||||
|
|
||||||
// Then twice without caching
|
// Then twice without caching
|
||||||
$this->_reset(true);
|
$this->_reset(true);
|
||||||
|
|
||||||
$this->assertEquals($this->_runtemplate('<% cached if False %>$Foo<% end_cached %>', ['Foo' => 1]), '1');
|
$this->assertEquals('1', $this->_runtemplate('<% cached if False %>$Foo<% end_cached %>', ['Foo' => 1]));
|
||||||
$this->assertEquals($this->_runtemplate('<% cached if False %>$Foo<% end_cached %>', ['Foo' => 2]), '2');
|
$this->assertEquals('2', $this->_runtemplate('<% cached if False %>$Foo<% end_cached %>', ['Foo' => 2]));
|
||||||
|
|
||||||
// Then once cached, once not (and the opposite)
|
// Then once cached, once not (and the opposite)
|
||||||
$this->_reset(true);
|
$this->_reset(true);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
'1',
|
||||||
$this->_runtemplate(
|
$this->_runtemplate(
|
||||||
'<% cached if Cache %>$Foo<% end_cached %>',
|
'<% cached if Cache %>$Foo<% end_cached %>',
|
||||||
['Foo' => 1, 'Cache' => true ]
|
['Foo' => 1, 'Cache' => true ]
|
||||||
),
|
)
|
||||||
'1'
|
|
||||||
);
|
);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
'2',
|
||||||
$this->_runtemplate(
|
$this->_runtemplate(
|
||||||
'<% cached if Cache %>$Foo<% end_cached %>',
|
'<% cached if Cache %>$Foo<% end_cached %>',
|
||||||
['Foo' => 2, 'Cache' => false]
|
['Foo' => 2, 'Cache' => false]
|
||||||
),
|
)
|
||||||
'2'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->_reset(true);
|
$this->_reset(true);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
'1',
|
||||||
$this->_runtemplate(
|
$this->_runtemplate(
|
||||||
'<% cached if Cache %>$Foo<% end_cached %>',
|
'<% cached if Cache %>$Foo<% end_cached %>',
|
||||||
['Foo' => 1, 'Cache' => false]
|
['Foo' => 1, 'Cache' => false]
|
||||||
),
|
)
|
||||||
'1'
|
|
||||||
);
|
);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
|
'2',
|
||||||
$this->_runtemplate(
|
$this->_runtemplate(
|
||||||
'<% cached if Cache %>$Foo<% end_cached %>',
|
'<% cached if Cache %>$Foo<% end_cached %>',
|
||||||
['Foo' => 2, 'Cache' => true ]
|
['Foo' => 2, 'Cache' => true ]
|
||||||
),
|
)
|
||||||
'2'
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace SilverStripe\View\Tests\SSViewerCacheBlockTest;
|
namespace SilverStripe\View\Tests\SSTemplateEngineCacheBlockTest;
|
||||||
|
|
||||||
use SilverStripe\Dev\TestOnly;
|
use SilverStripe\Dev\TestOnly;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
class TestModel extends DataObject implements TestOnly
|
class TestModel extends DataObject implements TestOnly
|
||||||
{
|
{
|
||||||
private static $table_name = 'SSViewerCacheBlockTest_Model';
|
private static $table_name = 'SSTemplateEngineCacheBlockTest_Model';
|
||||||
|
|
||||||
public function Test($arg = null)
|
public function Test($arg = null)
|
||||||
{
|
{
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace SilverStripe\View\Tests\SSViewerCacheBlockTest;
|
namespace SilverStripe\View\Tests\SSTemplateEngineCacheBlockTest;
|
||||||
|
|
||||||
use SilverStripe\Dev\TestOnly;
|
use SilverStripe\Dev\TestOnly;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
@ -8,7 +8,7 @@ use SilverStripe\Versioned\Versioned;
|
|||||||
|
|
||||||
class VersionedModel extends DataObject implements TestOnly
|
class VersionedModel extends DataObject implements TestOnly
|
||||||
{
|
{
|
||||||
private static $table_name = 'SSViewerCacheBlockTest_VersionedModel';
|
private static $table_name = 'SSTemplateEngineCacheBlockTest_VersionedModel';
|
||||||
|
|
||||||
protected $entropy = 'default';
|
protected $entropy = 'default';
|
||||||
|
|
321
tests/php/View/SSTemplateEngineFindTemplateTest.php
Normal file
321
tests/php/View/SSTemplateEngineFindTemplateTest.php
Normal file
@ -0,0 +1,321 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests;
|
||||||
|
|
||||||
|
use SilverStripe\Control\Director;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
use ReflectionMethod;
|
||||||
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
||||||
|
use SilverStripe\Core\Manifest\ModuleManifest;
|
||||||
|
use SilverStripe\View\SSTemplateEngine;
|
||||||
|
use SilverStripe\View\ThemeManifest;
|
||||||
|
use SilverStripe\View\ThemeResourceLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for SSTemplateEngine::findTemplate().
|
||||||
|
* These have been separated out from SSTemplateEngineTest because of the extreme setup requirements.
|
||||||
|
*/
|
||||||
|
class SSTemplateEngineFindTemplateTest extends SapphireTest
|
||||||
|
{
|
||||||
|
private string $base;
|
||||||
|
|
||||||
|
private ThemeResourceLoader $origLoader;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
// Fake project root
|
||||||
|
$this->base = dirname(__FILE__) . '/SSTemplateEngineTest_findTemplate';
|
||||||
|
Director::config()->set('alternate_base_folder', $this->base);
|
||||||
|
ModuleManifest::config()->set('module_priority', ['$project', '$other_modules']);
|
||||||
|
ModuleManifest::config()->set('project', 'myproject');
|
||||||
|
|
||||||
|
$moduleManifest = new ModuleManifest($this->base);
|
||||||
|
$moduleManifest->init();
|
||||||
|
$moduleManifest->sort();
|
||||||
|
ModuleLoader::inst()->pushManifest($moduleManifest);
|
||||||
|
|
||||||
|
// New ThemeManifest for that root
|
||||||
|
$themeManifest = new ThemeManifest($this->base);
|
||||||
|
$themeManifest->setProject('myproject');
|
||||||
|
$themeManifest->init();
|
||||||
|
// New Loader for that root
|
||||||
|
$this->origLoader = ThemeResourceLoader::inst();
|
||||||
|
$themeResourceLoader = new ThemeResourceLoader($this->base);
|
||||||
|
$themeResourceLoader->addSet('$default', $themeManifest);
|
||||||
|
ThemeResourceLoader::set_instance($themeResourceLoader);
|
||||||
|
|
||||||
|
// Ensure the cache is flushed between tests
|
||||||
|
ThemeResourceLoader::flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
ThemeResourceLoader::set_instance($this->origLoader);
|
||||||
|
ModuleLoader::inst()->popManifest();
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that 'main' and 'Layout' templates are loaded from module
|
||||||
|
*/
|
||||||
|
public function testFindTemplatesInModule()
|
||||||
|
{
|
||||||
|
$base = ThemeResourceLoader::inst()->getBase();
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/module/templates/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, 'Page', ['$default'])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/module/templates/Layout/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, ['type' => 'Layout', 'Page'], ['$default'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindNestedThemeTemplates()
|
||||||
|
{
|
||||||
|
$base = ThemeResourceLoader::inst()->getBase();
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
// Without including the theme this template cannot be found
|
||||||
|
$this->assertEquals(null, $reflectionFindTemplate->invoke($engine, 'NestedThemePage', ['$default']));
|
||||||
|
|
||||||
|
// With a nested theme available then it is available
|
||||||
|
$this->assertEquals(
|
||||||
|
"{$base}/module/themes/subtheme/templates/NestedThemePage.ss",
|
||||||
|
$reflectionFindTemplate->invoke(
|
||||||
|
$engine,
|
||||||
|
'NestedThemePage',
|
||||||
|
[
|
||||||
|
'silverstripe/module:subtheme',
|
||||||
|
'$default'
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Can also be found if excluding $default theme
|
||||||
|
$this->assertEquals(
|
||||||
|
"{$base}/module/themes/subtheme/templates/NestedThemePage.ss",
|
||||||
|
$reflectionFindTemplate->invoke(
|
||||||
|
$engine,
|
||||||
|
'NestedThemePage',
|
||||||
|
[
|
||||||
|
'silverstripe/module:subtheme',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindTemplateByType()
|
||||||
|
{
|
||||||
|
$base = ThemeResourceLoader::inst()->getBase();
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
// Test that "type" is respected properly
|
||||||
|
$this->assertEquals(
|
||||||
|
"{$base}/module/templates/MyNamespace/Layout/MyClass.ss",
|
||||||
|
$reflectionFindTemplate->invoke(
|
||||||
|
$engine,
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'type' => 'Layout',
|
||||||
|
'MyNamespace/NonExistantTemplate'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'Layout',
|
||||||
|
'MyNamespace/MyClass'
|
||||||
|
],
|
||||||
|
'MyNamespace/MyClass'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'silverstripe/module:subtheme',
|
||||||
|
'theme',
|
||||||
|
'$default',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Non-typed template can be found even if looking for typed theme at a lower priority
|
||||||
|
$this->assertEquals(
|
||||||
|
"{$base}/module/templates/MyNamespace/MyClass.ss",
|
||||||
|
$reflectionFindTemplate->invoke(
|
||||||
|
$engine,
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'type' => 'Layout',
|
||||||
|
'MyNamespace/NonExistantTemplate'
|
||||||
|
],
|
||||||
|
'MyNamespace/MyClass',
|
||||||
|
[
|
||||||
|
'type' => 'Layout',
|
||||||
|
'MyNamespace/MyClass'
|
||||||
|
]
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'silverstripe/module',
|
||||||
|
'theme',
|
||||||
|
'$default',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindTemplatesByPath()
|
||||||
|
{
|
||||||
|
$base = ThemeResourceLoader::inst()->getBase();
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
// Items given as full paths are returned directly
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/themes/theme/templates/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, "$base/themes/theme/templates/Page.ss", ['theme'])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/themes/theme/templates/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke(
|
||||||
|
$engine,
|
||||||
|
[
|
||||||
|
"$base/themes/theme/templates/Page.ss",
|
||||||
|
"Page"
|
||||||
|
],
|
||||||
|
['theme']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Ensure checks for file_exists
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/themes/theme/templates/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke(
|
||||||
|
$engine,
|
||||||
|
[
|
||||||
|
"$base/themes/theme/templates/NotAPage.ss",
|
||||||
|
"$base/themes/theme/templates/Page.ss",
|
||||||
|
],
|
||||||
|
['theme']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that 'main' and 'Layout' templates are loaded from set theme
|
||||||
|
*/
|
||||||
|
public function testFindTemplatesInTheme()
|
||||||
|
{
|
||||||
|
$base = ThemeResourceLoader::inst()->getBase();
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/themes/theme/templates/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, 'Page', ['theme'])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/themes/theme/templates/Layout/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, ['type' => 'Layout', 'Page'], ['theme'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that 'main' and 'Layout' templates are loaded from project without a set theme
|
||||||
|
*/
|
||||||
|
public function testFindTemplatesInApplication()
|
||||||
|
{
|
||||||
|
$base = ThemeResourceLoader::inst()->getBase();
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
$templates = [
|
||||||
|
$base . '/myproject/templates/Page.ss',
|
||||||
|
$base . '/myproject/templates/Layout/Page.ss'
|
||||||
|
];
|
||||||
|
foreach ($templates as $template) {
|
||||||
|
file_put_contents($template, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/myproject/templates/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, 'Page', ['$default'])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/myproject/templates/Layout/Page.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, ['type' => 'Layout', 'Page'], ['$default'])
|
||||||
|
);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
foreach ($templates as $template) {
|
||||||
|
unlink($template);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that 'main' template is found in theme and 'Layout' is found in module
|
||||||
|
*/
|
||||||
|
public function testFindTemplatesMainThemeLayoutModule()
|
||||||
|
{
|
||||||
|
$base = ThemeResourceLoader::inst()->getBase();
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/themes/theme/templates/CustomThemePage.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, 'CustomThemePage', ['theme', '$default'])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"$base/module/templates/Layout/CustomThemePage.ss",
|
||||||
|
$reflectionFindTemplate->invoke($engine, ['type' => 'Layout', 'CustomThemePage'], ['theme', '$default'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindTemplateWithCacheMiss()
|
||||||
|
{
|
||||||
|
$mockCache = $this->createMock(CacheInterface::class);
|
||||||
|
$mockCache->expects($this->once())->method('has')->willReturn(false);
|
||||||
|
$mockCache->expects($this->never())->method('get');
|
||||||
|
$mockCache->expects($this->once())->method('set');
|
||||||
|
ThemeResourceLoader::inst()->setCache($mockCache);
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
$reflectionFindTemplate->invoke($engine, 'Page', ['$default']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFindTemplateWithCacheHit()
|
||||||
|
{
|
||||||
|
$mockCache = $this->createMock(CacheInterface::class);
|
||||||
|
$mockCache->expects($this->once())->method('has')->willReturn(true);
|
||||||
|
$mockCache->expects($this->never())->method('set');
|
||||||
|
$mockCache->expects($this->once())->method('get')->willReturn('mock_template.ss');
|
||||||
|
ThemeResourceLoader::inst()->setCache($mockCache);
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
$reflectionFindTemplate = new ReflectionMethod($engine, 'findTemplate');
|
||||||
|
$reflectionFindTemplate->setAccessible(true);
|
||||||
|
|
||||||
|
$result = $reflectionFindTemplate->invoke($engine, 'Page', ['$default']);
|
||||||
|
$this->assertSame('mock_template.ss', $result);
|
||||||
|
}
|
||||||
|
}
|
2279
tests/php/View/SSTemplateEngineTest.php
Normal file
2279
tests/php/View/SSTemplateEngineTest.php
Normal file
@ -0,0 +1,2279 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests;
|
||||||
|
|
||||||
|
use LogicException;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
use Silverstripe\Assets\Dev\TestAssetStore;
|
||||||
|
use SilverStripe\Control\ContentNegotiator;
|
||||||
|
use SilverStripe\Control\Director;
|
||||||
|
use SilverStripe\Control\HTTPResponse;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\i18n\i18n;
|
||||||
|
use SilverStripe\Model\List\ArrayList;
|
||||||
|
use SilverStripe\Model\List\PaginatedList;
|
||||||
|
use SilverStripe\Security\Permission;
|
||||||
|
use SilverStripe\Security\Security;
|
||||||
|
use SilverStripe\Security\SecurityToken;
|
||||||
|
use SilverStripe\Model\ArrayData;
|
||||||
|
use SilverStripe\View\Requirements;
|
||||||
|
use SilverStripe\View\Requirements_Backend;
|
||||||
|
use SilverStripe\View\SSTemplateParseException;
|
||||||
|
use SilverStripe\View\SSTemplateParser;
|
||||||
|
use SilverStripe\View\SSViewer;
|
||||||
|
use SilverStripe\View\Tests\SSTemplateEngineTest\TestModelData;
|
||||||
|
use SilverStripe\Model\ModelData;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
|
||||||
|
use SilverStripe\View\Exception\MissingTemplateException;
|
||||||
|
use SilverStripe\View\SSTemplateEngine;
|
||||||
|
use SilverStripe\View\ViewLayerData;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
class SSTemplateEngineTest extends SapphireTest
|
||||||
|
{
|
||||||
|
protected static $extra_dataobjects = [
|
||||||
|
SSTemplateEngineTest\TestObject::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $usesDatabase = true;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
SSViewer::config()->set('source_file_comments', false);
|
||||||
|
TestAssetStore::activate('SSTemplateEngineTest');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
TestAssetStore::reset();
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that a template without a <head> tag still renders.
|
||||||
|
*/
|
||||||
|
public function testTemplateWithoutHeadRenders()
|
||||||
|
{
|
||||||
|
$data = new ArrayData([ 'Var' => 'var value' ]);
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestPartialTemplate');
|
||||||
|
$result = $engine->render(new ViewLayerData($data));
|
||||||
|
$this->assertEquals('Test partial template: var value', trim(preg_replace("/<!--.*-->/U", '', $result ?? '') ?? ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure global methods aren't executed
|
||||||
|
*/
|
||||||
|
public function testTemplateExecution()
|
||||||
|
{
|
||||||
|
$data = new ArrayData([ 'Var' => 'phpinfo' ]);
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestPartialTemplate');
|
||||||
|
$result = $engine->render(new ViewLayerData($data));
|
||||||
|
$this->assertEquals('Test partial template: phpinfo', trim(preg_replace("/<!--.*-->/U", '', $result ?? '') ?? ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIncludeScopeInheritance()
|
||||||
|
{
|
||||||
|
$data = $this->getScopeInheritanceTestData();
|
||||||
|
$expected = [
|
||||||
|
'Item 1 - First-ODD top:Item 1',
|
||||||
|
'Item 2 - EVEN top:Item 2',
|
||||||
|
'Item 3 - ODD top:Item 3',
|
||||||
|
'Item 4 - EVEN top:Item 4',
|
||||||
|
'Item 5 - ODD top:Item 5',
|
||||||
|
'Item 6 - Last-EVEN top:Item 6',
|
||||||
|
];
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestIncludeScopeInheritance');
|
||||||
|
$result = $engine->render(new ViewLayerData($data));
|
||||||
|
$this->assertExpectedStrings($result, $expected);
|
||||||
|
|
||||||
|
// reset results for the tests that include arguments (the title is passed as an arg)
|
||||||
|
$expected = [
|
||||||
|
'Item 1 _ Item 1 - First-ODD top:Item 1',
|
||||||
|
'Item 2 _ Item 2 - EVEN top:Item 2',
|
||||||
|
'Item 3 _ Item 3 - ODD top:Item 3',
|
||||||
|
'Item 4 _ Item 4 - EVEN top:Item 4',
|
||||||
|
'Item 5 _ Item 5 - ODD top:Item 5',
|
||||||
|
'Item 6 _ Item 6 - Last-EVEN top:Item 6',
|
||||||
|
];
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestIncludeScopeInheritanceWithArgs');
|
||||||
|
$result = $engine->render(new ViewLayerData($data));
|
||||||
|
$this->assertExpectedStrings($result, $expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIncludeTruthyness()
|
||||||
|
{
|
||||||
|
$data = new ArrayData([
|
||||||
|
'Title' => 'TruthyTest',
|
||||||
|
'Items' => new ArrayList([
|
||||||
|
new ArrayData(['Title' => 'Item 1']),
|
||||||
|
new ArrayData(['Title' => '']),
|
||||||
|
new ArrayData(['Title' => true]),
|
||||||
|
new ArrayData(['Title' => false]),
|
||||||
|
new ArrayData(['Title' => null]),
|
||||||
|
new ArrayData(['Title' => 0]),
|
||||||
|
new ArrayData(['Title' => 7])
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestIncludeScopeInheritanceWithArgs');
|
||||||
|
$result = $engine->render(new ViewLayerData($data));
|
||||||
|
|
||||||
|
// We should not end up with empty values appearing as empty
|
||||||
|
$expected = [
|
||||||
|
'Item 1 _ Item 1 - First-ODD top:Item 1',
|
||||||
|
'Untitled - EVEN top:',
|
||||||
|
'1 _ 1 - ODD top:1',
|
||||||
|
'Untitled - EVEN top:',
|
||||||
|
'Untitled - ODD top:',
|
||||||
|
'Untitled - EVEN top:0',
|
||||||
|
'7 _ 7 - Last-ODD top:7',
|
||||||
|
];
|
||||||
|
$this->assertExpectedStrings($result, $expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRequirements()
|
||||||
|
{
|
||||||
|
/** @var Requirements_Backend|MockObject $requirements */
|
||||||
|
$requirements = $this
|
||||||
|
->getMockBuilder(Requirements_Backend::class)
|
||||||
|
->onlyMethods(['javascript', 'css'])
|
||||||
|
->getMock();
|
||||||
|
$jsFile = FRAMEWORK_DIR . '/tests/forms/a.js';
|
||||||
|
$cssFile = FRAMEWORK_DIR . '/tests/forms/a.js';
|
||||||
|
|
||||||
|
$requirements->expects($this->once())->method('javascript')->with($jsFile);
|
||||||
|
$requirements->expects($this->once())->method('css')->with($cssFile);
|
||||||
|
|
||||||
|
$origReq = Requirements::backend();
|
||||||
|
Requirements::set_backend($requirements);
|
||||||
|
$result = $this->render(
|
||||||
|
"<% require javascript($jsFile) %>
|
||||||
|
<% require css($cssFile) %>"
|
||||||
|
);
|
||||||
|
Requirements::set_backend($origReq);
|
||||||
|
|
||||||
|
// Injecting the actual requirements is the responsibility of SSViewer, so we shouldn't see it in the result
|
||||||
|
$this->assertFalse((bool)trim($result), 'Should be no content in this return.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRequireCallInTemplateInclude()
|
||||||
|
{
|
||||||
|
/** @var Requirements_Backend|MockObject $requirements */
|
||||||
|
$requirements = $this
|
||||||
|
->getMockBuilder(Requirements_Backend::class)
|
||||||
|
->onlyMethods(['themedJavascript', 'css'])
|
||||||
|
->getMock();
|
||||||
|
|
||||||
|
$requirements->expects($this->once())->method('themedJavascript')->with('RequirementsTest_a');
|
||||||
|
$requirements->expects($this->never())->method('css');
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestProcess');
|
||||||
|
$origReq = Requirements::backend();
|
||||||
|
Requirements::set_backend($requirements);
|
||||||
|
Requirements::set_suffix_requirements(false);
|
||||||
|
$result = $engine->render(new ViewLayerData([]));
|
||||||
|
Requirements::set_backend($origReq);
|
||||||
|
|
||||||
|
// Injecting the actual requirements is the responsibility of SSViewer, so we shouldn't see it in the result
|
||||||
|
$this->assertEqualIgnoringWhitespace('<html><head></head><body></body></html>', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testComments()
|
||||||
|
{
|
||||||
|
$input = <<<SS
|
||||||
|
This is my template<%-- this is a comment --%>This is some content<%-- this is another comment --%>Final content
|
||||||
|
<%-- Alone multi
|
||||||
|
line comment --%>
|
||||||
|
Some more content
|
||||||
|
Mixing content and <%-- multi
|
||||||
|
line comment --%> Final final
|
||||||
|
content
|
||||||
|
<%--commentwithoutwhitespace--%>last content
|
||||||
|
SS;
|
||||||
|
$actual = $this->render($input);
|
||||||
|
$expected = <<<SS
|
||||||
|
This is my templateThis is some contentFinal content
|
||||||
|
|
||||||
|
Some more content
|
||||||
|
Mixing content and Final final
|
||||||
|
content
|
||||||
|
last content
|
||||||
|
SS;
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
|
||||||
|
$input = <<<SS
|
||||||
|
<%--
|
||||||
|
|
||||||
|
--%>empty comment1
|
||||||
|
<%-- --%>empty comment2
|
||||||
|
<%----%>empty comment3
|
||||||
|
SS;
|
||||||
|
$actual = $this->render($input);
|
||||||
|
$expected = <<<SS
|
||||||
|
empty comment1
|
||||||
|
empty comment2
|
||||||
|
empty comment3
|
||||||
|
SS;
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBasicText()
|
||||||
|
{
|
||||||
|
$this->assertEquals('"', $this->render('"'), 'Double-quotes are left alone');
|
||||||
|
$this->assertEquals("'", $this->render("'"), 'Single-quotes are left alone');
|
||||||
|
$this->assertEquals('A', $this->render('\\A'), 'Escaped characters are unescaped');
|
||||||
|
$this->assertEquals('\\A', $this->render('\\\\A'), 'Escaped back-slashed are correctly unescaped');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBasicInjection()
|
||||||
|
{
|
||||||
|
$this->assertEquals('[out:Test]', $this->render('$Test'), 'Basic stand-alone injection');
|
||||||
|
$this->assertEquals('[out:Test]', $this->render('{$Test}'), 'Basic stand-alone wrapped injection');
|
||||||
|
$this->assertEquals('A[out:Test]!', $this->render('A$Test!'), 'Basic surrounded injection');
|
||||||
|
$this->assertEquals('A[out:Test]B', $this->render('A{$Test}B'), 'Basic surrounded wrapped injection');
|
||||||
|
|
||||||
|
$this->assertEquals('A$B', $this->render('A\\$B'), 'No injection as $ escaped');
|
||||||
|
$this->assertEquals('A$ B', $this->render('A$ B'), 'No injection as $ not followed by word character');
|
||||||
|
$this->assertEquals('A{$ B', $this->render('A{$ B'), 'No injection as {$ not followed by word character');
|
||||||
|
|
||||||
|
$this->assertEquals('{$Test}', $this->render('{\\$Test}'), 'Escapes can be used to avoid injection');
|
||||||
|
$this->assertEquals(
|
||||||
|
'{\\[out:Test]}',
|
||||||
|
$this->render('{\\\\$Test}'),
|
||||||
|
'Escapes before injections are correctly unescaped'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBasicInjectionMismatchedBrackets()
|
||||||
|
{
|
||||||
|
$this->expectException(SSTemplateParseException::class);
|
||||||
|
$this->expectExceptionMessageMatches('/Malformed bracket injection {\$Value(.*)/');
|
||||||
|
$this->render('A {$Value here');
|
||||||
|
$this->fail("Parser didn't error when encountering mismatched brackets in an injection");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGlobalVariableCalls()
|
||||||
|
{
|
||||||
|
$this->assertEquals('automatic', $this->render('$SSTemplateEngineTest_GlobalAutomatic'));
|
||||||
|
$this->assertEquals('reference', $this->render('$SSTemplateEngineTest_GlobalReferencedByString'));
|
||||||
|
$this->assertEquals('reference', $this->render('$SSTemplateEngineTest_GlobalReferencedInArray'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGlobalVariableCallsWithArguments()
|
||||||
|
{
|
||||||
|
$this->assertEquals('zz', $this->render('$SSTemplateEngineTest_GlobalThatTakesArguments'));
|
||||||
|
$this->assertEquals('zFooz', $this->render('$SSTemplateEngineTest_GlobalThatTakesArguments("Foo")'));
|
||||||
|
$this->assertEquals(
|
||||||
|
'zFoo:Bar:Bazz',
|
||||||
|
$this->render('$SSTemplateEngineTest_GlobalThatTakesArguments("Foo", "Bar", "Baz")')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'zreferencez',
|
||||||
|
$this->render('$SSTemplateEngineTest_GlobalThatTakesArguments($SSTemplateEngineTest_GlobalReferencedByString)')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGlobalVariablesAreEscaped()
|
||||||
|
{
|
||||||
|
$this->assertEquals('<div></div>', $this->render('$SSTemplateEngineTest_GlobalHTMLFragment'));
|
||||||
|
$this->assertEquals('<div></div>', $this->render('$SSTemplateEngineTest_GlobalHTMLEscaped'));
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'z<div></div>z',
|
||||||
|
$this->render('$SSTemplateEngineTest_GlobalThatTakesArguments($SSTemplateEngineTest_GlobalHTMLFragment)')
|
||||||
|
);
|
||||||
|
// Don't escape value when passing into a method call
|
||||||
|
$this->assertEquals(
|
||||||
|
'z<div></div>z',
|
||||||
|
$this->render('$SSTemplateEngineTest_GlobalThatTakesArguments($SSTemplateEngineTest_GlobalHTMLEscaped)')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGlobalVariablesReturnNull()
|
||||||
|
{
|
||||||
|
$this->assertEquals('<p></p>', $this->render('<p>$SSTemplateEngineTest_GlobalReturnsNull</p>'));
|
||||||
|
$this->assertEquals('<p></p>', $this->render('<p>$SSTemplateEngineTest_GlobalReturnsNull.Chained.Properties</p>'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCoreGlobalVariableCalls()
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
Director::absoluteBaseURL(),
|
||||||
|
$this->render('{$absoluteBaseURL}'),
|
||||||
|
'Director::absoluteBaseURL can be called from within template'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
Director::absoluteBaseURL(),
|
||||||
|
$this->render('{$AbsoluteBaseURL}'),
|
||||||
|
'Upper-case %AbsoluteBaseURL can be called from within template'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
Director::is_ajax(),
|
||||||
|
$this->render('{$isAjax}'),
|
||||||
|
'All variations of is_ajax result in the correct call'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
Director::is_ajax(),
|
||||||
|
$this->render('{$IsAjax}'),
|
||||||
|
'All variations of is_ajax result in the correct call'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
Director::is_ajax(),
|
||||||
|
$this->render('{$is_ajax}'),
|
||||||
|
'All variations of is_ajax result in the correct call'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
Director::is_ajax(),
|
||||||
|
$this->render('{$Is_ajax}'),
|
||||||
|
'All variations of is_ajax result in the correct call'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
i18n::get_locale(),
|
||||||
|
$this->render('{$i18nLocale}'),
|
||||||
|
'i18n template functions result correct result'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
i18n::get_locale(),
|
||||||
|
$this->render('{$get_locale}'),
|
||||||
|
'i18n template functions result correct result'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
Security::getCurrentUser()->ID,
|
||||||
|
$this->render('{$CurrentMember.ID}'),
|
||||||
|
'Member template functions result correct result'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
Security::getCurrentUser()->ID,
|
||||||
|
$this->render('{$CurrentUser.ID}'),
|
||||||
|
'Member template functions result correct result'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
Security::getCurrentUser()->ID,
|
||||||
|
$this->render('{$currentMember.ID}'),
|
||||||
|
'Member template functions result correct result'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
Security::getCurrentUser()->ID,
|
||||||
|
$this->render('{$currentUser.ID}'),
|
||||||
|
'Member template functions result correct result'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
SecurityToken::getSecurityID(),
|
||||||
|
$this->render('{$getSecurityID}'),
|
||||||
|
'SecurityToken template functions result correct result'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
SecurityToken::getSecurityID(),
|
||||||
|
$this->render('{$SecurityID}'),
|
||||||
|
'SecurityToken template functions result correct result'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
Permission::check("ADMIN"),
|
||||||
|
(bool)$this->render('{$HasPerm(\'ADMIN\')}'),
|
||||||
|
'Permissions template functions result correct result'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
Permission::check("ADMIN"),
|
||||||
|
(bool)$this->render('{$hasPerm(\'ADMIN\')}'),
|
||||||
|
'Permissions template functions result correct result'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNonFieldCastingHelpersNotUsedInHasValue()
|
||||||
|
{
|
||||||
|
// check if Link without $ in front of variable
|
||||||
|
$result = $this->render(
|
||||||
|
'A<% if Link %>$Link<% end_if %>B',
|
||||||
|
new SSTemplateEngineTest\TestObject()
|
||||||
|
);
|
||||||
|
$this->assertEquals('Asome/url.htmlB', $result, 'casting helper not used for <% if Link %>');
|
||||||
|
|
||||||
|
// check if Link with $ in front of variable
|
||||||
|
$result = $this->render(
|
||||||
|
'A<% if $Link %>$Link<% end_if %>B',
|
||||||
|
new SSTemplateEngineTest\TestObject()
|
||||||
|
);
|
||||||
|
$this->assertEquals('Asome/url.htmlB', $result, 'casting helper not used for <% if $Link %>');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLocalFunctionsTakePriorityOverGlobals()
|
||||||
|
{
|
||||||
|
$data = new ArrayData([
|
||||||
|
'Page' => new SSTemplateEngineTest\TestObject()
|
||||||
|
]);
|
||||||
|
|
||||||
|
//call method with lots of arguments
|
||||||
|
$result = $this->render(
|
||||||
|
'<% with Page %>$lotsOfArguments11("a","b","c","d","e","f","g","h","i","j","k")<% end_with %>',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
$this->assertEquals("abcdefghijk", $result, "public function can accept up to 11 arguments");
|
||||||
|
|
||||||
|
//call method that does not exist
|
||||||
|
$result = $this->render('<% with Page %><% if IDoNotExist %>hello<% end_if %><% end_with %>', $data);
|
||||||
|
$this->assertEquals("", $result, "Method does not exist - empty result");
|
||||||
|
|
||||||
|
//call if that does not exist
|
||||||
|
$result = $this->render('<% with Page %>$IDoNotExist("hello")<% end_with %>', $data);
|
||||||
|
$this->assertEquals("", $result, "Method does not exist - empty result");
|
||||||
|
|
||||||
|
//call method with same name as a global method (local call should take priority)
|
||||||
|
$result = $this->render('<% with Page %>$absoluteBaseURL<% end_with %>', $data);
|
||||||
|
$this->assertEquals(
|
||||||
|
"testLocalFunctionPriorityCalled",
|
||||||
|
$result,
|
||||||
|
"Local Object's public function called. Did not return the actual baseURL of the current site"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCurrentScopeLoop(): void
|
||||||
|
{
|
||||||
|
$data = new ArrayList([['Val' => 'one'], ['Val' => 'two'], ['Val' => 'three']]);
|
||||||
|
$this->assertEqualIgnoringWhitespace(
|
||||||
|
'one two three',
|
||||||
|
$this->render('<% loop %>$Val<% end_loop %>', $data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCurrentScopeLoopWith()
|
||||||
|
{
|
||||||
|
// Data to run the loop tests on - one sequence of three items, each with a subitem
|
||||||
|
$data = new ArrayData([
|
||||||
|
'Foo' => new ArrayList([
|
||||||
|
'Subocean' => new ArrayData([
|
||||||
|
'Name' => 'Higher'
|
||||||
|
]),
|
||||||
|
new ArrayData([
|
||||||
|
'Sub' => new ArrayData([
|
||||||
|
'Name' => 'SubKid1'
|
||||||
|
])
|
||||||
|
]),
|
||||||
|
new ArrayData([
|
||||||
|
'Sub' => new ArrayData([
|
||||||
|
'Name' => 'SubKid2'
|
||||||
|
])
|
||||||
|
]),
|
||||||
|
new SSTemplateEngineTest\TestObject('Number6')
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
|
||||||
|
$result = $this->render(
|
||||||
|
'<% loop Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %><% end_if %><% end_loop %>',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop works");
|
||||||
|
|
||||||
|
$result = $this->render(
|
||||||
|
'<% loop Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %><% end_if %><% end_loop %>',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop works");
|
||||||
|
|
||||||
|
$result = $this->render('<% with Foo %>$Count<% end_with %>', $data);
|
||||||
|
$this->assertEquals("4", $result, "4 items in the DataObjectSet");
|
||||||
|
|
||||||
|
$result = $this->render(
|
||||||
|
'<% with Foo %><% loop Up.Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %>'
|
||||||
|
. '<% end_if %><% end_loop %><% end_with %>',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop in with Up.Foo scope works");
|
||||||
|
|
||||||
|
$result = $this->render(
|
||||||
|
'<% with Foo %><% loop %>$Number<% if Sub %><% with Sub %>$Name<% end_with %>'
|
||||||
|
. '<% end_if %><% end_loop %><% end_with %>',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop in current scope works");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideArgumentTypes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'arg0:0,arg1:"string",arg2:true',
|
||||||
|
'$methodWithTypedArguments(0, "string", true).RAW',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'arg0:false,arg1:"string",arg2:true',
|
||||||
|
'$methodWithTypedArguments(false, "string", true).RAW',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'arg0:null,arg1:"string",arg2:true',
|
||||||
|
'$methodWithTypedArguments(null, "string", true).RAW',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'arg0:"",arg1:"string",arg2:true',
|
||||||
|
'$methodWithTypedArguments("", "string", true).RAW',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'arg0:0,arg1:1,arg2:2',
|
||||||
|
'$methodWithTypedArguments(0, 1, 2).RAW',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideArgumentTypes')]
|
||||||
|
public function testArgumentTypes(string $expected, string $template)
|
||||||
|
{
|
||||||
|
$this->assertEquals($expected, $this->render($template, new TestModelData()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideEvaluatedArgumentTypes(): array
|
||||||
|
{
|
||||||
|
$stdobj = new stdClass();
|
||||||
|
$stdobj->key = 'value';
|
||||||
|
$scenarios = [
|
||||||
|
'null value' => [
|
||||||
|
'data' => ['Value' => null],
|
||||||
|
'useOverlay' => true,
|
||||||
|
'expected' => 'arg0:null',
|
||||||
|
],
|
||||||
|
'int value' => [
|
||||||
|
'data' => ['Value' => 1],
|
||||||
|
'useOverlay' => true,
|
||||||
|
'expected' => 'arg0:1',
|
||||||
|
],
|
||||||
|
'string value' => [
|
||||||
|
'data' => ['Value' => '1'],
|
||||||
|
'useOverlay' => true,
|
||||||
|
'expected' => 'arg0:"1"',
|
||||||
|
],
|
||||||
|
'boolean true' => [
|
||||||
|
'data' => ['Value' => true],
|
||||||
|
'useOverlay' => true,
|
||||||
|
'expected' => 'arg0:true',
|
||||||
|
],
|
||||||
|
'boolean false' => [
|
||||||
|
'data' => ['Value' => false],
|
||||||
|
'useOverlay' => true,
|
||||||
|
'expected' => 'arg0:false',
|
||||||
|
],
|
||||||
|
'object value' => [
|
||||||
|
'data' => ['Value' => $stdobj],
|
||||||
|
'useOverlay' => true,
|
||||||
|
'expected' => 'arg0:{"key":"value"}',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
foreach ($scenarios as $key => $scenario) {
|
||||||
|
$scenario['useOverlay'] = false;
|
||||||
|
$scenarios[$key . ' no overlay'] = $scenario;
|
||||||
|
}
|
||||||
|
return $scenarios;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideEvaluatedArgumentTypes')]
|
||||||
|
public function testEvaluatedArgumentTypes(array $data, bool $useOverlay, string $expected): void
|
||||||
|
{
|
||||||
|
$template = '$methodWithTypedArguments($Value).RAW';
|
||||||
|
$model = new TestModelData();
|
||||||
|
$overlay = $data;
|
||||||
|
if (!$useOverlay) {
|
||||||
|
$model = $model->customise($data);
|
||||||
|
$overlay = [];
|
||||||
|
}
|
||||||
|
$this->assertEquals($expected, $this->render($template, $model, $overlay));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testObjectDotArguments()
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'[out:TestObject.methodWithOneArgument(one)]
|
||||||
|
[out:TestObject.methodWithTwoArguments(one,two)]
|
||||||
|
[out:TestMethod(Arg1,Arg2).Bar.Val]
|
||||||
|
[out:TestMethod(Arg1,Arg2).Bar]
|
||||||
|
[out:TestMethod(Arg1,Arg2)]
|
||||||
|
[out:TestMethod(Arg1).Bar.Val]
|
||||||
|
[out:TestMethod(Arg1).Bar]
|
||||||
|
[out:TestMethod(Arg1)]',
|
||||||
|
$this->render(
|
||||||
|
'$TestObject.methodWithOneArgument(one)
|
||||||
|
$TestObject.methodWithTwoArguments(one,two)
|
||||||
|
$TestMethod(Arg1, Arg2).Bar.Val
|
||||||
|
$TestMethod(Arg1, Arg2).Bar
|
||||||
|
$TestMethod(Arg1, Arg2)
|
||||||
|
$TestMethod(Arg1).Bar.Val
|
||||||
|
$TestMethod(Arg1).Bar
|
||||||
|
$TestMethod(Arg1)'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEscapedArguments()
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'[out:Foo(Arg1,Arg2).Bar.Val].Suffix
|
||||||
|
[out:Foo(Arg1,Arg2).Val]_Suffix
|
||||||
|
[out:Foo(Arg1,Arg2)]/Suffix
|
||||||
|
[out:Foo(Arg1).Bar.Val]textSuffix
|
||||||
|
[out:Foo(Arg1).Bar].Suffix
|
||||||
|
[out:Foo(Arg1)].Suffix
|
||||||
|
[out:Foo.Bar.Val].Suffix
|
||||||
|
[out:Foo.Bar].Suffix
|
||||||
|
[out:Foo].Suffix',
|
||||||
|
$this->render(
|
||||||
|
'{$Foo(Arg1, Arg2).Bar.Val}.Suffix
|
||||||
|
{$Foo(Arg1, Arg2).Val}_Suffix
|
||||||
|
{$Foo(Arg1, Arg2)}/Suffix
|
||||||
|
{$Foo(Arg1).Bar.Val}textSuffix
|
||||||
|
{$Foo(Arg1).Bar}.Suffix
|
||||||
|
{$Foo(Arg1)}.Suffix
|
||||||
|
{$Foo.Bar.Val}.Suffix
|
||||||
|
{$Foo.Bar}.Suffix
|
||||||
|
{$Foo}.Suffix'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLoopWhitespace()
|
||||||
|
{
|
||||||
|
$data = new ArrayList([new SSTemplateEngineTest\TestFixture()]);
|
||||||
|
$this->assertEquals(
|
||||||
|
'before[out:Test]after
|
||||||
|
beforeTestafter',
|
||||||
|
$this->render(
|
||||||
|
'before<% loop %>$Test<% end_loop %>after
|
||||||
|
before<% loop %>Test<% end_loop %>after',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// The control tags are removed from the output, but no whitespace
|
||||||
|
// This is a quirk that could be changed, but included in the test to make the current
|
||||||
|
// behaviour explicit
|
||||||
|
$this->assertEquals(
|
||||||
|
'before
|
||||||
|
|
||||||
|
[out:ItemOnItsOwnLine]
|
||||||
|
|
||||||
|
after',
|
||||||
|
$this->render(
|
||||||
|
'before
|
||||||
|
<% loop %>
|
||||||
|
$ItemOnItsOwnLine
|
||||||
|
<% end_loop %>
|
||||||
|
after',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// The whitespace within the control tags is preserve in a loop
|
||||||
|
// This is a quirk that could be changed, but included in the test to make the current
|
||||||
|
// behaviour explicit
|
||||||
|
$this->assertEquals(
|
||||||
|
'before
|
||||||
|
|
||||||
|
[out:Loop3.ItemOnItsOwnLine]
|
||||||
|
|
||||||
|
[out:Loop3.ItemOnItsOwnLine]
|
||||||
|
|
||||||
|
[out:Loop3.ItemOnItsOwnLine]
|
||||||
|
|
||||||
|
after',
|
||||||
|
$this->render(
|
||||||
|
'before
|
||||||
|
<% loop Loop3 %>
|
||||||
|
$ItemOnItsOwnLine
|
||||||
|
<% end_loop %>
|
||||||
|
after'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function typePreservationDataProvider()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
// Null
|
||||||
|
['NULL:', 'null'],
|
||||||
|
['NULL:', 'NULL'],
|
||||||
|
// Booleans
|
||||||
|
['boolean:1', 'true'],
|
||||||
|
['boolean:1', 'TRUE'],
|
||||||
|
['boolean:', 'false'],
|
||||||
|
['boolean:', 'FALSE'],
|
||||||
|
// Strings which may look like booleans/null to the parser
|
||||||
|
['string:nullish', 'nullish'],
|
||||||
|
['string:notnull', 'notnull'],
|
||||||
|
['string:truethy', 'truethy'],
|
||||||
|
['string:untrue', 'untrue'],
|
||||||
|
['string:falsey', 'falsey'],
|
||||||
|
// Integers
|
||||||
|
['integer:0', '0'],
|
||||||
|
['integer:1', '1'],
|
||||||
|
['integer:15', '15'],
|
||||||
|
['integer:-15', '-15'],
|
||||||
|
// Octal integers
|
||||||
|
['integer:83', '0123'],
|
||||||
|
['integer:-83', '-0123'],
|
||||||
|
// Hexadecimal integers
|
||||||
|
['integer:26', '0x1A'],
|
||||||
|
['integer:-26', '-0x1A'],
|
||||||
|
// Binary integers
|
||||||
|
['integer:255', '0b11111111'],
|
||||||
|
['integer:-255', '-0b11111111'],
|
||||||
|
// Floats (aka doubles)
|
||||||
|
['double:0', '0.0'],
|
||||||
|
['double:1', '1.0'],
|
||||||
|
['double:15.25', '15.25'],
|
||||||
|
['double:-15.25', '-15.25'],
|
||||||
|
['double:1200', '1.2e3'],
|
||||||
|
['double:-1200', '-1.2e3'],
|
||||||
|
['double:0.07', '7E-2'],
|
||||||
|
['double:-0.07', '-7E-2'],
|
||||||
|
// Explicitly quoted strings
|
||||||
|
['string:0', '"0"'],
|
||||||
|
['string:1', '\'1\''],
|
||||||
|
['string:foobar', '"foobar"'],
|
||||||
|
['string:foo bar baz', '"foo bar baz"'],
|
||||||
|
['string:false', '\'false\''],
|
||||||
|
['string:true', '\'true\''],
|
||||||
|
['string:null', '\'null\''],
|
||||||
|
['string:false', '"false"'],
|
||||||
|
['string:true', '"true"'],
|
||||||
|
['string:null', '"null"'],
|
||||||
|
// Implicit strings
|
||||||
|
['string:foobar', 'foobar'],
|
||||||
|
['string:foo bar baz', 'foo bar baz']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('typePreservationDataProvider')]
|
||||||
|
public function testTypesArePreserved($expected, $templateArg)
|
||||||
|
{
|
||||||
|
$data = new ArrayData([
|
||||||
|
'Test' => new TestModelData()
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $this->render("\$Test.Type({$templateArg})", $data));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('typePreservationDataProvider')]
|
||||||
|
public function testTypesArePreservedAsIncludeArguments($expected, $templateArg)
|
||||||
|
{
|
||||||
|
$data = new ArrayData([
|
||||||
|
'Test' => new TestModelData()
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
$this->render("<% include SSTemplateEngineTestTypePreservation Argument={$templateArg} %>", $data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTypePreservationInConditionals()
|
||||||
|
{
|
||||||
|
$data = new ArrayData([
|
||||||
|
'Test' => new TestModelData()
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Types in conditionals
|
||||||
|
$this->assertEquals('pass', $this->render('<% if true %>pass<% else %>fail<% end_if %>', $data));
|
||||||
|
$this->assertEquals('pass', $this->render('<% if false %>fail<% else %>pass<% end_if %>', $data));
|
||||||
|
$this->assertEquals('pass', $this->render('<% if 1 %>pass<% else %>fail<% end_if %>', $data));
|
||||||
|
$this->assertEquals('pass', $this->render('<% if 0 %>fail<% else %>pass<% end_if %>', $data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testControls()
|
||||||
|
{
|
||||||
|
// Single item controls
|
||||||
|
$this->assertEquals(
|
||||||
|
'a[out:Foo.Bar.Item]b
|
||||||
|
[out:Foo.Bar(Arg1).Item]
|
||||||
|
[out:Foo(Arg1).Item]
|
||||||
|
[out:Foo(Arg1,Arg2).Item]
|
||||||
|
[out:Foo(Arg1,Arg2,Arg3).Item]',
|
||||||
|
$this->render(
|
||||||
|
'<% with Foo.Bar %>a{$Item}b<% end_with %>
|
||||||
|
<% with Foo.Bar(Arg1) %>$Item<% end_with %>
|
||||||
|
<% with Foo(Arg1) %>$Item<% end_with %>
|
||||||
|
<% with Foo(Arg1, Arg2) %>$Item<% end_with %>
|
||||||
|
<% with Foo(Arg1, Arg2, Arg3) %>$Item<% end_with %>'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Loop controls
|
||||||
|
$this->assertEquals(
|
||||||
|
'a[out:Foo.Loop2.Item]ba[out:Foo.Loop2.Item]b',
|
||||||
|
$this->render('<% loop Foo.Loop2 %>a{$Item}b<% end_loop %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'[out:Foo.Loop2(Arg1).Item][out:Foo.Loop2(Arg1).Item]',
|
||||||
|
$this->render('<% loop Foo.Loop2(Arg1) %>$Item<% end_loop %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'[out:Loop2(Arg1).Item][out:Loop2(Arg1).Item]',
|
||||||
|
$this->render('<% loop Loop2(Arg1) %>$Item<% end_loop %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'[out:Loop2(Arg1,Arg2).Item][out:Loop2(Arg1,Arg2).Item]',
|
||||||
|
$this->render('<% loop Loop2(Arg1, Arg2) %>$Item<% end_loop %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'[out:Loop2(Arg1,Arg2,Arg3).Item][out:Loop2(Arg1,Arg2,Arg3).Item]',
|
||||||
|
$this->render('<% loop Loop2(Arg1, Arg2, Arg3) %>$Item<% end_loop %>')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIfBlocks()
|
||||||
|
{
|
||||||
|
// Basic test
|
||||||
|
$this->assertEquals(
|
||||||
|
'AC',
|
||||||
|
$this->render('A<% if NotSet %>B$NotSet<% end_if %>C')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Nested test
|
||||||
|
$this->assertEquals(
|
||||||
|
'AB1C',
|
||||||
|
$this->render('A<% if IsSet %>B$NotSet<% if IsSet %>1<% else %>2<% end_if %><% end_if %>C')
|
||||||
|
);
|
||||||
|
|
||||||
|
// else_if
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if NotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'AD',
|
||||||
|
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ADE',
|
||||||
|
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% else_if IsSet %>D<% end_if %>E')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'ADE',
|
||||||
|
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% else_if IsSet %>D<% end_if %>E')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Dot syntax
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if Foo.NotSet %>B<% else_if Foo.IsSet %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if Foo.Bar.NotSet %>B<% else_if Foo.Bar.IsSet %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Params
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if NotSet(Param) %>B<% else %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABD',
|
||||||
|
$this->render('A<% if IsSet(Param) %>B<% else %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Negation
|
||||||
|
$this->assertEquals(
|
||||||
|
'AC',
|
||||||
|
$this->render('A<% if not IsSet %>B<% end_if %>C')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABC',
|
||||||
|
$this->render('A<% if not NotSet %>B<% end_if %>C')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Or
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABD',
|
||||||
|
$this->render('A<% if IsSet || NotSet %>B<% else_if A %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'AD',
|
||||||
|
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if NotSet3 %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if IsSet || NotSet %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'AD',
|
||||||
|
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if NotSet2 || NotSet3 %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Negated Or
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if not IsSet || AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABD',
|
||||||
|
$this->render('A<% if not NotSet || AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABD',
|
||||||
|
$this->render('A<% if NotSet || not AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
|
||||||
|
// And
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABD',
|
||||||
|
$this->render('A<% if IsSet && AlsoSet %>B<% else_if A %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if IsSet && NotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'AD',
|
||||||
|
$this->render('A<% if NotSet && NotSet2 %>B<% else_if NotSet3 %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if IsSet && NotSet %>B<% else_if IsSet && AlsoSet %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'AD',
|
||||||
|
$this->render('A<% if NotSet && NotSet2 %>B<% else_if IsSet && NotSet3 %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Equality
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABC',
|
||||||
|
$this->render('A<% if RawVal == RawVal %>B<% end_if %>C')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ACD',
|
||||||
|
$this->render('A<% if Right == Wrong %>B<% else_if RawVal == RawVal %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABC',
|
||||||
|
$this->render('A<% if Right != Wrong %>B<% end_if %>C')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'AD',
|
||||||
|
$this->render('A<% if Right == Wrong %>B<% else_if RawVal != RawVal %>C<% end_if %>D')
|
||||||
|
);
|
||||||
|
|
||||||
|
// test inequalities with simple numbers
|
||||||
|
$this->assertEquals('ABD', $this->render('A<% if 5 > 3 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ABD', $this->render('A<% if 5 >= 3 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ACD', $this->render('A<% if 3 > 5 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ACD', $this->render('A<% if 3 >= 5 %>B<% else %>C<% end_if %>D'));
|
||||||
|
|
||||||
|
$this->assertEquals('ABD', $this->render('A<% if 3 < 5 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ABD', $this->render('A<% if 3 <= 5 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ACD', $this->render('A<% if 5 < 3 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ACD', $this->render('A<% if 5 <= 3 %>B<% else %>C<% end_if %>D'));
|
||||||
|
|
||||||
|
$this->assertEquals('ABD', $this->render('A<% if 4 <= 4 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ABD', $this->render('A<% if 4 >= 4 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ACD', $this->render('A<% if 4 > 4 %>B<% else %>C<% end_if %>D'));
|
||||||
|
$this->assertEquals('ACD', $this->render('A<% if 4 < 4 %>B<% else %>C<% end_if %>D'));
|
||||||
|
|
||||||
|
// empty else_if and else tags, if this would not be supported,
|
||||||
|
// the output would stop after A, thereby failing the assert
|
||||||
|
$this->assertEquals('AD', $this->render('A<% if IsSet %><% else %><% end_if %>D'));
|
||||||
|
$this->assertEquals(
|
||||||
|
'AD',
|
||||||
|
$this->render('A<% if NotSet %><% else_if IsSet %><% else %><% end_if %>D')
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'AD',
|
||||||
|
$this->render('A<% if NotSet %><% else_if AlsoNotSet %><% else %><% end_if %>D')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Bare words with ending space
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABC',
|
||||||
|
$this->render('A<% if "RawVal" == RawVal %>B<% end_if %>C')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Else
|
||||||
|
$this->assertEquals(
|
||||||
|
'ADE',
|
||||||
|
$this->render('A<% if Right == Wrong %>B<% else_if RawVal != RawVal %>C<% else %>D<% end_if %>E')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Empty if with else
|
||||||
|
$this->assertEquals(
|
||||||
|
'ABC',
|
||||||
|
$this->render('A<% if NotSet %><% else %>B<% end_if %>C')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideIfBlockWithIterable(): array
|
||||||
|
{
|
||||||
|
$scenarios = [
|
||||||
|
'empty array' => [
|
||||||
|
'iterable' => [],
|
||||||
|
'inScope' => false,
|
||||||
|
],
|
||||||
|
'array' => [
|
||||||
|
'iterable' => [1, 2, 3],
|
||||||
|
'inScope' => false,
|
||||||
|
],
|
||||||
|
'ArrayList' => [
|
||||||
|
'iterable' => new ArrayList([['Val' => 1], ['Val' => 2], ['Val' => 3]]),
|
||||||
|
'inScope' => false,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
foreach ($scenarios as $name => $scenario) {
|
||||||
|
$scenario['inScope'] = true;
|
||||||
|
$scenarios[$name . ' in scope'] = $scenario;
|
||||||
|
}
|
||||||
|
return $scenarios;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideIfBlockWithIterable')]
|
||||||
|
public function testIfBlockWithIterable(iterable $iterable, bool $inScope): void
|
||||||
|
{
|
||||||
|
$expected = count($iterable) ? 'has value' : 'no value';
|
||||||
|
$data = new ArrayData(['Iterable' => $iterable]);
|
||||||
|
if ($inScope) {
|
||||||
|
$template = '<% with $Iterable %><% if $Me %>has value<% else %>no value<% end_if %><% end_with %>';
|
||||||
|
} else {
|
||||||
|
$template = '<% if $Iterable %>has value<% else %>no value<% end_if %>';
|
||||||
|
}
|
||||||
|
$this->assertEqualIgnoringWhitespace($expected, $this->render($template, $data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBaseTagGeneration()
|
||||||
|
{
|
||||||
|
// XHTML will have a closed base tag
|
||||||
|
$tmpl1 = '<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
|
||||||
|
. ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
|
<html>
|
||||||
|
<head><% base_tag %></head>
|
||||||
|
<body><p>test</p><body>
|
||||||
|
</html>';
|
||||||
|
$this->assertMatchesRegularExpression('/<head><base href=".*" \/><\/head>/', $this->render($tmpl1));
|
||||||
|
|
||||||
|
// HTML4 and 5 will only have it for IE
|
||||||
|
$tmpl2 = '<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head><% base_tag %></head>
|
||||||
|
<body><p>test</p><body>
|
||||||
|
</html>';
|
||||||
|
$this->assertMatchesRegularExpression(
|
||||||
|
'/<head><base href=".*"><\/head>/',
|
||||||
|
$this->render($tmpl2)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
$tmpl3 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||||
|
<html>
|
||||||
|
<head><% base_tag %></head>
|
||||||
|
<body><p>test</p><body>
|
||||||
|
</html>';
|
||||||
|
$this->assertMatchesRegularExpression(
|
||||||
|
'/<head><base href=".*"><\/head>/',
|
||||||
|
$this->render($tmpl3)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check that the content negotiator converts to the equally legal formats
|
||||||
|
$negotiator = new ContentNegotiator();
|
||||||
|
|
||||||
|
$response = new HTTPResponse($this->render($tmpl1));
|
||||||
|
$negotiator->html($response);
|
||||||
|
$this->assertMatchesRegularExpression(
|
||||||
|
'/<head><base href=".*"><\/head>/',
|
||||||
|
$response->getBody()
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = new HTTPResponse($this->render($tmpl1));
|
||||||
|
$negotiator->xhtml($response);
|
||||||
|
$this->assertMatchesRegularExpression('/<head><base href=".*" \/><\/head>/', $response->getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIncludeWithArguments()
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>[out:Arg1]</p><p>[out:Arg2]</p><p>[out:Arg2.Count]</p>',
|
||||||
|
$this->render('<% include SSTemplateEngineTestIncludeWithArguments %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>A</p><p>[out:Arg2]</p><p>[out:Arg2.Count]</p>',
|
||||||
|
$this->render('<% include SSTemplateEngineTestIncludeWithArguments Arg1=A %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>A</p><p>B</p><p></p>',
|
||||||
|
$this->render('<% include SSTemplateEngineTestIncludeWithArguments Arg1=A, Arg2=B %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>A Bare String</p><p>B Bare String</p><p></p>',
|
||||||
|
$this->render('<% include SSTemplateEngineTestIncludeWithArguments Arg1=A Bare String, Arg2=B Bare String %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>A</p><p>Bar</p><p></p>',
|
||||||
|
$this->render(
|
||||||
|
'<% include SSTemplateEngineTestIncludeWithArguments Arg1="A", Arg2=$B %>',
|
||||||
|
new ArrayData(['B' => 'Bar'])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>A</p><p>Bar</p><p></p>',
|
||||||
|
$this->render(
|
||||||
|
'<% include SSTemplateEngineTestIncludeWithArguments Arg1="A" %>',
|
||||||
|
new ArrayData(['Arg1' => 'Foo', 'Arg2' => 'Bar'])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>A</p><p>0</p><p></p>',
|
||||||
|
$this->render('<% include SSTemplateEngineTestIncludeWithArguments Arg1="A", Arg2=0 %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>A</p><p></p><p></p>',
|
||||||
|
$this->render('<% include SSTemplateEngineTestIncludeWithArguments Arg1="A", Arg2=false %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'<p>A</p><p></p><p></p>',
|
||||||
|
// Note Arg2 is explicitly overridden with null
|
||||||
|
$this->render('<% include SSTemplateEngineTestIncludeWithArguments Arg1="A", Arg2=null %>')
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'SomeArg - Foo - Bar - SomeArg',
|
||||||
|
$this->render(
|
||||||
|
'<% include SSTemplateEngineTestIncludeScopeInheritanceWithArgsInLoop Title="SomeArg" %>',
|
||||||
|
new ArrayData(
|
||||||
|
['Items' => new ArrayList(
|
||||||
|
[
|
||||||
|
new ArrayData(['Title' => 'Foo']),
|
||||||
|
new ArrayData(['Title' => 'Bar'])
|
||||||
|
]
|
||||||
|
)]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'A - B - A',
|
||||||
|
$this->render(
|
||||||
|
'<% include SSTemplateEngineTestIncludeScopeInheritanceWithArgsInWith Title="A" %>',
|
||||||
|
new ArrayData(['Item' => new ArrayData(['Title' =>'B'])])
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'A - B - C - B - A',
|
||||||
|
$this->render(
|
||||||
|
'<% include SSTemplateEngineTestIncludeScopeInheritanceWithArgsInNestedWith Title="A" %>',
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Item' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Title' =>'B', 'NestedItem' => new ArrayData(['Title' => 'C'])
|
||||||
|
]
|
||||||
|
)]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'A - A - A',
|
||||||
|
$this->render(
|
||||||
|
'<% include SSTemplateEngineTestIncludeScopeInheritanceWithUpAndTop Title="A" %>',
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Item' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Title' =>'B', 'NestedItem' => new ArrayData(['Title' => 'C'])
|
||||||
|
]
|
||||||
|
)]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$data = new ArrayData(
|
||||||
|
[
|
||||||
|
'Nested' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Object' => new ArrayData(['Key' => 'A'])
|
||||||
|
]
|
||||||
|
),
|
||||||
|
'Object' => new ArrayData(['Key' => 'B'])
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$res = $this->render('<% include SSTemplateEngineTestIncludeObjectArguments A=$Nested.Object, B=$Object %>', $data);
|
||||||
|
$this->assertEqualIgnoringWhitespace('A B', $res, 'Objects can be passed as named arguments');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNamespaceInclude()
|
||||||
|
{
|
||||||
|
$data = new ArrayData([]);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"tests:( NamespaceInclude\n )",
|
||||||
|
$this->render('tests:( <% include Namespace\NamespaceInclude %> )', $data),
|
||||||
|
'Backslashes work for namespace references in includes'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"tests:( NamespaceInclude\n )",
|
||||||
|
$this->render('tests:( <% include Namespace\\NamespaceInclude %> )', $data),
|
||||||
|
'Escaped backslashes work for namespace references in includes'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"tests:( NamespaceInclude\n )",
|
||||||
|
$this->render('tests:( <% include Namespace/NamespaceInclude %> )', $data),
|
||||||
|
'Forward slashes work for namespace references in includes'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test search for includes fallback to non-includes folder
|
||||||
|
*/
|
||||||
|
public function testIncludeFallbacks()
|
||||||
|
{
|
||||||
|
$data = new ArrayData([]);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"tests:( Namespace/Includes/IncludedTwice.ss\n )",
|
||||||
|
$this->render('tests:( <% include Namespace\\IncludedTwice %> )', $data),
|
||||||
|
'Prefer Includes in the Includes folder'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"tests:( Namespace/Includes/IncludedOnceSub.ss\n )",
|
||||||
|
$this->render('tests:( <% include Namespace\\IncludedOnceSub %> )', $data),
|
||||||
|
'Includes in only Includes folder can be found'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"tests:( Namespace/IncludedOnceBase.ss\n )",
|
||||||
|
$this->render('tests:( <% include Namespace\\IncludedOnceBase %> )', $data),
|
||||||
|
'Includes outside of Includes folder can be found'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRecursiveInclude()
|
||||||
|
{
|
||||||
|
$data = new ArrayData(
|
||||||
|
[
|
||||||
|
'Title' => 'A',
|
||||||
|
'Children' => new ArrayList(
|
||||||
|
[
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Title' => 'A1',
|
||||||
|
'Children' => new ArrayList(
|
||||||
|
[
|
||||||
|
new ArrayData([ 'Title' => 'A1 i', ]),
|
||||||
|
new ArrayData([ 'Title' => 'A1 ii', ]),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
new ArrayData([ 'Title' => 'A2', ]),
|
||||||
|
new ArrayData([ 'Title' => 'A3', ]),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine('Includes/SSTemplateEngineTestRecursiveInclude');
|
||||||
|
$result = $engine->render(new ViewLayerData($data));
|
||||||
|
// We don't care about whitespace
|
||||||
|
$rationalisedResult = trim(preg_replace('/\s+/', ' ', $result ?? '') ?? '');
|
||||||
|
|
||||||
|
$this->assertEquals('A A1 A1 i A1 ii A2 A3', $rationalisedResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See {@link ModelDataTest} for more extensive casting tests,
|
||||||
|
* this test just ensures that basic casting is correctly applied during template parsing.
|
||||||
|
*/
|
||||||
|
public function testCastingHelpers()
|
||||||
|
{
|
||||||
|
$vd = new SSTemplateEngineTest\TestModelData();
|
||||||
|
$vd->TextValue = '<b>html</b>';
|
||||||
|
$vd->HTMLValue = '<b>html</b>';
|
||||||
|
$vd->UncastedValue = '<b>html</b>';
|
||||||
|
|
||||||
|
// Value casted as "Text"
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$TextValue', $vd)
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$TextValue.RAW', $vd)
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$TextValue.XML', $vd)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Value casted as "HTMLText"
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$HTMLValue', $vd)
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$HTMLValue.RAW', $vd)
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$HTMLValue.XML', $vd)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Uncasted value (falls back to the relevant DBField class for the data type)
|
||||||
|
$vd = new SSTemplateEngineTest\TestModelData();
|
||||||
|
$vd->UncastedValue = '<b>html</b>';
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$UncastedValue', $vd)
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$UncastedValue.RAW', $vd)
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
'<b>html</b>',
|
||||||
|
$this->render('$UncastedValue.XML', $vd)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideLoop(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'nested array and iterator' => [
|
||||||
|
'iterable' => [
|
||||||
|
[
|
||||||
|
'value 1',
|
||||||
|
'value 2',
|
||||||
|
],
|
||||||
|
new ArrayList([
|
||||||
|
'value 3',
|
||||||
|
'value 4',
|
||||||
|
]),
|
||||||
|
],
|
||||||
|
'template' => '<% loop $Iterable %><% loop $Me %>$Me<% end_loop %><% end_loop %>',
|
||||||
|
'expected' => 'value 1 value 2 value 3 value 4',
|
||||||
|
],
|
||||||
|
'nested associative arrays' => [
|
||||||
|
'iterable' => [
|
||||||
|
[
|
||||||
|
'Foo' => 'one',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'Foo' => 'two',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'Foo' => 'three',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'template' => '<% loop $Iterable %>$Foo<% end_loop %>',
|
||||||
|
'expected' => 'one two three',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideLoop')]
|
||||||
|
public function testLoop(iterable $iterable, string $template, string $expected): void
|
||||||
|
{
|
||||||
|
$data = new ArrayData(['Iterable' => $iterable]);
|
||||||
|
$this->assertEqualIgnoringWhitespace($expected, $this->render($template, $data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideCountIterable(): array
|
||||||
|
{
|
||||||
|
$scenarios = [
|
||||||
|
'empty array' => [
|
||||||
|
'iterable' => [],
|
||||||
|
'inScope' => false,
|
||||||
|
],
|
||||||
|
'array' => [
|
||||||
|
'iterable' => [1, 2, 3],
|
||||||
|
'inScope' => false,
|
||||||
|
],
|
||||||
|
'ArrayList' => [
|
||||||
|
'iterable' => new ArrayList([['Val' => 1], ['Val' => 2], ['Val' => 3]]),
|
||||||
|
'inScope' => false,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
foreach ($scenarios as $name => $scenario) {
|
||||||
|
$scenario['inScope'] = true;
|
||||||
|
$scenarios[$name . ' in scope'] = $scenario;
|
||||||
|
}
|
||||||
|
return $scenarios;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideCountIterable')]
|
||||||
|
public function testCountIterable(iterable $iterable, bool $inScope): void
|
||||||
|
{
|
||||||
|
$expected = count($iterable);
|
||||||
|
$data = new ArrayData(['Iterable' => $iterable]);
|
||||||
|
if ($inScope) {
|
||||||
|
$template = '<% with $Iterable %>$Count<% end_with %>';
|
||||||
|
} else {
|
||||||
|
$template = '$Iterable.Count';
|
||||||
|
}
|
||||||
|
$this->assertEqualIgnoringWhitespace($expected, $this->render($template, $data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSSViewerBasicIteratorSupport()
|
||||||
|
{
|
||||||
|
$data = new ArrayData(
|
||||||
|
[
|
||||||
|
'Set' => new ArrayList(
|
||||||
|
[
|
||||||
|
new SSTemplateEngineTest\TestObject("1"),
|
||||||
|
new SSTemplateEngineTest\TestObject("2"),
|
||||||
|
new SSTemplateEngineTest\TestObject("3"),
|
||||||
|
new SSTemplateEngineTest\TestObject("4"),
|
||||||
|
new SSTemplateEngineTest\TestObject("5"),
|
||||||
|
new SSTemplateEngineTest\TestObject("6"),
|
||||||
|
new SSTemplateEngineTest\TestObject("7"),
|
||||||
|
new SSTemplateEngineTest\TestObject("8"),
|
||||||
|
new SSTemplateEngineTest\TestObject("9"),
|
||||||
|
new SSTemplateEngineTest\TestObject("10"),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
//base test
|
||||||
|
$result = $this->render('<% loop Set %>$Number<% end_loop %>', $data);
|
||||||
|
$this->assertEquals("12345678910", $result, "Numbers rendered in order");
|
||||||
|
|
||||||
|
//test First
|
||||||
|
$result = $this->render('<% loop Set %><% if $IsFirst %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("1", $result, "Only the first number is rendered");
|
||||||
|
|
||||||
|
//test Last
|
||||||
|
$result = $this->render('<% loop Set %><% if $IsLast %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("10", $result, "Only the last number is rendered");
|
||||||
|
|
||||||
|
//test Even
|
||||||
|
$result = $this->render('<% loop Set %><% if $Even() %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
||||||
|
|
||||||
|
//test Even with quotes
|
||||||
|
$result = $this->render('<% loop Set %><% if $Even("1") %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
||||||
|
|
||||||
|
//test Even without quotes
|
||||||
|
$result = $this->render('<% loop Set %><% if $Even(1) %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
||||||
|
|
||||||
|
//test Even with zero-based start index
|
||||||
|
$result = $this->render('<% loop Set %><% if $Even("0") %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("13579", $result, "Even (with zero-based index) numbers rendered in order");
|
||||||
|
|
||||||
|
//test Odd
|
||||||
|
$result = $this->render('<% loop Set %><% if $Odd %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("13579", $result, "Odd numbers rendered in order");
|
||||||
|
|
||||||
|
//test FirstLast
|
||||||
|
$result = $this->render('<% loop Set %><% if $FirstLast %>$Number$FirstLast<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("1first10last", $result, "First and last numbers rendered in order");
|
||||||
|
|
||||||
|
//test Middle
|
||||||
|
$result = $this->render('<% loop Set %><% if $Middle %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("23456789", $result, "Middle numbers rendered in order");
|
||||||
|
|
||||||
|
//test MiddleString
|
||||||
|
$result = $this->render(
|
||||||
|
'<% loop Set %><% if MiddleString == "middle" %>$Number$MiddleString<% end_if %>'
|
||||||
|
. '<% end_loop %>',
|
||||||
|
$data
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
"2middle3middle4middle5middle6middle7middle8middle9middle",
|
||||||
|
$result,
|
||||||
|
"Middle numbers rendered in order"
|
||||||
|
);
|
||||||
|
|
||||||
|
//test EvenOdd
|
||||||
|
$result = $this->render('<% loop Set %>$EvenOdd<% end_loop %>', $data);
|
||||||
|
$this->assertEquals(
|
||||||
|
"oddevenoddevenoddevenoddevenoddeven",
|
||||||
|
$result,
|
||||||
|
"Even and Odd is returned in sequence numbers rendered in order"
|
||||||
|
);
|
||||||
|
|
||||||
|
//test Pos
|
||||||
|
$result = $this->render('<% loop Set %>$Pos<% end_loop %>', $data);
|
||||||
|
$this->assertEquals("12345678910", $result, '$Pos is rendered in order');
|
||||||
|
|
||||||
|
//test Pos
|
||||||
|
$result = $this->render('<% loop Set %>$Pos(0)<% end_loop %>', $data);
|
||||||
|
$this->assertEquals("0123456789", $result, '$Pos(0) is rendered in order');
|
||||||
|
|
||||||
|
//test FromEnd
|
||||||
|
$result = $this->render('<% loop Set %>$FromEnd<% end_loop %>', $data);
|
||||||
|
$this->assertEquals("10987654321", $result, '$FromEnd is rendered in order');
|
||||||
|
|
||||||
|
//test FromEnd
|
||||||
|
$result = $this->render('<% loop Set %>$FromEnd(0)<% end_loop %>', $data);
|
||||||
|
$this->assertEquals("9876543210", $result, '$FromEnd(0) rendered in order');
|
||||||
|
|
||||||
|
//test Total
|
||||||
|
$result = $this->render('<% loop Set %>$TotalItems<% end_loop %>', $data);
|
||||||
|
$this->assertEquals("10101010101010101010", $result, "10 total items X 10 are returned");
|
||||||
|
|
||||||
|
//test Modulus
|
||||||
|
$result = $this->render('<% loop Set %>$Modulus(2,1)<% end_loop %>', $data);
|
||||||
|
$this->assertEquals("1010101010", $result, "1-indexed pos modular divided by 2 rendered in order");
|
||||||
|
|
||||||
|
//test MultipleOf 3
|
||||||
|
$result = $this->render('<% loop Set %><% if MultipleOf(3) %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("369", $result, "Only numbers that are multiples of 3 are returned");
|
||||||
|
|
||||||
|
//test MultipleOf 4
|
||||||
|
$result = $this->render('<% loop Set %><% if MultipleOf(4) %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("48", $result, "Only numbers that are multiples of 4 are returned");
|
||||||
|
|
||||||
|
//test MultipleOf 5
|
||||||
|
$result = $this->render('<% loop Set %><% if MultipleOf(5) %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("510", $result, "Only numbers that are multiples of 5 are returned");
|
||||||
|
|
||||||
|
//test MultipleOf 10
|
||||||
|
$result = $this->render('<% loop Set %><% if MultipleOf(10,1) %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("10", $result, "Only numbers that are multiples of 10 (with 1-based indexing) are returned");
|
||||||
|
|
||||||
|
//test MultipleOf 9 zero-based
|
||||||
|
$result = $this->render('<% loop Set %><% if MultipleOf(9,0) %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals(
|
||||||
|
"110",
|
||||||
|
$result,
|
||||||
|
"Only numbers that are multiples of 9 with zero-based indexing are returned. (The first and last item)"
|
||||||
|
);
|
||||||
|
|
||||||
|
//test MultipleOf 11
|
||||||
|
$result = $this->render('<% loop Set %><% if MultipleOf(11) %>$Number<% end_if %><% end_loop %>', $data);
|
||||||
|
$this->assertEquals("", $result, "Only numbers that are multiples of 11 are returned. I.e. nothing returned");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test $Up works when the scope $Up refers to was entered with a "with" block
|
||||||
|
*/
|
||||||
|
public function testUpInWith()
|
||||||
|
{
|
||||||
|
|
||||||
|
// Data to run the loop tests on - three levels deep
|
||||||
|
$data = new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Top',
|
||||||
|
'Foo' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Foo',
|
||||||
|
'Bar' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Bar',
|
||||||
|
'Baz' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Baz'
|
||||||
|
]
|
||||||
|
),
|
||||||
|
'Qux' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Qux'
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Basic functionality
|
||||||
|
$this->assertEquals(
|
||||||
|
'BarFoo',
|
||||||
|
$this->render('<% with Foo %><% with Bar %>{$Name}{$Up.Name}<% end_with %><% end_with %>', $data)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Two level with block, up refers to internally referenced Bar
|
||||||
|
$this->assertEquals(
|
||||||
|
'BarTop',
|
||||||
|
$this->render('<% with Foo.Bar %>{$Name}{$Up.Name}<% end_with %>', $data)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Stepping up & back down the scope tree
|
||||||
|
$this->assertEquals(
|
||||||
|
'BazFooBar',
|
||||||
|
$this->render('<% with Foo.Bar.Baz %>{$Name}{$Up.Foo.Name}{$Up.Foo.Bar.Name}<% end_with %>', $data)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Using $Up in a with block
|
||||||
|
$this->assertEquals(
|
||||||
|
'BazTopBar',
|
||||||
|
$this->render(
|
||||||
|
'<% with Foo.Bar.Baz %>{$Name}<% with $Up %>{$Name}{$Foo.Bar.Name}<% end_with %>'
|
||||||
|
. '<% end_with %>',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Stepping up & back down the scope tree with with blocks
|
||||||
|
$this->assertEquals(
|
||||||
|
'BazTopBarTopBaz',
|
||||||
|
$this->render(
|
||||||
|
'<% with Foo.Bar.Baz %>{$Name}<% with $Up %>{$Name}<% with Foo.Bar %>{$Name}<% end_with %>'
|
||||||
|
. '{$Name}<% end_with %>{$Name}<% end_with %>',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Using $Up.Up, where first $Up points to a previous scope entered using $Up, thereby skipping up to Foo
|
||||||
|
$this->assertEquals(
|
||||||
|
'Foo',
|
||||||
|
$this->render(
|
||||||
|
'<% with Foo %><% with Bar %><% with Baz %>{$Up.Up.Name}<% end_with %><% end_with %>'
|
||||||
|
. '<% end_with %>',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Using $Up as part of a lookup chain in <% with %>
|
||||||
|
$this->assertEquals(
|
||||||
|
'Top',
|
||||||
|
$this->render('<% with Foo.Bar.Baz.Up.Qux %>{$Up.Name}<% end_with %>', $data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTooManyUps()
|
||||||
|
{
|
||||||
|
$this->expectException(LogicException::class);
|
||||||
|
$this->expectExceptionMessage("Up called when we're already at the top of the scope");
|
||||||
|
$data = new ArrayData([
|
||||||
|
'Foo' => new ArrayData([
|
||||||
|
'Name' => 'Foo',
|
||||||
|
'Bar' => new ArrayData([
|
||||||
|
'Name' => 'Bar'
|
||||||
|
])
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'Foo',
|
||||||
|
$this->render('<% with Foo.Bar %>{$Up.Up.Name}<% end_with %>', $data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test $Up works when the scope $Up refers to was entered with a "loop" block
|
||||||
|
*/
|
||||||
|
public function testUpInLoop()
|
||||||
|
{
|
||||||
|
|
||||||
|
// Data to run the loop tests on - one sequence of three items, each with a subitem
|
||||||
|
$data = new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Top',
|
||||||
|
'Foo' => new ArrayList(
|
||||||
|
[
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => '1',
|
||||||
|
'Sub' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Bar'
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => '2',
|
||||||
|
'Sub' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Baz'
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => '3',
|
||||||
|
'Sub' => new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'Qux'
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Make sure inside a loop, $Up refers to the current item of the loop
|
||||||
|
$this->assertEqualIgnoringWhitespace(
|
||||||
|
'111 222 333',
|
||||||
|
$this->render(
|
||||||
|
'<% loop $Foo %>$Name<% with $Sub %>$Up.Name<% end_with %>$Name<% end_loop %>',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Make sure inside a loop, looping over $Up uses a separate iterator,
|
||||||
|
// and doesn't interfere with the original iterator
|
||||||
|
$this->assertEqualIgnoringWhitespace(
|
||||||
|
'1Bar123Bar1 2Baz123Baz2 3Qux123Qux3',
|
||||||
|
$this->render(
|
||||||
|
'<% loop $Foo %>
|
||||||
|
$Name
|
||||||
|
<% with $Sub %>
|
||||||
|
$Name
|
||||||
|
<% loop $Up %>$Name<% end_loop %>
|
||||||
|
$Name
|
||||||
|
<% end_with %>
|
||||||
|
$Name
|
||||||
|
<% end_loop %>',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Make sure inside a loop, looping over $Up uses a separate iterator,
|
||||||
|
// and doesn't interfere with the original iterator or local lookups
|
||||||
|
$this->assertEqualIgnoringWhitespace(
|
||||||
|
'1 Bar1 123 1Bar 1 2 Baz2 123 2Baz 2 3 Qux3 123 3Qux 3',
|
||||||
|
$this->render(
|
||||||
|
'<% loop $Foo %>
|
||||||
|
$Name
|
||||||
|
<% with $Sub %>
|
||||||
|
{$Name}{$Up.Name}
|
||||||
|
<% loop $Up %>$Name<% end_loop %>
|
||||||
|
{$Up.Name}{$Name}
|
||||||
|
<% end_with %>
|
||||||
|
$Name
|
||||||
|
<% end_loop %>',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that nested loops restore the loop variables correctly when pushing and popping states
|
||||||
|
*/
|
||||||
|
public function testNestedLoops()
|
||||||
|
{
|
||||||
|
|
||||||
|
// Data to run the loop tests on - one sequence of three items, one with child elements
|
||||||
|
// (of a different size to the main sequence)
|
||||||
|
$data = new ArrayData(
|
||||||
|
[
|
||||||
|
'Foo' => new ArrayList(
|
||||||
|
[
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => '1',
|
||||||
|
'Children' => new ArrayList(
|
||||||
|
[
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'a'
|
||||||
|
]
|
||||||
|
),
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => 'b'
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => '2',
|
||||||
|
'Children' => new ArrayList(),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
new ArrayData(
|
||||||
|
[
|
||||||
|
'Name' => '3',
|
||||||
|
'Children' => new ArrayList(),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Make sure that including a loop inside a loop will not destroy the internal count of
|
||||||
|
// items, checked by using "Last"
|
||||||
|
$this->assertEqualIgnoringWhitespace(
|
||||||
|
'1ab23last',
|
||||||
|
$this->render(
|
||||||
|
'<% loop $Foo %>$Name<% loop Children %>$Name<% end_loop %><% if $IsLast %>last<% end_if %>'
|
||||||
|
. '<% end_loop %>',
|
||||||
|
$data
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLayout()
|
||||||
|
{
|
||||||
|
$this->useTestTheme(
|
||||||
|
__DIR__ . '/SSTemplateEngineTest',
|
||||||
|
'layouttest',
|
||||||
|
function () {
|
||||||
|
$engine = new SSTemplateEngine('Page');
|
||||||
|
$this->assertEquals("Foo\n\n", $engine->render(new ViewLayerData([])));
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine(['Shortcodes', 'Page']);
|
||||||
|
$this->assertEquals("[file_link]\n\n", $engine->render(new ViewLayerData([])));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideRenderWithSourceFileComments(): array
|
||||||
|
{
|
||||||
|
$i = __DIR__ . '/SSTemplateEngineTest/templates/Includes';
|
||||||
|
$f = __DIR__ . '/SSTemplateEngineTest/templates/SSTemplateEngineTestComments';
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'name' => 'SSTemplateEngineTestCommentsFullSource',
|
||||||
|
'expected' => ""
|
||||||
|
. "<!doctype html>"
|
||||||
|
. "<!-- template $f/SSTemplateEngineTestCommentsFullSource.ss -->"
|
||||||
|
. "<html>"
|
||||||
|
. "\t<head></head>"
|
||||||
|
. "\t<body></body>"
|
||||||
|
. "</html>"
|
||||||
|
. "<!-- end template $f/SSTemplateEngineTestCommentsFullSource.ss -->",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'SSTemplateEngineTestCommentsFullSourceHTML4Doctype',
|
||||||
|
'expected' => ""
|
||||||
|
. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML "
|
||||||
|
. "4.01//EN\"\t\t\"http://www.w3.org/TR/html4/strict.dtd\">"
|
||||||
|
. "<!-- template $f/SSTemplateEngineTestCommentsFullSourceHTML4Doctype.ss -->"
|
||||||
|
. "<html>"
|
||||||
|
. "\t<head></head>"
|
||||||
|
. "\t<body></body>"
|
||||||
|
. "</html>"
|
||||||
|
. "<!-- end template $f/SSTemplateEngineTestCommentsFullSourceHTML4Doctype.ss -->",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'SSTemplateEngineTestCommentsFullSourceNoDoctype',
|
||||||
|
'expected' => ""
|
||||||
|
. "<html><!-- template $f/SSTemplateEngineTestCommentsFullSourceNoDoctype.ss -->"
|
||||||
|
. "\t<head></head>"
|
||||||
|
. "\t<body></body>"
|
||||||
|
. "<!-- end template $f/SSTemplateEngineTestCommentsFullSourceNoDoctype.ss --></html>",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'SSTemplateEngineTestCommentsFullSourceIfIE',
|
||||||
|
'expected' => ""
|
||||||
|
. "<!doctype html>"
|
||||||
|
. "<!-- template $f/SSTemplateEngineTestCommentsFullSourceIfIE.ss -->"
|
||||||
|
. "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->"
|
||||||
|
. "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->"
|
||||||
|
. "<!--[if !IE]><!--> <html class='no-ie'> <!--<![endif]-->"
|
||||||
|
. "\t<head></head>"
|
||||||
|
. "\t<body></body>"
|
||||||
|
. "</html>"
|
||||||
|
. "<!-- end template $f/SSTemplateEngineTestCommentsFullSourceIfIE.ss -->",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'SSTemplateEngineTestCommentsFullSourceIfIENoDoctype',
|
||||||
|
'expected' => ""
|
||||||
|
. "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->"
|
||||||
|
. "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->"
|
||||||
|
. "<!--[if !IE]><!--> <html class='no-ie'>"
|
||||||
|
. "<!-- template $f/SSTemplateEngineTestCommentsFullSourceIfIENoDoctype.ss -->"
|
||||||
|
. " <!--<![endif]-->"
|
||||||
|
. "\t<head></head>"
|
||||||
|
. "\t<body></body>"
|
||||||
|
. "<!-- end template $f/SSTemplateEngineTestCommentsFullSourceIfIENoDoctype.ss --></html>",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'SSTemplateEngineTestCommentsPartialSource',
|
||||||
|
'expected' =>
|
||||||
|
"<!-- template $f/SSTemplateEngineTestCommentsPartialSource.ss -->"
|
||||||
|
. "<div class='typography'></div>"
|
||||||
|
. "<!-- end template $f/SSTemplateEngineTestCommentsPartialSource.ss -->",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'SSTemplateEngineTestCommentsWithInclude',
|
||||||
|
'expected' =>
|
||||||
|
"<!-- template $f/SSTemplateEngineTestCommentsWithInclude.ss -->"
|
||||||
|
. "<div class='typography'>"
|
||||||
|
. "<!-- include 'SSTemplateEngineTestCommentsInclude' -->"
|
||||||
|
. "<!-- template $i/SSTemplateEngineTestCommentsInclude.ss -->"
|
||||||
|
. "Included"
|
||||||
|
. "<!-- end template $i/SSTemplateEngineTestCommentsInclude.ss -->"
|
||||||
|
. "<!-- end include 'SSTemplateEngineTestCommentsInclude' -->"
|
||||||
|
. "</div>"
|
||||||
|
. "<!-- end template $f/SSTemplateEngineTestCommentsWithInclude.ss -->",
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideRenderWithSourceFileComments')]
|
||||||
|
public function testRenderWithSourceFileComments(string $name, string $expected)
|
||||||
|
{
|
||||||
|
SSViewer::config()->set('source_file_comments', true);
|
||||||
|
$this->_renderWithSourceFileComments('SSTemplateEngineTestComments/' . $name, $expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideRenderWithMissingTemplate(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'templateCandidates' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'templateCandidates' => '',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'templateCandidates' => ['noTemplate'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'templateCandidates' => 'noTemplate',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideRenderWithMissingTemplate')]
|
||||||
|
public function testRenderWithMissingTemplate(string|array $templateCandidates): void
|
||||||
|
{
|
||||||
|
if (empty($templateCandidates)) {
|
||||||
|
$message = 'No template to render. Try calling setTemplate() or passing template candidates into the constructor.';
|
||||||
|
} else {
|
||||||
|
$message = 'None of the following templates could be found: '
|
||||||
|
. print_r($templateCandidates, true)
|
||||||
|
. ' in themes "' . print_r(SSViewer::get_themes(), true) . '"';
|
||||||
|
}
|
||||||
|
$engine = new SSTemplateEngine($templateCandidates);
|
||||||
|
$this->expectException(MissingTemplateException::class);
|
||||||
|
$this->expectExceptionMessage($message);
|
||||||
|
$engine->render(new ViewLayerData([]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLoopIteratorIterator()
|
||||||
|
{
|
||||||
|
$list = new PaginatedList(new ArrayList());
|
||||||
|
$result = $this->render(
|
||||||
|
'<% loop List %>$ID - $FirstName<br /><% end_loop %>',
|
||||||
|
new ArrayData(['List' => $list])
|
||||||
|
);
|
||||||
|
$this->assertEquals('', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
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(
|
||||||
|
[
|
||||||
|
'Set' => new ArrayList(
|
||||||
|
[
|
||||||
|
new SSTemplateEngineTest\TestObject("1"),
|
||||||
|
new SSTemplateEngineTest\TestObject("2"),
|
||||||
|
new SSTemplateEngineTest\TestObject("3"),
|
||||||
|
new SSTemplateEngineTest\TestObject("4"),
|
||||||
|
new SSTemplateEngineTest\TestObject("5"),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
'Level' => new SSTemplateEngineTest\LevelTestData(1),
|
||||||
|
'Nest' => [
|
||||||
|
'Level' => new SSTemplateEngineTest\LevelTestData(2),
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, trim($this->render($template, $data) ?? ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRepeatedCallsAreCached()
|
||||||
|
{
|
||||||
|
$data = new SSTemplateEngineTest\CacheTestData();
|
||||||
|
$template = '
|
||||||
|
<% if $TestWithCall %>
|
||||||
|
<% with $TestWithCall %>
|
||||||
|
{$Message}
|
||||||
|
<% end_with %>
|
||||||
|
|
||||||
|
{$TestWithCall.Message}
|
||||||
|
<% end_if %>';
|
||||||
|
|
||||||
|
$this->assertEquals('HiHi', preg_replace('/\s+/', '', $this->render($template, $data) ?? ''));
|
||||||
|
$this->assertEquals(
|
||||||
|
1,
|
||||||
|
$data->testWithCalls,
|
||||||
|
'SSTemplateEngineTest_CacheTestData::TestWithCall() should only be called once. Subsequent calls should be cached'
|
||||||
|
);
|
||||||
|
|
||||||
|
$data = new SSTemplateEngineTest\CacheTestData();
|
||||||
|
$template = '
|
||||||
|
<% if $TestLoopCall %>
|
||||||
|
<% loop $TestLoopCall %>
|
||||||
|
{$Message}
|
||||||
|
<% end_loop %>
|
||||||
|
<% end_if %>';
|
||||||
|
|
||||||
|
$this->assertEquals('OneTwo', preg_replace('/\s+/', '', $this->render($template, $data) ?? ''));
|
||||||
|
$this->assertEquals(
|
||||||
|
1,
|
||||||
|
$data->testLoopCalls,
|
||||||
|
'SSTemplateEngineTest_CacheTestData::TestLoopCall() should only be called once. Subsequent calls should be cached'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testClosedBlockExtension()
|
||||||
|
{
|
||||||
|
$count = 0;
|
||||||
|
$parser = new SSTemplateParser();
|
||||||
|
$parser->addClosedBlock(
|
||||||
|
'test',
|
||||||
|
function () use (&$count) {
|
||||||
|
$count++;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestRecursiveInclude');
|
||||||
|
$engine->setParser($parser);
|
||||||
|
$engine->renderString('<% test %><% end_test %>', new ViewLayerData([]));
|
||||||
|
|
||||||
|
$this->assertEquals(1, $count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOpenBlockExtension()
|
||||||
|
{
|
||||||
|
$count = 0;
|
||||||
|
$parser = new SSTemplateParser();
|
||||||
|
$parser->addOpenBlock(
|
||||||
|
'test',
|
||||||
|
function () use (&$count) {
|
||||||
|
$count++;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestRecursiveInclude');
|
||||||
|
$engine->setParser($parser);
|
||||||
|
$engine->renderString('<% test %>', new ViewLayerData([]));
|
||||||
|
|
||||||
|
$this->assertEquals(1, $count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromStringCaching()
|
||||||
|
{
|
||||||
|
$content = 'Test content';
|
||||||
|
$cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . '.cache.' . sha1($content ?? '');
|
||||||
|
if (file_exists($cacheFile ?? '')) {
|
||||||
|
unlink($cacheFile ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test instance behaviors
|
||||||
|
$this->render($content, cache: false);
|
||||||
|
$this->assertFileDoesNotExist($cacheFile, 'Cache file was created when caching was off');
|
||||||
|
|
||||||
|
$this->render($content, cache: true);
|
||||||
|
$this->assertFileExists($cacheFile, 'Cache file wasn\'t created when it was meant to');
|
||||||
|
unlink($cacheFile ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPrimitivesConvertedToDBFields()
|
||||||
|
{
|
||||||
|
$data = new ArrayData([
|
||||||
|
// null value should not be rendered, though should also not throw exception
|
||||||
|
'Foo' => new ArrayList(['hello', true, 456, 7.89, null])
|
||||||
|
]);
|
||||||
|
$this->assertEqualIgnoringWhitespace(
|
||||||
|
'hello 1 456 7.89',
|
||||||
|
$this->render('<% loop $Foo %>$Me<% end_loop %>', $data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DoesNotPerformAssertions]
|
||||||
|
public function testMe(): void
|
||||||
|
{
|
||||||
|
$myArrayData = new class extends ArrayData {
|
||||||
|
public function forTemplate(): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$this->render('$Me', $myArrayData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLoopingThroughArrayInOverlay(): void
|
||||||
|
{
|
||||||
|
$modelData = new ModelData();
|
||||||
|
$theArray = [
|
||||||
|
['Val' => 'one'],
|
||||||
|
['Val' => 'two'],
|
||||||
|
['Val' => 'red'],
|
||||||
|
['Val' => 'blue'],
|
||||||
|
];
|
||||||
|
$engine = new SSTemplateEngine('SSTemplateEngineTestLoopArray');
|
||||||
|
$output = $engine->render(new ViewLayerData($modelData), ['MyArray' => $theArray]);
|
||||||
|
$this->assertEqualIgnoringWhitespace('one two red blue', $output);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideGetterMethod(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'as property (not getter)' => [
|
||||||
|
'template' => '$MyProperty',
|
||||||
|
'expected' => 'Nothing passed in',
|
||||||
|
],
|
||||||
|
'as method (not getter)' => [
|
||||||
|
'template' => '$MyProperty()',
|
||||||
|
'expected' => 'Nothing passed in',
|
||||||
|
],
|
||||||
|
'as method (not getter), with arg' => [
|
||||||
|
'template' => '$MyProperty("Some Value")',
|
||||||
|
'expected' => 'Was passed in: Some Value',
|
||||||
|
],
|
||||||
|
'as property (getter)' => [
|
||||||
|
'template' => '$getMyProperty',
|
||||||
|
'expected' => 'Nothing passed in',
|
||||||
|
],
|
||||||
|
'as method (getter)' => [
|
||||||
|
'template' => '$getMyProperty()',
|
||||||
|
'expected' => 'Nothing passed in',
|
||||||
|
],
|
||||||
|
'as method (getter), with arg' => [
|
||||||
|
'template' => '$getMyProperty("Some Value")',
|
||||||
|
'expected' => 'Was passed in: Some Value',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideGetterMethod')]
|
||||||
|
public function testGetterMethod(string $template, string $expected): void
|
||||||
|
{
|
||||||
|
$model = new SSTemplateEngineTest\TestObject();
|
||||||
|
$this->assertSame($expected, $this->render($template, $model));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small helper to render templates from strings
|
||||||
|
*/
|
||||||
|
private function render(string $templateString, mixed $data = null, array $overlay = [], bool $cache = false): string
|
||||||
|
{
|
||||||
|
$engine = new SSTemplateEngine();
|
||||||
|
if ($data === null) {
|
||||||
|
$data = new SSTemplateEngineTest\TestFixture();
|
||||||
|
}
|
||||||
|
$data = new ViewLayerData($data);
|
||||||
|
return trim('' . $engine->renderString($templateString, $data, $overlay, $cache));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function _renderWithSourceFileComments($name, $expected)
|
||||||
|
{
|
||||||
|
$viewer = new SSViewer([$name]);
|
||||||
|
$data = new ArrayData([]);
|
||||||
|
$result = $viewer->process($data);
|
||||||
|
$expected = str_replace(["\r", "\n"], '', $expected ?? '');
|
||||||
|
$result = str_replace(["\r", "\n"], '', $result ?? '');
|
||||||
|
$this->assertEquals($result, $expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getScopeInheritanceTestData()
|
||||||
|
{
|
||||||
|
return new ArrayData([
|
||||||
|
'Title' => 'TopTitleValue',
|
||||||
|
'Items' => new ArrayList([
|
||||||
|
new ArrayData(['Title' => 'Item 1']),
|
||||||
|
new ArrayData(['Title' => 'Item 2']),
|
||||||
|
new ArrayData(['Title' => 'Item 3']),
|
||||||
|
new ArrayData(['Title' => 'Item 4']),
|
||||||
|
new ArrayData(['Title' => 'Item 5']),
|
||||||
|
new ArrayData(['Title' => 'Item 6'])
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function assertExpectedStrings($result, $expected)
|
||||||
|
{
|
||||||
|
foreach ($expected as $expectedStr) {
|
||||||
|
$this->assertTrue(
|
||||||
|
(boolean) preg_match("/{$expectedStr}/", $result ?? ''),
|
||||||
|
"Didn't find '{$expectedStr}' in:\n{$result}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function assertEqualIgnoringWhitespace($a, $b, $message = '')
|
||||||
|
{
|
||||||
|
$this->assertEquals(preg_replace('/\s+/', '', $a ?? ''), preg_replace('/\s+/', '', $b ?? ''), $message);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace SilverStripe\View\Tests\SSViewerTest;
|
namespace SilverStripe\View\Tests\SSTemplateEngineTest;
|
||||||
|
|
||||||
use SilverStripe\Dev\TestOnly;
|
use SilverStripe\Dev\TestOnly;
|
||||||
use SilverStripe\Model\List\ArrayList;
|
use SilverStripe\Model\List\ArrayList;
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace SilverStripe\View\Tests\SSViewerTest;
|
namespace SilverStripe\View\Tests\SSTemplateEngineTest;
|
||||||
|
|
||||||
use SilverStripe\Dev\TestOnly;
|
use SilverStripe\Dev\TestOnly;
|
||||||
use SilverStripe\Model\List\ArrayList;
|
use SilverStripe\Model\List\ArrayList;
|
85
tests/php/View/SSTemplateEngineTest/TestFixture.php
Normal file
85
tests/php/View/SSTemplateEngineTest/TestFixture.php
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\SSTemplateEngineTest;
|
||||||
|
|
||||||
|
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 implements TestOnly, Stringable
|
||||||
|
{
|
||||||
|
private ?string $name;
|
||||||
|
|
||||||
|
public function __construct($name = null)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __call(string $name, array $arguments = []): static|array|null
|
||||||
|
{
|
||||||
|
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 __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]+)$/', $name ?? '', $matches)) {
|
||||||
|
$output = [];
|
||||||
|
for ($i = 0; $i < $matches[1]; $i++) {
|
||||||
|
$output[] = new TestFixture($childName);
|
||||||
|
}
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/NotSet/i', $name)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TestFixture($childName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function argedName(string $fieldName, array $arguments): string
|
||||||
|
{
|
||||||
|
$childName = $this->name ? "$this->name.$fieldName" : $fieldName;
|
||||||
|
if ($arguments) {
|
||||||
|
return $childName . '(' . implode(',', $arguments) . ')';
|
||||||
|
} else {
|
||||||
|
return $childName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
tests/php/View/SSTemplateEngineTest/TestGlobalProvider.php
Normal file
51
tests/php/View/SSTemplateEngineTest/TestGlobalProvider.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\SSTemplateEngineTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\View\TemplateGlobalProvider;
|
||||||
|
|
||||||
|
class TestGlobalProvider implements TemplateGlobalProvider, TestOnly
|
||||||
|
{
|
||||||
|
|
||||||
|
public static function get_template_global_variables()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'SSTemplateEngineTest_GlobalHTMLFragment' => ['method' => 'get_html', 'casting' => 'HTMLFragment'],
|
||||||
|
'SSTemplateEngineTest_GlobalHTMLEscaped' => ['method' => 'get_html'],
|
||||||
|
|
||||||
|
'SSTemplateEngineTest_GlobalAutomatic',
|
||||||
|
'SSTemplateEngineTest_GlobalReferencedByString' => 'get_reference',
|
||||||
|
'SSTemplateEngineTest_GlobalReferencedInArray' => ['method' => 'get_reference'],
|
||||||
|
|
||||||
|
'SSTemplateEngineTest_GlobalThatTakesArguments' => ['method' => 'get_argmix', 'casting' => 'HTMLFragment'],
|
||||||
|
'SSTemplateEngineTest_GlobalReturnsNull' => 'getNull',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_html()
|
||||||
|
{
|
||||||
|
return '<div></div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function SSTemplateEngineTest_GlobalAutomatic()
|
||||||
|
{
|
||||||
|
return 'automatic';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_reference()
|
||||||
|
{
|
||||||
|
return 'reference';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_argmix()
|
||||||
|
{
|
||||||
|
$args = func_get_args();
|
||||||
|
return 'z' . implode(':', $args) . 'z';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getNull()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace SilverStripe\View\Tests\SSViewerTest;
|
namespace SilverStripe\View\Tests\SSTemplateEngineTest;
|
||||||
|
|
||||||
use SilverStripe\Dev\TestOnly;
|
use SilverStripe\Dev\TestOnly;
|
||||||
use SilverStripe\Model\ModelData;
|
use SilverStripe\Model\ModelData;
|
||||||
@ -29,9 +29,13 @@ class TestModelData extends ModelData implements TestOnly
|
|||||||
return "arg1:{$arg1},arg2:{$arg2}";
|
return "arg1:{$arg1},arg2:{$arg2}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function methodWithTypedArguments($arg1, $arg2, $arg3)
|
public function methodWithTypedArguments(...$args)
|
||||||
{
|
{
|
||||||
return 'arg1:' . json_encode($arg1) . ',arg2:' . json_encode($arg2) . ',arg3:' . json_encode($arg3);
|
$ret = [];
|
||||||
|
foreach ($args as $i => $arg) {
|
||||||
|
$ret[] = "arg$i:" . json_encode($arg);
|
||||||
|
}
|
||||||
|
return implode(',', $ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Type($arg)
|
public function Type($arg)
|
@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace SilverStripe\View\Tests\SSViewerTest;
|
namespace SilverStripe\View\Tests\SSTemplateEngineTest;
|
||||||
|
|
||||||
use SilverStripe\Dev\TestOnly;
|
use SilverStripe\Dev\TestOnly;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
class TestObject extends DataObject implements TestOnly
|
class TestObject extends DataObject implements TestOnly
|
||||||
{
|
{
|
||||||
private static $table_name = 'SSViewerTest_Object';
|
private static $table_name = 'SSTemplateEngineTest_Object';
|
||||||
|
|
||||||
public $number = null;
|
public $number = null;
|
||||||
|
|
||||||
@ -41,4 +41,12 @@ class TestObject extends DataObject implements TestOnly
|
|||||||
{
|
{
|
||||||
return 'some/url.html';
|
return 'some/url.html';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getMyProperty(mixed $someArg = null): string
|
||||||
|
{
|
||||||
|
if ($someArg) {
|
||||||
|
return "Was passed in: $someArg";
|
||||||
|
}
|
||||||
|
return 'Nothing passed in';
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1 @@
|
|||||||
|
alert('a');
|
@ -1,6 +1,6 @@
|
|||||||
$Title
|
$Title
|
||||||
<% if Children %>
|
<% if Children %>
|
||||||
<% loop Children %>
|
<% loop Children %>
|
||||||
<% include SSViewerTestRecursiveInclude %>
|
<% include SSTemplateEngineTestRecursiveInclude %>
|
||||||
<% end_loop %>
|
<% end_loop %>
|
||||||
<% end_if %>
|
<% end_if %>
|
@ -0,0 +1 @@
|
|||||||
|
<div class='typography'><% include SSTemplateEngineTestCommentsInclude %></div>
|
@ -0,0 +1,3 @@
|
|||||||
|
<% loop Items %>
|
||||||
|
<% include SSTemplateEngineTestIncludeScopeInheritanceInclude %>
|
||||||
|
<% end_loop %>
|
@ -0,0 +1,3 @@
|
|||||||
|
<% loop Items %>
|
||||||
|
<% include SSTemplateEngineTestIncludeScopeInheritanceInclude ArgA=$Title %>
|
||||||
|
<% end_loop %>
|
@ -0,0 +1,6 @@
|
|||||||
|
<html>
|
||||||
|
<% include SSTemplateEngineTestProcessHead %>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1 @@
|
|||||||
|
SSTemplateEngineTest
|
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"name": "silverstripe/module",
|
||||||
|
"type": "silverstripe-vendormodule"
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
<?php
|
@ -2,69 +2,19 @@
|
|||||||
|
|
||||||
namespace SilverStripe\View\Tests;
|
namespace SilverStripe\View\Tests;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use LogicException;
|
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
|
||||||
use Silverstripe\Assets\Dev\TestAssetStore;
|
|
||||||
use SilverStripe\Control\ContentNegotiator;
|
|
||||||
use SilverStripe\Control\Controller;
|
use SilverStripe\Control\Controller;
|
||||||
use SilverStripe\Control\Director;
|
|
||||||
use SilverStripe\Control\HTTPResponse;
|
|
||||||
use SilverStripe\Core\Convert;
|
use SilverStripe\Core\Convert;
|
||||||
use SilverStripe\Core\Injector\Injector;
|
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
use SilverStripe\i18n\i18n;
|
|
||||||
use SilverStripe\Model\List\ArrayList;
|
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
use SilverStripe\ORM\FieldType\DBField;
|
|
||||||
use SilverStripe\Model\List\PaginatedList;
|
|
||||||
use SilverStripe\Security\Permission;
|
|
||||||
use SilverStripe\Security\Security;
|
|
||||||
use SilverStripe\Security\SecurityToken;
|
|
||||||
use SilverStripe\Model\ArrayData;
|
|
||||||
use SilverStripe\View\Requirements;
|
use SilverStripe\View\Requirements;
|
||||||
use SilverStripe\View\Requirements_Backend;
|
|
||||||
use SilverStripe\View\SSTemplateParseException;
|
|
||||||
use SilverStripe\View\SSTemplateParser;
|
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
use SilverStripe\View\SSViewer_FromString;
|
|
||||||
use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModel;
|
use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModel;
|
||||||
use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModelController;
|
use SilverStripe\View\Tests\SSViewerTest\SSViewerTestModelController;
|
||||||
use SilverStripe\View\Tests\SSViewerTest\TestModelData;
|
use SilverStripe\View\Tests\SSViewerTest\DummyTemplateEngine;
|
||||||
use SilverStripe\Model\ModelData;
|
|
||||||
use PHPUnit\Framework\Attributes\DataProvider;
|
|
||||||
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
|
|
||||||
|
|
||||||
class SSViewerTest extends SapphireTest
|
class SSViewerTest extends SapphireTest
|
||||||
{
|
{
|
||||||
|
protected $usesDatabase = false;
|
||||||
/**
|
|
||||||
* Backup of $_SERVER global
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $oldServer = [];
|
|
||||||
|
|
||||||
protected static $extra_dataobjects = [
|
|
||||||
SSViewerTest\TestObject::class,
|
|
||||||
];
|
|
||||||
|
|
||||||
protected function setUp(): void
|
|
||||||
{
|
|
||||||
parent::setUp();
|
|
||||||
SSViewer::config()->set('source_file_comments', false);
|
|
||||||
SSViewer_FromString::config()->set('cache_template', false);
|
|
||||||
TestAssetStore::activate('SSViewerTest');
|
|
||||||
$this->oldServer = $_SERVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function tearDown(): void
|
|
||||||
{
|
|
||||||
$_SERVER = $this->oldServer;
|
|
||||||
TestAssetStore::reset();
|
|
||||||
parent::tearDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for themes helper functions, ensuring they behave as defined in the RFC at
|
* Tests for themes helper functions, ensuring they behave as defined in the RFC at
|
||||||
@ -85,1786 +35,28 @@ class SSViewerTest extends SapphireTest
|
|||||||
$this->assertEquals(['mytheme', 'my_more_important_theme', '$default'], SSViewer::get_themes());
|
$this->assertEquals(['mytheme', 'my_more_important_theme', '$default'], SSViewer::get_themes());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testRequirementsInjected()
|
||||||
* Test that a template without a <head> tag still renders.
|
|
||||||
*/
|
|
||||||
public function testTemplateWithoutHeadRenders()
|
|
||||||
{
|
{
|
||||||
$data = new ArrayData([ 'Var' => 'var value' ]);
|
Requirements::clear();
|
||||||
$result = $data->renderWith("SSViewerTestPartialTemplate");
|
|
||||||
$this->assertEquals('Test partial template: var value', trim(preg_replace("/<!--.*-->/U", '', $result ?? '') ?? ''));
|
try {
|
||||||
|
Requirements::customCSS('pretend this is real css');
|
||||||
|
$viewer = new SSViewer([], new DummyTemplateEngine());
|
||||||
|
$result1 = $viewer->process('pretend this is a model')->getValue();
|
||||||
|
// if we disable the requirements then we should get nothing
|
||||||
|
$viewer->includeRequirements(false);
|
||||||
|
$result2 = $viewer->process('pretend this is a model')->getValue();
|
||||||
|
} finally {
|
||||||
|
Requirements::restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensure global methods aren't executed
|
|
||||||
*/
|
|
||||||
public function testTemplateExecution()
|
|
||||||
{
|
|
||||||
$data = new ArrayData([ 'Var' => 'phpinfo' ]);
|
|
||||||
$result = $data->renderWith("SSViewerTestPartialTemplate");
|
|
||||||
$this->assertEquals('Test partial template: phpinfo', trim(preg_replace("/<!--.*-->/U", '', $result ?? '') ?? ''));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testIncludeScopeInheritance()
|
|
||||||
{
|
|
||||||
$data = $this->getScopeInheritanceTestData();
|
|
||||||
$expected = [
|
|
||||||
'Item 1 - First-ODD top:Item 1',
|
|
||||||
'Item 2 - EVEN top:Item 2',
|
|
||||||
'Item 3 - ODD top:Item 3',
|
|
||||||
'Item 4 - EVEN top:Item 4',
|
|
||||||
'Item 5 - ODD top:Item 5',
|
|
||||||
'Item 6 - Last-EVEN top:Item 6',
|
|
||||||
];
|
|
||||||
|
|
||||||
$result = $data->renderWith('SSViewerTestIncludeScopeInheritance');
|
|
||||||
$this->assertExpectedStrings($result, $expected);
|
|
||||||
|
|
||||||
// reset results for the tests that include arguments (the title is passed as an arg)
|
|
||||||
$expected = [
|
|
||||||
'Item 1 _ Item 1 - First-ODD top:Item 1',
|
|
||||||
'Item 2 _ Item 2 - EVEN top:Item 2',
|
|
||||||
'Item 3 _ Item 3 - ODD top:Item 3',
|
|
||||||
'Item 4 _ Item 4 - EVEN top:Item 4',
|
|
||||||
'Item 5 _ Item 5 - ODD top:Item 5',
|
|
||||||
'Item 6 _ Item 6 - Last-EVEN top:Item 6',
|
|
||||||
];
|
|
||||||
|
|
||||||
$result = $data->renderWith('SSViewerTestIncludeScopeInheritanceWithArgs');
|
|
||||||
$this->assertExpectedStrings($result, $expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testIncludeTruthyness()
|
|
||||||
{
|
|
||||||
$data = new ArrayData([
|
|
||||||
'Title' => 'TruthyTest',
|
|
||||||
'Items' => new ArrayList([
|
|
||||||
new ArrayData(['Title' => 'Item 1']),
|
|
||||||
new ArrayData(['Title' => '']),
|
|
||||||
new ArrayData(['Title' => true]),
|
|
||||||
new ArrayData(['Title' => false]),
|
|
||||||
new ArrayData(['Title' => null]),
|
|
||||||
new ArrayData(['Title' => 0]),
|
|
||||||
new ArrayData(['Title' => 7])
|
|
||||||
])
|
|
||||||
]);
|
|
||||||
$result = $data->renderWith('SSViewerTestIncludeScopeInheritanceWithArgs');
|
|
||||||
|
|
||||||
// We should not end up with empty values appearing as empty
|
|
||||||
$expected = [
|
|
||||||
'Item 1 _ Item 1 - First-ODD top:Item 1',
|
|
||||||
'Untitled - EVEN top:',
|
|
||||||
'1 _ 1 - ODD top:1',
|
|
||||||
'Untitled - EVEN top:',
|
|
||||||
'Untitled - ODD top:',
|
|
||||||
'Untitled - EVEN top:0',
|
|
||||||
'7 _ 7 - Last-ODD top:7',
|
|
||||||
];
|
|
||||||
$this->assertExpectedStrings($result, $expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getScopeInheritanceTestData()
|
|
||||||
{
|
|
||||||
return new ArrayData([
|
|
||||||
'Title' => 'TopTitleValue',
|
|
||||||
'Items' => new ArrayList([
|
|
||||||
new ArrayData(['Title' => 'Item 1']),
|
|
||||||
new ArrayData(['Title' => 'Item 2']),
|
|
||||||
new ArrayData(['Title' => 'Item 3']),
|
|
||||||
new ArrayData(['Title' => 'Item 4']),
|
|
||||||
new ArrayData(['Title' => 'Item 5']),
|
|
||||||
new ArrayData(['Title' => 'Item 6'])
|
|
||||||
])
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function assertExpectedStrings($result, $expected)
|
|
||||||
{
|
|
||||||
foreach ($expected as $expectedStr) {
|
|
||||||
$this->assertTrue(
|
|
||||||
(boolean) preg_match("/{$expectedStr}/", $result ?? ''),
|
|
||||||
"Didn't find '{$expectedStr}' in:\n{$result}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Small helper to render templates from strings
|
|
||||||
*
|
|
||||||
* @param string $templateString
|
|
||||||
* @param null $data
|
|
||||||
* @param bool $cacheTemplate
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function render($templateString, $data = null, $cacheTemplate = false)
|
|
||||||
{
|
|
||||||
$t = SSViewer::fromString($templateString, $cacheTemplate);
|
|
||||||
if (!$data) {
|
|
||||||
$data = new SSViewerTest\TestFixture();
|
|
||||||
}
|
|
||||||
return trim('' . $t->process($data));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRequirements()
|
|
||||||
{
|
|
||||||
/** @var Requirements_Backend|MockObject $requirements */
|
|
||||||
$requirements = $this
|
|
||||||
->getMockBuilder(Requirements_Backend::class)
|
|
||||||
->onlyMethods(["javascript", "css"])
|
|
||||||
->getMock();
|
|
||||||
$jsFile = FRAMEWORK_DIR . '/tests/forms/a.js';
|
|
||||||
$cssFile = FRAMEWORK_DIR . '/tests/forms/a.js';
|
|
||||||
|
|
||||||
$requirements->expects($this->once())->method('javascript')->with($jsFile);
|
|
||||||
$requirements->expects($this->once())->method('css')->with($cssFile);
|
|
||||||
|
|
||||||
$origReq = Requirements::backend();
|
|
||||||
Requirements::set_backend($requirements);
|
|
||||||
$template = $this->render(
|
|
||||||
"<% require javascript($jsFile) %>
|
|
||||||
<% require css($cssFile) %>"
|
|
||||||
);
|
|
||||||
Requirements::set_backend($origReq);
|
|
||||||
|
|
||||||
$this->assertFalse((bool)trim($template ?? ''), "Should be no content in this return.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRequirementsCombine()
|
|
||||||
{
|
|
||||||
/** @var Requirements_Backend $testBackend */
|
|
||||||
$testBackend = Injector::inst()->create(Requirements_Backend::class);
|
|
||||||
$testBackend->setSuffixRequirements(false);
|
|
||||||
$testBackend->setCombinedFilesEnabled(true);
|
|
||||||
|
|
||||||
//$combinedTestFilePath = BASE_PATH . '/' . $testBackend->getCombinedFilesFolder() . '/testRequirementsCombine.js';
|
|
||||||
|
|
||||||
$jsFile = $this->getCurrentRelativePath() . '/SSViewerTest/javascript/bad.js';
|
|
||||||
$jsFileContents = file_get_contents(BASE_PATH . '/' . $jsFile);
|
|
||||||
$testBackend->combineFiles('testRequirementsCombine.js', [$jsFile]);
|
|
||||||
|
|
||||||
// secondly, make sure that requirements is generated, even though minification failed
|
|
||||||
$testBackend->processCombinedFiles();
|
|
||||||
$js = array_keys($testBackend->getJavascript() ?? []);
|
|
||||||
$combinedTestFilePath = Director::publicFolder() . reset($js);
|
|
||||||
$this->assertStringContainsString('_combinedfiles/testRequirementsCombine-4c0e97a.js', $combinedTestFilePath);
|
|
||||||
|
|
||||||
// and make sure the combined content matches the input content, i.e. no loss of functionality
|
|
||||||
if (!file_exists($combinedTestFilePath ?? '')) {
|
|
||||||
$this->fail('No combined file was created at expected path: ' . $combinedTestFilePath);
|
|
||||||
}
|
|
||||||
$combinedTestFileContents = file_get_contents($combinedTestFilePath ?? '');
|
|
||||||
$this->assertStringContainsString($jsFileContents, $combinedTestFileContents);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testComments()
|
|
||||||
{
|
|
||||||
$input = <<<SS
|
|
||||||
This is my template<%-- this is a comment --%>This is some content<%-- this is another comment --%>Final content
|
|
||||||
<%-- Alone multi
|
|
||||||
line comment --%>
|
|
||||||
Some more content
|
|
||||||
Mixing content and <%-- multi
|
|
||||||
line comment --%> Final final
|
|
||||||
content
|
|
||||||
<%--commentwithoutwhitespace--%>last content
|
|
||||||
SS;
|
|
||||||
$actual = $this->render($input);
|
|
||||||
$expected = <<<SS
|
|
||||||
This is my templateThis is some contentFinal content
|
|
||||||
|
|
||||||
Some more content
|
|
||||||
Mixing content and Final final
|
|
||||||
content
|
|
||||||
last content
|
|
||||||
SS;
|
|
||||||
$this->assertEquals($expected, $actual);
|
|
||||||
|
|
||||||
$input = <<<SS
|
|
||||||
<%--
|
|
||||||
|
|
||||||
--%>empty comment1
|
|
||||||
<%-- --%>empty comment2
|
|
||||||
<%----%>empty comment3
|
|
||||||
SS;
|
|
||||||
$actual = $this->render($input);
|
|
||||||
$expected = <<<SS
|
|
||||||
empty comment1
|
|
||||||
empty comment2
|
|
||||||
empty comment3
|
|
||||||
SS;
|
|
||||||
$this->assertEquals($expected, $actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBasicText()
|
|
||||||
{
|
|
||||||
$this->assertEquals('"', $this->render('"'), 'Double-quotes are left alone');
|
|
||||||
$this->assertEquals("'", $this->render("'"), 'Single-quotes are left alone');
|
|
||||||
$this->assertEquals('A', $this->render('\\A'), 'Escaped characters are unescaped');
|
|
||||||
$this->assertEquals('\\A', $this->render('\\\\A'), 'Escaped back-slashed are correctly unescaped');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBasicInjection()
|
|
||||||
{
|
|
||||||
$this->assertEquals('[out:Test]', $this->render('$Test'), 'Basic stand-alone injection');
|
|
||||||
$this->assertEquals('[out:Test]', $this->render('{$Test}'), 'Basic stand-alone wrapped injection');
|
|
||||||
$this->assertEquals('A[out:Test]!', $this->render('A$Test!'), 'Basic surrounded injection');
|
|
||||||
$this->assertEquals('A[out:Test]B', $this->render('A{$Test}B'), 'Basic surrounded wrapped injection');
|
|
||||||
|
|
||||||
$this->assertEquals('A$B', $this->render('A\\$B'), 'No injection as $ escaped');
|
|
||||||
$this->assertEquals('A$ B', $this->render('A$ B'), 'No injection as $ not followed by word character');
|
|
||||||
$this->assertEquals('A{$ B', $this->render('A{$ B'), 'No injection as {$ not followed by word character');
|
|
||||||
|
|
||||||
$this->assertEquals('{$Test}', $this->render('{\\$Test}'), 'Escapes can be used to avoid injection');
|
|
||||||
$this->assertEquals(
|
|
||||||
'{\\[out:Test]}',
|
|
||||||
$this->render('{\\\\$Test}'),
|
|
||||||
'Escapes before injections are correctly unescaped'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBasicInjectionMismatchedBrackets()
|
|
||||||
{
|
|
||||||
$this->expectException(SSTemplateParseException::class);
|
|
||||||
$this->expectExceptionMessageMatches('/Malformed bracket injection {\$Value(.*)/');
|
|
||||||
$this->render('A {$Value here');
|
|
||||||
$this->fail("Parser didn't error when encountering mismatched brackets in an injection");
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGlobalVariableCalls()
|
|
||||||
{
|
|
||||||
$this->assertEquals('automatic', $this->render('$SSViewerTest_GlobalAutomatic'));
|
|
||||||
$this->assertEquals('reference', $this->render('$SSViewerTest_GlobalReferencedByString'));
|
|
||||||
$this->assertEquals('reference', $this->render('$SSViewerTest_GlobalReferencedInArray'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGlobalVariableCallsWithArguments()
|
|
||||||
{
|
|
||||||
$this->assertEquals('zz', $this->render('$SSViewerTest_GlobalThatTakesArguments'));
|
|
||||||
$this->assertEquals('zFooz', $this->render('$SSViewerTest_GlobalThatTakesArguments("Foo")'));
|
|
||||||
$this->assertEquals(
|
|
||||||
'zFoo:Bar:Bazz',
|
|
||||||
$this->render('$SSViewerTest_GlobalThatTakesArguments("Foo", "Bar", "Baz")')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'zreferencez',
|
|
||||||
$this->render('$SSViewerTest_GlobalThatTakesArguments($SSViewerTest_GlobalReferencedByString)')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGlobalVariablesAreEscaped()
|
|
||||||
{
|
|
||||||
$this->assertEquals('<div></div>', $this->render('$SSViewerTest_GlobalHTMLFragment'));
|
|
||||||
$this->assertEquals('<div></div>', $this->render('$SSViewerTest_GlobalHTMLEscaped'));
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'z<div></div>z',
|
|
||||||
$this->render('$SSViewerTest_GlobalThatTakesArguments($SSViewerTest_GlobalHTMLFragment)')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'z<div></div>z',
|
|
||||||
$this->render('$SSViewerTest_GlobalThatTakesArguments($SSViewerTest_GlobalHTMLEscaped)')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGlobalVariablesReturnNull()
|
|
||||||
{
|
|
||||||
$this->assertEquals('<p></p>', $this->render('<p>$SSViewerTest_GlobalReturnsNull</p>'));
|
|
||||||
$this->assertEquals('<p></p>', $this->render('<p>$SSViewerTest_GlobalReturnsNull.Chained.Properties</p>'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCoreGlobalVariableCalls()
|
|
||||||
{
|
|
||||||
$this->assertEquals(
|
|
||||||
Director::absoluteBaseURL(),
|
|
||||||
$this->render('{$absoluteBaseURL}'),
|
|
||||||
'Director::absoluteBaseURL can be called from within template'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
Director::absoluteBaseURL(),
|
|
||||||
$this->render('{$AbsoluteBaseURL}'),
|
|
||||||
'Upper-case %AbsoluteBaseURL can be called from within template'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
Director::is_ajax(),
|
|
||||||
$this->render('{$isAjax}'),
|
|
||||||
'All variations of is_ajax result in the correct call'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
Director::is_ajax(),
|
|
||||||
$this->render('{$IsAjax}'),
|
|
||||||
'All variations of is_ajax result in the correct call'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
Director::is_ajax(),
|
|
||||||
$this->render('{$is_ajax}'),
|
|
||||||
'All variations of is_ajax result in the correct call'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
Director::is_ajax(),
|
|
||||||
$this->render('{$Is_ajax}'),
|
|
||||||
'All variations of is_ajax result in the correct call'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
i18n::get_locale(),
|
|
||||||
$this->render('{$i18nLocale}'),
|
|
||||||
'i18n template functions result correct result'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
i18n::get_locale(),
|
|
||||||
$this->render('{$get_locale}'),
|
|
||||||
'i18n template functions result correct result'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
(string)Security::getCurrentUser(),
|
|
||||||
$this->render('{$CurrentMember}'),
|
|
||||||
'Member template functions result correct result'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
(string)Security::getCurrentUser(),
|
|
||||||
$this->render('{$CurrentUser}'),
|
|
||||||
'Member template functions result correct result'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
(string)Security::getCurrentUser(),
|
|
||||||
$this->render('{$currentMember}'),
|
|
||||||
'Member template functions result correct result'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
(string)Security::getCurrentUser(),
|
|
||||||
$this->render('{$currentUser}'),
|
|
||||||
'Member template functions result correct result'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
SecurityToken::getSecurityID(),
|
|
||||||
$this->render('{$getSecurityID}'),
|
|
||||||
'SecurityToken template functions result correct result'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
SecurityToken::getSecurityID(),
|
|
||||||
$this->render('{$SecurityID}'),
|
|
||||||
'SecurityToken template functions result correct result'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
Permission::check("ADMIN"),
|
|
||||||
(bool)$this->render('{$HasPerm(\'ADMIN\')}'),
|
|
||||||
'Permissions template functions result correct result'
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
Permission::check("ADMIN"),
|
|
||||||
(bool)$this->render('{$hasPerm(\'ADMIN\')}'),
|
|
||||||
'Permissions template functions result correct result'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testNonFieldCastingHelpersNotUsedInHasValue()
|
|
||||||
{
|
|
||||||
// check if Link without $ in front of variable
|
|
||||||
$result = $this->render(
|
|
||||||
'A<% if Link %>$Link<% end_if %>B',
|
|
||||||
new SSViewerTest\TestObject()
|
|
||||||
);
|
|
||||||
$this->assertEquals('Asome/url.htmlB', $result, 'casting helper not used for <% if Link %>');
|
|
||||||
|
|
||||||
// check if Link with $ in front of variable
|
|
||||||
$result = $this->render(
|
|
||||||
'A<% if $Link %>$Link<% end_if %>B',
|
|
||||||
new SSViewerTest\TestObject()
|
|
||||||
);
|
|
||||||
$this->assertEquals('Asome/url.htmlB', $result, 'casting helper not used for <% if $Link %>');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLocalFunctionsTakePriorityOverGlobals()
|
|
||||||
{
|
|
||||||
$data = new ArrayData([
|
|
||||||
'Page' => new SSViewerTest\TestObject()
|
|
||||||
]);
|
|
||||||
|
|
||||||
//call method with lots of arguments
|
|
||||||
$result = $this->render(
|
|
||||||
'<% with Page %>$lotsOfArguments11("a","b","c","d","e","f","g","h","i","j","k")<% end_with %>',
|
|
||||||
$data
|
|
||||||
);
|
|
||||||
$this->assertEquals("abcdefghijk", $result, "public function can accept up to 11 arguments");
|
|
||||||
|
|
||||||
//call method that does not exist
|
|
||||||
$result = $this->render('<% with Page %><% if IDoNotExist %>hello<% end_if %><% end_with %>', $data);
|
|
||||||
$this->assertEquals("", $result, "Method does not exist - empty result");
|
|
||||||
|
|
||||||
//call if that does not exist
|
|
||||||
$result = $this->render('<% with Page %>$IDoNotExist("hello")<% end_with %>', $data);
|
|
||||||
$this->assertEquals("", $result, "Method does not exist - empty result");
|
|
||||||
|
|
||||||
//call method with same name as a global method (local call should take priority)
|
|
||||||
$result = $this->render('<% with Page %>$absoluteBaseURL<% end_with %>', $data);
|
|
||||||
$this->assertEquals(
|
|
||||||
"testLocalFunctionPriorityCalled",
|
|
||||||
$result,
|
|
||||||
"Local Object's public function called. Did not return the actual baseURL of the current site"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCurrentScopeLoop(): void
|
|
||||||
{
|
|
||||||
$data = new ArrayList([['Val' => 'one'], ['Val' => 'two'], ['Val' => 'three']]);
|
|
||||||
$this->assertEqualIgnoringWhitespace(
|
$this->assertEqualIgnoringWhitespace(
|
||||||
'one two three',
|
'<html><head><style type="text/css">pretend this is real css</style></head><body></body></html>',
|
||||||
$this->render('<% loop %>$Val<% end_loop %>', $data)
|
$result1
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
public function testCurrentScopeLoopWith()
|
|
||||||
{
|
|
||||||
// Data to run the loop tests on - one sequence of three items, each with a subitem
|
|
||||||
$data = new ArrayData([
|
|
||||||
'Foo' => new ArrayList([
|
|
||||||
'Subocean' => new ArrayData([
|
|
||||||
'Name' => 'Higher'
|
|
||||||
]),
|
|
||||||
new ArrayData([
|
|
||||||
'Sub' => new ArrayData([
|
|
||||||
'Name' => 'SubKid1'
|
|
||||||
])
|
|
||||||
]),
|
|
||||||
new ArrayData([
|
|
||||||
'Sub' => new ArrayData([
|
|
||||||
'Name' => 'SubKid2'
|
|
||||||
])
|
|
||||||
]),
|
|
||||||
new SSViewerTest\TestObject('Number6')
|
|
||||||
])
|
|
||||||
]);
|
|
||||||
|
|
||||||
$result = $this->render(
|
|
||||||
'<% loop Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %><% end_if %><% end_loop %>',
|
|
||||||
$data
|
|
||||||
);
|
|
||||||
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop works");
|
|
||||||
|
|
||||||
$result = $this->render(
|
|
||||||
'<% loop Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %><% end_if %><% end_loop %>',
|
|
||||||
$data
|
|
||||||
);
|
|
||||||
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop works");
|
|
||||||
|
|
||||||
$result = $this->render('<% with Foo %>$Count<% end_with %>', $data);
|
|
||||||
$this->assertEquals("4", $result, "4 items in the DataObjectSet");
|
|
||||||
|
|
||||||
$result = $this->render(
|
|
||||||
'<% with Foo %><% loop Up.Foo %>$Number<% if Sub %><% with Sub %>$Name<% end_with %>'
|
|
||||||
. '<% end_if %><% end_loop %><% end_with %>',
|
|
||||||
$data
|
|
||||||
);
|
|
||||||
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop in with Up.Foo scope works");
|
|
||||||
|
|
||||||
$result = $this->render(
|
|
||||||
'<% with Foo %><% loop %>$Number<% if Sub %><% with Sub %>$Name<% end_with %>'
|
|
||||||
. '<% end_if %><% end_loop %><% end_with %>',
|
|
||||||
$data
|
|
||||||
);
|
|
||||||
$this->assertEquals("SubKid1SubKid2Number6", $result, "Loop in current scope works");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function provideArgumentTypes()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
[
|
|
||||||
'arg1:0,arg2:"string",arg3:true',
|
|
||||||
'$methodWithTypedArguments(0, "string", true).RAW',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'arg1:false,arg2:"string",arg3:true',
|
|
||||||
'$methodWithTypedArguments(false, "string", true).RAW',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'arg1:null,arg2:"string",arg3:true',
|
|
||||||
'$methodWithTypedArguments(null, "string", true).RAW',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'arg1:"",arg2:"string",arg3:true',
|
|
||||||
'$methodWithTypedArguments("", "string", true).RAW',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'arg1:0,arg2:1,arg3:2',
|
|
||||||
'$methodWithTypedArguments(0, 1, 2).RAW',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
#[DataProvider('provideArgumentTypes')]
|
|
||||||
public function testArgumentTypes(string $expected, string $template)
|
|
||||||
{
|
|
||||||
$this->assertEquals($expected, $this->render($template, new TestModelData()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testObjectDotArguments()
|
|
||||||
{
|
|
||||||
$this->assertEquals(
|
|
||||||
'[out:TestObject.methodWithOneArgument(one)]
|
|
||||||
[out:TestObject.methodWithTwoArguments(one,two)]
|
|
||||||
[out:TestMethod(Arg1,Arg2).Bar.Val]
|
|
||||||
[out:TestMethod(Arg1,Arg2).Bar]
|
|
||||||
[out:TestMethod(Arg1,Arg2)]
|
|
||||||
[out:TestMethod(Arg1).Bar.Val]
|
|
||||||
[out:TestMethod(Arg1).Bar]
|
|
||||||
[out:TestMethod(Arg1)]',
|
|
||||||
$this->render(
|
|
||||||
'$TestObject.methodWithOneArgument(one)
|
|
||||||
$TestObject.methodWithTwoArguments(one,two)
|
|
||||||
$TestMethod(Arg1, Arg2).Bar.Val
|
|
||||||
$TestMethod(Arg1, Arg2).Bar
|
|
||||||
$TestMethod(Arg1, Arg2)
|
|
||||||
$TestMethod(Arg1).Bar.Val
|
|
||||||
$TestMethod(Arg1).Bar
|
|
||||||
$TestMethod(Arg1)'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testEscapedArguments()
|
|
||||||
{
|
|
||||||
$this->assertEquals(
|
|
||||||
'[out:Foo(Arg1,Arg2).Bar.Val].Suffix
|
|
||||||
[out:Foo(Arg1,Arg2).Val]_Suffix
|
|
||||||
[out:Foo(Arg1,Arg2)]/Suffix
|
|
||||||
[out:Foo(Arg1).Bar.Val]textSuffix
|
|
||||||
[out:Foo(Arg1).Bar].Suffix
|
|
||||||
[out:Foo(Arg1)].Suffix
|
|
||||||
[out:Foo.Bar.Val].Suffix
|
|
||||||
[out:Foo.Bar].Suffix
|
|
||||||
[out:Foo].Suffix',
|
|
||||||
$this->render(
|
|
||||||
'{$Foo(Arg1, Arg2).Bar.Val}.Suffix
|
|
||||||
{$Foo(Arg1, Arg2).Val}_Suffix
|
|
||||||
{$Foo(Arg1, Arg2)}/Suffix
|
|
||||||
{$Foo(Arg1).Bar.Val}textSuffix
|
|
||||||
{$Foo(Arg1).Bar}.Suffix
|
|
||||||
{$Foo(Arg1)}.Suffix
|
|
||||||
{$Foo.Bar.Val}.Suffix
|
|
||||||
{$Foo.Bar}.Suffix
|
|
||||||
{$Foo}.Suffix'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLoopWhitespace()
|
|
||||||
{
|
|
||||||
$data = new ArrayList([new SSViewerTest\TestFixture()]);
|
|
||||||
$this->assertEquals(
|
|
||||||
'before[out:Test]after
|
|
||||||
beforeTestafter',
|
|
||||||
$this->render(
|
|
||||||
'before<% loop %>$Test<% end_loop %>after
|
|
||||||
before<% loop %>Test<% end_loop %>after',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// The control tags are removed from the output, but no whitespace
|
|
||||||
// This is a quirk that could be changed, but included in the test to make the current
|
|
||||||
// behaviour explicit
|
|
||||||
$this->assertEquals(
|
|
||||||
'before
|
|
||||||
|
|
||||||
[out:ItemOnItsOwnLine]
|
|
||||||
|
|
||||||
after',
|
|
||||||
$this->render(
|
|
||||||
'before
|
|
||||||
<% loop %>
|
|
||||||
$ItemOnItsOwnLine
|
|
||||||
<% end_loop %>
|
|
||||||
after',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// The whitespace within the control tags is preserve in a loop
|
|
||||||
// This is a quirk that could be changed, but included in the test to make the current
|
|
||||||
// behaviour explicit
|
|
||||||
$this->assertEquals(
|
|
||||||
'before
|
|
||||||
|
|
||||||
[out:Loop3.ItemOnItsOwnLine]
|
|
||||||
|
|
||||||
[out:Loop3.ItemOnItsOwnLine]
|
|
||||||
|
|
||||||
[out:Loop3.ItemOnItsOwnLine]
|
|
||||||
|
|
||||||
after',
|
|
||||||
$this->render(
|
|
||||||
'before
|
|
||||||
<% loop Loop3 %>
|
|
||||||
$ItemOnItsOwnLine
|
|
||||||
<% end_loop %>
|
|
||||||
after'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function typePreservationDataProvider()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
// Null
|
|
||||||
['NULL:', 'null'],
|
|
||||||
['NULL:', 'NULL'],
|
|
||||||
// Booleans
|
|
||||||
['boolean:1', 'true'],
|
|
||||||
['boolean:1', 'TRUE'],
|
|
||||||
['boolean:', 'false'],
|
|
||||||
['boolean:', 'FALSE'],
|
|
||||||
// Strings which may look like booleans/null to the parser
|
|
||||||
['string:nullish', 'nullish'],
|
|
||||||
['string:notnull', 'notnull'],
|
|
||||||
['string:truethy', 'truethy'],
|
|
||||||
['string:untrue', 'untrue'],
|
|
||||||
['string:falsey', 'falsey'],
|
|
||||||
// Integers
|
|
||||||
['integer:0', '0'],
|
|
||||||
['integer:1', '1'],
|
|
||||||
['integer:15', '15'],
|
|
||||||
['integer:-15', '-15'],
|
|
||||||
// Octal integers
|
|
||||||
['integer:83', '0123'],
|
|
||||||
['integer:-83', '-0123'],
|
|
||||||
// Hexadecimal integers
|
|
||||||
['integer:26', '0x1A'],
|
|
||||||
['integer:-26', '-0x1A'],
|
|
||||||
// Binary integers
|
|
||||||
['integer:255', '0b11111111'],
|
|
||||||
['integer:-255', '-0b11111111'],
|
|
||||||
// Floats (aka doubles)
|
|
||||||
['double:0', '0.0'],
|
|
||||||
['double:1', '1.0'],
|
|
||||||
['double:15.25', '15.25'],
|
|
||||||
['double:-15.25', '-15.25'],
|
|
||||||
['double:1200', '1.2e3'],
|
|
||||||
['double:-1200', '-1.2e3'],
|
|
||||||
['double:0.07', '7E-2'],
|
|
||||||
['double:-0.07', '-7E-2'],
|
|
||||||
// Explicitly quoted strings
|
|
||||||
['string:0', '"0"'],
|
|
||||||
['string:1', '\'1\''],
|
|
||||||
['string:foobar', '"foobar"'],
|
|
||||||
['string:foo bar baz', '"foo bar baz"'],
|
|
||||||
['string:false', '\'false\''],
|
|
||||||
['string:true', '\'true\''],
|
|
||||||
['string:null', '\'null\''],
|
|
||||||
['string:false', '"false"'],
|
|
||||||
['string:true', '"true"'],
|
|
||||||
['string:null', '"null"'],
|
|
||||||
// Implicit strings
|
|
||||||
['string:foobar', 'foobar'],
|
|
||||||
['string:foo bar baz', 'foo bar baz']
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
#[DataProvider('typePreservationDataProvider')]
|
|
||||||
public function testTypesArePreserved($expected, $templateArg)
|
|
||||||
{
|
|
||||||
$data = new ArrayData([
|
|
||||||
'Test' => new TestModelData()
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertEquals($expected, $this->render("\$Test.Type({$templateArg})", $data));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[DataProvider('typePreservationDataProvider')]
|
|
||||||
public function testTypesArePreservedAsIncludeArguments($expected, $templateArg)
|
|
||||||
{
|
|
||||||
$data = new ArrayData([
|
|
||||||
'Test' => new TestModelData()
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$expected,
|
|
||||||
$this->render("<% include SSViewerTestTypePreservation Argument={$templateArg} %>", $data)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testTypePreservationInConditionals()
|
|
||||||
{
|
|
||||||
$data = new ArrayData([
|
|
||||||
'Test' => new TestModelData()
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Types in conditionals
|
|
||||||
$this->assertEquals('pass', $this->render('<% if true %>pass<% else %>fail<% end_if %>', $data));
|
|
||||||
$this->assertEquals('pass', $this->render('<% if false %>fail<% else %>pass<% end_if %>', $data));
|
|
||||||
$this->assertEquals('pass', $this->render('<% if 1 %>pass<% else %>fail<% end_if %>', $data));
|
|
||||||
$this->assertEquals('pass', $this->render('<% if 0 %>fail<% else %>pass<% end_if %>', $data));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testControls()
|
|
||||||
{
|
|
||||||
// Single item controls
|
|
||||||
$this->assertEquals(
|
|
||||||
'a[out:Foo.Bar.Item]b
|
|
||||||
[out:Foo.Bar(Arg1).Item]
|
|
||||||
[out:Foo(Arg1).Item]
|
|
||||||
[out:Foo(Arg1,Arg2).Item]
|
|
||||||
[out:Foo(Arg1,Arg2,Arg3).Item]',
|
|
||||||
$this->render(
|
|
||||||
'<% with Foo.Bar %>a{$Item}b<% end_with %>
|
|
||||||
<% with Foo.Bar(Arg1) %>$Item<% end_with %>
|
|
||||||
<% with Foo(Arg1) %>$Item<% end_with %>
|
|
||||||
<% with Foo(Arg1, Arg2) %>$Item<% end_with %>
|
|
||||||
<% with Foo(Arg1, Arg2, Arg3) %>$Item<% end_with %>'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Loop controls
|
|
||||||
$this->assertEquals(
|
|
||||||
'a[out:Foo.Loop2.Item]ba[out:Foo.Loop2.Item]b',
|
|
||||||
$this->render('<% loop Foo.Loop2 %>a{$Item}b<% end_loop %>')
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'[out:Foo.Loop2(Arg1).Item][out:Foo.Loop2(Arg1).Item]',
|
|
||||||
$this->render('<% loop Foo.Loop2(Arg1) %>$Item<% end_loop %>')
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'[out:Loop2(Arg1).Item][out:Loop2(Arg1).Item]',
|
|
||||||
$this->render('<% loop Loop2(Arg1) %>$Item<% end_loop %>')
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'[out:Loop2(Arg1,Arg2).Item][out:Loop2(Arg1,Arg2).Item]',
|
|
||||||
$this->render('<% loop Loop2(Arg1, Arg2) %>$Item<% end_loop %>')
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'[out:Loop2(Arg1,Arg2,Arg3).Item][out:Loop2(Arg1,Arg2,Arg3).Item]',
|
|
||||||
$this->render('<% loop Loop2(Arg1, Arg2, Arg3) %>$Item<% end_loop %>')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testIfBlocks()
|
|
||||||
{
|
|
||||||
// Basic test
|
|
||||||
$this->assertEquals(
|
|
||||||
'AC',
|
|
||||||
$this->render('A<% if NotSet %>B$NotSet<% end_if %>C')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Nested test
|
|
||||||
$this->assertEquals(
|
|
||||||
'AB1C',
|
|
||||||
$this->render('A<% if IsSet %>B$NotSet<% if IsSet %>1<% else %>2<% end_if %><% end_if %>C')
|
|
||||||
);
|
|
||||||
|
|
||||||
// else_if
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if NotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'AD',
|
|
||||||
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ADE',
|
|
||||||
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% else_if IsSet %>D<% end_if %>E')
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'ADE',
|
|
||||||
$this->render('A<% if NotSet %>B<% else_if AlsoNotset %>C<% else_if IsSet %>D<% end_if %>E')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Dot syntax
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if Foo.NotSet %>B<% else_if Foo.IsSet %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if Foo.Bar.NotSet %>B<% else_if Foo.Bar.IsSet %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Params
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if NotSet(Param) %>B<% else %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABD',
|
|
||||||
$this->render('A<% if IsSet(Param) %>B<% else %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Negation
|
|
||||||
$this->assertEquals(
|
|
||||||
'AC',
|
|
||||||
$this->render('A<% if not IsSet %>B<% end_if %>C')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABC',
|
|
||||||
$this->render('A<% if not NotSet %>B<% end_if %>C')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Or
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABD',
|
|
||||||
$this->render('A<% if IsSet || NotSet %>B<% else_if A %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'AD',
|
|
||||||
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if NotSet3 %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if IsSet || NotSet %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'AD',
|
|
||||||
$this->render('A<% if NotSet || AlsoNotSet %>B<% else_if NotSet2 || NotSet3 %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Negated Or
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if not IsSet || AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABD',
|
|
||||||
$this->render('A<% if not NotSet || AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABD',
|
|
||||||
$this->render('A<% if NotSet || not AlsoNotSet %>B<% else_if A %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
|
|
||||||
// And
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABD',
|
|
||||||
$this->render('A<% if IsSet && AlsoSet %>B<% else_if A %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if IsSet && NotSet %>B<% else_if IsSet %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'AD',
|
|
||||||
$this->render('A<% if NotSet && NotSet2 %>B<% else_if NotSet3 %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if IsSet && NotSet %>B<% else_if IsSet && AlsoSet %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'AD',
|
|
||||||
$this->render('A<% if NotSet && NotSet2 %>B<% else_if IsSet && NotSet3 %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Equality
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABC',
|
|
||||||
$this->render('A<% if RawVal == RawVal %>B<% end_if %>C')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ACD',
|
|
||||||
$this->render('A<% if Right == Wrong %>B<% else_if RawVal == RawVal %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABC',
|
|
||||||
$this->render('A<% if Right != Wrong %>B<% end_if %>C')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'AD',
|
|
||||||
$this->render('A<% if Right == Wrong %>B<% else_if RawVal != RawVal %>C<% end_if %>D')
|
|
||||||
);
|
|
||||||
|
|
||||||
// test inequalities with simple numbers
|
|
||||||
$this->assertEquals('ABD', $this->render('A<% if 5 > 3 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ABD', $this->render('A<% if 5 >= 3 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ACD', $this->render('A<% if 3 > 5 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ACD', $this->render('A<% if 3 >= 5 %>B<% else %>C<% end_if %>D'));
|
|
||||||
|
|
||||||
$this->assertEquals('ABD', $this->render('A<% if 3 < 5 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ABD', $this->render('A<% if 3 <= 5 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ACD', $this->render('A<% if 5 < 3 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ACD', $this->render('A<% if 5 <= 3 %>B<% else %>C<% end_if %>D'));
|
|
||||||
|
|
||||||
$this->assertEquals('ABD', $this->render('A<% if 4 <= 4 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ABD', $this->render('A<% if 4 >= 4 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ACD', $this->render('A<% if 4 > 4 %>B<% else %>C<% end_if %>D'));
|
|
||||||
$this->assertEquals('ACD', $this->render('A<% if 4 < 4 %>B<% else %>C<% end_if %>D'));
|
|
||||||
|
|
||||||
// empty else_if and else tags, if this would not be supported,
|
|
||||||
// the output would stop after A, thereby failing the assert
|
|
||||||
$this->assertEquals('AD', $this->render('A<% if IsSet %><% else %><% end_if %>D'));
|
|
||||||
$this->assertEquals(
|
|
||||||
'AD',
|
|
||||||
$this->render('A<% if NotSet %><% else_if IsSet %><% else %><% end_if %>D')
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'AD',
|
|
||||||
$this->render('A<% if NotSet %><% else_if AlsoNotSet %><% else %><% end_if %>D')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Bare words with ending space
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABC',
|
|
||||||
$this->render('A<% if "RawVal" == RawVal %>B<% end_if %>C')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Else
|
|
||||||
$this->assertEquals(
|
|
||||||
'ADE',
|
|
||||||
$this->render('A<% if Right == Wrong %>B<% else_if RawVal != RawVal %>C<% else %>D<% end_if %>E')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Empty if with else
|
|
||||||
$this->assertEquals(
|
|
||||||
'ABC',
|
|
||||||
$this->render('A<% if NotSet %><% else %>B<% end_if %>C')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function provideIfBlockWithIterable(): array
|
|
||||||
{
|
|
||||||
$scenarios = [
|
|
||||||
'empty array' => [
|
|
||||||
'iterable' => [],
|
|
||||||
'inScope' => false,
|
|
||||||
],
|
|
||||||
'array' => [
|
|
||||||
'iterable' => [1, 2, 3],
|
|
||||||
'inScope' => false,
|
|
||||||
],
|
|
||||||
'ArrayList' => [
|
|
||||||
'iterable' => new ArrayList([['Val' => 1], ['Val' => 2], ['Val' => 3]]),
|
|
||||||
'inScope' => false,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
foreach ($scenarios as $name => $scenario) {
|
|
||||||
$scenario['inScope'] = true;
|
|
||||||
$scenarios[$name . ' in scope'] = $scenario;
|
|
||||||
}
|
|
||||||
return $scenarios;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[DataProvider('provideIfBlockWithIterable')]
|
|
||||||
public function testIfBlockWithIterable(iterable $iterable, bool $inScope): void
|
|
||||||
{
|
|
||||||
$expected = count($iterable) ? 'has value' : 'no value';
|
|
||||||
$data = new ArrayData(['Iterable' => $iterable]);
|
|
||||||
if ($inScope) {
|
|
||||||
$template = '<% with $Iterable %><% if $Me %>has value<% else %>no value<% end_if %><% end_with %>';
|
|
||||||
} else {
|
|
||||||
$template = '<% if $Iterable %>has value<% else %>no value<% end_if %>';
|
|
||||||
}
|
|
||||||
$this->assertEqualIgnoringWhitespace($expected, $this->render($template, $data));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testBaseTagGeneration()
|
|
||||||
{
|
|
||||||
// XHTML will have a closed base tag
|
|
||||||
$tmpl1 = '<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
|
|
||||||
. ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
||||||
<html>
|
|
||||||
<head><% base_tag %></head>
|
|
||||||
<body><p>test</p><body>
|
|
||||||
</html>';
|
|
||||||
$this->assertMatchesRegularExpression('/<head><base href=".*" \/><\/head>/', $this->render($tmpl1));
|
|
||||||
|
|
||||||
// HTML4 and 5 will only have it for IE
|
|
||||||
$tmpl2 = '<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head><% base_tag %></head>
|
|
||||||
<body><p>test</p><body>
|
|
||||||
</html>';
|
|
||||||
$this->assertMatchesRegularExpression(
|
|
||||||
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
|
|
||||||
$this->render($tmpl2)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
$tmpl3 = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
||||||
<html>
|
|
||||||
<head><% base_tag %></head>
|
|
||||||
<body><p>test</p><body>
|
|
||||||
</html>';
|
|
||||||
$this->assertMatchesRegularExpression(
|
|
||||||
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
|
|
||||||
$this->render($tmpl3)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check that the content negotiator converts to the equally legal formats
|
|
||||||
$negotiator = new ContentNegotiator();
|
|
||||||
|
|
||||||
$response = new HTTPResponse($this->render($tmpl1));
|
|
||||||
$negotiator->html($response);
|
|
||||||
$this->assertMatchesRegularExpression(
|
|
||||||
'/<head><base href=".*"><!--\[if lte IE 6\]><\/base><!\[endif\]--><\/head>/',
|
|
||||||
$response->getBody()
|
|
||||||
);
|
|
||||||
|
|
||||||
$response = new HTTPResponse($this->render($tmpl1));
|
|
||||||
$negotiator->xhtml($response);
|
|
||||||
$this->assertMatchesRegularExpression('/<head><base href=".*" \/><\/head>/', $response->getBody());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testIncludeWithArguments()
|
|
||||||
{
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render('<% include SSViewerTestIncludeWithArguments %>'),
|
|
||||||
'<p>[out:Arg1]</p><p>[out:Arg2]</p><p>[out:Arg2.Count]</p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render('<% include SSViewerTestIncludeWithArguments Arg1=A %>'),
|
|
||||||
'<p>A</p><p>[out:Arg2]</p><p>[out:Arg2.Count]</p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render('<% include SSViewerTestIncludeWithArguments Arg1=A, Arg2=B %>'),
|
|
||||||
'<p>A</p><p>B</p><p></p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render('<% include SSViewerTestIncludeWithArguments Arg1=A Bare String, Arg2=B Bare String %>'),
|
|
||||||
'<p>A Bare String</p><p>B Bare String</p><p></p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render(
|
|
||||||
'<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=$B %>',
|
|
||||||
new ArrayData(['B' => 'Bar'])
|
|
||||||
),
|
|
||||||
'<p>A</p><p>Bar</p><p></p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render(
|
|
||||||
'<% include SSViewerTestIncludeWithArguments Arg1="A" %>',
|
|
||||||
new ArrayData(['Arg1' => 'Foo', 'Arg2' => 'Bar'])
|
|
||||||
),
|
|
||||||
'<p>A</p><p>Bar</p><p></p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=0 %>'),
|
|
||||||
'<p>A</p><p>0</p><p></p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=false %>'),
|
|
||||||
'<p>A</p><p></p><p></p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render('<% include SSViewerTestIncludeWithArguments Arg1="A", Arg2=null %>'),
|
|
||||||
'<p>A</p><p></p><p></p>'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render(
|
|
||||||
'<% include SSViewerTestIncludeScopeInheritanceWithArgsInLoop Title="SomeArg" %>',
|
|
||||||
new ArrayData(
|
|
||||||
['Items' => new ArrayList(
|
|
||||||
[
|
|
||||||
new ArrayData(['Title' => 'Foo']),
|
|
||||||
new ArrayData(['Title' => 'Bar'])
|
|
||||||
]
|
|
||||||
)]
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'SomeArg - Foo - Bar - SomeArg'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render(
|
|
||||||
'<% include SSViewerTestIncludeScopeInheritanceWithArgsInWith Title="A" %>',
|
|
||||||
new ArrayData(['Item' => new ArrayData(['Title' =>'B'])])
|
|
||||||
),
|
|
||||||
'A - B - A'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render(
|
|
||||||
'<% include SSViewerTestIncludeScopeInheritanceWithArgsInNestedWith Title="A" %>',
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Item' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Title' =>'B', 'NestedItem' => new ArrayData(['Title' => 'C'])
|
|
||||||
]
|
|
||||||
)]
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'A - B - C - B - A'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
$this->render(
|
|
||||||
'<% include SSViewerTestIncludeScopeInheritanceWithUpAndTop Title="A" %>',
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Item' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Title' =>'B', 'NestedItem' => new ArrayData(['Title' => 'C'])
|
|
||||||
]
|
|
||||||
)]
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'A - A - A'
|
|
||||||
);
|
|
||||||
|
|
||||||
$data = new ArrayData(
|
|
||||||
[
|
|
||||||
'Nested' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Object' => new ArrayData(['Key' => 'A'])
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'Object' => new ArrayData(['Key' => 'B'])
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$tmpl = SSViewer::fromString('<% include SSViewerTestIncludeObjectArguments A=$Nested.Object, B=$Object %>');
|
|
||||||
$res = $tmpl->process($data);
|
|
||||||
$this->assertEqualIgnoringWhitespace('A B', $res, 'Objects can be passed as named arguments');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testNamespaceInclude()
|
|
||||||
{
|
|
||||||
$data = new ArrayData([]);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"tests:( NamespaceInclude\n )",
|
|
||||||
$this->render('tests:( <% include Namespace\NamespaceInclude %> )', $data),
|
|
||||||
'Backslashes work for namespace references in includes'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"tests:( NamespaceInclude\n )",
|
|
||||||
$this->render('tests:( <% include Namespace\\NamespaceInclude %> )', $data),
|
|
||||||
'Escaped backslashes work for namespace references in includes'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"tests:( NamespaceInclude\n )",
|
|
||||||
$this->render('tests:( <% include Namespace/NamespaceInclude %> )', $data),
|
|
||||||
'Forward slashes work for namespace references in includes'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test search for includes fallback to non-includes folder
|
|
||||||
*/
|
|
||||||
public function testIncludeFallbacks()
|
|
||||||
{
|
|
||||||
$data = new ArrayData([]);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"tests:( Namespace/Includes/IncludedTwice.ss\n )",
|
|
||||||
$this->render('tests:( <% include Namespace\\IncludedTwice %> )', $data),
|
|
||||||
'Prefer Includes in the Includes folder'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"tests:( Namespace/Includes/IncludedOnceSub.ss\n )",
|
|
||||||
$this->render('tests:( <% include Namespace\\IncludedOnceSub %> )', $data),
|
|
||||||
'Includes in only Includes folder can be found'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
"tests:( Namespace/IncludedOnceBase.ss\n )",
|
|
||||||
$this->render('tests:( <% include Namespace\\IncludedOnceBase %> )', $data),
|
|
||||||
'Includes outside of Includes folder can be found'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRecursiveInclude()
|
|
||||||
{
|
|
||||||
$view = new SSViewer(['Includes/SSViewerTestRecursiveInclude']);
|
|
||||||
|
|
||||||
$data = new ArrayData(
|
|
||||||
[
|
|
||||||
'Title' => 'A',
|
|
||||||
'Children' => new ArrayList(
|
|
||||||
[
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Title' => 'A1',
|
|
||||||
'Children' => new ArrayList(
|
|
||||||
[
|
|
||||||
new ArrayData([ 'Title' => 'A1 i', ]),
|
|
||||||
new ArrayData([ 'Title' => 'A1 ii', ]),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
new ArrayData([ 'Title' => 'A2', ]),
|
|
||||||
new ArrayData([ 'Title' => 'A3', ]),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$result = $view->process($data);
|
|
||||||
// We don't care about whitespace
|
|
||||||
$rationalisedResult = trim(preg_replace('/\s+/', ' ', $result ?? '') ?? '');
|
|
||||||
|
|
||||||
$this->assertEquals('A A1 A1 i A1 ii A2 A3', $rationalisedResult);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function assertEqualIgnoringWhitespace($a, $b, $message = '')
|
|
||||||
{
|
|
||||||
$this->assertEquals(preg_replace('/\s+/', '', $a ?? ''), preg_replace('/\s+/', '', $b ?? ''), $message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* See {@link ModelDataTest} for more extensive casting tests,
|
|
||||||
* this test just ensures that basic casting is correctly applied during template parsing.
|
|
||||||
*/
|
|
||||||
public function testCastingHelpers()
|
|
||||||
{
|
|
||||||
$vd = new SSViewerTest\TestModelData();
|
|
||||||
$vd->TextValue = '<b>html</b>';
|
|
||||||
$vd->HTMLValue = '<b>html</b>';
|
|
||||||
$vd->UncastedValue = '<b>html</b>';
|
|
||||||
|
|
||||||
// Value casted as "Text"
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$TextValue')->process($vd)
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$TextValue.RAW')->process($vd)
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$TextValue.XML')->process($vd)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Value casted as "HTMLText"
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$HTMLValue')->process($vd)
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$HTMLValue.RAW')->process($vd)
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$HTMLValue.XML')->process($vd)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Uncasted value (falls back to ModelData::$default_cast="Text")
|
|
||||||
$vd = new SSViewerTest\TestModelData();
|
|
||||||
$vd->UncastedValue = '<b>html</b>';
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$UncastedValue')->process($vd)
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$UncastedValue.RAW')->process($vd)
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
'<b>html</b>',
|
|
||||||
$t = SSViewer::fromString('$UncastedValue.XML')->process($vd)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function provideLoop(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'nested array and iterator' => [
|
|
||||||
'iterable' => [
|
|
||||||
[
|
|
||||||
'value 1',
|
|
||||||
'value 2',
|
|
||||||
],
|
|
||||||
new ArrayList([
|
|
||||||
'value 3',
|
|
||||||
'value 4',
|
|
||||||
]),
|
|
||||||
],
|
|
||||||
'template' => '<% loop $Iterable %><% loop $Me %>$Me<% end_loop %><% end_loop %>',
|
|
||||||
'expected' => 'value 1 value 2 value 3 value 4',
|
|
||||||
],
|
|
||||||
'nested associative arrays' => [
|
|
||||||
'iterable' => [
|
|
||||||
[
|
|
||||||
'Foo' => 'one',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'Foo' => 'two',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'Foo' => 'three',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'template' => '<% loop $Iterable %>$Foo<% end_loop %>',
|
|
||||||
'expected' => 'one two three',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
#[DataProvider('provideLoop')]
|
|
||||||
public function testLoop(iterable $iterable, string $template, string $expected): void
|
|
||||||
{
|
|
||||||
$data = new ArrayData(['Iterable' => $iterable]);
|
|
||||||
$this->assertEqualIgnoringWhitespace($expected, $this->render($template, $data));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function provideCountIterable(): array
|
|
||||||
{
|
|
||||||
$scenarios = [
|
|
||||||
'empty array' => [
|
|
||||||
'iterable' => [],
|
|
||||||
'inScope' => false,
|
|
||||||
],
|
|
||||||
'array' => [
|
|
||||||
'iterable' => [1, 2, 3],
|
|
||||||
'inScope' => false,
|
|
||||||
],
|
|
||||||
'ArrayList' => [
|
|
||||||
'iterable' => new ArrayList([['Val' => 1], ['Val' => 2], ['Val' => 3]]),
|
|
||||||
'inScope' => false,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
foreach ($scenarios as $name => $scenario) {
|
|
||||||
$scenario['inScope'] = true;
|
|
||||||
$scenarios[$name . ' in scope'] = $scenario;
|
|
||||||
}
|
|
||||||
return $scenarios;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[DataProvider('provideCountIterable')]
|
|
||||||
public function testCountIterable(iterable $iterable, bool $inScope): void
|
|
||||||
{
|
|
||||||
$expected = count($iterable);
|
|
||||||
$data = new ArrayData(['Iterable' => $iterable]);
|
|
||||||
if ($inScope) {
|
|
||||||
$template = '<% with $Iterable %>$Count<% end_with %>';
|
|
||||||
} else {
|
|
||||||
$template = '$Iterable.Count';
|
|
||||||
}
|
|
||||||
$this->assertEqualIgnoringWhitespace($expected, $this->render($template, $data));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSSViewerBasicIteratorSupport()
|
|
||||||
{
|
|
||||||
$data = new ArrayData(
|
|
||||||
[
|
|
||||||
'Set' => new ArrayList(
|
|
||||||
[
|
|
||||||
new SSViewerTest\TestObject("1"),
|
|
||||||
new SSViewerTest\TestObject("2"),
|
|
||||||
new SSViewerTest\TestObject("3"),
|
|
||||||
new SSViewerTest\TestObject("4"),
|
|
||||||
new SSViewerTest\TestObject("5"),
|
|
||||||
new SSViewerTest\TestObject("6"),
|
|
||||||
new SSViewerTest\TestObject("7"),
|
|
||||||
new SSViewerTest\TestObject("8"),
|
|
||||||
new SSViewerTest\TestObject("9"),
|
|
||||||
new SSViewerTest\TestObject("10"),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
//base test
|
|
||||||
$result = $this->render('<% loop Set %>$Number<% end_loop %>', $data);
|
|
||||||
$this->assertEquals("12345678910", $result, "Numbers rendered in order");
|
|
||||||
|
|
||||||
//test First
|
|
||||||
$result = $this->render('<% loop Set %><% if $IsFirst %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("1", $result, "Only the first number is rendered");
|
|
||||||
|
|
||||||
//test Last
|
|
||||||
$result = $this->render('<% loop Set %><% if $IsLast %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("10", $result, "Only the last number is rendered");
|
|
||||||
|
|
||||||
//test Even
|
|
||||||
$result = $this->render('<% loop Set %><% if $Even() %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
|
||||||
|
|
||||||
//test Even with quotes
|
|
||||||
$result = $this->render('<% loop Set %><% if $Even("1") %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
|
||||||
|
|
||||||
//test Even without quotes
|
|
||||||
$result = $this->render('<% loop Set %><% if $Even(1) %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("246810", $result, "Even numbers rendered in order");
|
|
||||||
|
|
||||||
//test Even with zero-based start index
|
|
||||||
$result = $this->render('<% loop Set %><% if $Even("0") %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("13579", $result, "Even (with zero-based index) numbers rendered in order");
|
|
||||||
|
|
||||||
//test Odd
|
|
||||||
$result = $this->render('<% loop Set %><% if $Odd %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("13579", $result, "Odd numbers rendered in order");
|
|
||||||
|
|
||||||
//test FirstLast
|
|
||||||
$result = $this->render('<% loop Set %><% if $FirstLast %>$Number$FirstLast<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("1first10last", $result, "First and last numbers rendered in order");
|
|
||||||
|
|
||||||
//test Middle
|
|
||||||
$result = $this->render('<% loop Set %><% if $Middle %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("23456789", $result, "Middle numbers rendered in order");
|
|
||||||
|
|
||||||
//test MiddleString
|
|
||||||
$result = $this->render(
|
|
||||||
'<% loop Set %><% if MiddleString == "middle" %>$Number$MiddleString<% end_if %>'
|
|
||||||
. '<% end_loop %>',
|
|
||||||
$data
|
|
||||||
);
|
|
||||||
$this->assertEquals(
|
|
||||||
"2middle3middle4middle5middle6middle7middle8middle9middle",
|
|
||||||
$result,
|
|
||||||
"Middle numbers rendered in order"
|
|
||||||
);
|
|
||||||
|
|
||||||
//test EvenOdd
|
|
||||||
$result = $this->render('<% loop Set %>$EvenOdd<% end_loop %>', $data);
|
|
||||||
$this->assertEquals(
|
|
||||||
"oddevenoddevenoddevenoddevenoddeven",
|
|
||||||
$result,
|
|
||||||
"Even and Odd is returned in sequence numbers rendered in order"
|
|
||||||
);
|
|
||||||
|
|
||||||
//test Pos
|
|
||||||
$result = $this->render('<% loop Set %>$Pos<% end_loop %>', $data);
|
|
||||||
$this->assertEquals("12345678910", $result, '$Pos is rendered in order');
|
|
||||||
|
|
||||||
//test Pos
|
|
||||||
$result = $this->render('<% loop Set %>$Pos(0)<% end_loop %>', $data);
|
|
||||||
$this->assertEquals("0123456789", $result, '$Pos(0) is rendered in order');
|
|
||||||
|
|
||||||
//test FromEnd
|
|
||||||
$result = $this->render('<% loop Set %>$FromEnd<% end_loop %>', $data);
|
|
||||||
$this->assertEquals("10987654321", $result, '$FromEnd is rendered in order');
|
|
||||||
|
|
||||||
//test FromEnd
|
|
||||||
$result = $this->render('<% loop Set %>$FromEnd(0)<% end_loop %>', $data);
|
|
||||||
$this->assertEquals("9876543210", $result, '$FromEnd(0) rendered in order');
|
|
||||||
|
|
||||||
//test Total
|
|
||||||
$result = $this->render('<% loop Set %>$TotalItems<% end_loop %>', $data);
|
|
||||||
$this->assertEquals("10101010101010101010", $result, "10 total items X 10 are returned");
|
|
||||||
|
|
||||||
//test Modulus
|
|
||||||
$result = $this->render('<% loop Set %>$Modulus(2,1)<% end_loop %>', $data);
|
|
||||||
$this->assertEquals("1010101010", $result, "1-indexed pos modular divided by 2 rendered in order");
|
|
||||||
|
|
||||||
//test MultipleOf 3
|
|
||||||
$result = $this->render('<% loop Set %><% if MultipleOf(3) %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("369", $result, "Only numbers that are multiples of 3 are returned");
|
|
||||||
|
|
||||||
//test MultipleOf 4
|
|
||||||
$result = $this->render('<% loop Set %><% if MultipleOf(4) %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("48", $result, "Only numbers that are multiples of 4 are returned");
|
|
||||||
|
|
||||||
//test MultipleOf 5
|
|
||||||
$result = $this->render('<% loop Set %><% if MultipleOf(5) %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("510", $result, "Only numbers that are multiples of 5 are returned");
|
|
||||||
|
|
||||||
//test MultipleOf 10
|
|
||||||
$result = $this->render('<% loop Set %><% if MultipleOf(10,1) %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("10", $result, "Only numbers that are multiples of 10 (with 1-based indexing) are returned");
|
|
||||||
|
|
||||||
//test MultipleOf 9 zero-based
|
|
||||||
$result = $this->render('<% loop Set %><% if MultipleOf(9,0) %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals(
|
|
||||||
"110",
|
|
||||||
$result,
|
|
||||||
"Only numbers that are multiples of 9 with zero-based indexing are returned. (The first and last item)"
|
|
||||||
);
|
|
||||||
|
|
||||||
//test MultipleOf 11
|
|
||||||
$result = $this->render('<% loop Set %><% if MultipleOf(11) %>$Number<% end_if %><% end_loop %>', $data);
|
|
||||||
$this->assertEquals("", $result, "Only numbers that are multiples of 11 are returned. I.e. nothing returned");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test $Up works when the scope $Up refers to was entered with a "with" block
|
|
||||||
*/
|
|
||||||
public function testUpInWith()
|
|
||||||
{
|
|
||||||
|
|
||||||
// Data to run the loop tests on - three levels deep
|
|
||||||
$data = new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Top',
|
|
||||||
'Foo' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Foo',
|
|
||||||
'Bar' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Bar',
|
|
||||||
'Baz' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Baz'
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'Qux' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Qux'
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Basic functionality
|
|
||||||
$this->assertEquals(
|
|
||||||
'BarFoo',
|
|
||||||
$this->render('<% with Foo %><% with Bar %>{$Name}{$Up.Name}<% end_with %><% end_with %>', $data)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Two level with block, up refers to internally referenced Bar
|
|
||||||
$this->assertEquals(
|
|
||||||
'BarTop',
|
|
||||||
$this->render('<% with Foo.Bar %>{$Name}{$Up.Name}<% end_with %>', $data)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Stepping up & back down the scope tree
|
|
||||||
$this->assertEquals(
|
|
||||||
'BazFooBar',
|
|
||||||
$this->render('<% with Foo.Bar.Baz %>{$Name}{$Up.Foo.Name}{$Up.Foo.Bar.Name}<% end_with %>', $data)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Using $Up in a with block
|
|
||||||
$this->assertEquals(
|
|
||||||
'BazTopBar',
|
|
||||||
$this->render(
|
|
||||||
'<% with Foo.Bar.Baz %>{$Name}<% with $Up %>{$Name}{$Foo.Bar.Name}<% end_with %>'
|
|
||||||
. '<% end_with %>',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Stepping up & back down the scope tree with with blocks
|
|
||||||
$this->assertEquals(
|
|
||||||
'BazTopBarTopBaz',
|
|
||||||
$this->render(
|
|
||||||
'<% with Foo.Bar.Baz %>{$Name}<% with $Up %>{$Name}<% with Foo.Bar %>{$Name}<% end_with %>'
|
|
||||||
. '{$Name}<% end_with %>{$Name}<% end_with %>',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Using $Up.Up, where first $Up points to a previous scope entered using $Up, thereby skipping up to Foo
|
|
||||||
$this->assertEquals(
|
|
||||||
'Foo',
|
|
||||||
$this->render(
|
|
||||||
'<% with Foo %><% with Bar %><% with Baz %>{$Up.Up.Name}<% end_with %><% end_with %>'
|
|
||||||
. '<% end_with %>',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Using $Up as part of a lookup chain in <% with %>
|
|
||||||
$this->assertEquals(
|
|
||||||
'Top',
|
|
||||||
$this->render('<% with Foo.Bar.Baz.Up.Qux %>{$Up.Name}<% end_with %>', $data)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testTooManyUps()
|
|
||||||
{
|
|
||||||
$this->expectException(LogicException::class);
|
|
||||||
$this->expectExceptionMessage("Up called when we're already at the top of the scope");
|
|
||||||
$data = new ArrayData([
|
|
||||||
'Foo' => new ArrayData([
|
|
||||||
'Name' => 'Foo',
|
|
||||||
'Bar' => new ArrayData([
|
|
||||||
'Name' => 'Bar'
|
|
||||||
])
|
|
||||||
])
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
'Foo',
|
|
||||||
$this->render('<% with Foo.Bar %>{$Up.Up.Name}<% end_with %>', $data)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test $Up works when the scope $Up refers to was entered with a "loop" block
|
|
||||||
*/
|
|
||||||
public function testUpInLoop()
|
|
||||||
{
|
|
||||||
|
|
||||||
// Data to run the loop tests on - one sequence of three items, each with a subitem
|
|
||||||
$data = new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Top',
|
|
||||||
'Foo' => new ArrayList(
|
|
||||||
[
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => '1',
|
|
||||||
'Sub' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Bar'
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
),
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => '2',
|
|
||||||
'Sub' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Baz'
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
),
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => '3',
|
|
||||||
'Sub' => new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'Qux'
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Make sure inside a loop, $Up refers to the current item of the loop
|
|
||||||
$this->assertEqualIgnoringWhitespace(
|
$this->assertEqualIgnoringWhitespace(
|
||||||
'111 222 333',
|
'<html><head></head><body></body></html>',
|
||||||
$this->render(
|
$result2
|
||||||
'<% loop $Foo %>$Name<% with $Sub %>$Up.Name<% end_with %>$Name<% end_loop %>',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Make sure inside a loop, looping over $Up uses a separate iterator,
|
|
||||||
// and doesn't interfere with the original iterator
|
|
||||||
$this->assertEqualIgnoringWhitespace(
|
|
||||||
'1Bar123Bar1 2Baz123Baz2 3Qux123Qux3',
|
|
||||||
$this->render(
|
|
||||||
'<% loop $Foo %>
|
|
||||||
$Name
|
|
||||||
<% with $Sub %>
|
|
||||||
$Name
|
|
||||||
<% loop $Up %>$Name<% end_loop %>
|
|
||||||
$Name
|
|
||||||
<% end_with %>
|
|
||||||
$Name
|
|
||||||
<% end_loop %>',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Make sure inside a loop, looping over $Up uses a separate iterator,
|
|
||||||
// and doesn't interfere with the original iterator or local lookups
|
|
||||||
$this->assertEqualIgnoringWhitespace(
|
|
||||||
'1 Bar1 123 1Bar 1 2 Baz2 123 2Baz 2 3 Qux3 123 3Qux 3',
|
|
||||||
$this->render(
|
|
||||||
'<% loop $Foo %>
|
|
||||||
$Name
|
|
||||||
<% with $Sub %>
|
|
||||||
{$Name}{$Up.Name}
|
|
||||||
<% loop $Up %>$Name<% end_loop %>
|
|
||||||
{$Up.Name}{$Name}
|
|
||||||
<% end_with %>
|
|
||||||
$Name
|
|
||||||
<% end_loop %>',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test that nested loops restore the loop variables correctly when pushing and popping states
|
|
||||||
*/
|
|
||||||
public function testNestedLoops()
|
|
||||||
{
|
|
||||||
|
|
||||||
// Data to run the loop tests on - one sequence of three items, one with child elements
|
|
||||||
// (of a different size to the main sequence)
|
|
||||||
$data = new ArrayData(
|
|
||||||
[
|
|
||||||
'Foo' => new ArrayList(
|
|
||||||
[
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => '1',
|
|
||||||
'Children' => new ArrayList(
|
|
||||||
[
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'a'
|
|
||||||
]
|
|
||||||
),
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => 'b'
|
|
||||||
]
|
|
||||||
),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => '2',
|
|
||||||
'Children' => new ArrayList(),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
new ArrayData(
|
|
||||||
[
|
|
||||||
'Name' => '3',
|
|
||||||
'Children' => new ArrayList(),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Make sure that including a loop inside a loop will not destroy the internal count of
|
|
||||||
// items, checked by using "Last"
|
|
||||||
$this->assertEqualIgnoringWhitespace(
|
|
||||||
'1ab23last',
|
|
||||||
$this->render(
|
|
||||||
'<% loop $Foo %>$Name<% loop Children %>$Name<% end_loop %><% if $IsLast %>last<% end_if %>'
|
|
||||||
. '<% end_loop %>',
|
|
||||||
$data
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLayout()
|
|
||||||
{
|
|
||||||
$this->useTestTheme(
|
|
||||||
__DIR__ . '/SSViewerTest',
|
|
||||||
'layouttest',
|
|
||||||
function () {
|
|
||||||
$template = new SSViewer(['Page']);
|
|
||||||
$this->assertEquals("Foo\n\n", $template->process(new ArrayData([])));
|
|
||||||
|
|
||||||
$template = new SSViewer(['Shortcodes', 'Page']);
|
|
||||||
$this->assertEquals("[file_link]\n\n", $template->process(new ArrayData([])));
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1939,7 +131,7 @@ after'
|
|||||||
|
|
||||||
// Let's throw something random in there.
|
// Let's throw something random in there.
|
||||||
$this->expectException(\InvalidArgumentException::class);
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
SSViewer::get_templates_by_class(null);
|
SSViewer::get_templates_by_class('no-class');
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -1947,43 +139,31 @@ after'
|
|||||||
public function testRewriteHashlinks()
|
public function testRewriteHashlinks()
|
||||||
{
|
{
|
||||||
SSViewer::setRewriteHashLinksDefault(true);
|
SSViewer::setRewriteHashLinksDefault(true);
|
||||||
|
$oldServerVars = $_SERVER;
|
||||||
|
|
||||||
$_SERVER['HTTP_HOST'] = 'www.mysite.com';
|
try {
|
||||||
$_SERVER['REQUEST_URI'] = '//file.com?foo"onclick="alert(\'xss\')""';
|
$_SERVER['REQUEST_URI'] = '//file.com?foo"onclick="alert(\'xss\')""';
|
||||||
|
|
||||||
// Emulate SSViewer::process()
|
|
||||||
// Note that leading double slashes have been rewritten to prevent these being mis-interepreted
|
// Note that leading double slashes have been rewritten to prevent these being mis-interepreted
|
||||||
// as protocol-less absolute urls
|
// as protocol-less absolute urls
|
||||||
$base = Convert::raw2att('/file.com?foo"onclick="alert(\'xss\')""');
|
$base = Convert::raw2att('/file.com?foo"onclick="alert(\'xss\')""');
|
||||||
|
|
||||||
$tmplFile = TEMP_PATH . DIRECTORY_SEPARATOR . 'SSViewerTest_testRewriteHashlinks_' . sha1(rand()) . '.ss';
|
$engine = new DummyTemplateEngine();
|
||||||
|
$engine->setOutput(
|
||||||
// Note: SSViewer_FromString doesn't rewrite hash links.
|
|
||||||
file_put_contents(
|
|
||||||
$tmplFile ?? '',
|
|
||||||
'<!DOCTYPE html>
|
'<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head><% base_tag %></head>
|
<head><base href="http://www.example.com/"></head>
|
||||||
<body>
|
<body>
|
||||||
<a class="external-inline" href="http://google.com#anchor">ExternalInlineLink</a>
|
<a class="external-inline" href="http://google.com#anchor">ExternalInlineLink</a>
|
||||||
$ExternalInsertedLink
|
<a class="external-inserted" href="http://google.com#anchor">ExternalInsertedLink</a>
|
||||||
<a class="inline" href="#anchor">InlineLink</a>
|
<a class="inline" href="#anchor">InlineLink</a>
|
||||||
$InsertedLink
|
<a class="inserted" href="#anchor">InsertedLink</a>
|
||||||
<svg><use xlink:href="#sprite"></use></svg>
|
<svg><use xlink:href="#sprite"></use></svg>
|
||||||
<body>
|
<body>
|
||||||
</html>'
|
</html>'
|
||||||
);
|
);
|
||||||
$tmpl = new SSViewer($tmplFile);
|
$tmpl = new SSViewer([], $engine);
|
||||||
$obj = new ModelData();
|
$result = $tmpl->process('pretend this is a model');
|
||||||
$obj->InsertedLink = DBField::create_field(
|
|
||||||
'HTMLFragment',
|
|
||||||
'<a class="inserted" href="#anchor">InsertedLink</a>'
|
|
||||||
);
|
|
||||||
$obj->ExternalInsertedLink = DBField::create_field(
|
|
||||||
'HTMLFragment',
|
|
||||||
'<a class="external-inserted" href="http://google.com#anchor">ExternalInsertedLink</a>'
|
|
||||||
);
|
|
||||||
$result = $tmpl->process($obj);
|
|
||||||
$this->assertStringContainsString(
|
$this->assertStringContainsString(
|
||||||
'<a class="inserted" href="' . $base . '#anchor">InsertedLink</a>',
|
'<a class="inserted" href="' . $base . '#anchor">InsertedLink</a>',
|
||||||
$result
|
$result
|
||||||
@ -2005,36 +185,28 @@ after'
|
|||||||
$result,
|
$result,
|
||||||
'SSTemplateParser should only rewrite anchor hrefs'
|
'SSTemplateParser should only rewrite anchor hrefs'
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
unlink($tmplFile ?? '');
|
$_SERVER = $oldServerVars;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRewriteHashlinksInPhpMode()
|
public function testRewriteHashlinksInPhpMode()
|
||||||
{
|
{
|
||||||
SSViewer::setRewriteHashLinksDefault('php');
|
SSViewer::setRewriteHashLinksDefault('php');
|
||||||
|
$engine = new DummyTemplateEngine();
|
||||||
$tmplFile = TEMP_PATH . DIRECTORY_SEPARATOR . 'SSViewerTest_testRewriteHashlinksInPhpMode_' . sha1(rand()) . '.ss';
|
$engine->setOutput(
|
||||||
|
|
||||||
// Note: SSViewer_FromString doesn't rewrite hash links.
|
|
||||||
file_put_contents(
|
|
||||||
$tmplFile ?? '',
|
|
||||||
'<!DOCTYPE html>
|
'<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head><% base_tag %></head>
|
<head><base href="http://www.example.com/"></head>
|
||||||
<body>
|
<body>
|
||||||
<a class="inline" href="#anchor">InlineLink</a>
|
<a class="inline" href="#anchor">InlineLink</a>
|
||||||
$InsertedLink
|
<a class="inserted" href="#anchor">InsertedLink</a>
|
||||||
<svg><use xlink:href="#sprite"></use></svg>
|
<svg><use xlink:href="#sprite"></use></svg>
|
||||||
<body>
|
<body>
|
||||||
</html>'
|
</html>'
|
||||||
);
|
);
|
||||||
$tmpl = new SSViewer($tmplFile);
|
$tmpl = new SSViewer([], $engine);
|
||||||
$obj = new ModelData();
|
$result = $tmpl->process('pretend this is a model');
|
||||||
$obj->InsertedLink = DBField::create_field(
|
|
||||||
'HTMLFragment',
|
|
||||||
'<a class="inserted" href="#anchor">InsertedLink</a>'
|
|
||||||
);
|
|
||||||
$result = $tmpl->process($obj);
|
|
||||||
|
|
||||||
$code = <<<'EOC'
|
$code = <<<'EOC'
|
||||||
<a class="inserted" href="<?php echo \SilverStripe\Core\Convert::raw2att(preg_replace("/^(\/)+/", "/", $_SERVER['REQUEST_URI'])); ?>#anchor">InsertedLink</a>
|
<a class="inserted" href="<?php echo \SilverStripe\Core\Convert::raw2att(preg_replace("/^(\/)+/", "/", $_SERVER['REQUEST_URI'])); ?>#anchor">InsertedLink</a>
|
||||||
@ -2045,339 +217,10 @@ EOC;
|
|||||||
$result,
|
$result,
|
||||||
'SSTemplateParser should only rewrite anchor hrefs'
|
'SSTemplateParser should only rewrite anchor hrefs'
|
||||||
);
|
);
|
||||||
|
|
||||||
unlink($tmplFile ?? '');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRenderWithSourceFileComments()
|
private function assertEqualIgnoringWhitespace(string $a, string $b, string $message = ''): void
|
||||||
{
|
{
|
||||||
SSViewer::config()->set('source_file_comments', true);
|
$this->assertEquals(preg_replace('/\s+/', '', $a ), preg_replace('/\s+/', '', $b), $message);
|
||||||
$i = __DIR__ . '/SSViewerTest/templates/Includes';
|
|
||||||
$f = __DIR__ . '/SSViewerTest/templates/SSViewerTestComments';
|
|
||||||
$templates = [
|
|
||||||
[
|
|
||||||
'name' => 'SSViewerTestCommentsFullSource',
|
|
||||||
'expected' => ""
|
|
||||||
. "<!doctype html>"
|
|
||||||
. "<!-- template $f/SSViewerTestCommentsFullSource.ss -->"
|
|
||||||
. "<html>"
|
|
||||||
. "\t<head></head>"
|
|
||||||
. "\t<body></body>"
|
|
||||||
. "</html>"
|
|
||||||
. "<!-- end template $f/SSViewerTestCommentsFullSource.ss -->",
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'SSViewerTestCommentsFullSourceHTML4Doctype',
|
|
||||||
'expected' => ""
|
|
||||||
. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML "
|
|
||||||
. "4.01//EN\"\t\t\"http://www.w3.org/TR/html4/strict.dtd\">"
|
|
||||||
. "<!-- template $f/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->"
|
|
||||||
. "<html>"
|
|
||||||
. "\t<head></head>"
|
|
||||||
. "\t<body></body>"
|
|
||||||
. "</html>"
|
|
||||||
. "<!-- end template $f/SSViewerTestCommentsFullSourceHTML4Doctype.ss -->",
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'SSViewerTestCommentsFullSourceNoDoctype',
|
|
||||||
'expected' => ""
|
|
||||||
. "<html><!-- template $f/SSViewerTestCommentsFullSourceNoDoctype.ss -->"
|
|
||||||
. "\t<head></head>"
|
|
||||||
. "\t<body></body>"
|
|
||||||
. "<!-- end template $f/SSViewerTestCommentsFullSourceNoDoctype.ss --></html>",
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'SSViewerTestCommentsFullSourceIfIE',
|
|
||||||
'expected' => ""
|
|
||||||
. "<!doctype html>"
|
|
||||||
. "<!-- template $f/SSViewerTestCommentsFullSourceIfIE.ss -->"
|
|
||||||
. "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->"
|
|
||||||
. "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->"
|
|
||||||
. "<!--[if !IE]><!--> <html class='no-ie'> <!--<![endif]-->"
|
|
||||||
. "\t<head></head>"
|
|
||||||
. "\t<body></body>"
|
|
||||||
. "</html>"
|
|
||||||
. "<!-- end template $f/SSViewerTestCommentsFullSourceIfIE.ss -->",
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'SSViewerTestCommentsFullSourceIfIENoDoctype',
|
|
||||||
'expected' => ""
|
|
||||||
. "<!--[if lte IE 8]> <html class='old-ie'> <![endif]-->"
|
|
||||||
. "<!--[if gt IE 8]> <html class='new-ie'> <![endif]-->"
|
|
||||||
. "<!--[if !IE]><!--> <html class='no-ie'>"
|
|
||||||
. "<!-- template $f/SSViewerTestCommentsFullSourceIfIENoDoctype.ss -->"
|
|
||||||
. " <!--<![endif]-->"
|
|
||||||
. "\t<head></head>"
|
|
||||||
. "\t<body></body>"
|
|
||||||
. "<!-- end template $f/SSViewerTestCommentsFullSourceIfIENoDoctype.ss --></html>",
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'SSViewerTestCommentsPartialSource',
|
|
||||||
'expected' =>
|
|
||||||
"<!-- template $f/SSViewerTestCommentsPartialSource.ss -->"
|
|
||||||
. "<div class='typography'></div>"
|
|
||||||
. "<!-- end template $f/SSViewerTestCommentsPartialSource.ss -->",
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'SSViewerTestCommentsWithInclude',
|
|
||||||
'expected' =>
|
|
||||||
"<!-- template $f/SSViewerTestCommentsWithInclude.ss -->"
|
|
||||||
. "<div class='typography'>"
|
|
||||||
. "<!-- include 'SSViewerTestCommentsInclude' -->"
|
|
||||||
. "<!-- template $i/SSViewerTestCommentsInclude.ss -->"
|
|
||||||
. "Included"
|
|
||||||
. "<!-- end template $i/SSViewerTestCommentsInclude.ss -->"
|
|
||||||
. "<!-- end include 'SSViewerTestCommentsInclude' -->"
|
|
||||||
. "</div>"
|
|
||||||
. "<!-- end template $f/SSViewerTestCommentsWithInclude.ss -->",
|
|
||||||
],
|
|
||||||
];
|
|
||||||
foreach ($templates as $template) {
|
|
||||||
$this->_renderWithSourceFileComments('SSViewerTestComments/' . $template['name'], $template['expected']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private function _renderWithSourceFileComments($name, $expected)
|
|
||||||
{
|
|
||||||
$viewer = new SSViewer([$name]);
|
|
||||||
$data = new ArrayData([]);
|
|
||||||
$result = $viewer->process($data);
|
|
||||||
$expected = str_replace(["\r", "\n"], '', $expected ?? '');
|
|
||||||
$result = str_replace(["\r", "\n"], '', $result ?? '');
|
|
||||||
$this->assertEquals($result, $expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLoopIteratorIterator()
|
|
||||||
{
|
|
||||||
$list = new PaginatedList(new ArrayList());
|
|
||||||
$viewer = new SSViewer_FromString('<% loop List %>$ID - $FirstName<br /><% end_loop %>');
|
|
||||||
$result = $viewer->process(new ArrayData(['List' => $list]));
|
|
||||||
$this->assertEquals($result, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testProcessOnlyIncludesRequirementsOnce()
|
|
||||||
{
|
|
||||||
$template = new SSViewer(['SSViewerTestProcess']);
|
|
||||||
$basePath = $this->getCurrentRelativePath() . '/SSViewerTest';
|
|
||||||
|
|
||||||
$backend = Injector::inst()->create(Requirements_Backend::class);
|
|
||||||
$backend->setCombinedFilesEnabled(false);
|
|
||||||
$backend->combineFiles(
|
|
||||||
'RequirementsTest_ab.css',
|
|
||||||
[
|
|
||||||
$basePath . '/css/RequirementsTest_a.css',
|
|
||||||
$basePath . '/css/RequirementsTest_b.css'
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
Requirements::set_backend($backend);
|
|
||||||
|
|
||||||
$this->assertEquals(1, substr_count($template->process(new ModelData()) ?? '', "a.css"));
|
|
||||||
$this->assertEquals(1, substr_count($template->process(new ModelData()) ?? '', "b.css"));
|
|
||||||
|
|
||||||
// if we disable the requirements then we should get nothing
|
|
||||||
$template->includeRequirements(false);
|
|
||||||
$this->assertEquals(0, substr_count($template->process(new ModelData()) ?? '', "a.css"));
|
|
||||||
$this->assertEquals(0, substr_count($template->process(new ModelData()) ?? '', "b.css"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRequireCallInTemplateInclude()
|
|
||||||
{
|
|
||||||
if (FRAMEWORK_DIR === 'framework') {
|
|
||||||
$template = new SSViewer(['SSViewerTestProcess']);
|
|
||||||
|
|
||||||
Requirements::set_suffix_requirements(false);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
|
||||||
1,
|
|
||||||
substr_count(
|
|
||||||
$template->process(new ModelData()) ?? '',
|
|
||||||
"tests/php/View/SSViewerTest/javascript/RequirementsTest_a.js"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$this->markTestSkipped(
|
|
||||||
'Requirement will always fail if the framework dir is not ' .
|
|
||||||
'named \'framework\', since templates require hard coded paths'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCallsWithArguments()
|
|
||||||
{
|
|
||||||
$data = new ArrayData(
|
|
||||||
[
|
|
||||||
'Set' => new ArrayList(
|
|
||||||
[
|
|
||||||
new SSViewerTest\TestObject("1"),
|
|
||||||
new SSViewerTest\TestObject("2"),
|
|
||||||
new SSViewerTest\TestObject("3"),
|
|
||||||
new SSViewerTest\TestObject("4"),
|
|
||||||
new SSViewerTest\TestObject("5"),
|
|
||||||
]
|
|
||||||
),
|
|
||||||
'Level' => new SSViewerTest\LevelTestData(1),
|
|
||||||
'Nest' => [
|
|
||||||
'Level' => new SSViewerTest\LevelTestData(2),
|
|
||||||
],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$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) ?? ''));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRepeatedCallsAreCached()
|
|
||||||
{
|
|
||||||
$data = new SSViewerTest\CacheTestData();
|
|
||||||
$template = '
|
|
||||||
<% if $TestWithCall %>
|
|
||||||
<% with $TestWithCall %>
|
|
||||||
{$Message}
|
|
||||||
<% end_with %>
|
|
||||||
|
|
||||||
{$TestWithCall.Message}
|
|
||||||
<% end_if %>';
|
|
||||||
|
|
||||||
$this->assertEquals('HiHi', preg_replace('/\s+/', '', $this->render($template, $data) ?? ''));
|
|
||||||
$this->assertEquals(
|
|
||||||
1,
|
|
||||||
$data->testWithCalls,
|
|
||||||
'SSViewerTest_CacheTestData::TestWithCall() should only be called once. Subsequent calls should be cached'
|
|
||||||
);
|
|
||||||
|
|
||||||
$data = new SSViewerTest\CacheTestData();
|
|
||||||
$template = '
|
|
||||||
<% if $TestLoopCall %>
|
|
||||||
<% loop $TestLoopCall %>
|
|
||||||
{$Message}
|
|
||||||
<% end_loop %>
|
|
||||||
<% end_if %>';
|
|
||||||
|
|
||||||
$this->assertEquals('OneTwo', preg_replace('/\s+/', '', $this->render($template, $data) ?? ''));
|
|
||||||
$this->assertEquals(
|
|
||||||
1,
|
|
||||||
$data->testLoopCalls,
|
|
||||||
'SSViewerTest_CacheTestData::TestLoopCall() should only be called once. Subsequent calls should be cached'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testClosedBlockExtension()
|
|
||||||
{
|
|
||||||
$count = 0;
|
|
||||||
$parser = new SSTemplateParser();
|
|
||||||
$parser->addClosedBlock(
|
|
||||||
'test',
|
|
||||||
function ($res) use (&$count) {
|
|
||||||
$count++;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$template = new SSViewer_FromString("<% test %><% end_test %>", $parser);
|
|
||||||
$template->process(new SSViewerTest\TestFixture());
|
|
||||||
|
|
||||||
$this->assertEquals(1, $count);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testOpenBlockExtension()
|
|
||||||
{
|
|
||||||
$count = 0;
|
|
||||||
$parser = new SSTemplateParser();
|
|
||||||
$parser->addOpenBlock(
|
|
||||||
'test',
|
|
||||||
function ($res) use (&$count) {
|
|
||||||
$count++;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$template = new SSViewer_FromString("<% test %>", $parser);
|
|
||||||
$template->process(new SSViewerTest\TestFixture());
|
|
||||||
|
|
||||||
$this->assertEquals(1, $count);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests if caching for SSViewer_FromString is working
|
|
||||||
*/
|
|
||||||
public function testFromStringCaching()
|
|
||||||
{
|
|
||||||
$content = 'Test content';
|
|
||||||
$cacheFile = TEMP_PATH . DIRECTORY_SEPARATOR . '.cache.' . sha1($content ?? '');
|
|
||||||
if (file_exists($cacheFile ?? '')) {
|
|
||||||
unlink($cacheFile ?? '');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test global behaviors
|
|
||||||
$this->render($content, null, null);
|
|
||||||
$this->assertFalse(file_exists($cacheFile ?? ''), 'Cache file was created when caching was off');
|
|
||||||
|
|
||||||
SSViewer_FromString::config()->set('cache_template', true);
|
|
||||||
$this->render($content, null, null);
|
|
||||||
$this->assertTrue(file_exists($cacheFile ?? ''), 'Cache file wasn\'t created when it was meant to');
|
|
||||||
unlink($cacheFile ?? '');
|
|
||||||
|
|
||||||
// Test instance behaviors
|
|
||||||
$this->render($content, null, false);
|
|
||||||
$this->assertFalse(file_exists($cacheFile ?? ''), 'Cache file was created when caching was off');
|
|
||||||
|
|
||||||
$this->render($content, null, true);
|
|
||||||
$this->assertTrue(file_exists($cacheFile ?? ''), 'Cache file wasn\'t created when it was meant to');
|
|
||||||
unlink($cacheFile ?? '');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testPrimitivesConvertedToDBFields()
|
|
||||||
{
|
|
||||||
$data = new ArrayData([
|
|
||||||
// null value should not be rendered, though should also not throw exception
|
|
||||||
'Foo' => new ArrayList(['hello', true, 456, 7.89, null])
|
|
||||||
]);
|
|
||||||
$this->assertEqualIgnoringWhitespace(
|
|
||||||
'hello 1 456 7.89',
|
|
||||||
$this->render('<% loop $Foo %>$Me<% end_loop %>', $data)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[DoesNotPerformAssertions]
|
|
||||||
public function testMe(): void
|
|
||||||
{
|
|
||||||
$myArrayData = new class extends ArrayData {
|
|
||||||
public function forTemplate()
|
|
||||||
{
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
$this->render('$Me', $myArrayData);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLoopingThroughArrayInOverlay(): void
|
|
||||||
{
|
|
||||||
$modelData = new ModelData();
|
|
||||||
$theArray = [
|
|
||||||
['Val' => 'one'],
|
|
||||||
['Val' => 'two'],
|
|
||||||
['Val' => 'red'],
|
|
||||||
['Val' => 'blue'],
|
|
||||||
];
|
|
||||||
$output = $modelData->renderWith('SSViewerTestLoopArray', ['MyArray' => $theArray]);
|
|
||||||
$this->assertEqualIgnoringWhitespace('one two red blue', $output);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
45
tests/php/View/SSViewerTest/DummyTemplateEngine.php
Normal file
45
tests/php/View/SSViewerTest/DummyTemplateEngine.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\SSViewerTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\View\TemplateEngine;
|
||||||
|
use SilverStripe\View\ViewLayerData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A dummy template renderer that doesn't actually render any templates.
|
||||||
|
*/
|
||||||
|
class DummyTemplateEngine implements TemplateEngine, TestOnly
|
||||||
|
{
|
||||||
|
private string $output = '<html><head></head><body></body></html>';
|
||||||
|
|
||||||
|
public function __construct(string|array $templateCandidates = [])
|
||||||
|
{
|
||||||
|
// no-op
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTemplate(string|array $templateCandidates): static
|
||||||
|
{
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hasTemplate(string|array $templateCandidates): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true): string
|
||||||
|
{
|
||||||
|
return $this->output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(ViewLayerData $model, array $overlay = []): string
|
||||||
|
{
|
||||||
|
return $this->output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setOutput(string $output): void
|
||||||
|
{
|
||||||
|
$this->output = $output;
|
||||||
|
}
|
||||||
|
}
|
@ -1,72 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace SilverStripe\View\Tests\SSViewerTest;
|
|
||||||
|
|
||||||
use SilverStripe\Model\List\ArrayList;
|
|
||||||
use SilverStripe\Model\ModelData;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A test fixture that will echo back the template item
|
|
||||||
*/
|
|
||||||
class TestFixture extends ModelData
|
|
||||||
{
|
|
||||||
protected $name;
|
|
||||||
|
|
||||||
public function __construct($name = null)
|
|
||||||
{
|
|
||||||
$this->name = $name;
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function argedName($fieldName, $arguments)
|
|
||||||
{
|
|
||||||
$childName = $this->name ? "$this->name.$fieldName" : $fieldName;
|
|
||||||
if ($arguments) {
|
|
||||||
return $childName . '(' . implode(',', $arguments) . ')';
|
|
||||||
} else {
|
|
||||||
return $childName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function obj(
|
|
||||||
string $fieldName,
|
|
||||||
array $arguments = [],
|
|
||||||
bool $cache = false,
|
|
||||||
?string $cacheName = null
|
|
||||||
): ?object {
|
|
||||||
$childName = $this->argedName($fieldName, $arguments);
|
|
||||||
|
|
||||||
// Special field name Loop### to create a list
|
|
||||||
if (preg_match('/^Loop([0-9]+)$/', $fieldName ?? '', $matches)) {
|
|
||||||
$output = new ArrayList();
|
|
||||||
for ($i = 0; $i < $matches[1]; $i++) {
|
|
||||||
$output->push(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) . ']';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hasValue(string $fieldName, array $arguments = [], bool $cache = true): bool
|
|
||||||
{
|
|
||||||
return (bool)$this->XML_val($fieldName, $arguments);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace SilverStripe\View\Tests\SSViewerTest;
|
|
||||||
|
|
||||||
use SilverStripe\Dev\TestOnly;
|
|
||||||
use SilverStripe\View\TemplateGlobalProvider;
|
|
||||||
|
|
||||||
class TestGlobalProvider implements TemplateGlobalProvider, TestOnly
|
|
||||||
{
|
|
||||||
|
|
||||||
public static function get_template_global_variables()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'SSViewerTest_GlobalHTMLFragment' => ['method' => 'get_html', 'casting' => 'HTMLFragment'],
|
|
||||||
'SSViewerTest_GlobalHTMLEscaped' => ['method' => 'get_html'],
|
|
||||||
|
|
||||||
'SSViewerTest_GlobalAutomatic',
|
|
||||||
'SSViewerTest_GlobalReferencedByString' => 'get_reference',
|
|
||||||
'SSViewerTest_GlobalReferencedInArray' => ['method' => 'get_reference'],
|
|
||||||
|
|
||||||
'SSViewerTest_GlobalThatTakesArguments' => ['method' => 'get_argmix', 'casting' => 'HTMLFragment'],
|
|
||||||
'SSViewerTest_GlobalReturnsNull' => 'getNull',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function get_html()
|
|
||||||
{
|
|
||||||
return '<div></div>';
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function SSViewerTest_GlobalAutomatic()
|
|
||||||
{
|
|
||||||
return 'automatic';
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function get_reference()
|
|
||||||
{
|
|
||||||
return 'reference';
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function get_argmix()
|
|
||||||
{
|
|
||||||
$args = func_get_args();
|
|
||||||
return 'z' . implode(':', $args) . 'z';
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getNull()
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
<div class='typography'><% include SSViewerTestCommentsInclude %></div>
|
|
@ -1,3 +0,0 @@
|
|||||||
<% loop Items %>
|
|
||||||
<% include SSViewerTestIncludeScopeInheritanceInclude %>
|
|
||||||
<% end_loop %>
|
|
@ -1,3 +0,0 @@
|
|||||||
<% loop Items %>
|
|
||||||
<% include SSViewerTestIncludeScopeInheritanceInclude ArgA=$Title %>
|
|
||||||
<% end_loop %>
|
|
@ -1,6 +0,0 @@
|
|||||||
<html>
|
|
||||||
<% include SSViewerTestProcessHead %>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
@ -1 +0,0 @@
|
|||||||
SSViewerTest
|
|
751
tests/php/View/ViewLayerDataTest.php
Normal file
751
tests/php/View/ViewLayerDataTest.php
Normal file
@ -0,0 +1,751 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests;
|
||||||
|
|
||||||
|
use ArrayIterator;
|
||||||
|
use BadMethodCallException;
|
||||||
|
use Error;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Model\ArrayData;
|
||||||
|
use SilverStripe\Model\List\ArrayList;
|
||||||
|
use SilverStripe\Model\ModelData;
|
||||||
|
use SilverStripe\ORM\FieldType\DBDate;
|
||||||
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||||
|
use SilverStripe\View\Exception\MissingTemplateException;
|
||||||
|
use SilverStripe\View\Tests\ViewLayerDataTest\CountableObject;
|
||||||
|
use SilverStripe\View\Tests\ViewLayerDataTest\ExtensibleObject;
|
||||||
|
use SilverStripe\View\Tests\ViewLayerDataTest\ExtensibleObjectExtension;
|
||||||
|
use SilverStripe\View\Tests\ViewLayerDataTest\GetCountObject;
|
||||||
|
use SilverStripe\View\Tests\ViewLayerDataTest\NonIterableObject;
|
||||||
|
use SilverStripe\View\Tests\ViewLayerDataTest\StringableObject;
|
||||||
|
use SilverStripe\View\Tests\ViewLayerDataTest\TestFixture;
|
||||||
|
use SilverStripe\View\Tests\ViewLayerDataTest\TestFixtureComplex;
|
||||||
|
use SilverStripe\View\ViewLayerData;
|
||||||
|
use stdClass;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class ViewLayerDataTest extends SapphireTest
|
||||||
|
{
|
||||||
|
protected static $required_extensions = [
|
||||||
|
ExtensibleObject::class => [
|
||||||
|
ExtensibleObjectExtension::class
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
public static function provideGetIterator(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'non-iterable object' => [
|
||||||
|
'data' => new ArrayData(['Field1' => 'value1', 'Field2' => 'value2']),
|
||||||
|
'expected' => BadMethodCallException::class,
|
||||||
|
],
|
||||||
|
'non-iterable scalar' => [
|
||||||
|
'data' => 'This is some text, aint iterable',
|
||||||
|
'expected' => BadMethodCallException::class,
|
||||||
|
],
|
||||||
|
'empty array' => [
|
||||||
|
'data' => [],
|
||||||
|
'expected' => [],
|
||||||
|
],
|
||||||
|
'single item array' => [
|
||||||
|
'data' => ['one value'],
|
||||||
|
'expected' => ['one value'],
|
||||||
|
],
|
||||||
|
'multi-item array' => [
|
||||||
|
'data' => ['one', 'two', 'three'],
|
||||||
|
'expected' => ['one', 'two', 'three'],
|
||||||
|
],
|
||||||
|
'object implements an Iterable interface' => [
|
||||||
|
'data' => new ArrayList(['one', 'two', 'three']),
|
||||||
|
'expected' => ['one', 'two', 'three'],
|
||||||
|
],
|
||||||
|
'built-in PHP iterator' => [
|
||||||
|
'data' => new ArrayIterator(['one', 'two', 'three']),
|
||||||
|
'expected' => ['one', 'two', 'three'],
|
||||||
|
],
|
||||||
|
'non-iterable object with getIterator method' => [
|
||||||
|
'data' => new NonIterableObject(),
|
||||||
|
'expected' => ['some value', 'another value', 'isnt this nice'],
|
||||||
|
],
|
||||||
|
'extensible object with getIterator extension' => [
|
||||||
|
'data' => new ExtensibleObject(),
|
||||||
|
'expected' => ['1','2','3','4','5','6','7','8','9','a','b','c','d','e'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideGetIterator')]
|
||||||
|
public function testGetIterator(mixed $data, string|array $expected): void
|
||||||
|
{
|
||||||
|
$viewLayerData = new ViewLayerData($data);
|
||||||
|
if ($expected === BadMethodCallException::class) {
|
||||||
|
$this->expectException(BadMethodCallException::class);
|
||||||
|
$this->expectExceptionMessageMatches('/is not iterable.$/');
|
||||||
|
}
|
||||||
|
$this->assertEquals($expected, iterator_to_array($viewLayerData->getIterator()));
|
||||||
|
// Ensure the iterator is always wrapping values
|
||||||
|
foreach ($viewLayerData as $value) {
|
||||||
|
$this->assertInstanceOf(ViewLayerData::class, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideGetIteratorCount(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'uncountable object' => [
|
||||||
|
'data' => new ArrayData(['Field1' => 'value1', 'Field2' => 'value2']),
|
||||||
|
'expected' => 0,
|
||||||
|
],
|
||||||
|
'uncountable object - has count field' => [
|
||||||
|
'data' => new ArrayData(['count' => 12, 'Field2' => 'value2']),
|
||||||
|
'expected' => 12,
|
||||||
|
],
|
||||||
|
'uncountable object - has count field (non-int)' => [
|
||||||
|
'data' => new ArrayData(['count' => 'aahhh', 'Field2' => 'value2']),
|
||||||
|
'expected' => 0,
|
||||||
|
],
|
||||||
|
'empty array' => [
|
||||||
|
'data' => [],
|
||||||
|
'expected' => 0,
|
||||||
|
],
|
||||||
|
'array with values' => [
|
||||||
|
'data' => [1, 2],
|
||||||
|
'expected' => 2,
|
||||||
|
],
|
||||||
|
'explicitly countable object' => [
|
||||||
|
'data' => new CountableObject(),
|
||||||
|
'expected' => 53,
|
||||||
|
],
|
||||||
|
'non-countable object with getCount method' => [
|
||||||
|
'data' => new GetCountObject(),
|
||||||
|
'expected' => 12,
|
||||||
|
],
|
||||||
|
'non-countable object with getIterator method' => [
|
||||||
|
'data' => new NonIterableObject(),
|
||||||
|
'expected' => 3,
|
||||||
|
],
|
||||||
|
'extensible object with getIterator extension' => [
|
||||||
|
'data' => new ExtensibleObject(),
|
||||||
|
'expected' => 14,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideGetIteratorCount')]
|
||||||
|
public function testGetIteratorCount(mixed $data, int $expected): void
|
||||||
|
{
|
||||||
|
$viewLayerData = new ViewLayerData($data);
|
||||||
|
$this->assertSame($expected, $viewLayerData->getIteratorCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideIsSet(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'list array' => [
|
||||||
|
'data' => ['anything'],
|
||||||
|
'name' => 'anything',
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'associative array has key' => [
|
||||||
|
'data' => ['anything' => 'some value'],
|
||||||
|
'name' => 'anything',
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'ModelData without field' => [
|
||||||
|
'data' => new ArrayData(['nothing' => 'some value']),
|
||||||
|
'name' => 'anything',
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'ModelData with field' => [
|
||||||
|
'data' => new ArrayData(['anything' => 'some value']),
|
||||||
|
'name' => 'anything',
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'extensible class with getter extension' => [
|
||||||
|
'data' => new ExtensibleObject(),
|
||||||
|
'name' => 'anything',
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'extensible class not set' => [
|
||||||
|
'data' => new ExtensibleObject(),
|
||||||
|
'name' => 'anythingelse',
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'class with method' => [
|
||||||
|
'data' => new CountableObject(),
|
||||||
|
'name' => 'count',
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideIsSet')]
|
||||||
|
public function testIsSet(mixed $data, string $name, bool $expected): void
|
||||||
|
{
|
||||||
|
$viewLayerData = new ViewLayerData($data);
|
||||||
|
$this->assertSame($expected, isset($viewLayerData->$name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideGet(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'basic field' => [
|
||||||
|
'name' => 'SomeField',
|
||||||
|
'throwException' => true,
|
||||||
|
'expected' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'SomeField',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getSomeField',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'SomeField',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'getter as property' => [
|
||||||
|
'name' => 'getSomeField',
|
||||||
|
'throwException' => true,
|
||||||
|
'expected' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getSomeField',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getgetSomeField',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'getSomeField',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'basic field (lowercase)' => [
|
||||||
|
'name' => 'somefield',
|
||||||
|
'throwException' => true,
|
||||||
|
'expected' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'somefield',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getsomefield',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'somefield',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'property not set, dont even try it' => [
|
||||||
|
'name' => 'NotSet',
|
||||||
|
'throwException' => true,
|
||||||
|
'expected' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'NotSet',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getNotSet',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'stops after method when not throwing' => [
|
||||||
|
'name' => 'SomeField',
|
||||||
|
'throwException' => false,
|
||||||
|
'expected' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'SomeField',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideGet')]
|
||||||
|
public function testGet(string $name, bool $throwException, array $expected): void
|
||||||
|
{
|
||||||
|
$fixture = new TestFixture();
|
||||||
|
$fixture->throwException = $throwException;
|
||||||
|
$viewLayerData = new ViewLayerData($fixture);
|
||||||
|
$value = $viewLayerData->$name;
|
||||||
|
$this->assertSame($expected, $fixture->getRequested());
|
||||||
|
$this->assertNull($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideGetComplex(): array
|
||||||
|
{
|
||||||
|
// Note the actual value checks aren't very comprehensive here because that's done
|
||||||
|
// in more detail in testGetRawDataValue
|
||||||
|
return [
|
||||||
|
'exception gets thrown if not __call() method' => [
|
||||||
|
'name' => 'badMethodCall',
|
||||||
|
'expectRequested' => BadMethodCallException::class,
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
'returning nothing is like returning null' => [
|
||||||
|
'name' => 'voidMethod',
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'voidMethod',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
'returned value is caught' => [
|
||||||
|
'name' => 'justCallMethod',
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'justCallMethod',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => 'This is a method value',
|
||||||
|
],
|
||||||
|
'getter is used' => [
|
||||||
|
'name' => 'ActualValue',
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getActualValue',
|
||||||
|
'args' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => 'this is the value',
|
||||||
|
],
|
||||||
|
'if no method exists, only property is fetched' => [
|
||||||
|
'name' => 'NoMethod',
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'NoMethod',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
'property value is caught' => [
|
||||||
|
'name' => 'ActualValueField',
|
||||||
|
'expectRequested' => [
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'ActualValueField',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'expected' => 'the value is here',
|
||||||
|
],
|
||||||
|
'not set and no method' => [
|
||||||
|
'name' => 'NotSet',
|
||||||
|
'expectRequested' => [],
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideGetComplex')]
|
||||||
|
public function testGetComplex(string $name, string|array $expectRequested, ?string $expected): void
|
||||||
|
{
|
||||||
|
$fixture = new TestFixtureComplex();
|
||||||
|
$viewLayerData = new ViewLayerData($fixture);
|
||||||
|
if ($expectRequested === BadMethodCallException::class) {
|
||||||
|
$this->expectException(BadMethodCallException::class);
|
||||||
|
}
|
||||||
|
$value = $viewLayerData->$name;
|
||||||
|
$this->assertSame($expectRequested, $fixture->getRequested());
|
||||||
|
$this->assertEquals($expected, $value);
|
||||||
|
// Ensure value is being wrapped when not null
|
||||||
|
if ($value !== null) {
|
||||||
|
$this->assertInstanceOf(ViewLayerData::class, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideCall(): array
|
||||||
|
{
|
||||||
|
// Currently there is no distinction between trying to get a property or call a method from ViewLayerData
|
||||||
|
// so the "get" examples should produce the same results when calling a method.
|
||||||
|
$scenarios = static::provideGet();
|
||||||
|
foreach ($scenarios as &$scenario) {
|
||||||
|
$scenario['args'] = [];
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
...$scenarios,
|
||||||
|
'basic field with args' => [
|
||||||
|
'name' => 'SomeField',
|
||||||
|
'args' => ['abc', 123],
|
||||||
|
'throwException' => true,
|
||||||
|
'expected' => [
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'SomeField',
|
||||||
|
'args' => ['abc', 123],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => 'getSomeField',
|
||||||
|
'args' => ['abc', 123],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => 'SomeField',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideCall')]
|
||||||
|
public function testCall(string $name, array $args, bool $throwException, array $expected): void
|
||||||
|
{
|
||||||
|
$fixture = new TestFixture();
|
||||||
|
$fixture->throwException = $throwException;
|
||||||
|
$viewLayerData = new ViewLayerData($fixture);
|
||||||
|
$value = $viewLayerData->$name(...$args);
|
||||||
|
$this->assertSame($expected, $fixture->getRequested());
|
||||||
|
$this->assertNull($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideCallComplex(): array
|
||||||
|
{
|
||||||
|
// Currently there is no distinction between trying to get a property or call a method from ViewLayerData
|
||||||
|
// so the "get" examples should produce the same results when calling a method.
|
||||||
|
return static::provideGetComplex();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideCallComplex')]
|
||||||
|
public function testCallComplex(string $name, string|array $expectRequested, ?string $expected): void
|
||||||
|
{
|
||||||
|
$fixture = new TestFixtureComplex();
|
||||||
|
$viewLayerData = new ViewLayerData($fixture);
|
||||||
|
if ($expectRequested === BadMethodCallException::class) {
|
||||||
|
$this->expectException(BadMethodCallException::class);
|
||||||
|
}
|
||||||
|
$value = $viewLayerData->$name();
|
||||||
|
$this->assertSame($expectRequested, $fixture->getRequested());
|
||||||
|
$this->assertEquals($expected, $value);
|
||||||
|
// Ensure value is being wrapped when not null
|
||||||
|
if ($value !== null) {
|
||||||
|
$this->assertInstanceOf(ViewLayerData::class, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideToString(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
// These three all evaluate to ArrayList or ArrayData, which don't have templates to render
|
||||||
|
'empty array' => [
|
||||||
|
'data' => [],
|
||||||
|
'expected' => MissingTemplateException::class,
|
||||||
|
],
|
||||||
|
'array with values' => [
|
||||||
|
'data' => ['value1', 'value2'],
|
||||||
|
'expected' => MissingTemplateException::class,
|
||||||
|
],
|
||||||
|
'Class with no template' => [
|
||||||
|
// Note we won't check classes WITH templates because we're not testing the template engine here
|
||||||
|
'data' => new ArrayData(['Field1' => 'value1', 'Field2' => 'value2']),
|
||||||
|
'expected' => MissingTemplateException::class,
|
||||||
|
],
|
||||||
|
'string value' => [
|
||||||
|
'data' => 'just a string',
|
||||||
|
'expected' => 'just a string',
|
||||||
|
],
|
||||||
|
'html gets escaped by default' => [
|
||||||
|
'data' => '<span>HTML string</span>',
|
||||||
|
'expected' => '<span>HTML string</span>',
|
||||||
|
],
|
||||||
|
'explicit HTML text not escaped' => [
|
||||||
|
'data' => (new DBHTMLText())->setValue('<span>HTML string</span>'),
|
||||||
|
'expected' => '<span>HTML string</span>',
|
||||||
|
],
|
||||||
|
'DBField' => [
|
||||||
|
'data' => (new DBDate())->setValue('2024-03-24'),
|
||||||
|
'expected' => (new DBDate())->setValue('2024-03-24')->forTemplate(),
|
||||||
|
],
|
||||||
|
'__toString() method' => [
|
||||||
|
'data' => new StringableObject(),
|
||||||
|
'expected' => 'This is the string representation',
|
||||||
|
],
|
||||||
|
'forTemplate called from extension' => [
|
||||||
|
'data' => new ExtensibleObject(),
|
||||||
|
'expected' => 'This text comes from the extension class',
|
||||||
|
],
|
||||||
|
'cannot convert this class to string' => [
|
||||||
|
'data' => new CountableObject(),
|
||||||
|
'expected' => Error::class,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideToString')]
|
||||||
|
public function testToString(mixed $data, string $expected): void
|
||||||
|
{
|
||||||
|
$viewLayerData = new ViewLayerData($data);
|
||||||
|
if (is_a($expected, Throwable::class, true)) {
|
||||||
|
$this->expectException($expected);
|
||||||
|
}
|
||||||
|
$this->assertSame($expected, (string) $viewLayerData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideHasDataValue(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'empty array' => [
|
||||||
|
'data' => [],
|
||||||
|
'name' => null,
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'empty ArrayList' => [
|
||||||
|
'data' => new ArrayList(),
|
||||||
|
'name' => null,
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'empty ArrayData' => [
|
||||||
|
'data' => new ArrayData(),
|
||||||
|
'name' => null,
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'empty ArrayIterator' => [
|
||||||
|
'data' => new ArrayIterator(),
|
||||||
|
'name' => null,
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'empty ModelData' => [
|
||||||
|
'data' => new ModelData(),
|
||||||
|
'name' => null,
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'non-countable object' => [
|
||||||
|
'data' => new ExtensibleObject(),
|
||||||
|
'name' => null,
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'array with data' => [
|
||||||
|
'data' => [1,2,3],
|
||||||
|
'name' => null,
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'associative array' => [
|
||||||
|
'data' => ['one' => 1, 'two' => 2],
|
||||||
|
'name' => null,
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'ArrayList with data' => [
|
||||||
|
'data' => new ArrayList([1,2,3]),
|
||||||
|
'name' => null,
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'ArrayData with data' => [
|
||||||
|
'data' => new ArrayData(['one' => 1, 'two' => 2]),
|
||||||
|
'name' => null,
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'ArrayIterator with data' => [
|
||||||
|
'data' => new ArrayIterator([1,2,3]),
|
||||||
|
'name' => null,
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'ArrayData missing value' => [
|
||||||
|
'data' => new ArrayData(['one' => 1, 'two' => 2]),
|
||||||
|
'name' => 'three',
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'ArrayData with truthy value' => [
|
||||||
|
'data' => new ArrayData(['one' => 1, 'two' => 2]),
|
||||||
|
'name' => 'one',
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'ArrayData with null value' => [
|
||||||
|
'data' => new ArrayData(['nullVal' => null, 'two' => 2]),
|
||||||
|
'name' => 'nullVal',
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'ArrayData with falsy value' => [
|
||||||
|
'data' => new ArrayData(['zero' => 0, 'two' => 2]),
|
||||||
|
'name' => 'zero',
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'Empty string' => [
|
||||||
|
'data' => '',
|
||||||
|
'name' => null,
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
'Truthy string' => [
|
||||||
|
'data' => 'has a value',
|
||||||
|
'name' => null,
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
'Field on a string' => [
|
||||||
|
'data' => 'has a value',
|
||||||
|
'name' => 'SomeField',
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideHasDataValue')]
|
||||||
|
public function testHasDataValue(mixed $data, ?string $name, bool $expected): void
|
||||||
|
{
|
||||||
|
$viewLayerData = new ViewLayerData($data);
|
||||||
|
$this->assertSame($expected, $viewLayerData->hasDataValue($name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideGetRawDataValue(): array
|
||||||
|
{
|
||||||
|
$dbHtml = (new DBHTMLText())->setValue('Some html text');
|
||||||
|
// Note we're not checking the fetch order or passing args here - see testGet and testCall for that.
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'data' => ['MyField' => 'some value'],
|
||||||
|
'name' => 'MissingField',
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['MyField' => null],
|
||||||
|
'name' => 'MyField',
|
||||||
|
'expected' => null,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['MyField' => 'some value'],
|
||||||
|
'name' => 'MyField',
|
||||||
|
'expected' => 'some value',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['MyField' => 123],
|
||||||
|
'name' => 'MyField',
|
||||||
|
'expected' => 123,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['MyField' => true],
|
||||||
|
'name' => 'MyField',
|
||||||
|
'expected' => true,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['MyField' => false],
|
||||||
|
'name' => 'MyField',
|
||||||
|
'expected' => false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => ['MyField' => $dbHtml],
|
||||||
|
'name' => 'MyField',
|
||||||
|
'expected' => $dbHtml,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => (new ArrayData(['MyField' => 1234]))->customise(new ArrayData(['MyField' => 'overridden value'])),
|
||||||
|
'name' => 'MyField',
|
||||||
|
'expected' => 'overridden value',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => (new ArrayData(['MyField' => 1234]))->customise(new ArrayData(['FieldTwo' => 'checks here'])),
|
||||||
|
'name' => 'FieldTwo',
|
||||||
|
'expected' => 'checks here',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'data' => (new ArrayData(['MyField' => 1234]))->customise(new ArrayData(['FieldTwo' => 'not here'])),
|
||||||
|
'name' => 'MyField',
|
||||||
|
'expected' => 1234,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideGetRawDataValue')]
|
||||||
|
public function testGetRawDataValue(mixed $data, string $name, mixed $expected): void
|
||||||
|
{
|
||||||
|
$viewLayerData = new ViewLayerData($data);
|
||||||
|
$this->assertSame($expected, $viewLayerData->getRawDataValue($name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideGetRawDataValueType(): array
|
||||||
|
{
|
||||||
|
// The types aren't currently used, but are passed in so we can use them later
|
||||||
|
// if we find the distinction useful. We should test they do what we expect
|
||||||
|
// in the meantime.
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'type' => 'property',
|
||||||
|
'shouldThrow' => false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'method',
|
||||||
|
'shouldThrow' => false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'any',
|
||||||
|
'shouldThrow' => false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'constant',
|
||||||
|
'shouldThrow' => true,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'type' => 'randomtext',
|
||||||
|
'shouldThrow' => true,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[DataProvider('provideGetRawDataValueType')]
|
||||||
|
public function testGetRawDataValueType(string $type, bool $shouldThrow): void
|
||||||
|
{
|
||||||
|
$viewLayerData = new ViewLayerData([]);
|
||||||
|
if ($shouldThrow) {
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
} else {
|
||||||
|
$this->expectNotToPerformAssertions();
|
||||||
|
}
|
||||||
|
$viewLayerData->getRawDataValue('something', type: $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCache(): void
|
||||||
|
{
|
||||||
|
$data = new ArrayData(['MyField' => 'some value']);
|
||||||
|
$viewLayerData = new ViewLayerData($data);
|
||||||
|
|
||||||
|
// No cache because we haven't fetched anything
|
||||||
|
$this->assertNull($data->objCacheGet('MyField'));
|
||||||
|
|
||||||
|
// Fetching the value caches it
|
||||||
|
$viewLayerData->MyField;
|
||||||
|
$this->assertSame('some value', $data->objCacheGet('MyField'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSpecialNames(): void
|
||||||
|
{
|
||||||
|
$data = new stdClass;
|
||||||
|
$viewLayerData = new ViewLayerData($data);
|
||||||
|
|
||||||
|
// Metadata values are available when there's nothing in the actual data
|
||||||
|
$this->assertTrue(isset($viewLayerData->ClassName));
|
||||||
|
$this->assertTrue(isset($viewLayerData->Me));
|
||||||
|
$this->assertSame(stdClass::class, $viewLayerData->getRawDataValue('ClassName')->getValue());
|
||||||
|
$this->assertSame($data, $viewLayerData->getRawDataValue('Me'));
|
||||||
|
|
||||||
|
// Metadata values are lower priority than real values in the actual data
|
||||||
|
$data->ClassName = 'some other class';
|
||||||
|
$data->Me = 'something else';
|
||||||
|
$this->assertTrue(isset($viewLayerData->ClassName));
|
||||||
|
$this->assertTrue(isset($viewLayerData->Me));
|
||||||
|
$this->assertSame('some other class', $viewLayerData->getRawDataValue('ClassName'));
|
||||||
|
$this->assertSame('something else', $viewLayerData->getRawDataValue('Me'));
|
||||||
|
}
|
||||||
|
}
|
14
tests/php/View/ViewLayerDataTest/CountableObject.php
Normal file
14
tests/php/View/ViewLayerDataTest/CountableObject.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\ViewLayerDataTest;
|
||||||
|
|
||||||
|
use Countable;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class CountableObject implements Countable, TestOnly
|
||||||
|
{
|
||||||
|
public function count(): int
|
||||||
|
{
|
||||||
|
return 53;
|
||||||
|
}
|
||||||
|
}
|
11
tests/php/View/ViewLayerDataTest/ExtensibleObject.php
Normal file
11
tests/php/View/ViewLayerDataTest/ExtensibleObject.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\ViewLayerDataTest;
|
||||||
|
|
||||||
|
use SilverStripe\Core\Extensible;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class ExtensibleObject implements TestOnly
|
||||||
|
{
|
||||||
|
use Extensible;
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\ViewLayerDataTest;
|
||||||
|
|
||||||
|
use SilverStripe\Core\Extension;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class ExtensibleObjectExtension extends Extension implements TestOnly
|
||||||
|
{
|
||||||
|
public function getIterator(): iterable
|
||||||
|
{
|
||||||
|
return ['1','2','3','4','5','6','7','8','9','a','b','c','d','e'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAnything(): string
|
||||||
|
{
|
||||||
|
return 'something';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function forTemplate(): string
|
||||||
|
{
|
||||||
|
return 'This text comes from the extension class';
|
||||||
|
}
|
||||||
|
}
|
13
tests/php/View/ViewLayerDataTest/GetCountObject.php
Normal file
13
tests/php/View/ViewLayerDataTest/GetCountObject.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\ViewLayerDataTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class GetCountObject implements TestOnly
|
||||||
|
{
|
||||||
|
public function getCount(): int
|
||||||
|
{
|
||||||
|
return 12;
|
||||||
|
}
|
||||||
|
}
|
17
tests/php/View/ViewLayerDataTest/NonIterableObject.php
Normal file
17
tests/php/View/ViewLayerDataTest/NonIterableObject.php
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\ViewLayerDataTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class NonIterableObject implements TestOnly
|
||||||
|
{
|
||||||
|
public function getIterator(): iterable
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'some value',
|
||||||
|
'another value',
|
||||||
|
'isnt this nice',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
14
tests/php/View/ViewLayerDataTest/StringableObject.php
Normal file
14
tests/php/View/ViewLayerDataTest/StringableObject.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\ViewLayerDataTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
|
class StringableObject implements Stringable, TestOnly
|
||||||
|
{
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return 'This is the string representation';
|
||||||
|
}
|
||||||
|
}
|
49
tests/php/View/ViewLayerDataTest/TestFixture.php
Normal file
49
tests/php/View/ViewLayerDataTest/TestFixture.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\ViewLayerDataTest;
|
||||||
|
|
||||||
|
use BadMethodCallException;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A test fixture that captures information about what's being fetched on it
|
||||||
|
*/
|
||||||
|
class TestFixture implements TestOnly
|
||||||
|
{
|
||||||
|
private array $requested = [];
|
||||||
|
|
||||||
|
public bool $throwException = true;
|
||||||
|
|
||||||
|
public function __call(string $name, array $arguments = []): null
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => $name,
|
||||||
|
'args' => $arguments,
|
||||||
|
];
|
||||||
|
if ($this->throwException) {
|
||||||
|
throw new BadMethodCallException('We need this so ViewLayerData will try the next step');
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __get(string $name): null
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => $name,
|
||||||
|
];
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __isset(string $name): bool
|
||||||
|
{
|
||||||
|
return $name !== 'NotSet';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRequested(): array
|
||||||
|
{
|
||||||
|
return $this->requested;
|
||||||
|
}
|
||||||
|
}
|
79
tests/php/View/ViewLayerDataTest/TestFixtureComplex.php
Normal file
79
tests/php/View/ViewLayerDataTest/TestFixtureComplex.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\View\Tests\ViewLayerDataTest;
|
||||||
|
|
||||||
|
use BadMethodCallException;
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A test fixture that captures information about what's being fetched on it
|
||||||
|
* Has explicit methods instead of relying on __call()
|
||||||
|
*/
|
||||||
|
class TestFixtureComplex implements TestOnly
|
||||||
|
{
|
||||||
|
private array $requested = [];
|
||||||
|
|
||||||
|
public function badMethodCall(): void
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => __FUNCTION__,
|
||||||
|
'args' => func_get_args(),
|
||||||
|
];
|
||||||
|
throw new BadMethodCallException('Without a __call() method this will actually be thrown');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function voidMethod(): void
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => __FUNCTION__,
|
||||||
|
'args' => func_get_args(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function justCallMethod(): string
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => __FUNCTION__,
|
||||||
|
'args' => func_get_args(),
|
||||||
|
];
|
||||||
|
return 'This is a method value';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getActualValue(): string
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'method',
|
||||||
|
'name' => __FUNCTION__,
|
||||||
|
'args' => func_get_args(),
|
||||||
|
];
|
||||||
|
return 'this is the value';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __get(string $name): ?string
|
||||||
|
{
|
||||||
|
$this->requested[] = [
|
||||||
|
'type' => 'property',
|
||||||
|
'name' => $name,
|
||||||
|
];
|
||||||
|
if ($name === 'ActualValueField') {
|
||||||
|
return 'the value is here';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We need this so we always try to fetch a property.
|
||||||
|
*/
|
||||||
|
public function __isset(string $name): bool
|
||||||
|
{
|
||||||
|
return $name !== 'NotSet';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRequested(): array
|
||||||
|
{
|
||||||
|
return $this->requested;
|
||||||
|
}
|
||||||
|
}
|
@ -17,10 +17,11 @@ use SilverStripe\i18n\Tests\i18nTest\MyObject;
|
|||||||
use SilverStripe\i18n\Tests\i18nTest\MySubObject;
|
use SilverStripe\i18n\Tests\i18nTest\MySubObject;
|
||||||
use SilverStripe\i18n\Tests\i18nTest\TestDataObject;
|
use SilverStripe\i18n\Tests\i18nTest\TestDataObject;
|
||||||
use SilverStripe\View\SSViewer;
|
use SilverStripe\View\SSViewer;
|
||||||
use SilverStripe\View\SSViewer_DataPresenter;
|
|
||||||
use SilverStripe\View\ThemeResourceLoader;
|
use SilverStripe\View\ThemeResourceLoader;
|
||||||
use SilverStripe\View\ThemeManifest;
|
use SilverStripe\View\ThemeManifest;
|
||||||
use SilverStripe\Model\ModelData;
|
use SilverStripe\Model\ModelData;
|
||||||
|
use SilverStripe\View\SSViewer_Scope;
|
||||||
|
use SilverStripe\View\ViewLayerData;
|
||||||
use Symfony\Component\Translation\Loader\ArrayLoader;
|
use Symfony\Component\Translation\Loader\ArrayLoader;
|
||||||
use Symfony\Component\Translation\Translator;
|
use Symfony\Component\Translation\Translator;
|
||||||
|
|
||||||
@ -71,9 +72,9 @@ trait i18nTestManifest
|
|||||||
|
|
||||||
public function setupManifest()
|
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)
|
// test-project class manifest (since it will lose visibility of core classes)
|
||||||
$presenter = new SSViewer_DataPresenter(new ModelData());
|
$presenter = new SSViewer_Scope(new ViewLayerData([]));
|
||||||
unset($presenter);
|
unset($presenter);
|
||||||
|
|
||||||
// Switch to test manifest
|
// Switch to test manifest
|
||||||
|
Loading…
Reference in New Issue
Block a user