Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Update API to suit its status as its own module #1

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .editorconfig
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from module standardiser, minus the bit about admin which isn't relevant here. Wanted it in here while I'm working to reduce cleanup required later.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,js,json,css,scss,eslintrc,feature}]
indent_size = 2
indent_style = space

[composer.json]
indent_size = 4

# Don't perform any clean-up on thirdparty files

[thirdparty/**]
trim_trailing_whitespace = false
insert_final_newline = false
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/tests/php export-ignore
/.tx export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.github export-ignore
/*.dist export-ignore
/phpcs.xml.dist export-ignore
/.editorconfig export-ignore
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

jobs:
ci:
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
silverstripe-cache/
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "silverstripe/template-engine",
"description": "Template engine for rendering templates in Silverstripe CMS projects",
"type": "silverstripe-vendormodule",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Silverstripe",
"homepage": "https://silverstripe.com"
},
{
"name": "The Silverstripe Community",
"homepage": "https://silverstripe.org"
}
],
"require": {
"php": "^8.3",
"silverstripe/framework": "^6"
},
"require-dev": {
"silverstripe/recipe-testing": "^4",
"silverstripe/standards": "^1",
"squizlabs/php_codesniffer": "^3",
"phpstan/extension-installer": "^1.3"
},
"autoload": {
"psr-4": {
"SilverStripe\\TemplateEngine\\": "src/",
"SilverStripe\\TemplateEngine\\Tests\\": "tests/php/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
44 changes: 44 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<file>src</file>
<file>tests</file>

<!-- Show progress and output sniff names on violation, and add colours -->
<arg value="p" />
<arg name="colors" />
<arg value="s" />

<!-- base rules are PSR-12 -->
<rule ref="PSR12" >
<!-- Current exclusions -->
<!--
<exclude name="PSR1.Methods.CamelCapsMethodName" />
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
<exclude name="PSR2.Classes.PropertyDeclaration" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration" /> <!-- causes php notice while linting -->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will clean this up before marking ready-to-merge.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should clean it up then as this PR's in review?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Sorry. I thought I'd forget that's why I added that comment lol.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up, and resolved linting failures from a local run.

<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenercase" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenerdefault" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration.TerminatingComment" />
<exclude name="PSR2.Methods.MethodDeclaration.Underscore" />
<exclude name="Squiz.Scope.MethodScope" />
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
<exclude name="Generic.Files.LineLength.TooLong" />
<exclude name="PEAR.Functions.ValidDefaultValue.NotAtEnd" />
-->
</rule>

<!--
<rule ref="Squiz.Strings.ConcatenationSpacing">
<properties>
<property name="spacing" value="1" />
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>
-->

<!-- PHP-PEG generated file not intended for human consumption -->
<exclude-pattern>*/SSTemplateParser.php$</exclude-pattern>
</ruleset>

3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
paths:
- src
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace SilverStripe\View;
namespace SilverStripe\TemplateEngine;

/**
* Defines an extra set of basic methods that can be used in templates
* that are not defined on sub-classes of {@link ModelData}.
*/
class SSViewer_BasicIteratorSupport implements TemplateIteratorProvider
class BasicIteratorSupport implements TemplateIteratorProvider
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing SSViewer_ prefix from any classes in this repo. They don't have anything to do with SSViewer anymore.

{
/**
* @var int
Expand Down
12 changes: 0 additions & 12 deletions src/Exception/MissingTemplateException.php
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm keeping the SSTemplate prefix on classes in this repo which already had it because:

  1. It makes it instantly clear what kind of templates it's related to when used in project code
  2. It keeps things nicely tied together
  3. It removes the need to rename them, which (even if only slightly) means we're not introducing more upgrade pain.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\View;
namespace SilverStripe\TemplateEngine\Exception;

use Exception;

Expand Down
18 changes: 11 additions & 7 deletions src/SSTemplateEngine.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\View;
namespace SilverStripe\TemplateEngine;

use InvalidArgumentException;
use Psr\SimpleCache\CacheInterface;
Expand All @@ -14,6 +14,10 @@
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Permission;
use SilverStripe\View\Exception\MissingTemplateException;
use SilverStripe\View\SSViewer;
use SilverStripe\View\TemplateEngine;
use SilverStripe\View\ThemeResourceLoader;
use SilverStripe\View\ViewLayerData;

/**
* Parses template files with an *.ss file extension, or strings representing templates in that format.
Expand Down Expand Up @@ -90,7 +94,7 @@ public function __construct(string|array $templateCandidates = [])
* 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
public static function execute_template(array|string $template, ViewLayerData $data, array $overlay = [], ?ScopeManager $scope = null): string
{
$engine = static::create($template);
return $engine->render($data, $overlay, $scope);
Expand Down Expand Up @@ -167,7 +171,7 @@ public function renderString(string $template, ViewLayerData $model, array $over
return $output;
}

public function render(ViewLayerData $model, array $overlay = [], ?SSViewer_Scope $scope = null): string
public function render(ViewLayerData $model, array $overlay = [], ?ScopeManager $scope = null): string
{
SSTemplateEngine::$topLevel[] = $model;
$template = $this->chosen;
Expand Down Expand Up @@ -221,7 +225,7 @@ public function render(ViewLayerData $model, array $overlay = [], ?SSViewer_Scop
// 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.
// This mirrors how ScopeManager handles null values.
if (!$subtemplateViewer->chosen) {
return null;
}
Expand Down Expand Up @@ -293,14 +297,14 @@ public function getPartialCacheStore(): CacheInterface
* @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
* @param ScopeManager|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
?ScopeManager $inheritedScope = null
): string {
if (isset($_GET['showtemplate']) && $_GET['showtemplate'] && Permission::check('ADMIN')) {
$lines = file($cacheFile ?? '');
Expand All @@ -313,7 +317,7 @@ protected function includeGeneratedTemplate(
}

$cache = $this->getPartialCacheStore();
$scope = new SSViewer_Scope($model, $overlay, $underlay, $inheritedScope);
$scope = new ScopeManager($model, $overlay, $underlay, $inheritedScope);
$val = '';

// Placeholder for values exposed to $cacheFile
Expand Down
20 changes: 8 additions & 12 deletions src/SSTemplateParser.peg
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,25 @@ parser.
It gets run through the php-peg parser compiler to have those comments turned into code that match parts of the
template language, producing the executable version SSTemplateParser.php

To recompile after changing this file, run this from the 'framework/View' directory via command line (in most cases
this is: framework/src/View):
To recompile after changing this file, run this from the 'src' directory via command line:

php ../../thirdparty/php-peg/cli.php SSTemplateParser.peg > SSTemplateParser.php
php ../thirdparty/php-peg/cli.php SSTemplateParser.peg > SSTemplateParser.php

See the php-peg docs for more information on the parser format, and how to convert this file into SSTemplateParser.php

This comment will not appear in the output
*/

namespace SilverStripe\View;
namespace SilverStripe\TemplateEngine;

use SilverStripe\Core\Injector\Injector;
use Parser;
use InvalidArgumentException;
use SilverStripe\TemplateEngine\Exception\SSTemplateParseException;

// We want this to work when run by hand too
if (defined('THIRDPARTY_PATH')) {
require_once(THIRDPARTY_PATH . '/php-peg/Parser.php');
} else {
$base = dirname(__FILE__);
require_once($base.'/../thirdparty/php-peg/Parser.php');
}
// Make sure to include the base parser code
$base = dirname(__FILE__);
require_once($base.'/../thirdparty/php-peg/Parser.php');
Comment on lines -29 to +30
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That const was pointing to the thirdparty directory in framework, which isn't going to exist anymore anyway.


/**
* This is the parser for the SilverStripe template language. It gets called on a string and uses a php-peg parser
Expand Down Expand Up @@ -936,7 +932,7 @@ class SSTemplateParser extends Parser implements TemplateParser
$arguments = $res['arguments'];

// Note: 'type' here is important to disable subTemplates in SSTemplateEngine::getSubtemplateFor()
$res['php'] = '$val .= \\SilverStripe\\View\\SSTemplateEngine::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' .
$res['php'] = '$val .= \\SilverStripe\\TemplateEngine\\SSTemplateEngine::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' .
implode(',', $arguments)."], \$scope, true);\n";

if ($this->includeDebuggingComments) { // Add include filename comments on dev sites
Expand Down
15 changes: 6 additions & 9 deletions src/SSTemplateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@



namespace SilverStripe\View;
namespace SilverStripe\TemplateEngine;

use SilverStripe\Core\Injector\Injector;
use Parser;
use InvalidArgumentException;
use SilverStripe\TemplateEngine\Exception\SSTemplateParseException;

// We want this to work when run by hand too
if (defined('THIRDPARTY_PATH')) {
require_once(THIRDPARTY_PATH . '/php-peg/Parser.php');
} else {
$base = dirname(__FILE__);
require_once($base.'/../thirdparty/php-peg/Parser.php');
}
// Make sure to include the base parser code
$base = dirname(__FILE__);
require_once($base.'/../thirdparty/php-peg/Parser.php');

/**
* This is the parser for the SilverStripe template language. It gets called on a string and uses a php-peg parser
Expand Down Expand Up @@ -3894,7 +3891,7 @@ function Include__finalise(&$res)
$arguments = $res['arguments'];

// Note: 'type' here is important to disable subTemplates in SSTemplateEngine::getSubtemplateFor()
$res['php'] = '$val .= \\SilverStripe\\View\\SSTemplateEngine::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' .
$res['php'] = '$val .= \\SilverStripe\\TemplateEngine\\SSTemplateEngine::execute_template([["type" => "Includes", '.$template.'], '.$template.'], $scope->getCurrentItem(), [' .
implode(',', $arguments)."], \$scope, true);\n";

if ($this->includeDebuggingComments) { // Add include filename comments on dev sites
Expand Down
Loading