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

Issue #323 dynamic property assignment #326

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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: 23 additions & 4 deletions doc/content/engine/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ use League\Plates\Extension\ExtensionInterface;

class ChangeCase implements ExtensionInterface
{
public function register(Engine $engine)
protected ?Template $template;

public function register(Engine $engine): void
{
$engine->registerFunction('uppercase', [$this, 'uppercaseString']);
$engine->registerFunction('lowercase', [$this, 'lowercaseString']);
}

public function setTemplate(?Template $template): void
{
$this->template = $template;
}

public function uppercaseString($var)
{
return strtoupper($var);
Expand Down Expand Up @@ -56,11 +63,18 @@ use League\Plates\Extension\ExtensionInterface;

class ChangeCase implements ExtensionInterface
{
public function register(Engine $engine)
protected ?Template $template;

public function register(Engine $engine): void
{
$engine->registerFunction('case', [$this, 'getObject']);
}

public function setTemplate(?Template $template): void
{
$this->template = $template;
}

public function getObject()
{
return $this;
Expand Down Expand Up @@ -94,7 +108,7 @@ $engine->loadExtension(new ChangeCase());

## Accessing the engine and template

It may be desirable to access the `engine` or `template` objects from within your extension. Plates makes both of these objects available to you. The engine is automatically passed to the `register()` method, and the template is assigned as a parameter on each function call.
It may be desirable to access the `engine` or `template` objects from within your extension. Plates makes both of these objects available to you. The engine is automatically passed to the `register()` method, and the template is assigned with a call to `setTemplate()` with the according template when available.

~~~ php
use League\Plates\Engine;
Expand All @@ -103,7 +117,7 @@ use League\Plates\Extension\ExtensionInterface;
class MyExtension implements ExtensionInterface
{
protected $engine;
public $template; // must be public
protected $template;

public function register(Engine $engine)
{
Expand All @@ -115,5 +129,10 @@ class MyExtension implements ExtensionInterface
// Register functions
// ...
}

public function setTemplate(?Template $template): void
{
$this->template = $template;
}
}
~~~
18 changes: 13 additions & 5 deletions src/Extension/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
class Asset implements ExtensionInterface
{
/**
* Instance of the current template.
* @var Template
* @var Template|null
*/
public $template;
protected ?Template $template;

/**
* Path to asset directory.
Expand Down Expand Up @@ -43,13 +42,22 @@ public function __construct($path, $filenameMethod = false)
/**
* Register extension function.
* @param Engine $engine
* @return null
* @return void
*/
public function register(Engine $engine)
public function register(Engine $engine): void
{
$engine->registerFunction('asset', array($this, 'cachedAssetUrl'));
}

/**
* @param Template|null $template
* @return void
*/
public function setTemplate(?Template $template): void
{
$this->template = $template;
}

/**
* Create "cache busted" asset URL.
* @param string $url
Expand Down
4 changes: 3 additions & 1 deletion src/Extension/ExtensionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace League\Plates\Extension;

use League\Plates\Engine;
use League\Plates\Template\Template;

/**
* A common interface for extensions.
*/
interface ExtensionInterface
{
public function register(Engine $engine);
public function register(Engine $engine): void;
public function setTemplate(?Template $template): void;
}
18 changes: 13 additions & 5 deletions src/Extension/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
class URI implements ExtensionInterface
{
/**
* Instance of the current template.
* @var Template
* @var Template|null
*/
public $template;
protected ?Template $template;

/**
* The request URI.
Expand All @@ -42,13 +41,22 @@ public function __construct($uri)
/**
* Register extension functions.
* @param Engine $engine
* @return null
* @return void
*/
public function register(Engine $engine)
public function register(Engine $engine): void
{
$engine->registerFunction('uri', array($this, 'runUri'));
}

/**
* @param Template|null $template
* @return void
*/
public function setTemplate(?Template $template): void
{
$this->template = $template;
}

/**
* Perform URI check.
* @param null|integer|string $var1
Expand Down
12 changes: 7 additions & 5 deletions src/Template/Func.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ public function getCallback()
*/
public function call(Template $template = null, $arguments = array())
{
if (is_array($this->callback) and
isset($this->callback[0]) and
$this->callback[0] instanceof ExtensionInterface
) {
$this->callback[0]->template = $template;
if ($template) {
if (is_array($this->callback) and
isset($this->callback[0]) and
$this->callback[0] instanceof ExtensionInterface
) {
$this->callback[0]->setTemplate($template);
}
}

return call_user_func_array($this->callback, $arguments);
Expand Down
18 changes: 17 additions & 1 deletion tests/Template/FuncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
use League\Plates\Template\Func;
use League\Plates\Template\Template;
use PHPUnit\Framework\TestCase;

class FuncTest extends TestCase
Expand Down Expand Up @@ -59,9 +60,24 @@ public function testFunctionCall()
public function testExtensionFunctionCall()
{
$extension = new class() implements ExtensionInterface {
public function register(Engine $engine)
protected ?Template $template;

/**
* Do nothing
*/
public function register(Engine $engine): void
{
}

/**
* @param Template|null $template
* @return void
*/
public function setTemplate(?Template $template): void
{
$this->template = $template;
}

public function foo(): string
{
return 'bar';
Expand Down