Skip to content

Commit

Permalink
up: update some method sign for compiler class
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 3, 2021
1 parent a5a0cb4 commit 1002991
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 101 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
}
],
"require": {
"php": ">8.0",
"php": ">8.0.1",
"ext-mbstring": "*",
"toolkit/fsutil":"~1.0",
"toolkit/stdlib":"~1.0"
"toolkit/fsutil":"~2.0",
"toolkit/stdlib":"~2.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class AbstractTemplate implements TemplateInterface
*
* @return static
*/
public static function new(array $config = []): self
public static function new(array $config = []): static
{
return new static($config);
}
Expand All @@ -82,7 +82,7 @@ public function __construct(array $config = [])
*
* @return $this
*/
public function configThis(callable $fn): self
public function configThis(callable $fn): static
{
$fn($this);
return $this;
Expand Down Expand Up @@ -242,7 +242,7 @@ public function setGlobalVars(array $globalVars): void
*
* @return AbstractTemplate
*/
public function setPathResolver(callable $pathResolver): self
public function setPathResolver(callable $pathResolver): static
{
$this->pathResolver = $pathResolver;
return $this;
Expand Down
29 changes: 23 additions & 6 deletions src/Compiler/AbstractCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,36 @@ abstract class AbstractCompiler implements CompilerInterface
/**
* custom directive, control statement token.
*
* eg: implement include()
* -----
* eg: implements: `include('parts/header.tpl')`
*
* ```php
* $compiler->addDirective('include', function(string $body, string $name): string {
* // $name : 'include'
* // $body : "('parts/header.tpl')"
* // do something...
* });
* ```
*
* @var array{string, callable(string, string): string}
*/
public array $customDirectives = [];

/**
* @return static
*/
public static function new(): static
{
return new static();
}

/**
* @param string $open
* @param string $close
*
* @return $this
*/
public function setOpenCloseTag(string $open, string $close): self
public function setOpenCloseTag(string $open, string $close): static
{
$this->openTag = $open;
$this->closeTag = $close;
Expand Down Expand Up @@ -173,18 +190,18 @@ public function addFilter(string $name, string $callExpr): self
* @param string $name
* @param callable $handler
*
* @return $this
* @return static
*/
public function addDirective(string $name, callable $handler): self
public function addDirective(string $name, callable $handler): static
{
$this->customDirectives[$name] = $handler;
return $this;
}

/**
* @return $this
* @return static
*/
public function disableEchoFilter(): self
public function disableEchoFilter(): static
{
$this->echoFilterFunc = self::RAW_OUTPUT;
return $this;
Expand Down
8 changes: 0 additions & 8 deletions src/Compiler/LineCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ class LineCompiler extends AbstractCompiler
*/
private bool $insideTag = false;

/**
* @return static
*/
public static function new(): self
{
return new self();
}

/**
* compile template contents to raw PHP template codes
*
Expand Down
15 changes: 1 addition & 14 deletions src/Compiler/PregCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,13 @@ class PregCompiler extends AbstractCompiler
private string $openTagE = '\{\{';
private string $closeTagE = '\}\}';

/**
* @return static
*/
public static function new(): self
{
return new self();
}

/**
* @param string $open
* @param string $close
*
* @return $this
*/
public function setOpenCloseTag(string $open, string $close): self
public function setOpenCloseTag(string $open, string $close): static
{
parent::setOpenCloseTag($open, $close);

Expand Down Expand Up @@ -113,11 +105,6 @@ public function parseCodeBlock(string $block): string
return self::PHP_TAG_OPEN . ' } ' . self::PHP_TAG_CLOSE;
}

// comments block. `{{# comments #}}`
// if ($block[0] === '#' && str_ends_with($block, '#')) {
// return '';
// }

$isInline = !str_contains($trimmed, "\n");
$kwPattern = Token::getBlockNamePattern();

Expand Down
12 changes: 10 additions & 2 deletions src/Contract/CompilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ interface CompilerInterface
*
* @return $this
*/
public function setOpenCloseTag(string $open, string $close): self;
public function setOpenCloseTag(string $open, string $close): static;

/**
* @return $this
*/
public function disableEchoFilter(): self;
public function disableEchoFilter(): static;

/**
* @param string $name
* @param callable $handler
*
* @return static
*/
public function addDirective(string $name, callable $handler): static;

/**
* compile template contents to raw PHP template codes
Expand Down
7 changes: 4 additions & 3 deletions src/EasyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ protected function init(CompilerInterface $compiler): void
{
// add built-in filters
$this->addFilters([
'upper' => 'strtoupper',
'lower' => 'strtolower',
'nl' => function ($str): string {
'upper' => 'strtoupper',
'lower' => 'strtolower',
'escape' => 'htmlspecialchars',
'nl' => function ($str): string {
return $str . "\n";
},
]);
Expand Down
10 changes: 10 additions & 0 deletions src/Extended/ExtendLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace PhpPkg\EasyTpl\Extended;

use Toolkit\Stdlib\Obj\Traits\AutoConfigTrait;

class ExtendLoader
{
use AutoConfigTrait;
}
51 changes: 51 additions & 0 deletions src/Extended/ExtendedTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace PhpPkg\EasyTpl\Extended;

use PhpPkg\EasyTpl\Contract\CompilerInterface;
use PhpPkg\EasyTpl\EasyTemplate;
use function sprintf;
use function trim;

/**
* Class ExtendedTemplate TODO
*
* @package PhpPkg\EasyTpl
*/
class ExtendedTemplate extends EasyTemplate
{
public const BLOCK_VAR_PREFIX = 'TPL_BLOCK_';

private $currentBlock = '';

protected function init(CompilerInterface $compiler): void
{
parent::init($compiler);

$compiler
->addDirective('extend', function (string $tplName) {
return ExtendLoader::new()->handle($tplName);
})
->addDirective('extends', function () {

})
->addDirective('block', function (string $body) {
$this->currentBlock = trim($body, '() ');

return sprintf('$this->startBlock(%s);', $this->currentBlock);
})
->addDirective('endblock', function () {
return '$this->endBlock();';
});
}

protected function startBlock(string $name): void
{

}

protected function endBlock(): void
{

}
}
27 changes: 27 additions & 0 deletions src/Extended/LayoutTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace PhpPkg\EasyTpl\Extended;

use PhpPkg\EasyTpl\Contract\CompilerInterface;
use PhpPkg\EasyTpl\EasyTemplate;

/**
* Class LayoutTemplate TODO
* @package PhpPkg\EasyTpl\Extended
*/
class LayoutTemplate extends EasyTemplate
{

protected function init(CompilerInterface $compiler): void
{
parent::init($compiler);

$compiler
->addDirective('layout', function (string $tplName) {
return ExtendLoader::new()->handle();
})
->addDirective('contents', function () {
return '$this->contents();';
});
}
}
Loading

0 comments on commit 1002991

Please sign in to comment.