Skip to content

Commit

Permalink
Merge pull request #25 from rawsrc/6.1.0
Browse files Browse the repository at this point in the history
6.1.0
  • Loading branch information
rawsrc authored Sep 24, 2023
2 parents 0254cbd + ae68101 commit 783406e
Show file tree
Hide file tree
Showing 16 changed files with 2,149 additions and 837 deletions.
269 changes: 208 additions & 61 deletions PhpEcho.php

Large diffs are not rendered by default.

757 changes: 3 additions & 754 deletions README.md

Large diffs are not rendered by default.

782 changes: 782 additions & 0 deletions README_en.md

Large diffs are not rendered by default.

798 changes: 798 additions & 0 deletions README_fr.md

Large diffs are not rendered by default.

34 changes: 21 additions & 13 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
# **PhpEcho**

**Changelog 6.1.0**<br>
1. New options to parameter the engine values extractor using `setSeekValueMode(string $mode)`, `$mode` among `current|parents|root`
2. If the current block is not able to provide a value to be rendered then the engine will automatically seek for it using the `seekValueMode` parameter
3. Detect an infinite loop when building a view
4. Allowing the render of recursive arrays of PhpEcho blocks
5. Cloning a `PhpEcho` block is now forbidden, the engine will throw an `BadMethodCallException`
6. Tests are updated

**Changelog 6.0.1**<br>
1. Minor bugfix in `addBlock()`

**Changelog 6.0.0**<br>
1. Code refactoring
2. As PHP is now a pretty self-describing and self-documenting language, the quantity of PHPDoc is now heavily reduced
3. Removed feature: space notation for arrays. Any space in a key is now preserved, the engine doesn't interpret them as sub-arrays anymore
5. New feature: management of local and global vars, please note that the local always override the global ones
3. New feature: defining global values that will be available through the whole tree of blocks using: `injectVars(array $vars)`
4. New feature: defining local values after instantiating a `PhpEcho` block at once using: `setVars(array $p)`
6. Internal heavy change: there's no more copy of variables between blocks (reduce the memory footprint and increase the global performance)
7. If the current block is not able to provide a value to be rendered then the engine will automatically seek for it in the root of the tree
8. Better management of values composed of nested array
9. Cloning a `PhpEcho` block is now possible, the cloned value keeps everything but the link to its parent block. The new one is orphan
4. New feature: management of local and global vars, please note that the local always override the global ones
5. New feature: defining global values that will be available through the whole tree of blocks using: `injectVars(array $vars)`
6. New feature: defining local values after instantiating a `PhpEcho` block at once using: `setVars(array $p)`
7. Internal heavy change: there's no more copy of variables between blocks (reduce the memory footprint and increase the global performance)
8. If the current block is not able to provide a value to be rendered then the engine will automatically seek for it in the root of the tree
9. Better management of values composed of nested array
10. Cloning a `PhpEcho` block is now possible, the cloned value keeps everything but the link to its parent block. The new one is orphan

**Changelog 5.4.1:**<br>
1. Minor bugfix in method `isArrayOfPhpEchoBlocks(mixed $p)` when `$p` is an empty array
Expand All @@ -32,7 +40,7 @@
3. You can now define the seek order to get the first value either
from the `local` or `global` context using `getAnyParam(string $name, string $seek_order = 'local'): mixed`
4. It's possible to set at once a parameter into the local and global context using `setAnyParam(string $name, mixed $value)`
4. It's possible to unset at once a parameter from the local and the global context using `unsetAnyParam(string $name)`<br>
5. It's possible to unset at once a parameter from the local and the global context using `unsetAnyParam(string $name)`<br>
Test files are updated

**Changelog 5.2.1:**<br>
Expand Down Expand Up @@ -65,12 +73,12 @@
1. Removing th constant `HELPER_BOUND_TO_CLASS_INSTANCE`, it's replaced by `PhpEcho::addBindableHelper`
2. Removing the constant `HELPER_RETURN_ESCAPED_DATA`. Now, the engine is able to check when data must
be escaped and preserve the native datatype when it's safe in HTML context
2. Instead of dying silently with `null` or empty string, the engine now throws in all case an `Exception`
3. Instead of dying silently with `null` or empty string, the engine now throws in all case an `Exception`
You must produce a better code as it will crash on each low quality segment.
3. Add new method `renderBlock()` to link easily a child block to its parent
4. Many code improvements
5. Fully tested: the core and all helpers have been fully tested
6. Add new helper to the standard library `renderIfNotSet()` that render a default value instead
4. Add new method `renderBlock()` to link easily a child block to its parent
5. Many code improvements
6. Fully tested: the core and all helpers have been fully tested
7. Add new helper to the standard library `renderIfNotSet()` that render a default value instead
of throwing an `Exception` for any missing key in the stored key-value pairs

**Changelog 5.0.0:**<br>
Expand Down
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# PhpEcho : a native PHP templating engine in one class

`2023-08-12` `PHP 8.0+` `6.0.0`
`2023-09-24` `PHP 8.0+` `6.1.0`

## TESTS

Expand Down
39 changes: 37 additions & 2 deletions tests/autowire.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

/** @var Pilot $pilot */

PhpEcho::setSeekValueMode('parents');

$root = new PhpEcho(id: 'root');
$block_1 = new PhpEcho(id: 'block_1');
$block_11 = new PhpEcho(id: 'block_11');
Expand All @@ -24,15 +26,48 @@
$pilot->run(
id: 'autowire_001',
test: fn() => $block_1['a'],
description: 'no scalar vars defined, after injecting them, check the values are available for the whole tree components',
description: 'no scalar vars defined, after directly injecting them into the root, check the values are available for the whole tree components',
);
$pilot->assertIsInt();
$pilot->assertEqual(1);

$pilot->run(
id: 'autowire_002',
test: fn() => $block_121['b'],
description: 'no scalar vars defined, after injecting them, check the values are available for the whole tree components and escaped',
description: 'no scalar vars defined, after directly injecting them into the root, check the values are available for the whole tree components and escaped',
);
$pilot->assertIsString();
$pilot->assertEqual('abc &quot; &lt; &gt;');


$root = new PhpEcho(id: 'root');
$block_1 = new PhpEcho(id: 'block_1');
$block_11 = new PhpEcho(id: 'block_11');
$block_12 = new PhpEcho(id: 'block_12');
$block_121 = new PhpEcho(id: 'block_121');
$block_1211 = new PhpEcho(id: 'block_1211');

$root['block_1'] = $block_1;
$block_1['block_11'] = $block_11;
$block_1['block_12'] = $block_12;
$block_12['block_121'] = $block_121;
$block_121['block_1211'] = $block_1211;


$block_121->injectVars(['a' => 1, 'b' => 'abc " < >']);

$pilot->run(
id: 'autowire_003',
test: fn() => $block_1['a'],
description: 'no scalar vars defined, after directly injecting them into one leaf, check the values are available for the whole tree components',
);
$pilot->assertIsInt();
$pilot->assertEqual(1);

$pilot->run(
id: 'autowire_004',
test: fn() => $block_121['b'],
description: 'no scalar vars defined, after directly injecting them into one leaf, check the values are available for the whole tree components and escaped',
);
$pilot->assertIsString();
$pilot->assertEqual('abc &quot; &lt; &gt;');
Expand Down
79 changes: 79 additions & 0 deletions tests/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,82 @@ class: $block,
);
$pilot->assertIsBool();
$pilot->assertEqual(false);


PhpEcho::setTemplateDirRoot(__DIR__.DIRECTORY_SEPARATOR.'view');

$data = [
'abc' => new PhpEcho('block/block_02.php', ['block_02_text' => 'abc'], 'block_abc'),
'def' => new PhpEcho('block/block_02.php', ['block_02_text' => 'def'], 'block_def'),
'ghi' => new PhpEcho('block/block_02.php', [
'jkl' => new PhpEcho('block/block_02.php', ['block_02_text' => 'jkl'], 'block_jkl'),
'block_02_text' => 'ghi',
], 'block_ghi'),
'mno' => new PhpEcho('block/block_02.php', ['block_02_text' => 'mno'], 'block_mno'),
'pqr' => new PhpEcho('block/block_02.php', ['block_02_text' => 'pqr'], 'block_pqr'),
];

$root = new PhpEcho(id: 'root');

$pilot->runClassMethod(
id: 'core_015',
class: $root,
description: 'check isArrayOfPhpEchoBlocks with a recursive array',
method: 'isArrayOfPhpEchoBlocks',
params: [$data],
);
$pilot->assertIsBool();
$pilot->assertEqual(true);

$data['xyz'] = 'break php echo array';

$pilot->runClassMethod(
id: 'core_016',
class: $root,
description: 'check isArrayOfPhpEchoBlocks with a recursive array',
method: 'isArrayOfPhpEchoBlocks',
params: [$data],
);
$pilot->assertIsBool();
$pilot->assertEqual(false);

$data = [
'abc' => new PhpEcho('block/block_02.php', ['block_02_text' => 'abc'], 'block_abc'),
'def' => new PhpEcho('block/block_02.php', ['block_02_text' => 'def'], 'block_def'),
'ghi' => new PhpEcho('block/block_02.php', [
'block_02_text' => [
new PhpEcho('block/block_02.php', ['block_02_text' => 'ghi555'], 'block_555'),
new PhpEcho('block/block_02.php', ['block_02_text' => 'ghi666'], 'block_666')],
], 'block_ghi'),
'mno' => new PhpEcho('block/block_02.php', ['block_02_text' => 'mno'], 'block_mno'),
'pqr' => new PhpEcho('block/block_02.php', ['block_02_text' => 'pqr'], 'block_pqr'),
];

$root = new PhpEcho(file: 'layout_01.php', vars: ['body' => $data], id: 'root');

ob_start();
echo $root;
$html = ob_get_clean();
$pilot->run(
id : 'core_017',
test : fn() => $html,
description : 'recursive array of php echo block rendering'
);
$pilot->assertEqual(<<<html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>abc</p>
<p>def</p>
<p><p>ghi555</p>
<p>ghi666</p>
</p>
<p>mno</p>
<p>pqr</p>
</body>
</html>
html);

Binary file modified tests/global_tests_result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/heredoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,4 @@
</body>
</html>
html);
html);
53 changes: 53 additions & 0 deletions tests/infinite_loop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

use Exacodis\Pilot;
use rawsrc\PhpEcho\PhpEcho;

/** @var Pilot $pilot */

$layout = new PhpEcho(file: 'layout_02.php', id: 'root');

$a = new PhpEcho(id: 'a');
$a->setCode('<p>Block a</p>');

$b = new PhpEcho(id: 'b');
$b->setCode('<p>Block b</p>');


$layout['a'] = $a;
$layout['b'] = $b;

$layout['block_01'] = $layout['a'];
$layout['block_02'] = $layout['a'];

ob_start();
echo $layout;
$html = ob_get_clean();

$pilot->run(
id: 'infinite_loop_01',
test: fn() => $html,
description: 'multiple usage of the same block, no infinite loop'
);
$pilot->assertIsString();
$pilot->assertEqual(<<<html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>Block a</p><p>Block a</p></body>
</html>
html);


$layout = new PhpEcho(file: 'layout_07.php', id: 'root');
$layout['block'] = new PhpEcho(file: 'block/block_07.php');

$pilot->run(
id: 'infinite_loop_02',
test: fn() => (string)$layout,
description: 'infinite loop, block calling each others'
);
$pilot->assertException(InvalidArgumentException::class);
Loading

0 comments on commit 783406e

Please sign in to comment.