Compare commits
95
Commits
2f8abebc44
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f95171575 | ||
|
|
131204d838 | ||
|
|
562950447e | ||
|
|
54f209f8e2 | ||
|
|
28210836cd | ||
|
|
1f771b1932 | ||
|
|
778f84116b | ||
|
|
722131d801 | ||
|
|
210c8ae0bd | ||
|
|
c975de0557 | ||
|
|
0c0ba39072 | ||
|
|
ef95576d74 | ||
|
|
e737e82d7e | ||
|
|
830a643a56 | ||
|
|
b24bbbc150 | ||
|
|
3ffb69342b | ||
|
|
fa815a506e | ||
|
|
35a7bd4cf7 | ||
|
|
7970679894 | ||
|
|
d22d26c9c8 | ||
|
|
934643214e | ||
|
|
5e54407f74 | ||
|
|
b856743741 | ||
|
|
10fb17c7c3 | ||
|
|
8b6dd85fd5 | ||
|
|
8c7a4d730b | ||
|
|
ed0eb8225b | ||
|
|
0e655fd387 | ||
|
|
ce43abb954 | ||
|
|
5aa70dfd17 | ||
|
|
043fb90f8d | ||
|
|
d5de5e665b | ||
|
|
3ccbf6bfab | ||
|
|
42d84796ee | ||
|
|
386e0aa448 | ||
|
|
be65d45b41 | ||
|
|
7d2bb8d407 | ||
|
|
215bc26981 | ||
|
|
3dce6a41cb | ||
|
|
f9c955a82a | ||
|
|
2fe97c60ed | ||
|
|
710ecad98d | ||
|
|
3bcd5c9780 | ||
|
|
7a2fa872fa | ||
|
|
5182ba19bc | ||
|
|
57367968ca | ||
|
|
b1e1324bfa | ||
|
|
44b0283938 | ||
|
|
1611ebe047 | ||
|
|
6ee3e1bbae | ||
|
|
169400cd61 | ||
|
|
b4964402fd | ||
|
|
a65aa72733 | ||
|
|
b3a8e3aca2 | ||
|
|
e9b2f0ece3 | ||
|
|
acd9f8c72e | ||
|
|
afeb8789b5 | ||
|
|
ded3d5e907 | ||
|
|
9ea4856f49 | ||
|
|
84020a871c | ||
|
|
2dc16de7c3 | ||
|
|
af212b07ca | ||
|
|
58766decaf | ||
|
|
08211282c2 | ||
|
|
58f73d914b | ||
|
|
46535c2734 | ||
|
|
bb29a56077 | ||
|
|
33298ff2e4 | ||
|
|
3de5f46de6 | ||
|
|
4bd4cddbff | ||
|
|
7e80d4be6d | ||
|
|
14cfb46c98 | ||
|
|
08201b1958 | ||
|
|
5bac6cd843 | ||
|
|
4c3772376c | ||
|
|
aa0bdea0c5 | ||
|
|
531f786747 | ||
|
|
22e046dfcd | ||
|
|
b08325049b | ||
|
|
48ecd9f7d9 | ||
|
|
18a0fd7fdc | ||
|
|
2b81e9cef8 | ||
|
|
b836fc5bd8 | ||
|
|
45c8173029 | ||
|
|
67040db155 | ||
|
|
7588748ebd | ||
|
|
f143b082ae | ||
|
|
09408827da | ||
|
|
30af321215 | ||
|
|
8df38b568c | ||
|
|
4e909b2840 | ||
|
|
82eca2e520 | ||
|
|
082bdd4ad3 | ||
|
|
69db3efdce | ||
|
|
1334278e37 |
No files matched your search
+12
-3
@@ -6,7 +6,7 @@
|
|||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Dave Mc Nicoll",
|
"name": "Dave Mc Nicoll",
|
||||||
"email": "mcndave@gmail.com"
|
"email": "info@mcnd.ca"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
@@ -15,6 +15,15 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"psr/http-message": "^1.0"
|
"psr/http-message": "^2.0"
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"lean": {
|
||||||
|
"autoload": {
|
||||||
|
"definitions": [
|
||||||
|
"meta/definitions.php"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Generated
-68
@@ -1,68 +0,0 @@
|
|||||||
{
|
|
||||||
"_readme": [
|
|
||||||
"This file locks the dependencies of your project to a known state",
|
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
|
||||||
"This file is @generated automatically"
|
|
||||||
],
|
|
||||||
"content-hash": "9771fcd7256da80520a2d0d0f95bf311",
|
|
||||||
"packages": [
|
|
||||||
{
|
|
||||||
"name": "psr/http-message",
|
|
||||||
"version": "1.0.1",
|
|
||||||
"source": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/php-fig/http-message.git",
|
|
||||||
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
|
|
||||||
},
|
|
||||||
"dist": {
|
|
||||||
"type": "zip",
|
|
||||||
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
|
|
||||||
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
|
|
||||||
"shasum": ""
|
|
||||||
},
|
|
||||||
"require": {
|
|
||||||
"php": ">=5.3.0"
|
|
||||||
},
|
|
||||||
"type": "library",
|
|
||||||
"extra": {
|
|
||||||
"branch-alias": {
|
|
||||||
"dev-master": "1.0.x-dev"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"autoload": {
|
|
||||||
"psr-4": {
|
|
||||||
"Psr\\Http\\Message\\": "src/"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"notification-url": "https://packagist.org/downloads/",
|
|
||||||
"license": [
|
|
||||||
"MIT"
|
|
||||||
],
|
|
||||||
"authors": [
|
|
||||||
{
|
|
||||||
"name": "PHP-FIG",
|
|
||||||
"homepage": "http://www.php-fig.org/"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"description": "Common interface for HTTP messages",
|
|
||||||
"homepage": "https://github.com/php-fig/http-message",
|
|
||||||
"keywords": [
|
|
||||||
"http",
|
|
||||||
"http-message",
|
|
||||||
"psr",
|
|
||||||
"psr-7",
|
|
||||||
"request",
|
|
||||||
"response"
|
|
||||||
],
|
|
||||||
"time": "2016-08-06T14:39:51+00:00"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"packages-dev": [],
|
|
||||||
"aliases": [],
|
|
||||||
"minimum-stability": "stable",
|
|
||||||
"stability-flags": [],
|
|
||||||
"prefer-stable": false,
|
|
||||||
"prefer-lowest": false,
|
|
||||||
"platform": [],
|
|
||||||
"platform-dev": []
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
# Picea
|
||||||
|
|
||||||
|
Welcome to the official Picea documentation.
|
||||||
|
|
||||||
|
This library offers modern features from templating language, while simply generating normal PHP after compilation, such as view inheritance, definable blocks, 'or' token on unrunned looped, etc...
|
||||||
|
|
||||||
|
Picea uses the same delimiters that Twig uses, which are `{% %}`, `{{ }}` and `{# #}`.
|
||||||
|
|
||||||
|
The first `{% %}` is used for most **control structure** and **extensions**.
|
||||||
|
|
||||||
|
The `{{ }}` delimiter is used to **echo escaped content** in a page. (see *01-echoing*)
|
||||||
|
|
||||||
|
The `{# #}` is exclusively used as a **comment** enclosure. (see *01-comment*)
|
||||||
|
|
||||||
|
## Quick start
|
||||||
|
|
||||||
|
Render a simple Picea view:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$picea = new Picea\Picea();
|
||||||
|
$picea->renderHtml('view/path/hello_world');
|
||||||
|
```
|
||||||
|
|
||||||
|
And the view content could look like:
|
||||||
|
|
||||||
|
*path/hello_world*
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Picea's simple </title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<nav>
|
||||||
|
{% foreach $navigation as $navItem %}
|
||||||
|
<a href="{% route $navItem->route %}" title="{{ $navItem->description }}">
|
||||||
|
{{ $navItem->caption }}
|
||||||
|
</a>
|
||||||
|
{% endforeach %}
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
{{ $someText }}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# Control structures - Comments
|
||||||
|
|
||||||
|
There is no single-line token to comment code within Picea, it's a tag working as such :
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
{# This is how you comment in a Picea view #}
|
||||||
|
|
||||||
|
<div>
|
||||||
|
{# Is it hello or good bye ? not sure on this ! #}
|
||||||
|
<i>Good bye world !</i>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{# This is a multi-line comment
|
||||||
|
<div>This part will not be rendered</div>
|
||||||
|
#}
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Would render as such:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<div>
|
||||||
|
<i>Good bye world !</i>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
# Echoing text or data
|
||||||
|
|
||||||
|
There is two token groups that you can use to print content into a view, `echo` or `print`.
|
||||||
|
|
||||||
|
Escaped by default : `{% echo $argument %}` = `{{ $argument }}` = `{% print $argument %}`
|
||||||
|
|
||||||
|
Raw when needed: `{% echo.raw $argument %}` = `{{= $argument }}` = `{% print.raw $argument %}`
|
||||||
|
|
||||||
|
## Outputing content to a view using `echo` / `{{ }}` / `print`, `echo.raw` / `{{= }}` / `print.raw`
|
||||||
|
|
||||||
|
Using `print` or `echo`, which are, by default, made safer by using PHP's native `htmlspecialchars`.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
{{ "Hello World !" }}
|
||||||
|
|
||||||
|
{% echo "This is another way to output <safe> content" %}
|
||||||
|
|
||||||
|
{% print "This is the third way to output <safe> content" %}
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
{% echo.raw $someHTML %}
|
||||||
|
|
||||||
|
{% print.raw $someOtherHTML %}
|
||||||
|
|
||||||
|
{{= $someMoreHTML }}
|
||||||
|
```
|
||||||
|
|
||||||
|
**[PHP]** Would yield internally:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<?php echo htmlspecialchars((string) "Hello World !", 3, 'UTF-8', true) ?>
|
||||||
|
|
||||||
|
<?php echo htmlspecialchars((string) "This is another way to output <safe> content", 3, 'UTF-8', true) ?>
|
||||||
|
|
||||||
|
<?php echo htmlspecialchars((string) "This is another way to output <safe> content", 3, 'UTF-8', true) ?>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<?php echo $someHTML ?>
|
||||||
|
|
||||||
|
<?php echo $someOtherHTML ?>
|
||||||
|
|
||||||
|
<?php echo $someMoreHTML ?>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Using string format variant `printf` / `printf.raw`
|
||||||
|
|
||||||
|
Those tokens represents the equivalent of the printf() function from PHP.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
{% php
|
||||||
|
$num = 5;
|
||||||
|
$location = 'tree';
|
||||||
|
%}
|
||||||
|
<span>{% printf 'There are %d monkeys in the %s', $num, $location %}</span>
|
||||||
|
<i>{% printf.raw 'There are %d monkeys in the %s', $num, $location %}</i>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[PHP]** Would render internally as :
|
||||||
|
|
||||||
|
```html
|
||||||
|
<?php
|
||||||
|
$num = 5;
|
||||||
|
$location = 'tree';
|
||||||
|
?>
|
||||||
|
<span><?php echo htmlspecialchars((string) sprintf('There are %d monkeys in the %s', $num, location), 3, 'UTF-8', true); ?></span>
|
||||||
|
<i><?php printf('There are %d monkeys in the %s', $num, location) ?></i>
|
||||||
|
```
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
# Control structure - Comparisons
|
||||||
|
|
||||||
|
Most control structures used within PHP view templates are supported natively in Picea.
|
||||||
|
|
||||||
|
The goal for this project always has been to not loose original feature while removing some limitations with PHP's original templating syntax.
|
||||||
|
|
||||||
|
## Comparison using `if` / `then` / `else` / `elseif` / `endif`
|
||||||
|
|
||||||
|
Comparisons works the same way as using PHP's alternative syntax.
|
||||||
|
|
||||||
|
This is also how they are rendered in the compilation process.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
{% php $search = 'Pack my box with five dozen liquor jugs' %}
|
||||||
|
|
||||||
|
{% if strpos($search, 'beer') !== false %}
|
||||||
|
<span>Found 'beer' into {{ $search }} !</span>
|
||||||
|
{% elseif strpos($search, 'liquor') !== false %}
|
||||||
|
<span>Found 'liquor' into {{ $search }} !</span>
|
||||||
|
{% else %}
|
||||||
|
<b>Neither 'beer' or 'liquor' were found in {{ $search }}</b>
|
||||||
|
{% endif %}
|
||||||
|
```
|
||||||
|
|
||||||
|
**[PHP]** Would yield:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php $search = 'Pack my box with five dozen liquor jugs'; ?>
|
||||||
|
|
||||||
|
<?php if (strpos($search, 'beer') !== false): ?>
|
||||||
|
<span>Found 'beer' into <?php echo htmlspecialchars((string) $search, 3, 'UTF-8', true) ?> !</span>
|
||||||
|
<?php elseif (strpos($search, 'beer') !== false): ?>
|
||||||
|
<span>Found 'liquor' into <?php echo htmlspecialchars((string) $search, 3, 'UTF-8', true) ?> !</span>
|
||||||
|
<?php else: ?>
|
||||||
|
<b>Neither 'beer' or 'liquor' were found in <?php echo htmlspecialchars((string) $search, 3, 'UTF-8', true) ?></b>
|
||||||
|
<?php endif ?>
|
||||||
|
```
|
||||||
|
|
||||||
|
And then, the code would be runned through PHP's native `include` mechanic.
|
||||||
|
|
||||||
|
## Comparison using `switch` / `case` / `break` / `endswitch`
|
||||||
|
|
||||||
|
Using switches within HTML in plain PHP can be quite cumbersome because of it's limitation
|
||||||
|
disallowing any output (including whitespace) between it's statement.
|
||||||
|
|
||||||
|
Picea will allow some more complex (and readable) switches within your views.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```php
|
||||||
|
{% php $selected = random_int(0,5) %}
|
||||||
|
|
||||||
|
{% switch $selected %}
|
||||||
|
|
||||||
|
{% case 1 %}
|
||||||
|
{{ "One person selected" }}
|
||||||
|
{% break %}
|
||||||
|
|
||||||
|
{% case 2 %}
|
||||||
|
{% default %}
|
||||||
|
{{ "Multiple person ($selected) selected" }}
|
||||||
|
{% break %}
|
||||||
|
|
||||||
|
{% endswitch %}
|
||||||
|
```
|
||||||
|
|
||||||
|
**[PHP]** Would render as such:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php $selected = random_int(0,5) ?>
|
||||||
|
|
||||||
|
<?php switch($selected):
|
||||||
|
|
||||||
|
case 1: ?>
|
||||||
|
<?php echo htmlspecialchars((string) "One person selected", 3, 'UTF-8', true) ?>
|
||||||
|
<?php break; ?>
|
||||||
|
|
||||||
|
<?php case 2: ?>
|
||||||
|
<?php default: ?>
|
||||||
|
<?php echo htmlspecialchars((string) "Multiple person ($selected) selected", 3, 'UTF-8', true) ?>
|
||||||
|
<?php break; ?>
|
||||||
|
|
||||||
|
<?php endswitch; ?>
|
||||||
|
```
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
# Control structure - `extends` / `section`
|
||||||
|
|
||||||
|
A nice feature of most robust templating engine is the ability to inherit from other view.
|
||||||
|
|
||||||
|
Picea follows a similar train of tought, since it's also possible create a chain of inheritance
|
||||||
|
using `extends` which have definable parts you can declare using `section`.
|
||||||
|
|
||||||
|
## Basic `extends` (string $path)
|
||||||
|
|
||||||
|
You must provide a valid `$path` from which the template will be inherited.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
*path/base/layout.phtml*
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
{% section "head" %}
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>{{ title() }} - AnExampleApp</title>
|
||||||
|
{% section %}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header id="header"">
|
||||||
|
{% section "header" %}{% view "path/base/navigation" %}{% endsection %}
|
||||||
|
</header>
|
||||||
|
<main id="main">{% section "main" %}{% endsection %}</main>
|
||||||
|
<footer id="footer">
|
||||||
|
{% section "footer" %}
|
||||||
|
© Copyright {{ date('Y') }}
|
||||||
|
{% endsection %}
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
*path/home.phtml*
|
||||||
|
```html
|
||||||
|
{% extends "path/base/layout" %}
|
||||||
|
|
||||||
|
{% title "Home page" %}
|
||||||
|
|
||||||
|
{% section "main" %}
|
||||||
|
<h1>Welcome !</h1>
|
||||||
|
{# @TODO integrate our new blog engine below ! #}
|
||||||
|
<article>
|
||||||
|
This is our new blog ! We hope you are gonna enjoy your stay on our new platform !
|
||||||
|
</article>
|
||||||
|
{% endsection %}
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Would render as such :
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Home page - AnExampleApp</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header id="header"">
|
||||||
|
<nav>
|
||||||
|
<a href="#home">Home</a>
|
||||||
|
<a href="#login">Login</a>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
<main id="main">
|
||||||
|
<h1>Welcome !</h1>
|
||||||
|
|
||||||
|
<article>
|
||||||
|
This is our new blog ! We hope you are gonna enjoy your stay on our new platform !
|
||||||
|
</article>
|
||||||
|
</main>
|
||||||
|
<footer id="footer">
|
||||||
|
© Copyright 2022
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Inherit an already extended view
|
||||||
|
|
||||||
|
We could use the previous file `path/home` and generate, let's say, the same page, but without a navigation menu.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
*path/home-navless.phtml*
|
||||||
|
```html
|
||||||
|
{% extends "path/home" %}
|
||||||
|
|
||||||
|
{% section "header" %}{% endsection %}
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Would render as such :
|
||||||
|
```html
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Home page - AnExampleApp</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header id="header""></header>
|
||||||
|
<main id="main">
|
||||||
|
<h1>Welcome !</h1>
|
||||||
|
|
||||||
|
<article>
|
||||||
|
This is our new blog ! We hope you are gonna enjoy your stay on our new platform !
|
||||||
|
</article>
|
||||||
|
</main>
|
||||||
|
<footer id="footer">
|
||||||
|
© Copyright 2022
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
```
|
||||||
|
|
||||||
|
Notice that the `<header>` tag is now empty, since we've redeclared it in our navless view.
|
||||||
|
|
||||||
|
## Overview of `section` (string $name, array $options)
|
||||||
|
|
||||||
|
In your `$name` variable, accepted characters are alpha-numeric and those specific caracters : `.-_:`, so
|
||||||
|
names suches as :
|
||||||
|
|
||||||
|
`body.content`
|
||||||
|
`my-cool-section:heading`
|
||||||
|
`MyOtherSection`
|
||||||
|
`_another_accepted_name`
|
||||||
|
|
||||||
|
Allowed options are :
|
||||||
|
|
||||||
|
string `action` : allowed are `prepend`, `default`, `append`
|
||||||
|
int `order` : if you must
|
||||||
|
|
||||||
|
### Actions can also be passed using specials tokens :
|
||||||
|
|
||||||
|
`section.prepend` and `section.append` can also be used without passing an `action` option.
|
||||||
|
|
||||||
|
*path/home-nav.phtml*
|
||||||
|
```html
|
||||||
|
{% extends "path/home" %}
|
||||||
|
|
||||||
|
{% section "header" %}
|
||||||
|
<a href="#mylink">First link here !</a>
|
||||||
|
{% endsection %}
|
||||||
|
```
|
||||||
|
|
||||||
|
*path/home-admin-nav.phtml*
|
||||||
|
```html
|
||||||
|
{% extends "path/home-nav" %}
|
||||||
|
|
||||||
|
{% section.prepend "header" %}
|
||||||
|
<h4>My new NAV header</h4>
|
||||||
|
{% endsection %}
|
||||||
|
|
||||||
|
{% section.append "header" %}
|
||||||
|
<a href="#mylink">Second link here !</a>
|
||||||
|
{% endsection %}
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Would render as such :
|
||||||
|
```html
|
||||||
|
<h4>My new NAV header</h4>
|
||||||
|
<a href="#mylink">First link here !</a>
|
||||||
|
<a href="#mylink">Second link here !</a>
|
||||||
|
```
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
# Control structure - `function`
|
||||||
|
|
||||||
|
Sometimes comes a need to have a small subset of code which is gonna be used twice (or more)
|
||||||
|
in the same view. Instead of simply duplicating the code (with everything that can then go wrong if you ever need
|
||||||
|
to play with it later), you could create a `function`.
|
||||||
|
|
||||||
|
Functions are declared exactly like vanilla PHP.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
|
||||||
|
{% use Psr\Http\Message\ServerRequestInterface %}
|
||||||
|
|
||||||
|
{% title "My generic title" %}
|
||||||
|
|
||||||
|
{% function printCustomTitle(ServerRequestInterface $request) : bool %}
|
||||||
|
{% if $request->getAttribute('lean.route')->name === 'home' %}
|
||||||
|
<h4 class="title">This is a custom title !</h4>
|
||||||
|
|
||||||
|
{% return true %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% return false %}
|
||||||
|
{% endfunction %}
|
||||||
|
|
||||||
|
{% if ! printCustomTitle($request) %}
|
||||||
|
<h1>{{ title() }}</h1>
|
||||||
|
{% endif %}
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Would yield:
|
||||||
|
|
||||||
|
*page **is** 'home'*
|
||||||
|
```php
|
||||||
|
<h4>This is a custom title !</h4>
|
||||||
|
```
|
||||||
|
|
||||||
|
*page **is not** 'home'*
|
||||||
|
```php
|
||||||
|
<h4>My generic title</h4>
|
||||||
|
```
|
||||||
@@ -0,0 +1,193 @@
|
|||||||
|
# Control structure - Loops
|
||||||
|
|
||||||
|
Picea's loop works the same as PHP original alternative syntax.
|
||||||
|
|
||||||
|
There is, however, some minors improvments and a new `{% or %}` clause working much like the `{% else %}` clause in a comparison.
|
||||||
|
|
||||||
|
## Loop using `for` / `or` / `continue` / `break` / `endfor`
|
||||||
|
|
||||||
|
The simplest of sequence loop, `for` / `endfor` simply iterate over a given counter (or whatever your needs).
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
{% php $users = array_slice([ 'Tom', 'Sam', 'Mario', 'Steve' ], 0, random_int(0, 4)) %}
|
||||||
|
|
||||||
|
<h1>User list</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% for $i = 0; $i < count($users); $i++ %}
|
||||||
|
{% if $users[$i] === 'Steve' %}
|
||||||
|
{# Will leave the loop if user's name is Steve #}
|
||||||
|
{% break %}
|
||||||
|
{% else %}
|
||||||
|
<li>{{ $users[$i] }}</li>
|
||||||
|
{% endif %}
|
||||||
|
{% or %}
|
||||||
|
<li class="empty">Given user list was empty</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[PHP]** Would compile like :
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php $users = array_slice([ 'Tom', 'Sam', 'Mario', 'Steve' ], 0, random_int(0, 4)) ?>
|
||||||
|
|
||||||
|
<h1>User list</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<?php for($i = 0; $i < count($users); $i++): ?><?php $for_a1kro2k2o = true;
|
||||||
|
<?php if ($users[$i] === 'Steve'): ?>
|
||||||
|
<?php break ?>
|
||||||
|
<?php else: ?>
|
||||||
|
<li><?php echo htmlspecialchars((string) $users[$i], 3, 'UTF-8', true) ?></li>
|
||||||
|
<?php endif ?>
|
||||||
|
<?php endfor ?><?php if (empty($for_a1kro2k2o)): ?><li class="empty">Given user list was empty</li><?php endif ?>
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** And would render as such given random_int() returns a **3**:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<h1>User list</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Tom</li>
|
||||||
|
<li>Sam</li>
|
||||||
|
<li>Mario</li>
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Or would render as such given random_int() returns a **0**:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<h1>User list</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li class="empty">Tom</li>
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Loop using `foreach` / `or` / `continue` / `break` / `endforeach`
|
||||||
|
|
||||||
|
The more complex `foreach` / `endforeach` allows to iterate over keys and values of an array.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
```html
|
||||||
|
{# Generate a random list of 0 to 4 names #}
|
||||||
|
{% php $users = array_slice([ 'Tom', 'Sam', 'Mario', 'Steve', 'Joan' ], 0, random_int(0, 5)) %}
|
||||||
|
|
||||||
|
<h1>Random User list</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% foreach $users as $index => $name %}
|
||||||
|
{% if $name === 'Steve' %}
|
||||||
|
{# We skip Steve, but allows Joan #}
|
||||||
|
{% continue %}
|
||||||
|
{% endif %}
|
||||||
|
<li>{{ $name }}</li>
|
||||||
|
{% or %}
|
||||||
|
<li class="empty">Given user list was empty</li>
|
||||||
|
{% endforeach %}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Could render as such if prior random_int() returns '**2**':
|
||||||
|
```html
|
||||||
|
<h1>User list</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Tom</li>
|
||||||
|
<li>Sam</li>
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Could render as such given random_int() returns '**0**':
|
||||||
|
```html
|
||||||
|
<h1>User list</h1>
|
||||||
|
|
||||||
|
<ul></ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Loop using `while` / `or` / `continue` / `break` / `endwhile`
|
||||||
|
|
||||||
|
This syntax allows to loop on a computed iteration count.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
{% php $fruits = [ 'apple', 'pear', 'banana', 'tomato' ] %}
|
||||||
|
|
||||||
|
<h1>Grocery list</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% while $item = array_pop($fruits) %}
|
||||||
|
<li>{{ ucfirst($item) }}</li>
|
||||||
|
{% or %}
|
||||||
|
<li class="empty">We should never see this, since the list is always populated !</li>
|
||||||
|
{% endwhile %}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Would render as such:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<h1>Grocery list</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Apple</li>
|
||||||
|
<li>Pear</li>
|
||||||
|
<li>Banana</li>
|
||||||
|
<li>Tomato</li>
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
## Loop using `do` / `continue` / `break` / `while`
|
||||||
|
|
||||||
|
This syntax do not have an alternative syntax in vanilla PHP.
|
||||||
|
|
||||||
|
Picea allows for this unusual syntax to be used the same way you would with any other control structure.
|
||||||
|
|
||||||
|
There is, however, no support for the `or` clause for this structure since it will iterate at least once.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<h1>Random number generator</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{% do %}
|
||||||
|
{% php $number = random_int(1, 25); %}
|
||||||
|
|
||||||
|
{% if $number === 10 %}
|
||||||
|
{# For a reason I'm not entirely sure why, 10 must not be displayed ! #}
|
||||||
|
{% continue %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<li>{{ str_pad($number, 2, '0' ,STR_PAD_LEFT) }}</li>
|
||||||
|
{% while $number !== 15 %} {# Loop stops whenever $number is equal to 15 #}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Could render as such:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<h1>Random number generator</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>02</li>
|
||||||
|
<li>12</li>
|
||||||
|
<li>07</li>
|
||||||
|
<li>13</li>
|
||||||
|
<li>04</li>
|
||||||
|
<li>07</li>
|
||||||
|
<li>01</li>
|
||||||
|
<li>16</li>
|
||||||
|
<li>14</li>
|
||||||
|
<li>24</li>
|
||||||
|
<li>14</li>
|
||||||
|
<li>01</li>
|
||||||
|
<li>15</li>
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
# Control structure - `Use`
|
||||||
|
|
||||||
|
While working with inside a template, it might be needed that you reference some other namespaces.
|
||||||
|
|
||||||
|
The use clause works exaclty like PHP's vanilla top of the page `use`.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
```html
|
||||||
|
{% use AnyVendor\My\Super\Randomizer %}
|
||||||
|
|
||||||
|
{# One chance out of ten to end this loop %}
|
||||||
|
{% while Randomizer::run(1, 10) === 10 %}
|
||||||
|
{% if Randomizer::run(1, 100) < 50 %}
|
||||||
|
<span>Lower than 50 !</span>
|
||||||
|
{% else %}
|
||||||
|
<strong>Greater than 50 !</strong>
|
||||||
|
{% endif %}
|
||||||
|
{% endwhile %}
|
||||||
|
|
||||||
|
<h4>DONE !</h4>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Could render as such output:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<span>Lower than 50 !</span>
|
||||||
|
<span>Greater than 50 !</span>
|
||||||
|
<span>Lower than 50 !</span>
|
||||||
|
<span>Greater than 50 !</span>
|
||||||
|
<span>Lower than 50 !</span>
|
||||||
|
<span>Lower than 50 !</span>
|
||||||
|
<h4>DONE !</h4>
|
||||||
|
```
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
# Control structure - View (or `include`) / Block
|
||||||
|
|
||||||
|
There is three ways to include other views into your current view `{% view %} / {% include %}` or
|
||||||
|
using `{% block %}{% endblock %}`.
|
||||||
|
|
||||||
|
The order of loading depends on the order given within your directory configuration; the first file found from it
|
||||||
|
will be used.
|
||||||
|
|
||||||
|
## Inline `view` (string $path, array|null $arguments = null)
|
||||||
|
|
||||||
|
You must provide a valid `$path` from which the template will be loaded. If no `$arguments` are provided, defined
|
||||||
|
variables from the caller's scope will be given (from `get_defined_vars()`).
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
*path/base/nav.phtml*
|
||||||
|
```html
|
||||||
|
<nav>
|
||||||
|
{% foreach $navigation as $index => $navItem %}
|
||||||
|
{% view "path/base/nav-item", [ 'item' => $navItem, 'index' => $index] %}
|
||||||
|
{% endforeach %}
|
||||||
|
</nav>
|
||||||
|
```
|
||||||
|
|
||||||
|
*path/base/nav-item.phtml*
|
||||||
|
```html
|
||||||
|
<a href="{{ $item->anchor }}" tabindex="{{ $index }}">{{ $item->name }}"</a>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Could render such as :
|
||||||
|
```html
|
||||||
|
<nav>
|
||||||
|
<a href="#top">Top</a>
|
||||||
|
<a href="#bottom">Bottom</a>
|
||||||
|
<a href="#left">Left</a>
|
||||||
|
<a href="#right">Right</a>
|
||||||
|
</nav>
|
||||||
|
```
|
||||||
|
|
||||||
|
## `include` (string $path) content from external file
|
||||||
|
|
||||||
|
Whenever you need to `include` a raw file from one of your view directories
|
||||||
|
|
||||||
|
## Reusable `block` (string $path, ...$arguments) / `endblock`
|
||||||
|
|
||||||
|
A better way to achieve this behaviour could be to create a `block` which you can define as needed.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
*path/base/nav.phtml*
|
||||||
|
```html
|
||||||
|
<nav>
|
||||||
|
{% foreach $navigation as $index => $navItem %}
|
||||||
|
{% block "path/base/nav-item-block", $navItem->name, $navItem->anchor, $index %}{% endblock %}
|
||||||
|
{% endforeach %}
|
||||||
|
</nav>
|
||||||
|
```
|
||||||
|
|
||||||
|
*path/base/nav-item-block.phtml*
|
||||||
|
```html
|
||||||
|
{% arguments string $name, string $anchor, int $index = 0 %}
|
||||||
|
|
||||||
|
<a href="{{ $anchor }}" tabindex="{{ $index }}">{{ $name }}"</a>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Would render the same as the `view` example :
|
||||||
|
```html
|
||||||
|
<nav>
|
||||||
|
<a href="#top">Top</a>
|
||||||
|
<a href="#bottom">Bottom</a>
|
||||||
|
<a href="#left">Left</a>
|
||||||
|
<a href="#right">Right</a>
|
||||||
|
</nav>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Extending a `block` using `define` and `slot`
|
||||||
|
|
||||||
|
You might need to define some custom content inside of a `block`.
|
||||||
|
|
||||||
|
You can do so by using `define` and `slot`.
|
||||||
|
|
||||||
|
**[PICEA]** So, using this code:
|
||||||
|
|
||||||
|
*path/base/nav.phtml*
|
||||||
|
```html
|
||||||
|
<nav>
|
||||||
|
{% foreach $navigation as $index => $navItem %}
|
||||||
|
{% block "path/base/nav-item-block", $navItem->name, $navItem->anchor, $index %}
|
||||||
|
{% slot "attributes" %} class="nav-item" href="{{ $anchor }}" {% endslot %}
|
||||||
|
{% endblock %}
|
||||||
|
{% endforeach %}
|
||||||
|
</nav>
|
||||||
|
```
|
||||||
|
|
||||||
|
*path/base/nav-item-block.phtml*
|
||||||
|
```html
|
||||||
|
{% arguments string $name, string $anchor, int $index = 0 %}
|
||||||
|
|
||||||
|
{% define "attributes" %}
|
||||||
|
|
||||||
|
<a {% slot "attributes" %}{% endslot %} href="{{ $anchor }}" tabindex="{{ $index }}"{% endslot %}>{{ $name }}"</a>
|
||||||
|
```
|
||||||
|
|
||||||
|
**[HTML]** Would render the same as the `view` example :
|
||||||
|
```html
|
||||||
|
<nav>
|
||||||
|
<a href="#top">Top</a>
|
||||||
|
<a href="#bottom">Bottom</a>
|
||||||
|
<a href="#left">Left</a>
|
||||||
|
<a href="#right">Right</a>
|
||||||
|
</nav>
|
||||||
|
```
|
||||||
Whitespace-only changes.
@@ -0,0 +1,4 @@
|
|||||||
|
# Extension - Creating an extension
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
# Extension - `json`, `json.pretty`, `json.html`
|
||||||
|
|
||||||
|
This extension
|
||||||
|
|
||||||
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1 @@
|
|||||||
|
<?php
|
||||||
Whitespace-only changes.
@@ -0,0 +1,82 @@
|
|||||||
|
# Using Dependency Injection
|
||||||
|
|
||||||
|
Here's an excerpt from Lean's definition file 'template.php' which also loads Picea\Ui.
|
||||||
|
|
||||||
|
It also insert `Tell` inside the LanguageHandler which handles the `{% _ 'relative.lang.key' %}` and `{% lang 'my.full.lang.key' %}`.
|
||||||
|
|
||||||
|
```php
|
||||||
|
use function DI\autowire, DI\create, DI\get;
|
||||||
|
|
||||||
|
use Laminas\Diactoros\Response\HtmlResponse;
|
||||||
|
|
||||||
|
use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request };
|
||||||
|
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
|
||||||
|
use Picea\Ui\{ Method, Ui };
|
||||||
|
|
||||||
|
return [
|
||||||
|
Picea::class => function($c) {
|
||||||
|
return new Picea($c->get(Context::class), $c->get(Cache::class), $c->get(Compiler::class), null, $c->get(FileFetcher::class), null, getenv("DEBUG"));
|
||||||
|
},
|
||||||
|
|
||||||
|
Context::class => function($c) {
|
||||||
|
return new BaseContext( $c->get(Lean\Lean::class)->getPiceaContext() );
|
||||||
|
},
|
||||||
|
|
||||||
|
Compiler::class => function($c) {
|
||||||
|
return new Compiler(new class(array_merge([
|
||||||
|
$c->get(LanguageExtension::class),
|
||||||
|
$c->get(TitleExtension::class),
|
||||||
|
$c->get(NumberExtension::class),
|
||||||
|
$c->get(UrlExtension::class),
|
||||||
|
$c->get(Method\Form::class),
|
||||||
|
$c->get(Method\Pagination::class),
|
||||||
|
$c->get(Request::class),
|
||||||
|
], class_exists(\Taxus\Picea\Extension::class) ? [ $c->get(\Taxus\Picea\Extension::class) ] : [],
|
||||||
|
array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions() ))) extends DefaultRegistrations {
|
||||||
|
|
||||||
|
public function registerAll(Compiler $compiler) : void
|
||||||
|
{
|
||||||
|
parent::registerAll($compiler);
|
||||||
|
( new Ui() )->registerFormExtension($compiler);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
Request::class => autowire(Request::class),
|
||||||
|
|
||||||
|
Method\Form::class => autowire(Method\Form::class),
|
||||||
|
|
||||||
|
Method\Pagination::class => autowire(Method\Pagination::class),
|
||||||
|
|
||||||
|
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)),
|
||||||
|
|
||||||
|
LanguageHandlerInterface::class => function($c) {
|
||||||
|
return new class( $c->get(Tell\I18n::class) ) implements LanguageHandlerInterface {
|
||||||
|
public Tell\I18n $tell;
|
||||||
|
|
||||||
|
public function __construct(Tell\I18n $tell) {
|
||||||
|
$this->tell = $tell;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function languageFromKey(string $key, array $variables = []) : array|string
|
||||||
|
{
|
||||||
|
return $this->tell->fromKey($key, $variables) ?: "";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
TitleExtension::class => autowire(TitleExtension::class),
|
||||||
|
|
||||||
|
NumberExtension::class => autowire(NumberExtension::class),
|
||||||
|
|
||||||
|
UrlExtension::class => create(UrlExtension::class)->constructor(get(Context::class), getenv("URL_BASE"), get('git.commit')),
|
||||||
|
|
||||||
|
Cache::class => create(Opcache::class)->constructor(getenv("CACHE_PATH"), get(Context::class)),
|
||||||
|
|
||||||
|
FileFetcher::class => function($c) {
|
||||||
|
return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths());
|
||||||
|
},
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use function DI\autowire, DI\create, DI\get;
|
||||||
|
|
||||||
|
use Picea\{
|
||||||
|
Caching\Cache,
|
||||||
|
Caching\Opcache,
|
||||||
|
Compiler,
|
||||||
|
Compiler\Context,
|
||||||
|
Compiler\BaseContext,
|
||||||
|
FileFetcher,
|
||||||
|
Method\Request
|
||||||
|
};
|
||||||
|
|
||||||
|
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension, DiExtension };
|
||||||
|
|
||||||
|
return [
|
||||||
|
Picea\Picea::class => function($c) {
|
||||||
|
return new Picea\Picea($c->get(Context::class), $c->get(Cache::class), $c->get(Compiler::class), null, $c->get(FileFetcher::class), null, getenv("DEBUG"));
|
||||||
|
},
|
||||||
|
|
||||||
|
Context::class => function($c) {
|
||||||
|
return new BaseContext(
|
||||||
|
defaultNamespace: $c->get(Lean\Lean::class)->getPiceaContext(),
|
||||||
|
declare: getenv('PICEA_DECLARE') ?? 'declare(strict_types=1)'
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
Compiler::class => autowire(Compiler::class),
|
||||||
|
|
||||||
|
Request::class => autowire(Request::class),
|
||||||
|
|
||||||
|
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)),
|
||||||
|
|
||||||
|
# LanguageHandlerInterface::class => autowire(\Lean\LanguageHandler::class),
|
||||||
|
|
||||||
|
# LanguageRegistration::class => create(\Lean\PiceaDefaultRegistration::class)->constructor(get('picea.extensions'), [], [], get(Ui::class), null),
|
||||||
|
|
||||||
|
'picea.extensions' => function(\Psr\Container\ContainerInterface $c) {
|
||||||
|
return array_merge([
|
||||||
|
$c->get(LanguageExtension::class),
|
||||||
|
$c->get(TitleExtension::class),
|
||||||
|
$c->get(NumberExtension::class),
|
||||||
|
$c->get(DiExtension::class),
|
||||||
|
$c->get(UrlExtension::class),
|
||||||
|
$c->get(Request::class),
|
||||||
|
], class_exists(\Taxus\Picea\Extension::class) ? [ $c->get(\Taxus\Picea\Extension::class) ] : [],
|
||||||
|
array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions())
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
TitleExtension::class => autowire(TitleExtension::class),
|
||||||
|
|
||||||
|
NumberExtension::class => autowire(NumberExtension::class),
|
||||||
|
|
||||||
|
DiExtension::class => autowire(DiExtension::class),
|
||||||
|
|
||||||
|
UrlExtension::class => create(UrlExtension::class)->constructor(getenv("URL_BASE"), get('git.commit'), explode(',', getenv('APP_URL')), (bool) getenv('FORCE_SSL')),
|
||||||
|
|
||||||
|
Cache::class => create(Opcache::class)->constructor(getenv("CACHE_PATH"), get(Context::class)),
|
||||||
|
|
||||||
|
FileFetcher::class => function($c) {
|
||||||
|
return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths());
|
||||||
|
},
|
||||||
|
];
|
||||||
+8
-3
@@ -4,7 +4,7 @@ namespace Picea;
|
|||||||
|
|
||||||
class Builder
|
class Builder
|
||||||
{
|
{
|
||||||
const TEMPLATE_CLASSNAME_PREFIX = "PiceaTemplate_";
|
public const TEMPLATE_CLASSNAME_PREFIX = "PiceaTemplate_";
|
||||||
|
|
||||||
protected string $templatePath = "";
|
protected string $templatePath = "";
|
||||||
|
|
||||||
@@ -15,13 +15,18 @@ class Builder
|
|||||||
|
|
||||||
public function build(Compiler\Context &$context, string $compiledSource) : Compiler\Context
|
public function build(Compiler\Context &$context, string $compiledSource) : Compiler\Context
|
||||||
{
|
{
|
||||||
|
$path = fn($p) => addslashes(str_replace('/', DIRECTORY_SEPARATOR, $p));
|
||||||
|
|
||||||
$context->className = static::generateClassName($context->viewPath);
|
$context->className = static::generateClassName($context->viewPath);
|
||||||
|
|
||||||
$replace = [
|
$replace = [
|
||||||
'%NAMESPACE%' => $context->namespace,
|
'%NAMESPACE%' => $context->namespace,
|
||||||
'%USE%' => ( $uses = $context->renderUses() ) ? "use $uses;" : false,
|
'%USE%' => ( $uses = $context->renderUses() ) ? $uses : false,
|
||||||
|
'%DECLARE%' => $context->declare ? rtrim($context->declare, ';') . ';' : "",
|
||||||
'%CLASSNAME%' => $context->className,
|
'%CLASSNAME%' => $context->className,
|
||||||
'%PATHNAME%' => $context->viewPath,
|
'%PATHNAME%' => $path($context->viewPath),
|
||||||
|
'%FULLPATH%' => $path($context->filePath),
|
||||||
|
'%TEMPLATE%' => $path($this->templatePath),
|
||||||
'%EXTENDS%' => $context->extendFrom ? "extends " . static::TEMPLATE_CLASSNAME_PREFIX . static::generateClassUID($context->extendFrom) : '',
|
'%EXTENDS%' => $context->extendFrom ? "extends " . static::TEMPLATE_CLASSNAME_PREFIX . static::generateClassUID($context->extendFrom) : '',
|
||||||
'%EXTENDS_TEMPLATE%' => $context->extendFrom,
|
'%EXTENDS_TEMPLATE%' => $context->extendFrom,
|
||||||
'%CONTENT%' => $compiledSource,
|
'%CONTENT%' => $compiledSource,
|
||||||
|
|||||||
+156
-47
@@ -1,69 +1,178 @@
|
|||||||
<?php
|
<?php %DECLARE%
|
||||||
|
|
||||||
namespace %NAMESPACE%;
|
namespace %NAMESPACE%;
|
||||||
|
|
||||||
%USE%
|
%USE%
|
||||||
|
|
||||||
# %PATHNAME%
|
# %PATHNAME%
|
||||||
class %CLASSNAME% %EXTENDS% {
|
if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
|
||||||
public array $blockList = [];
|
|
||||||
|
|
||||||
public array $sectionList = [];
|
class %CLASSNAME% %EXTENDS% {
|
||||||
|
|
||||||
public array $variableList = [];
|
public array $blockList = [];
|
||||||
|
|
||||||
public ?object $thisProxy = null;
|
public array $sectionList = [];
|
||||||
|
|
||||||
public \Picea\Picea $picea;
|
public array $sectionStack = [];
|
||||||
|
|
||||||
public static $context;
|
public array $variableList = [];
|
||||||
|
|
||||||
public function __construct(\Picea\Picea $picea, array $variablesList = [], ?object $thisProxy = null) {
|
|
||||||
$this->picea = $picea;
|
|
||||||
$this->variableList = $variablesList;
|
|
||||||
$this->thisProxy = $thisProxy;
|
|
||||||
$this->exportFunctions();
|
|
||||||
|
|
||||||
static::$context = $picea->context;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function output(array $variablesList = []) : void
|
public ?object $thisProxy = null;
|
||||||
{
|
|
||||||
( function($___class__template, $___global_variables, $___variables, $picea) {
|
|
||||||
extract($___global_variables);
|
|
||||||
extract($___variables, \EXTR_OVERWRITE);
|
|
||||||
?>%CONTENT%<?php
|
|
||||||
} )->call($this->thisProxy ?? new class(){}, $this, $this->variableList, $variablesList, $this->picea);
|
|
||||||
%PARENT_OUTPUT%
|
|
||||||
}
|
|
||||||
|
|
||||||
public function renderSection($name) : void
|
public \Picea\Picea $picea;
|
||||||
{
|
|
||||||
foreach([ 'prepend', 'default', 'append' ] as $item) {
|
|
||||||
usort($this->sectionList[$name][$item], fn($a, $b) => $a['order'] <=> $b['order']);
|
|
||||||
|
|
||||||
foreach($this->sectionList[$name][$item] as $section) {
|
public static $context;
|
||||||
$section['callback']();
|
|
||||||
|
|
||||||
if ( $item === 'default' ) {
|
public bool $rendering = false;
|
||||||
break 1;
|
|
||||||
|
public bool $renderingInsideSection = false;
|
||||||
|
|
||||||
|
public int $depth = 0;
|
||||||
|
|
||||||
|
public function __construct(\Picea\Picea $picea, array $variablesList = [], ?object $thisProxy = null) {
|
||||||
|
$this->picea = $picea;
|
||||||
|
$this->variableList = $variablesList;
|
||||||
|
$this->thisProxy = $thisProxy;
|
||||||
|
$this->exportFunctions();
|
||||||
|
|
||||||
|
static::$context = $picea->context;
|
||||||
|
|
||||||
|
$picea->declareLoadedTemplate(static::class, __FILE__);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function output(array $variablesList = []) : void
|
||||||
|
{
|
||||||
|
$__event = new \Picea\Builder\ClassTemplateEvent();
|
||||||
|
|
||||||
|
$this->rendering = true;
|
||||||
|
|
||||||
|
$this->depth++;
|
||||||
|
|
||||||
|
$__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputing::class, $variablesList);
|
||||||
|
|
||||||
|
try {
|
||||||
|
( function($___class__template, $___global_variables, $___variables, $__event, $picea) {
|
||||||
|
extract($___global_variables);
|
||||||
|
extract($___variables, \EXTR_OVERWRITE);
|
||||||
|
?>%CONTENT%<?php
|
||||||
|
} )->call($this->thisProxy ?? new class() {}, $this, $this->variableList, $variablesList, $__event, $this->picea);
|
||||||
|
} catch (\Throwable $error) {
|
||||||
|
throw $this->errorHandler($error);
|
||||||
|
}
|
||||||
|
|
||||||
|
$__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputDone::class, $variablesList);
|
||||||
|
|
||||||
|
try { %PARENT_OUTPUT% } catch (\Throwable $error) {
|
||||||
|
throw $this->errorHandler($error);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->depth--;
|
||||||
|
|
||||||
|
$this->rendering = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderSection(string $name, bool $return = false) /*: string|void*/
|
||||||
|
{
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
foreach([ 'prepend', 'default', 'append' ] as $item) {
|
||||||
|
if ( !isset($this->sectionList[$name][$item]) || ! is_array($this->sectionList[$name][$item]) ) continue;
|
||||||
|
|
||||||
|
usort($this->sectionList[$name][$item], fn($a, $b) => $a['order'] <=> $b['order']);
|
||||||
|
|
||||||
|
foreach($this->sectionList[$name][$item] as $section) {
|
||||||
|
if ($return) {
|
||||||
|
ob_start();
|
||||||
|
}
|
||||||
|
|
||||||
|
$section['callback']();
|
||||||
|
|
||||||
|
if ($return) {
|
||||||
|
$result[] = ob_get_clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $item === 'default' ) {
|
||||||
|
break 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $return ? implode("", $result) : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exportFunctions() : void
|
||||||
|
{
|
||||||
|
%FUNCTIONS%
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getSourceLineFromException(int $sourceLine) : ? int
|
||||||
|
{
|
||||||
|
$selfSource = file_get_contents(__FILE__);
|
||||||
|
|
||||||
|
foreach(explode("\n", $selfSource) as $line => $content) {
|
||||||
|
if ( strpos($content, str_replace('$', '%', '/*$EXCEPTION_LINE_BASE$*/')) !== false ) {
|
||||||
|
return $sourceLine - $line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$sourceFile = file_get_contents("%TEMPLATE%");
|
||||||
|
|
||||||
|
if ( $sourceFile ) {
|
||||||
|
foreach(explode("\n", $sourceFile) as $line => $content) {
|
||||||
|
if ( strpos($content, str_replace('$', '%', '$CONTENT$')) !== false ) {
|
||||||
|
return $sourceLine - $line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getParam(string $param) : ? string
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'classname' => "%CLASSNAME%",
|
||||||
|
'namespace' => "%NAMESPACE%",
|
||||||
|
'extends' => "%EXTENDS_TEMPLATE%",
|
||||||
|
'view' => "%FULLPATH%",
|
||||||
|
][$param] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke(array $variablesList = []) : string
|
||||||
|
{
|
||||||
|
ob_start();
|
||||||
|
$this->output($variablesList);
|
||||||
|
return ob_get_clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isTemplateError(\Throwable $error) : bool
|
||||||
|
{
|
||||||
|
$class = substr(static::class, strripos(static::class, '\\') + 1) . ".php";
|
||||||
|
|
||||||
|
return substr($error->getFile(), -strlen($class)) === $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function errorHandler(\Throwable $error) : \Throwable
|
||||||
|
{
|
||||||
|
$class = substr(static::class, strrpos(static::class, '\\') + 1) . ".php";
|
||||||
|
|
||||||
|
#dump( __NAMESPACE__, basename($error->getFile()), $class );
|
||||||
|
$basename = basename($error->getFile());
|
||||||
|
|
||||||
|
#dump(is_subclass_of(sprintf("\\%s\\%s", __NAMESPACE__, substr($basename,0 , strrpos($basename, '.'))), static::class, true));
|
||||||
|
|
||||||
|
if (str_ends_with($error->getFile(), $class)) {
|
||||||
|
$error = new \Picea\Exception\RenderHtmlException($this, $error->getMessage(), $error->getCode(), $error);
|
||||||
|
}
|
||||||
|
#elseif (is_subclass_of(sprintf("\\%s\\%s", __NAMESPACE__, substr($basename,0 , strrpos($basename, '.'))), static::class, true)) {
|
||||||
|
# $error = new \Picea\Exception\RenderHtmlException($this, $error->getMessage(), $error->getCode(), $error);
|
||||||
|
#}
|
||||||
|
|
||||||
|
return $error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exportFunctions() : void
|
|
||||||
{
|
|
||||||
static $caching = [];
|
|
||||||
%FUNCTIONS%
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __invoke(array $variablesList = []) : string
|
|
||||||
{
|
|
||||||
ob_start();
|
|
||||||
$this->output($variablesList);
|
|
||||||
return ob_get_clean();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [ 'classname' => "%CLASSNAME%", 'namespace' => "%NAMESPACE%", 'extends' => "%EXTENDS_TEMPLATE%" ];
|
|
||||||
|
return [ 'classname' => %CLASSNAME%::getParam('classname'), 'namespace' => %CLASSNAME%::getParam('namespace'), 'extends' => %CLASSNAME%::getParam('extends'), 'view' => %CLASSNAME%::getParam('view') ];
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Picea\Builder;
|
||||||
|
|
||||||
|
class ClassTemplateEvent
|
||||||
|
{
|
||||||
|
use \Picea\EventTrait;
|
||||||
|
}
|
||||||
@@ -29,4 +29,9 @@ interface Cache {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function save(Context $context) : bool;
|
public function save(Context $context) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purge path cache
|
||||||
|
*/
|
||||||
|
public function purge() : void;
|
||||||
}
|
}
|
||||||
+21
-13
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
namespace Picea\Caching;
|
namespace Picea\Caching;
|
||||||
|
|
||||||
use Picea\Builder,
|
use Picea\ { Builder, Compiler\Context };
|
||||||
Picea\Compiler\Context;
|
|
||||||
|
|
||||||
class Opcache implements Cache {
|
class Opcache implements Cache {
|
||||||
|
|
||||||
@@ -13,9 +12,9 @@ class Opcache implements Cache {
|
|||||||
|
|
||||||
protected array $compiled = [];
|
protected array $compiled = [];
|
||||||
|
|
||||||
protected ? Context $context;
|
protected null|Context $context;
|
||||||
|
|
||||||
public function __construct(?string $cachePath = null, ? Context $context)
|
public function __construct(?string $cachePath = null, null|Context $context = null)
|
||||||
{
|
{
|
||||||
$this->cachePath = $cachePath ?? sys_get_temp_dir();
|
$this->cachePath = $cachePath ?? sys_get_temp_dir();
|
||||||
$this->context = $context;
|
$this->context = $context;
|
||||||
@@ -35,7 +34,7 @@ class Opcache implements Cache {
|
|||||||
|
|
||||||
$compiledContext = $this->compiled[$viewPath];
|
$compiledContext = $this->compiled[$viewPath];
|
||||||
|
|
||||||
$fullName = isset($compiledContext['namespace']) ? "\\{$compiledContext['namespace']}\\{$compiledContext['classname']}" : $compiledContext['classname'];
|
$fullName = isset($compiledContext['namespace']) ? "\\{$compiledContext['namespace']}\\{$compiledContext['classname']}" : ($compiledContext['classname'] ?? $viewPath);
|
||||||
|
|
||||||
return new $fullName(...$arguments);
|
return new $fullName(...$arguments);
|
||||||
}
|
}
|
||||||
@@ -57,11 +56,7 @@ class Opcache implements Cache {
|
|||||||
|
|
||||||
if ( file_exists($path = $this->cachePath($viewPath)) ) {
|
if ( file_exists($path = $this->cachePath($viewPath)) ) {
|
||||||
$this->compiled[$viewPath] = include($path);
|
$this->compiled[$viewPath] = include($path);
|
||||||
|
|
||||||
# if ( $this->compiled[$viewPath]['extends'] ?? false ) {
|
|
||||||
# $this->compiled($this->compiled[$viewPath]['extends']);
|
|
||||||
# }
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +81,7 @@ class Opcache implements Cache {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function cachePath(string $fileName = "") : string
|
public function cachePath(string $fileName = "") : string
|
||||||
{
|
{
|
||||||
return implode(DIRECTORY_SEPARATOR, array_filter([ $this->cachePath, $this->cacheFolder, $fileName ? str_replace([ "/", DIRECTORY_SEPARATOR ], "~", Builder::generateClassName($fileName) . ".php") : null ]));
|
return implode(DIRECTORY_SEPARATOR, array_filter([ $this->cachePath, $this->cacheFolder, $fileName ? str_replace([ "/", DIRECTORY_SEPARATOR ], "~", Builder::generateClassName($fileName) . ".php") : null ]));
|
||||||
}
|
}
|
||||||
@@ -95,7 +90,7 @@ class Opcache implements Cache {
|
|||||||
spl_autoload_register(function ($class) use ($namespace) {
|
spl_autoload_register(function ($class) use ($namespace) {
|
||||||
$prefix = "$namespace\\";
|
$prefix = "$namespace\\";
|
||||||
|
|
||||||
$baseDir = $this->cachePath() . "/";
|
$baseDir = $this->cachePath() . DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
$len = strlen($prefix);
|
$len = strlen($prefix);
|
||||||
|
|
||||||
@@ -103,11 +98,24 @@ class Opcache implements Cache {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$file = $baseDir . str_replace('\\', '/', substr($class, $len)) . '.php';
|
$file = $baseDir . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $len)) . '.php';
|
||||||
|
|
||||||
if ( file_exists($file) ) {
|
if ( file_exists($file) ) {
|
||||||
require $file;
|
require $file;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getFilelist() : array
|
||||||
|
{
|
||||||
|
return glob($this->cachePath() . DIRECTORY_SEPARATOR . Builder::TEMPLATE_CLASSNAME_PREFIX . "*");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function purge() : void
|
||||||
|
{
|
||||||
|
foreach($this->getFilelist() as $dir)
|
||||||
|
{
|
||||||
|
dump($dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+69
-24
@@ -2,10 +2,17 @@
|
|||||||
|
|
||||||
namespace Picea;
|
namespace Picea;
|
||||||
|
|
||||||
|
use Picea\Event\Compiler\CompileTokenExtension;
|
||||||
|
use Picea\Event\Compiler\CompileTokenExtensionEvent;
|
||||||
|
use Picea\Event\Compiler\CompileTokenTagEvent;
|
||||||
|
use Picea\Exception\RegisterExtensionToken;
|
||||||
|
use Picea\Extension\FunctionExtension;
|
||||||
use Picea\Language\LanguageRegistration;
|
use Picea\Language\LanguageRegistration;
|
||||||
|
|
||||||
class Compiler
|
class Compiler
|
||||||
{
|
{
|
||||||
|
use EventTrait;
|
||||||
|
|
||||||
protected string $sourceCode = "";
|
protected string $sourceCode = "";
|
||||||
|
|
||||||
protected array $syntaxObjectList = [];
|
protected array $syntaxObjectList = [];
|
||||||
@@ -18,11 +25,10 @@ class Compiler
|
|||||||
|
|
||||||
public array $extensionList = [];
|
public array $extensionList = [];
|
||||||
|
|
||||||
public ? LanguageRegistration $languageRegistration;
|
public function __construct(
|
||||||
|
public LanguageRegistration $languageRegistration
|
||||||
public function __construct(?LanguageRegistration $languageRegistration = null)
|
)
|
||||||
{
|
{
|
||||||
$this->languageRegistration = $languageRegistration;
|
|
||||||
$this->languageRegistration->registerAll($this);
|
$this->languageRegistration->registerAll($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,21 +51,24 @@ class Compiler
|
|||||||
$this->sourceCode = preg_replace_callback($replace, function ($matches) use (&$context) {
|
$this->sourceCode = preg_replace_callback($replace, function ($matches) use (&$context) {
|
||||||
$matches[2] = trim($matches[2]);
|
$matches[2] = trim($matches[2]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null);
|
list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null);
|
||||||
|
|
||||||
$token = trim($token);
|
$token = strtolower(trim($token));
|
||||||
|
$tokenName = $context->tokenName($token);
|
||||||
# @TODO Refractor this parts to allows registration to the tag's name
|
$tokenOptions = $context->tokenOptions($token);
|
||||||
if ( $this->tagList[$token] ?? false ) {
|
|
||||||
return $this->tagList[$token]->parse($context, $arguments, $token);
|
if ( $this->tagList[$tokenName] ?? false ) {
|
||||||
|
$this->eventExecute(CompileTokenTagEvent::class, $context, $arguments, $tokenName, $tokenOptions);
|
||||||
|
|
||||||
|
return $this->tagList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
|
||||||
}
|
}
|
||||||
elseif ( $this->extensionList[$token] ?? false ) {
|
elseif ( $this->extensionList[$tokenName] ?? false ) {
|
||||||
return $this->extensionList[$token]->parse($context, $arguments, $token);
|
$this->eventExecute(CompileTokenExtensionEvent::class, $context, $arguments, $tokenName, $tokenOptions);
|
||||||
|
|
||||||
|
return $this->extensionList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new \LogicException("Impossible to find token `$token` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?");
|
throw new \LogicException("Impossible to find token `$tokenName` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?");
|
||||||
}
|
}
|
||||||
|
|
||||||
}, $this->sourceCode);
|
}, $this->sourceCode);
|
||||||
@@ -81,13 +90,18 @@ class Compiler
|
|||||||
public function registerSyntax(Syntax\Syntax $syntaxObject) : self
|
public function registerSyntax(Syntax\Syntax $syntaxObject) : self
|
||||||
{
|
{
|
||||||
$this->syntaxObjectList[get_class($syntaxObject)] = $syntaxObject;
|
$this->syntaxObjectList[get_class($syntaxObject)] = $syntaxObject;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerControlStructure(ControlStructure\ControlStructure $controlStructureObject) : self
|
public function registerControlStructure(ControlStructure\ControlStructure $controlStructureObject) : self
|
||||||
{
|
{
|
||||||
foreach($controlStructureObject->tokens ?? (array) ( $controlStructureObject->token ?? [] ) as $token) {
|
foreach($controlStructureObject->tokens ?? (array) ( $controlStructureObject->token ?? [] ) as $token) {
|
||||||
$this->tagList[$token] = $controlStructureObject;
|
if ( strpos($token, '.') !== false ) {
|
||||||
|
throw new RegisterExtensionToken(sprintf("Could not register token '%s' from control structure '%s' in this version. Options are now delt by extensions themselve instead.", $token, $controlStructureObject::class));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->tagList[strtolower($token)] = $controlStructureObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@@ -95,24 +109,55 @@ class Compiler
|
|||||||
|
|
||||||
public function registerExtension(Extension\Extension $extension) : self
|
public function registerExtension(Extension\Extension $extension) : self
|
||||||
{
|
{
|
||||||
foreach($extension->tokens ?? (array) ( $extension->token ?? [] ) as $token) {
|
$tokens = $extension->tokens ?? (array) ( $extension->token ?? [] );
|
||||||
$this->extensionList[$token] = $extension;
|
|
||||||
|
foreach($tokens as $token) {
|
||||||
|
if ( strpos($token, '.') !== false ) {
|
||||||
|
throw new RegisterExtensionToken(sprintf("Could not register token '%s' from extension '%s' in this version. Options are now delt by extensions themselve instead.", $token, $extension::class));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->extensionList[strtolower($token)] = $extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $tokens ) {
|
||||||
|
$this->extensionList[] = $extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function registerExtensionsFunctions(Compiler\Context $context) : void
|
||||||
|
{
|
||||||
|
foreach($this->extensionList as $ext) {
|
||||||
|
if ($ext instanceof FunctionExtension) {
|
||||||
|
foreach ($ext->exportFunctions() as $name => $value) {
|
||||||
|
$callable = is_string($value) ? fn(...$args) => call_user_func_array([ $ext, $value ], $args) : null;
|
||||||
|
|
||||||
|
$context->pushFunction(is_numeric($name) ? $value : $name, $callable ?? $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString() : string
|
||||||
|
{
|
||||||
|
return "???";
|
||||||
|
}
|
||||||
|
|
||||||
public function getExtensionFromToken(string $name) : Extension\Extension
|
public function getExtensionFromToken(string $name) : Extension\Extension
|
||||||
{
|
{
|
||||||
if ( false === $this->extensionList[$name] ?? false ) {
|
$tokenName = explode('.', $name)[0];
|
||||||
|
|
||||||
|
if ( ! isset($this->extensionList[$tokenName]) ) {
|
||||||
throw new \InvalidArgumentException(<<<MSG
|
throw new \InvalidArgumentException(<<<MSG
|
||||||
Requested extension from token `$name` could not be found from loaded Picea compiler's extension.
|
Requested extension from token `$tokenName` could not be found from loaded Picea compiler's extension.
|
||||||
MSG);
|
MSG);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->extensionList[$name];
|
return $this->extensionList[$tokenName];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exportFunctions() : void
|
public function exportFunctions() : void
|
||||||
{
|
{
|
||||||
static $caching = [];
|
static $caching = [];
|
||||||
|
|||||||
@@ -4,7 +4,25 @@ namespace Picea\Compiler;
|
|||||||
|
|
||||||
class BaseContext extends Context {
|
class BaseContext extends Context {
|
||||||
|
|
||||||
public function __construct(?string $defaultNamespace = null) {
|
public function __construct(
|
||||||
|
?string $defaultNamespace = null,
|
||||||
|
public string $namespace = "",
|
||||||
|
public string $extendFrom = "",
|
||||||
|
public string $className = "",
|
||||||
|
public string $compiledSource = "",
|
||||||
|
public ? string $sectionAction = null,
|
||||||
|
public string $viewPath = "",
|
||||||
|
public string $filePath = "",
|
||||||
|
public string $classPath = "",
|
||||||
|
public string $declare = "declare(strict_types=1);",
|
||||||
|
public array $switchStack = [],
|
||||||
|
public array $iterationStack = [],
|
||||||
|
public array $useStack = [],
|
||||||
|
public array $sections = [],
|
||||||
|
public int $functions = 0,
|
||||||
|
public array $functionStack = [],
|
||||||
|
public array $hooks = []
|
||||||
|
) {
|
||||||
if ( $defaultNamespace !== null ) {
|
if ( $defaultNamespace !== null ) {
|
||||||
$this->namespace = $defaultNamespace;
|
$this->namespace = $defaultNamespace;
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-27
@@ -6,26 +6,6 @@ use Picea\Compiler;
|
|||||||
|
|
||||||
abstract class Context {
|
abstract class Context {
|
||||||
|
|
||||||
public string $namespace = "";
|
|
||||||
|
|
||||||
public string $extendFrom = "";
|
|
||||||
|
|
||||||
public string $className = "";
|
|
||||||
|
|
||||||
public string $compiledSource = "";
|
|
||||||
|
|
||||||
public string $viewPath = "";
|
|
||||||
|
|
||||||
public array $switchStack = [];
|
|
||||||
|
|
||||||
public array $iterateStack = [];
|
|
||||||
|
|
||||||
public array $useStack = [];
|
|
||||||
|
|
||||||
public array $functionStack = [];
|
|
||||||
|
|
||||||
public array $hooks = [];
|
|
||||||
|
|
||||||
public Compiler $compiler;
|
public Compiler $compiler;
|
||||||
|
|
||||||
public function hook(string $path, Callable $callback) : self
|
public function hook(string $path, Callable $callback) : self
|
||||||
@@ -47,16 +27,22 @@ abstract class Context {
|
|||||||
|
|
||||||
public function renderUses() : string
|
public function renderUses() : string
|
||||||
{
|
{
|
||||||
return implode(",", $this->useStack ?? []);
|
return implode(PHP_EOL, array_map(fn($use) => "use $use;", $this->useStack ?? []));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderFunctions() : string
|
public function renderFunctions() : string
|
||||||
{
|
{
|
||||||
$cls = $this->compiledClassPath();
|
if ($this->extendFrom) {
|
||||||
$ns = $this->namespace ? "\\{$this->namespace}\\" : "";
|
return "parent::exportFunctions();";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$cls = $this->compiledClassPath();
|
||||||
|
$ns = $this->namespace ? "\\{$this->namespace}\\" : "";
|
||||||
|
|
||||||
foreach($this->functionStack as $name => $function) {
|
$list = ['static $caching = [];'];
|
||||||
$list[] = <<<FUNC
|
|
||||||
|
foreach ($this->functionStack as $name => $function) {
|
||||||
|
$list[] = <<<FUNC
|
||||||
if ( false === ( ( \$caching['$ns$name'] ?? false) || function_exists( '$ns$name' ) ) ) {
|
if ( false === ( ( \$caching['$ns$name'] ?? false) || function_exists( '$ns$name' ) ) ) {
|
||||||
\$caching['$ns$name'] = true;
|
\$caching['$ns$name'] = true;
|
||||||
|
|
||||||
@@ -65,9 +51,10 @@ abstract class Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
FUNC;
|
FUNC;
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode(PHP_EOL, $list);
|
return implode(PHP_EOL, $list);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exportFunctions() : void
|
public function exportFunctions() : void
|
||||||
@@ -75,6 +62,18 @@ abstract class Context {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function tokenName(string $token) : string
|
||||||
|
{
|
||||||
|
return explode('.', $token)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tokenOptions(string $token, bool $export = false) : array|string
|
||||||
|
{
|
||||||
|
$options = array_slice(explode('.', strtolower($token)), 1);
|
||||||
|
|
||||||
|
return $export ? var_export($options, true) : $options;
|
||||||
|
}
|
||||||
|
|
||||||
public function pushFunction($name, Callable $callable) : void
|
public function pushFunction($name, Callable $callable) : void
|
||||||
{
|
{
|
||||||
$this->functionStack[$name] = $callable;
|
$this->functionStack[$name] = $callable;
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
abstract class AbstractLoop implements ControlStructure {
|
||||||
|
|
||||||
|
protected function openLoop(\Picea\Compiler\Context &$context, ?string $arguments, string $token,) : string
|
||||||
|
{
|
||||||
|
$name = "$".uniqid("{$token}_");
|
||||||
|
|
||||||
|
$stack = array_filter($context->iterationStack ?? [], function($item) {
|
||||||
|
return ! $item['or'];
|
||||||
|
});
|
||||||
|
|
||||||
|
$count = count($stack);
|
||||||
|
|
||||||
|
if ( $count > 0 ) {
|
||||||
|
$name .= "[" . end($stack)['uid'] . "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
$context->iterationStack[] = [
|
||||||
|
'or' => false,
|
||||||
|
'uid' => $name,
|
||||||
|
'token' => "end{$token}",
|
||||||
|
];
|
||||||
|
|
||||||
|
return "<?php $token ($arguments): \$__loop_index = $name = ( $name ?? 0 ) + 1; ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function closeLoop(\Picea\Compiler\Context &$context, ?string $arguments, string $token,) : string
|
||||||
|
{
|
||||||
|
$last = end($context->iterationStack);
|
||||||
|
|
||||||
|
if ( $last['or'] === false ) {
|
||||||
|
$output = "<?php $token; ?>";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$output = "<?php endif; if ( isset({$last['uid']}) ) unset({$last['uid']}); ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
array_pop($context->iterationStack);
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -2,103 +2,141 @@
|
|||||||
|
|
||||||
namespace Picea\ControlStructure;
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
use http\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
class BlockToken implements ControlStructure {
|
class BlockToken implements ControlStructure {
|
||||||
|
|
||||||
public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ];
|
public array $token = [ "arguments", "block", "endblock", "/block", "define", "slot", "endslot", "/slot", "using", "public", "private", "protected", "set" ];
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
|
static $depth = 0;
|
||||||
|
static $slotDepth = 0;
|
||||||
static $slotDefinitions = [];
|
static $slotDefinitions = [];
|
||||||
|
static $slotName = [];
|
||||||
|
$class= static::class;
|
||||||
|
|
||||||
|
# dump($slotDepth, $token, $arguments, $slotDefinitions);
|
||||||
|
|
||||||
switch($token) {
|
switch($token) {
|
||||||
case "block":
|
case "block":
|
||||||
$slotDefinitions[] = $this->slotDefinitions();
|
$slotDefinitions[] = $this->slotDefinitions();
|
||||||
|
$depth++;
|
||||||
|
|
||||||
return "<?php \$___block = \Picea\ControlStructure\BlockToken::instanciateBlock($arguments); ?>";
|
return "<?php \$___block = \Picea\ControlStructure\BlockToken::instanciateBlock($arguments); ?>";
|
||||||
|
|
||||||
|
case "/block":
|
||||||
case "endblock":
|
case "endblock":
|
||||||
|
$depth--;
|
||||||
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
|
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
|
||||||
|
|
||||||
case "arguments":
|
case "arguments":
|
||||||
$class = static::class;
|
|
||||||
|
|
||||||
return <<<PHP
|
return <<<PHP
|
||||||
<?php
|
<?php
|
||||||
try {
|
try {
|
||||||
|
extract( \$this->using ?? [], \EXTR_OVERWRITE);
|
||||||
extract( \\$class::parseArguments(function($arguments) {}, \$inlineVariables), \EXTR_OVERWRITE);
|
extract( \\$class::parseArguments(function($arguments) {}, \$inlineVariables), \EXTR_OVERWRITE);
|
||||||
|
|
||||||
unset(\$inlineVariables);
|
unset(\$inlineVariables);
|
||||||
}
|
}
|
||||||
catch(\TypeError \$ex) {
|
catch(\TypeError \$ex) {
|
||||||
throw new \Exception('A block awaiting arguments `$arguments` instead received `' . implode(', ', array_map('gettype', \$inlineVariables)) . '`');
|
\$showArgs = fn(\$e) => (\$type = gettype(\$e)) === 'object' ? \$e::class : \$type;
|
||||||
|
|
||||||
|
throw new \Exception(
|
||||||
|
sprintf('A block awaiting arguments (%s) instead received (%s) with values (%s)', '$arguments', implode(', ', array_map(\$showArgs, \$inlineVariables ?? [])), json_encode(\$inlineVariables))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
?>
|
/*%EXCEPTION_LINE_BASE%*/?>
|
||||||
PHP;
|
PHP;
|
||||||
|
|
||||||
case "define":
|
case "define":
|
||||||
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
|
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
|
||||||
|
|
||||||
end($slotDefinitions)->setDefinition(eval("return $name;"), $definition);
|
|
||||||
|
|
||||||
return <<<PHP
|
|
||||||
<?php \$this->defineSlot($name, function($definition) {}); ?>
|
|
||||||
PHP;
|
|
||||||
|
|
||||||
case "slot":
|
|
||||||
$def = end($slotDefinitions);
|
|
||||||
|
|
||||||
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
|
|
||||||
|
|
||||||
if ($def->hasDefinitions() ) {
|
|
||||||
$slotName = eval("return $name;");
|
|
||||||
$def->currentSlot = $slotName;
|
|
||||||
$def->setDefinitionVars($slotName, $definition);
|
|
||||||
|
|
||||||
$definition = $def->printDefinition($slotName);
|
|
||||||
|
|
||||||
return <<<PHP
|
( $slotDefinitions[$depth] ?? end($slotDefinitions) )->setDefinition(eval("return $name;"), $definition);
|
||||||
<?php \$this->printSlot($name, function($definition, array \$___using = []) { extract(\$___using, \EXTR_SKIP); ?>
|
|
||||||
PHP;
|
return <<<PHP
|
||||||
}
|
<?php \$this->defineSlot($name, function($definition) {}); ?>
|
||||||
else {
|
PHP;
|
||||||
return <<<PHP
|
|
||||||
<?php \$___block->slotIsSet($name) || \$___block->setSlot($name, function($definition, array \$___using = []) { extract(\$___using, \EXTR_SKIP); ?>
|
case "slot":
|
||||||
PHP;
|
$def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
|
||||||
}
|
|
||||||
|
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
|
||||||
case "endslot":
|
|
||||||
$def =end($slotDefinitions);
|
$loops = count($context->iterationStack ?? []) ? ",". implode(', ', array_filter(array_column($context->iterationStack, 'uid'), fn($e) => strpos($e, '[') === false)) : null;
|
||||||
|
$slotDepth++;
|
||||||
|
|
||||||
if ($def->hasDefinitions() ) {
|
if ($def->hasDefinitions() ) {
|
||||||
$definition = $def->getCurrentSlotDefinitionVars();
|
$slotName[] = eval("return $name;");
|
||||||
|
$def->currentSlot = end($slotName);
|
||||||
|
$def->setDefinitionVars(end($slotName), $definition);
|
||||||
|
|
||||||
|
$definition = $def->printDefinition(end($slotName));
|
||||||
|
|
||||||
if ($definition) {
|
if ($definition) {
|
||||||
$definition = "$definition,";
|
$definition .= ",";
|
||||||
}
|
}
|
||||||
|
|
||||||
return <<<PHP
|
return <<<PHP
|
||||||
<?php })->call(\$this, $definition \$this->using); ?>
|
<?php \$this->printSlot($name, function($definition array \$___using = []) use (\$picea, \$___class__template $loops) { extract(\$___using, \EXTR_SKIP); ?>
|
||||||
|
PHP;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if ($definition) {
|
||||||
|
$definition .= ",";
|
||||||
|
}
|
||||||
|
|
||||||
|
return <<<PHP
|
||||||
|
<?php (\$___block ?? \$this)->slotIsSet($name) || (\$___block ?? \$this)->setSlot($name, function($definition array \$___using = []) use (\$picea, \$___class__template $loops) { extract(\$___using, \EXTR_SKIP); ?>
|
||||||
|
PHP;
|
||||||
|
}
|
||||||
|
|
||||||
|
case "/slot":
|
||||||
|
case "endslot":
|
||||||
|
$slotDepth--;
|
||||||
|
$def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
|
||||||
|
|
||||||
|
if ($def->hasDefinitions() ) {
|
||||||
|
$def->currentSlot = array_pop($slotName);
|
||||||
|
$definition = $def->getCurrentSlotDefinitionVars();
|
||||||
|
|
||||||
|
if ($definition) {
|
||||||
|
$definition .= ",";
|
||||||
|
}
|
||||||
|
|
||||||
|
return <<<PHP
|
||||||
|
<?php })->call(\$this, $definition array_merge(get_defined_vars(), \$this->using)); ?>
|
||||||
PHP;
|
PHP;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return "<?php }); ?>";
|
return "<?php }); ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "public":
|
||||||
|
case "private":
|
||||||
|
case "protected":
|
||||||
|
$arguments = rtrim($arguments, ';');
|
||||||
|
return "<?php \$this->createProperty(\\$class::parseProperty('$token', fn($arguments) => func_get_arg(0))) ?>";
|
||||||
|
|
||||||
|
case "set":
|
||||||
|
return "<?php \$___block->setPropertyValue({$arguments}); ?>";
|
||||||
|
|
||||||
|
# DEPRECATED ; should be removed, to be superseded by 'set' instead
|
||||||
case "using":
|
case "using":
|
||||||
return "<?php \$___block->setUsing($arguments); ?>";
|
return "<?php \$___block->setUsing($arguments); ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new \LogicException("Unhandled token ? Should never have been here !");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function inlineHtml(? object $proxy, string $viewPath, ... $variables) {
|
public function inlineHtml(? object $proxy, string $viewPath, ... $variables) {
|
||||||
return $this->renderHtml($viewPath, [ 'inlineVariables' => $variables ], $proxy);
|
return $this->renderHtml($viewPath, [ 'inlineVariables' => $variables ], $proxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function parseSlotArguments(Callable $method) : array
|
public static function parseSlotArguments(Callable $method) : array
|
||||||
{
|
{
|
||||||
#return
|
#return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function parseArguments(Callable $method, array $arguments) : array
|
public static function parseArguments(Callable $method, array $arguments) : array
|
||||||
{
|
{
|
||||||
try{
|
try{
|
||||||
@@ -106,133 +144,208 @@ class BlockToken implements ControlStructure {
|
|||||||
}
|
}
|
||||||
catch(\TypeError $ex) {
|
catch(\TypeError $ex) {
|
||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parameters = [];
|
$parameters = [];
|
||||||
|
|
||||||
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
|
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
|
||||||
|
$name = $value->getName();
|
||||||
|
|
||||||
|
# Ordered arguments
|
||||||
if ( isset($arguments[$key]) ) {
|
if ( isset($arguments[$key]) ) {
|
||||||
$parameters[ $value->getName() ] = $arguments[$key];
|
$parameters[ $name ] = $arguments[$key];
|
||||||
}
|
}
|
||||||
|
# Named arguments
|
||||||
|
elseif (isset($arguments[$name])) {
|
||||||
|
$parameters[ $name ] = $arguments[$name];
|
||||||
|
}
|
||||||
|
# Using default value
|
||||||
elseif ( $value->isDefaultValueAvailable() ) {
|
elseif ( $value->isDefaultValueAvailable() ) {
|
||||||
$parameters[ $value->getName() ] = $value->getDefaultValue();
|
$parameters[ $name ] = $value->getDefaultValue();
|
||||||
}
|
}
|
||||||
elseif ( $value->isVariadic() ) {
|
elseif ( $value->isVariadic() ) {
|
||||||
$parameters[ $value->getName() ] = [];
|
$parameters[ $name ] = [];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$parameters[ $name ] = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $parameters;
|
return $parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function parseProperty(string $token, \Closure $method) : BlockToken\Property
|
||||||
|
{
|
||||||
|
foreach((new \ReflectionFunction($method))->getParameters() as $value) {
|
||||||
|
$property = new BlockToken\Property(
|
||||||
|
name: $value->getName(),
|
||||||
|
scope: BlockToken\PropertyScopeEnum::from($token),
|
||||||
|
setter: $method,
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($value->isDefaultValueAvailable()) {
|
||||||
|
$property->value = $value->getDefaultValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
# For now, we only allow one declaration per token, might be revised at another moment...
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $property ?? throw new \LogicException("No parameters found in property declaration.");
|
||||||
|
}
|
||||||
|
|
||||||
public static function slotDefinitions() {
|
public static function slotDefinitions() {
|
||||||
return new class() {
|
return new class() {
|
||||||
|
|
||||||
public array $definitions = [];
|
public array $definitions = [];
|
||||||
|
|
||||||
public array $variables = [];
|
public array $variables = [];
|
||||||
|
|
||||||
public bool $rendering = false;
|
public bool $rendering = false;
|
||||||
|
|
||||||
public string $currentSlot;
|
public string $currentSlot;
|
||||||
|
|
||||||
public function setDefinition(string $name, string $definition) : self
|
public function setDefinition(string $name, string $definition) : self
|
||||||
{
|
{
|
||||||
$this->definitions[$name] = $definition;
|
$this->definitions[$name] = $definition;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setDefinitionVars(string $name, string $variables) : self
|
public function setDefinitionVars(string $name, string $variables) : self
|
||||||
{
|
{
|
||||||
$this->variables[$name] = $variables;
|
$this->variables[$name] = $variables;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefinitionVars(string $name) : string
|
public function getDefinitionVars(string $name) : string
|
||||||
{
|
{
|
||||||
return $this->variables[$name] ?? "";
|
return $this->variables[$name] ?? "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCurrentSlotDefinitionVars() : string
|
public function getCurrentSlotDefinitionVars() : string
|
||||||
{
|
{
|
||||||
return $this->getDefinitionVars($this->currentSlot);
|
return $this->getDefinitionVars($this->currentSlot);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasDefinitions() : bool
|
public function hasDefinitions() : bool
|
||||||
{
|
{
|
||||||
return count($this->definitions) > 0;
|
return count($this->definitions) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function printDefinition(string $name) : string
|
public function printDefinition(string $name) : string
|
||||||
{
|
{
|
||||||
if ( ! isset($this->definitions[$name]) ) {
|
if ( ! isset($this->definitions[$name]) ) {
|
||||||
throw new \Exception("Slot definition for `$name` was not found. Have you defined it in your block header ?");
|
throw new \Exception("Slot definition for `$name` was not found. Have you defined it in your block header using something like '{% define \"$name\", ...\$arguments %}' ?");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->definitions[$name];
|
return $this->definitions[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render() : void
|
public function render() : void
|
||||||
{
|
{
|
||||||
$this->rendering = true;
|
$this->rendering = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function instanciateBlock(string $viewPath, ... $arguments)
|
public static function instanciateBlock(string $viewPath, ... $arguments)
|
||||||
{
|
{
|
||||||
return new class($viewPath, ...$arguments) {
|
return new class($viewPath, ...$arguments) {
|
||||||
|
|
||||||
public bool $rendering = false;
|
public bool $rendering = false;
|
||||||
|
|
||||||
public string $viewPath;
|
public string $viewPath;
|
||||||
|
|
||||||
public array $using = [];
|
public array $using = [];
|
||||||
|
|
||||||
public array $arguments = [];
|
public array $arguments = [];
|
||||||
|
|
||||||
public array $slots = [];
|
public array $slots = [];
|
||||||
|
|
||||||
public array $definition = [];
|
public array $definition = [];
|
||||||
|
|
||||||
|
protected array $properties = [];
|
||||||
|
|
||||||
|
private array $definedPropertyValues = [];
|
||||||
|
|
||||||
public function __construct(string $viewPath, ...$arguments) {
|
public function __construct(string $viewPath, ...$arguments) {
|
||||||
$this->viewPath = $viewPath;
|
$this->viewPath = $viewPath;
|
||||||
$this->arguments = $arguments;
|
$this->arguments = $arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render(object $classTemplate) : string
|
public function render(object $classTemplate) : string
|
||||||
{
|
{
|
||||||
$this->rendering = true;
|
$this->rendering = true;
|
||||||
|
|
||||||
return $classTemplate->picea->inlineHtml($this, $this->viewPath, ...$this->arguments);
|
if ($this->using['this'] ?? false) {
|
||||||
|
throw new \InvalidArgumentException("Block '{$this->viewPath}' cannot overwrite 'this' variable.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $classTemplate->picea->inlineBlock($this, $this->viewPath, ...$this->arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setSlot(string $name, Callable $method) : void
|
public function setSlot(string $name, Callable $method) : void
|
||||||
{
|
{
|
||||||
$this->slots[$name] = $method;
|
$this->slots[$name] = $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function defineSlot(string $name, Callable $method) : void
|
public function defineSlot(string $name, Callable $method) : void
|
||||||
{
|
{
|
||||||
$this->definition[$name] = $method;
|
$this->definition[$name] = $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function slotIsSet(string $name) : bool
|
public function slotIsSet(string $name) : bool
|
||||||
{
|
{
|
||||||
return ! empty($this->slots[$name]);
|
return ! empty($this->slots[$name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function printSlot(string $name, Callable $default)
|
public function printSlot(string $name, Callable $default) : mixed
|
||||||
{
|
{
|
||||||
return $this->slotIsSet($name) ? $this->slots[$name] : $default;
|
return $this->slotIsSet($name) ? $this->slots[$name] : $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUsing(array $variables) {
|
public function setUsing(array $variables) : void
|
||||||
|
{
|
||||||
$this->using = $variables;
|
$this->using = $variables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setPropertyValue(array $vars) : void
|
||||||
|
{
|
||||||
|
foreach($vars as $key => $value) {
|
||||||
|
$this->definedPropertyValues[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createProperty(BlockToken\Property $property) : void
|
||||||
|
{
|
||||||
|
if (isset($this->properties[$property->name])) {
|
||||||
|
throw new \InvalidArgumentException("Given property '{$property->name}' is already defined somewhere else in this block.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->properties[$property->name] = $property;
|
||||||
|
|
||||||
|
if (isset($this->definedPropertyValues[$property->name])) {
|
||||||
|
if ($property->scope !== BlockToken\PropertyScopeEnum::Public) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf("Trying to set property %s but it's scope (%s) makes it impossible", $property->name, $property->scope->name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->__set($property->name, $this->definedPropertyValues[$property->name]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __set(string $name, mixed $value) : void
|
||||||
|
{
|
||||||
|
$this->properties[$name]->value = call_user_func($this->properties[$name]->setter, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __get(string $name) : mixed
|
||||||
|
{
|
||||||
|
return $this->properties[$name]->value;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure\BlockToken;
|
||||||
|
|
||||||
|
class Property
|
||||||
|
{
|
||||||
|
public mixed $value;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public readonly string $name,
|
||||||
|
public readonly PropertyScopeEnum $scope,
|
||||||
|
public readonly \Closure $setter,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure\BlockToken;
|
||||||
|
|
||||||
|
enum PropertyScopeEnum: string
|
||||||
|
{
|
||||||
|
case Private = "private";
|
||||||
|
case Protected = "protected";
|
||||||
|
case Public = "public";
|
||||||
|
}
|
||||||
@@ -4,9 +4,10 @@ namespace Picea\ControlStructure;
|
|||||||
|
|
||||||
class BreakToken implements ControlStructure {
|
class BreakToken implements ControlStructure {
|
||||||
|
|
||||||
public string $token = "break";
|
public array $token = [ "break", "/case" ];
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
return "<?php break; ?>";
|
return "<?php break; ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ class CaseToken implements ControlStructure {
|
|||||||
|
|
||||||
public string $token = "case";
|
public string $token = "case";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
$output = "";
|
$output = "";
|
||||||
|
|
||||||
if ( $context->switchStack ) {
|
if ( $context->switchStack ) {
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
class ContinueToken implements ControlStructure {
|
||||||
|
|
||||||
|
public string $token = "continue";
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
|
return "<?php continue; ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,5 +3,5 @@
|
|||||||
namespace Picea\ControlStructure;
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
interface ControlStructure {
|
interface ControlStructure {
|
||||||
public function parse(\Picae\Compiler\Context &$context, string $sourceCode, string $token);
|
public function parse(\Picea\Compiler\Context &$context, string $sourceCode, string $token);
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ class DefaultToken implements ControlStructure {
|
|||||||
|
|
||||||
public string $token = "default";
|
public string $token = "default";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
$output = "";
|
$output = "";
|
||||||
|
|
||||||
if ( $context->switchStack ) {
|
if ( $context->switchStack ) {
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
class EchoToken implements ControlStructure {
|
||||||
|
|
||||||
|
public array $token = [ "echo", "echo.raw" ];
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
|
switch($token) {
|
||||||
|
case "echo":
|
||||||
|
return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>";
|
||||||
|
|
||||||
|
case "echo.raw":
|
||||||
|
return "<?php \\Picea\\ControlStructure\\EchoToken::echoRaw($arguments) ?>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function echoRaw($arguments) : string
|
||||||
|
{
|
||||||
|
return "<?php echo $arguments ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function echoSafe($arguments) : string
|
||||||
|
{
|
||||||
|
return "<?php echo $arguments ?>";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Picea\ControlStructure;
|
|
||||||
|
|
||||||
class EndRawToken implements ControlStructure {
|
|
||||||
|
|
||||||
public string $token = "endraw";
|
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,7 @@ class ExtendsToken implements ControlStructure {
|
|||||||
|
|
||||||
public string $token = "extends";
|
public string $token = "extends";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $path, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $path, string $token) {
|
||||||
# Triming string's quotes
|
# Triming string's quotes
|
||||||
$path = trim($path, "\"\' \t");
|
$path = trim($path, "\"\' \t");
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ namespace Picea\ControlStructure;
|
|||||||
|
|
||||||
class ForToken implements ControlStructure {
|
class ForToken implements ControlStructure {
|
||||||
|
|
||||||
public array $token = [ "for", "endfor" ];
|
public array $token = [ "for", "endfor", "/for" ];
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
switch($token) {
|
switch($token) {
|
||||||
case "for":
|
case "for":
|
||||||
$uid = "$".uniqid("for_");
|
$uid = "$".uniqid("for_");
|
||||||
@@ -18,13 +18,16 @@ class ForToken implements ControlStructure {
|
|||||||
];
|
];
|
||||||
|
|
||||||
return "<?php for ($arguments): {$uid} = 1; ?>";
|
return "<?php for ($arguments): {$uid} = 1; ?>";
|
||||||
|
|
||||||
|
case "/for":
|
||||||
case "endfor":
|
case "endfor":
|
||||||
if ( end($context->iterationStack)['or'] === false ) {
|
$last = end($context->iterationStack);
|
||||||
|
|
||||||
|
if ( $last['or'] === false ) {
|
||||||
$output = "<?php endfor; ?>";
|
$output = "<?php endfor; ?>";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$output = "<?php endif; ?>";
|
$output = "<?php endif; unset({$last['uid']}) ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
array_pop($context->iterationStack);
|
array_pop($context->iterationStack);
|
||||||
|
|||||||
@@ -2,44 +2,20 @@
|
|||||||
|
|
||||||
namespace Picea\ControlStructure;
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
class ForeachToken implements ControlStructure {
|
use DI\Definition\Source\AnnotationBasedAutowiring;
|
||||||
|
|
||||||
public array $token = [ "foreach", "endforeach" ];
|
class ForeachToken extends AbstractLoop {
|
||||||
|
public array $token = [ "foreach", "endforeach", "/foreach" ];
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
switch($token) {
|
switch($token) {
|
||||||
case "foreach":
|
case "foreach":
|
||||||
$name = "$".uniqid("foreach_");
|
return $this->openLoop($context, $arguments, $token);
|
||||||
|
|
||||||
$stack = array_filter($context->iterationStack ?? [], function($item) {
|
|
||||||
return ! $item['or'];
|
|
||||||
});
|
|
||||||
|
|
||||||
$count = count($stack);
|
|
||||||
|
|
||||||
if ( $count > 0 ) {
|
|
||||||
$name .= "[" . end($stack)['uid'] . "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
$context->iterationStack[] = [
|
|
||||||
'or' => false,
|
|
||||||
'uid' => $name,
|
|
||||||
'token' => 'endforeach',
|
|
||||||
];
|
|
||||||
|
|
||||||
return "<?php foreach ($arguments): $name = ( $name ?? 0 ) + 1; ; ?>";
|
|
||||||
|
|
||||||
case "endforeach":
|
case "endforeach":
|
||||||
if ( end($context->iterationStack)['or'] === false ) {
|
case "/foreach":
|
||||||
$output = "<?php endforeach; ?>";
|
return $this->closeLoop($context, $arguments, $token);
|
||||||
}
|
|
||||||
else {
|
|
||||||
$output = "<?php endif; ?>";
|
|
||||||
}
|
|
||||||
|
|
||||||
array_pop($context->iterationStack);
|
|
||||||
|
|
||||||
return $output;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
class FunctionToken implements ControlStructure {
|
||||||
|
|
||||||
|
public array $token = [ "function", "endfunction", "return" ];
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
|
switch($token) {
|
||||||
|
case "function":
|
||||||
|
$context->functions++;
|
||||||
|
|
||||||
|
return $this->printFunction($context, $arguments);
|
||||||
|
|
||||||
|
case "return":
|
||||||
|
if ( empty($context->functions) ) {
|
||||||
|
throw new \RuntimeException("A function return tag {% return %} was found without an opening {% function ... %} tag");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->printReturn($context, $arguments);
|
||||||
|
|
||||||
|
case "endfunction":
|
||||||
|
if ( empty($context->functions) ) {
|
||||||
|
throw new \RuntimeException("A function closing tag {% endfunction %} was found without an opening {% function ... %} tag");
|
||||||
|
}
|
||||||
|
|
||||||
|
$context->functions--;
|
||||||
|
|
||||||
|
return $this->printEndFunction($context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function printFunction($context, ?string $arguments) : string
|
||||||
|
{
|
||||||
|
$name = trim(explode('(', $arguments, 2)[0]);
|
||||||
|
|
||||||
|
return "<?php if (! function_exists(__NAMESPACE__ . '\\$name')) { function $arguments { ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function printReturn($context, ?string $arguments) : string
|
||||||
|
{
|
||||||
|
return "<?php return $arguments; ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function printEndFunction($context) : string
|
||||||
|
{
|
||||||
|
return "<?php } } ?>";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,9 +4,9 @@ namespace Picea\ControlStructure;
|
|||||||
|
|
||||||
class IfToken implements ControlStructure {
|
class IfToken implements ControlStructure {
|
||||||
|
|
||||||
public array $token = [ "if", "else", "elseif", "endif" ];
|
public array $token = [ "if", "else", "elseif", "endif", "/if" ];
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
switch($token) {
|
switch($token) {
|
||||||
case "if":
|
case "if":
|
||||||
return "<?php if ($arguments): ?>";
|
return "<?php if ($arguments): ?>";
|
||||||
@@ -16,7 +16,8 @@ class IfToken implements ControlStructure {
|
|||||||
|
|
||||||
case "elseif":
|
case "elseif":
|
||||||
return "<?php elseif ($arguments): ?>";
|
return "<?php elseif ($arguments): ?>";
|
||||||
|
|
||||||
|
case "/if":
|
||||||
case "endif":
|
case "endif":
|
||||||
return "<?php endif ?>";
|
return "<?php endif ?>";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class IncludeToken implements ControlStructure {
|
|||||||
|
|
||||||
public string $token = "include";
|
public string $token = "include";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ? string $viewPath, string $token) {
|
||||||
return "<?php echo \$___class__template->picea->inlineContent($arguments); ?>";
|
return "<?php echo \$___class__template->picea->inlineContent($viewPath); ?>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ class NamespaceToken implements ControlStructure {
|
|||||||
|
|
||||||
public string $token = "namespace";
|
public string $token = "namespace";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
$context->namespace = $arguments;
|
$context->namespace = $arguments;
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ class OrToken implements ControlStructure {
|
|||||||
|
|
||||||
public string $token = "or";
|
public string $token = "or";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
if ( empty($context->iterationStack) ) {
|
if ( empty($context->iterationStack) ) {
|
||||||
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for` or `foreach` declaration are properly made.");
|
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for`, `foreach`, `while`, `do/until` declaration are properly made.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$key = count( $context->iterationStack ) - 1;
|
$key = count( $context->iterationStack ) - 1;
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Picea\ControlStructure;
|
|
||||||
|
|
||||||
class RawToken implements ControlStructure {
|
|
||||||
|
|
||||||
public string $token = "raw";
|
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,25 +2,45 @@
|
|||||||
|
|
||||||
namespace Picea\ControlStructure;
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
enum PrintActionEnum : string {
|
||||||
|
case prepend = "prepend";
|
||||||
|
case default = "default";
|
||||||
|
case append = "append";
|
||||||
|
# case both = "both";
|
||||||
|
}
|
||||||
|
|
||||||
class SectionToken implements ControlStructure {
|
class SectionToken implements ControlStructure {
|
||||||
|
|
||||||
public array $token = [ "section", "endsection" ];
|
public array $token = [ "section", "endsection", "/section" ];
|
||||||
|
|
||||||
|
protected PrintActionEnum $action = PrintActionEnum::default;
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
|
if (in_array('prepend', $options)) {
|
||||||
|
$this->action = PrintActionEnum::prepend;
|
||||||
|
}
|
||||||
|
elseif (in_array('append', $options)) {
|
||||||
|
$this->action = PrintActionEnum::append;
|
||||||
|
}
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
|
||||||
switch($token) {
|
switch($token) {
|
||||||
case "section":
|
case "section":
|
||||||
return $this->printSection($context, $arguments);
|
return $this->printSection($context, $arguments);
|
||||||
|
|
||||||
case "endsection":
|
case "endsection":
|
||||||
|
case "/endsection":
|
||||||
if ( empty($context->sections) ) {
|
if ( empty($context->sections) ) {
|
||||||
throw new \RuntimeException("A section closing tag {% endsection %} was found without an opening {% section %} tag");
|
throw new \RuntimeException("A section closing tag {% endsection %} was found without an opening {% section %} tag");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->action = PrintActionEnum::default;
|
||||||
|
|
||||||
return $this->printEndSection($context);
|
return $this->printEndSection($context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function printSection($context, ?string $arguments) : string
|
protected function printSection($context, ? string $arguments) : string
|
||||||
{
|
{
|
||||||
list($name, $options) = array_pad(explode(',', $arguments, 2), 2, null);
|
list($name, $options) = array_pad(explode(',', $arguments, 2), 2, null);
|
||||||
|
|
||||||
@@ -28,7 +48,7 @@ class SectionToken implements ControlStructure {
|
|||||||
$options = eval("return $options;");
|
$options = eval("return $options;");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! ctype_alnum(str_replace([".", "\"", "'", "-", "_"], "", $name)) ) {
|
if ( ! ctype_alnum(str_replace([".", "\"", "'", "-", "_", ":"], "", $name)) ) {
|
||||||
throw new \RuntimeException("Your section named `{$name}` contains invalid character. Allowed are only letters, numbers, dashes, underscores and dots");
|
throw new \RuntimeException("Your section named `{$name}` contains invalid character. Allowed are only letters, numbers, dashes, underscores and dots");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +57,7 @@ class SectionToken implements ControlStructure {
|
|||||||
'options' => $options,
|
'options' => $options,
|
||||||
];
|
];
|
||||||
|
|
||||||
$action = $options['action'] ?? "default";
|
$action = $options['action'] ?? $this->action->value;
|
||||||
|
|
||||||
if (! in_array($action, ['prepend', 'append', 'default'])) {
|
if (! in_array($action, ['prepend', 'append', 'default'])) {
|
||||||
throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag");
|
throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag");
|
||||||
@@ -45,15 +65,24 @@ class SectionToken implements ControlStructure {
|
|||||||
|
|
||||||
$order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])";
|
$order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])";
|
||||||
|
|
||||||
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];
|
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];" .
|
||||||
\$___class__template->sectionList[$name]['$action'][] = [ 'order' => $order, 'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables) {
|
"\$___class__template->sectionList[$name]['$action'][] = [ ".
|
||||||
extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE); ?>";
|
"'order' => $order, " .
|
||||||
|
"'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {" .
|
||||||
|
"extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);" .
|
||||||
|
"\$___class__template->sectionStack[] = $name;" .
|
||||||
|
"\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function printEndSection($context) : string
|
protected function printEndSection($context) : string
|
||||||
{
|
{
|
||||||
$section = array_pop($context->sections);
|
$section = array_pop($context->sections);
|
||||||
$build = $context->extendFrom ? "" : "\$___class__template->renderSection({$section['name']});";
|
$build = $context->extendFrom ? "!empty(\$___class__template->sectionStack) && \$___class__template->renderSection({$section['name']}, false);" : "\$___class__template->renderSection({$section['name']}, false);";
|
||||||
return "<?php }]; $build?>";
|
|
||||||
|
return "<?php \$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']});" .
|
||||||
|
"array_pop(\$___class__template->sectionStack); }];" .
|
||||||
|
$build .
|
||||||
|
"?>";
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,9 +4,9 @@ namespace Picea\ControlStructure;
|
|||||||
|
|
||||||
class SwitchToken implements ControlStructure {
|
class SwitchToken implements ControlStructure {
|
||||||
|
|
||||||
public array $token = [ "switch", "case", "endswitch" ];
|
public array $token = [ "switch", "case", "endcase", "endswitch", "/switch" ];
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
switch($token) {
|
switch($token) {
|
||||||
case "switch":
|
case "switch":
|
||||||
$context->switchStack[] = true;
|
$context->switchStack[] = true;
|
||||||
@@ -23,11 +23,13 @@ class SwitchToken implements ControlStructure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ( $output ?? "" ) . "case $arguments: ?>";
|
return ( $output ?? "" ) . "case $arguments: ?>";
|
||||||
|
|
||||||
case "endcase":
|
case "endcase":
|
||||||
|
case "/case":
|
||||||
return "<?php break; ?>";
|
return "<?php break; ?>";
|
||||||
|
|
||||||
case "endswitch":
|
case "endswitch":
|
||||||
|
case "/switch":
|
||||||
return "<?php endswitch; ?>";
|
return "<?php endswitch; ?>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
class TryCatchToken implements ControlStructure {
|
||||||
|
|
||||||
|
public array $token = [ "try", "catch", "finally", "endtry", "/try" ];
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
|
switch($token) {
|
||||||
|
case "try":
|
||||||
|
return "<?php try { ?>";
|
||||||
|
|
||||||
|
case "catch":
|
||||||
|
return "<?php } catch($arguments) { ?>";
|
||||||
|
|
||||||
|
case "finally":
|
||||||
|
return "<?php } finally { ?>";
|
||||||
|
|
||||||
|
case "endtry":
|
||||||
|
case "/try":
|
||||||
|
return "<?php } ?>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ class UseToken implements ControlStructure {
|
|||||||
|
|
||||||
public string $token = "use";
|
public string $token = "use";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
$context->useStack[] = $arguments;
|
$context->useStack[] = $arguments;
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ class ViewToken implements ControlStructure {
|
|||||||
|
|
||||||
public string $token = "view";
|
public string $token = "view";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
return "<?php echo \$___class__template->picea->renderHtml($arguments, get_defined_vars(), \$this); ?>";
|
# The way this is ordered, if you provide a second arguments, being an array of variables, get_defined_vars() will not be pushed inside the view
|
||||||
|
return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments, get_defined_vars()); ?>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
class WhileToken extends AbstractLoop {
|
||||||
|
public array $token = [ "do", "while", "endwhile", "/while" ];
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
|
switch($token) {
|
||||||
|
case "do":
|
||||||
|
$context->iterationStack[] = [
|
||||||
|
'or' => false,
|
||||||
|
'token' => "do",
|
||||||
|
];
|
||||||
|
|
||||||
|
return "<?php do { ?>";
|
||||||
|
|
||||||
|
case "while":
|
||||||
|
if ( $context->iterationStack ?? false ) {
|
||||||
|
if ( end($context->iterationStack)['token'] === 'do' ) {
|
||||||
|
array_pop($context->iterationStack);
|
||||||
|
|
||||||
|
return "<?php } while($arguments); ?>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->openLoop($context, $arguments, $token);
|
||||||
|
|
||||||
|
case "endwhile":
|
||||||
|
case "/while":
|
||||||
|
return $this->closeLoop($context, $arguments, $token);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Builder;
|
||||||
|
|
||||||
|
interface ClassTemplateOutputDone {
|
||||||
|
public function execute(string $name, array $variablesList) : mixed;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Builder;
|
||||||
|
|
||||||
|
interface ClassTemplateOutputing {
|
||||||
|
public function execute(string $name, array $variablesList) : mixed;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Builder;
|
||||||
|
|
||||||
|
interface ClassTemplateRenderSection {
|
||||||
|
public function execute(string $name) : mixed;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Builder;
|
||||||
|
|
||||||
|
interface ClassTemplateRenderSectionDone {
|
||||||
|
public function execute(string $section) : mixed;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Compiler;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
|
interface CompileTokenExtensionEvent
|
||||||
|
{
|
||||||
|
public function execute(Context $context, ?string $arguments, string $token, array $options = []);
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Compiler;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
|
interface CompileTokenTagEvent
|
||||||
|
{
|
||||||
|
public function execute(Context &$context, ?string $arguments, string $token, array $options = []);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Extension;
|
||||||
|
|
||||||
|
interface BuildAssetUrl
|
||||||
|
{
|
||||||
|
public function execute(string $uri, array $parameters = [], bool $appendVersion = true) : mixed;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Extension;
|
||||||
|
|
||||||
|
interface UrlBuildAssetEvent
|
||||||
|
{
|
||||||
|
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Extension;
|
||||||
|
|
||||||
|
interface UrlBuildRouteEvent
|
||||||
|
{
|
||||||
|
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Extension;
|
||||||
|
|
||||||
|
interface UrlBuildUrlEvent
|
||||||
|
{
|
||||||
|
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Event\Extension;
|
||||||
|
|
||||||
|
interface UrlRegisterRouteEvent
|
||||||
|
{
|
||||||
|
public function execute(string $name, array $route) : void;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea;
|
||||||
|
|
||||||
|
trait EventTrait
|
||||||
|
{
|
||||||
|
protected $eventTraitMethod = "execute";
|
||||||
|
|
||||||
|
public array $eventTraitList = [];
|
||||||
|
|
||||||
|
protected array $eventTraitReturnList = [];
|
||||||
|
|
||||||
|
public function eventRegister(object $event) : void
|
||||||
|
{
|
||||||
|
$this->eventTraitList[] = $event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function eventFromType(string $type) : array
|
||||||
|
{
|
||||||
|
return array_filter($this->eventTraitList, fn($ev) => $ev instanceof $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function eventExecute(string $type, ...$arguments) : void
|
||||||
|
{
|
||||||
|
foreach($this->eventFromType($type) as $event) {
|
||||||
|
$this->eventTraitReturnList[$event::class][] = call_user_func_array([ $event, $this->eventTraitMethod ], $arguments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Exception;
|
||||||
|
|
||||||
|
use Picea\Picea;
|
||||||
|
|
||||||
|
class ParseExtensionException extends \Exception {}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Exception;
|
||||||
|
|
||||||
|
use Picea\Picea;
|
||||||
|
|
||||||
|
class RegisterExtensionToken extends \Exception {}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Exception;
|
||||||
|
|
||||||
|
use Picea\Picea;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whenever an error occured rendering a view, this is the exception that will show up.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class RenderHtmlException extends \Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct(object $template, string $message, int $code, \Throwable $previous)
|
||||||
|
{
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
|
||||||
|
$this->defineError($previous, $template);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defineError(\Throwable $previous, object $template) : void
|
||||||
|
{
|
||||||
|
/*$loadedTemplates = array_flip($this->picea->loadedTemplateFile);*/
|
||||||
|
|
||||||
|
$this->file = $template::getParam('view');
|
||||||
|
$this->line = $template::getSourceLineFromException($previous->getLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getTemplateFile(string $filePath) : ? array
|
||||||
|
{
|
||||||
|
$content = null;
|
||||||
|
|
||||||
|
if ( is_array($content) && isset($content['classname'], $content['namespace'], $content['view'], $content['extends']) ) {
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Extension;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
|
class BatchExtension implements Extension, FunctionExtension {
|
||||||
|
|
||||||
|
public string $token = "batch";
|
||||||
|
|
||||||
|
public function exportFunctions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
"batch" => [ $this, 'batch' ]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
|
return "<?php echo batch($arguments) ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function batch(iterable $array, int $size, mixed $default = null) : \Generator
|
||||||
|
{
|
||||||
|
$split = ceil( count($array) / $size );
|
||||||
|
|
||||||
|
for($i = 0; $i < $split; $i++) {
|
||||||
|
$slice = array_slice(is_array($array) ? $array : iterator_to_array($array, true), $i * $size, $size, true);
|
||||||
|
$pad = ( count($slice) !== $size ) && ( $default !== null );
|
||||||
|
yield $pad ? array_pad($slice, $size, $default) : $slice;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Extension;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
|
class DiExtension implements Extension, FunctionExtension {
|
||||||
|
|
||||||
|
public array $token = [ "di:has", "di:get" ];
|
||||||
|
|
||||||
|
public function __construct(public readonly ContainerInterface $container) {}
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
|
if ( $token === 'di:get' ) {
|
||||||
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->container->get($arguments) ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $token === 'di:has' ) {
|
||||||
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->container->has($arguments) ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException("You must use di:get/di:has tokens or di_get()/di_has() methods.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exportFunctions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'di_get' => function(string $classname) {
|
||||||
|
return $this->container->get($classname);
|
||||||
|
},
|
||||||
|
|
||||||
|
'di_has' => function(string $classname) {
|
||||||
|
return $this->container->has($classname);
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,5 +3,5 @@
|
|||||||
namespace Picea\Extension;
|
namespace Picea\Extension;
|
||||||
|
|
||||||
interface Extension {
|
interface Extension {
|
||||||
public function parse(\Picae\Compiler\Context &$context, string $sourceCode, string $token);
|
public function parse(\Picea\Compiler\Context &$context, string $arguments, string $token, array $options = []) : string;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Extension;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
|
interface FunctionExtension
|
||||||
|
{
|
||||||
|
public function exportFunctions() : array;
|
||||||
|
}
|
||||||
@@ -4,38 +4,50 @@ namespace Picea\Extension;
|
|||||||
|
|
||||||
use Picea\Compiler\Context;
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
class JsonExtension implements Extension {
|
class JsonExtension implements Extension, FunctionExtension {
|
||||||
|
|
||||||
public array $token = [ "json", "json.pretty" ];
|
public array $token = [ "json" ];
|
||||||
|
|
||||||
public int $flags = JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR;
|
public int $flags = JSON_HEX_TAG | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE;
|
||||||
|
|
||||||
public function __construct(? Context $context = null) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
if ($context) {
|
|
||||||
$this->register($context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token)
|
|
||||||
{
|
{
|
||||||
$flag = $this->flags;
|
$flag = $this->flags;
|
||||||
|
|
||||||
switch ($token) {
|
if ( in_array('pretty', $options) ) {
|
||||||
case "json.pretty":
|
$flag |= \JSON_PRETTY_PRINT;
|
||||||
$flag |= \JSON_PRETTY_PRINT;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "json.html":
|
|
||||||
return "<?php echo htmlentities(json_encode($arguments, {$this->flags}), ENT_QUOTES, 'UTF-8') ?>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "<?php echo json_encode($arguments, $flag) ?>";
|
if ( in_array('html', $options) ) {
|
||||||
|
return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
$cls = static::class;
|
||||||
|
|
||||||
|
return "<?php echo json_encode(\\$cls::utf8($arguments), $flag) ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register(Context $context) : void
|
public function exportFunctions(): array
|
||||||
{
|
{
|
||||||
$context->pushFunction("json", function($arguments, ? int $flags = null) {
|
return [
|
||||||
return json_encode($arguments, \JSON_FORCE_OBJECT);
|
'json' => function($arguments, ? int $flags = null) {
|
||||||
});
|
return json_encode($arguments, $flags ?? $this->flags);
|
||||||
|
},
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function utf8($src) {
|
||||||
|
|
||||||
|
if (is_array($src)) {
|
||||||
|
foreach ($src as $key => $value) {
|
||||||
|
$src[$key] = static::utf8($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif (is_string($src)) {
|
||||||
|
return mb_convert_encoding($src, "UTF-8", "UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $src;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,31 +3,60 @@
|
|||||||
namespace Picea\Extension;
|
namespace Picea\Extension;
|
||||||
|
|
||||||
use Picea\Compiler\Context;
|
use Picea\Compiler\Context;
|
||||||
|
use Picea\Event\Builder\ClassTemplateRenderSectionDone;
|
||||||
|
|
||||||
class LanguageExtension implements Extension {
|
class LanguageExtension implements Extension, FunctionExtension {
|
||||||
|
|
||||||
public array $tokens = [ "lang", "_", "language.set" ];
|
public array $tokens = [ "lang", "_", "language" ];
|
||||||
|
|
||||||
public string $currentLanguage = "";
|
public string $currentLanguage = "";
|
||||||
|
|
||||||
protected LanguageHandler $languageHandler;
|
public function __construct(
|
||||||
|
protected LanguageHandlerInterface $languageHandler
|
||||||
|
) {}
|
||||||
|
|
||||||
public function __construct(Context $context, LanguageHandler $handler) {
|
public function exportFunctions(): array
|
||||||
$this->register($context);
|
{
|
||||||
$this->languageHandler = $handler;
|
return [
|
||||||
|
'_' => 'relativeLang',
|
||||||
|
'lang' => 'absoluteLang',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
{
|
{
|
||||||
switch($token) {
|
switch(explode('.', $token)[0]) {
|
||||||
case "language.set":
|
case "language":
|
||||||
return "<?php \$picea->compiler->getExtensionFromToken('$token')->currentLanguage = $arguments; ?>";
|
$cls = $this::class;
|
||||||
|
|
||||||
|
return <<<PHP
|
||||||
|
<?php
|
||||||
|
(function(\\$cls \$ext) use (\$___class__template, \$__event) {
|
||||||
|
#\$extends = \$___class__template->getParam('extends');
|
||||||
|
|
||||||
|
if ( \$___class__template->depth === 1 || \$___class__template->sectionStack ) {
|
||||||
|
\$ext->currentLanguage = $arguments;
|
||||||
|
|
||||||
|
# @TODO Make sure this event is only registered when we output() a template, if we are in a section, we must attach it a view/section/block output event instead !
|
||||||
|
\$__event->eventRegister(\\$cls::outputDoneEvent(\$ext));
|
||||||
|
}
|
||||||
|
})(\$picea->compiler->getExtensionFromToken('$token'));
|
||||||
|
?>
|
||||||
|
PHP;
|
||||||
|
|
||||||
case "lang":
|
case "lang":
|
||||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>";
|
if ( in_array('raw', $options) ) {
|
||||||
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>";
|
||||||
|
|
||||||
case "_":
|
case "_":
|
||||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>";
|
if ( in_array('raw', $options) ) {
|
||||||
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
@@ -35,8 +64,6 @@ class LanguageExtension implements Extension {
|
|||||||
|
|
||||||
public function register(Context $context) : void
|
public function register(Context $context) : void
|
||||||
{
|
{
|
||||||
$context->pushFunction("_", [ $this, 'relativeLang' ]);
|
|
||||||
$context->pushFunction("lang", [ $this, 'absoluteLang' ]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function relativeLang(string $key, array $variables = []) #: array|string
|
public function relativeLang(string $key, array $variables = []) #: array|string
|
||||||
@@ -48,4 +75,21 @@ class LanguageExtension implements Extension {
|
|||||||
{
|
{
|
||||||
return $this->languageHandler->languageFromKey($key, $variables);
|
return $this->languageHandler->languageFromKey($key, $variables);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function outputDoneEvent(LanguageExtension $languageExtension) : ClassTemplateRenderSectionDone
|
||||||
|
{
|
||||||
|
return new class($languageExtension) implements ClassTemplateRenderSectionDone {
|
||||||
|
|
||||||
|
protected string $current;
|
||||||
|
|
||||||
|
public function __construct(protected LanguageExtension $languageExtension) {
|
||||||
|
$this->current = $this->languageExtension->currentLanguage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute(string $name) : mixed
|
||||||
|
{
|
||||||
|
return $this->current ? $this->languageExtension->currentLanguage = $this->current : null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Picea\Extension;
|
namespace Picea\Extension;
|
||||||
|
|
||||||
interface LanguageHandler
|
interface LanguageHandlerInterface
|
||||||
{
|
{
|
||||||
public function languageFromKey(string $key, array $variables = []); #: array|string;
|
public function languageFromKey(string $key, array $variables = []); #: array|string;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Extension;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
|
class NumberExtension implements Extension, FunctionExtension {
|
||||||
|
|
||||||
|
public string $locale;
|
||||||
|
|
||||||
|
public \NumberFormatter $formatter;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public string|array $token = [ "money", "format", "format_zero" ],
|
||||||
|
public string $title = "",
|
||||||
|
) {
|
||||||
|
$this->locale = explode('.', \Locale::getDefault())[0];
|
||||||
|
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exportFunctions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
"money" => [ $this, 'money' ],
|
||||||
|
"format" => [ $this, 'format' ],
|
||||||
|
"format_zero" => [ $this, 'formatZero' ],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
|
switch($token) {
|
||||||
|
case "format":
|
||||||
|
return "<?php echo format($arguments) ?>";
|
||||||
|
|
||||||
|
case "format_zero":
|
||||||
|
return "<?php echo format_zero($arguments) ?>";
|
||||||
|
|
||||||
|
case "money":
|
||||||
|
return "<?php echo money($arguments) ?>";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function format(int|float $num, int $decimals = 0, null|string $decimalSeparator = ".", null|string $thousandsSeparator = ",") : string
|
||||||
|
{
|
||||||
|
return number_format($num, $decimals, $decimalSeparator, $thousandsSeparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function formatZero(int $num, int $length) : string
|
||||||
|
{
|
||||||
|
return sprintf("%0{$length}d", $num);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function money(float $money, ? string $currency = null) /* : string|false */
|
||||||
|
{
|
||||||
|
$this->formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'CAD');
|
||||||
|
|
||||||
|
$this->formatter->setPattern( str_replace('¤#','¤ #', $this->formatter->getPattern() ) );
|
||||||
|
|
||||||
|
return $this->formatter->formatCurrency($money, $currency ?? $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,7 +6,8 @@ class PhpExtension implements Extension {
|
|||||||
|
|
||||||
public array $token = [ "php" ];
|
public array $token = [ "php" ];
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
return "<?php $arguments ?>";
|
return "<?php $arguments ?>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Extension;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
|
class PrintExtension implements Extension {
|
||||||
|
|
||||||
|
public array $token = [ "echo", "print", "printf", "no_html" ];
|
||||||
|
|
||||||
|
public int $flag = \ENT_QUOTES | \ENT_DISALLOWED | \ENT_SUBSTITUTE;
|
||||||
|
|
||||||
|
public string $encoding;
|
||||||
|
|
||||||
|
public bool $doubleEncode = true;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->encoding = ini_get("default_charset");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
|
{
|
||||||
|
switch($token) {
|
||||||
|
case 'echo':
|
||||||
|
case 'print':
|
||||||
|
if (in_array('raw', $options)) {
|
||||||
|
return "<?php echo $arguments ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
||||||
|
|
||||||
|
case 'printf':
|
||||||
|
if (in_array('raw', $options)) {
|
||||||
|
return "<?php printf((string) $arguments) ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
||||||
|
|
||||||
|
case 'no_html':
|
||||||
|
return "<?php echo nl2br(strip_tags((string) $arguments)) ?>";
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,28 +2,28 @@
|
|||||||
|
|
||||||
namespace Picea\Extension;
|
namespace Picea\Extension;
|
||||||
|
|
||||||
use Picea\Compiler\Context;
|
class TitleExtension implements Extension, FunctionExtension {
|
||||||
|
|
||||||
class TitleExtension implements Extension {
|
|
||||||
|
|
||||||
public string $token = "title";
|
public string $token = "title";
|
||||||
|
|
||||||
public string $title = "";
|
public string $title = "";
|
||||||
|
|
||||||
public function __construct(Context $context) {
|
public function exportFunctions(): array
|
||||||
$this->register($context);
|
{
|
||||||
|
return [
|
||||||
|
"title" => [$this, 'handleTitle'],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register(Context $context) : void
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
{
|
{
|
||||||
$context->pushFunction("title", [ $this, 'handleTitle' ]);
|
return "<?php ".
|
||||||
|
"if ( null !== \$title = title($arguments) ) {".
|
||||||
|
"echo \$title;".
|
||||||
|
"} ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public function handleTitle(null|\Stringable|string $set = null, ...$arguments) : ? string
|
||||||
return "<?php echo title($arguments) ?>";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function handleTitle(? string $set = null) : ? string
|
|
||||||
{
|
{
|
||||||
if ( $set === null ) {
|
if ( $set === null ) {
|
||||||
return $this->title;
|
return $this->title;
|
||||||
@@ -31,7 +31,8 @@ class TitleExtension implements Extension {
|
|||||||
else {
|
else {
|
||||||
# Fixed a bug where template inheritance was rewriting title
|
# Fixed a bug where template inheritance was rewriting title
|
||||||
if ( empty($this->title) ) {
|
if ( empty($this->title) ) {
|
||||||
$this->title = $set;
|
$set = (string) $set;
|
||||||
|
$this->title = $arguments ? sprintf($set, ...$arguments) : $set;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+171
-61
@@ -2,27 +2,39 @@
|
|||||||
|
|
||||||
namespace Picea\Extension;
|
namespace Picea\Extension;
|
||||||
|
|
||||||
use Picea\Compiler\Context;
|
use Picea\EventTrait;
|
||||||
|
use Picea\Event\Extension\{ UrlBuildAssetEvent, UrlBuildUrlEvent, UrlBuildRouteEvent, UrlRegisterRouteEvent };
|
||||||
|
|
||||||
class UrlExtension implements Extension {
|
class UrlExtension implements Extension, FunctionExtension {
|
||||||
|
use EventTrait;
|
||||||
|
|
||||||
protected string $urlBase;
|
public const URLIZE_PATTERN_URL = <<<PATTERN
|
||||||
|
~(?<!href=['"])https?://[\w/._\-&?]*(?!</a>)(?=[^\w/._\-&])~s
|
||||||
|
PATTERN;
|
||||||
|
|
||||||
protected string $assetToken;
|
public const URLIZE_PATTERN_EMAIL = <<<PATTERN
|
||||||
|
/(?:[a-z0-9!#$%&'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+\\/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])/
|
||||||
|
PATTERN;
|
||||||
|
|
||||||
protected array $routes;
|
protected array $routes;
|
||||||
|
|
||||||
protected array $routesTarget;
|
protected array $routesTarget;
|
||||||
|
|
||||||
public array $tokens = [ "url" , "route", "asset", "url.parameters" ];
|
public array $tokens = [ "url" , "route", "asset", "slug" ];
|
||||||
|
|
||||||
public function __construct(Context $context, string $urlBase = "", string $assetToken = "") {
|
|
||||||
$this->urlBase = trim($urlBase, "/");
|
|
||||||
$this->assetToken = $assetToken;
|
|
||||||
$this->register($context);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : ?string
|
##[\Deprecated]
|
||||||
|
public bool $enforceExistingArguments = true;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected string $urlBase = "",
|
||||||
|
protected string $assetToken = "",
|
||||||
|
protected array $appUrl = [],
|
||||||
|
protected bool $forceSSL = false
|
||||||
|
) {
|
||||||
|
$this->urlBase = trim($urlBase, "/");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
{
|
{
|
||||||
switch($token) {
|
switch($token) {
|
||||||
case "asset":
|
case "asset":
|
||||||
@@ -32,20 +44,32 @@ class UrlExtension implements Extension {
|
|||||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>";
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>";
|
||||||
|
|
||||||
case "url":
|
case "url":
|
||||||
|
if ( in_array('parameters', $options) ) {
|
||||||
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>";
|
||||||
|
}
|
||||||
|
elseif ( in_array('current', $options) ) {
|
||||||
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>";
|
||||||
|
}
|
||||||
|
|
||||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>";
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>";
|
||||||
|
|
||||||
case "url.parameters":
|
case "slug":
|
||||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>";
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
throw new \Exception("Unknown token given $token in UrlExtension.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register(Context $context) : void
|
public function exportFunctions(): array
|
||||||
{
|
{
|
||||||
$context->pushFunction("url", [ $this, 'buildUrl' ]);
|
return [
|
||||||
$context->pushFunction("asset", [ $this, 'buildAssetUrl' ]);
|
"url" => [ $this, 'buildUrl' ],
|
||||||
$context->pushFunction("route", [ $this, 'buildRouteUrl' ]);
|
"current_url" => [ $this, 'currentUrl' ],
|
||||||
|
"asset" => [ $this, 'buildAssetUrl' ],
|
||||||
|
"route" => [ $this, 'buildRouteUrl' ],
|
||||||
|
"slug" => [ $this, 'slug' ],
|
||||||
|
"urlize" => [ $this, 'urlize' ]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRouteList(bool $full = false) : array
|
public function getRouteList(bool $full = false) : array
|
||||||
@@ -58,20 +82,40 @@ class UrlExtension implements Extension {
|
|||||||
return $url . ( $parameters ? "?" . http_build_query($parameters) : "" );
|
return $url . ( $parameters ? "?" . http_build_query($parameters) : "" );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildUrl(string $uri = "", array $parameters = []) : string
|
public function urlize(string $string) : string
|
||||||
{
|
{
|
||||||
return $this->setUrlParameters($this->url() . "/" . ltrim($uri, "/"), $parameters);
|
# Normal URL patterns
|
||||||
}
|
$string = preg_replace(static::URLIZE_PATTERN_URL, '<a href="$0" target="_blank" title="$0">$0</a>', $string);
|
||||||
|
|
||||||
public function buildAssetUrl(string $uri, array $parameters = []) : string
|
# Email patterns
|
||||||
{
|
$string = preg_replace(static::URLIZE_PATTERN_EMAIL, '<a href="mailto:$0" title="$0">$0</a>', $string);
|
||||||
return $this->buildUrl($uri, array_replace([ 'v' => $this->assetToken ], $parameters));
|
|
||||||
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildRouteUrl(string $name, array $parameters = []) : string
|
public function currentUrl(array $parameters = []) : string
|
||||||
{
|
{
|
||||||
if ( false !== ( $route = $this->routes[$name] ?? false ) ) {
|
return $this->buildUrl($this->uri(), $parameters);
|
||||||
return $this->buildUrl($this->prepareRoute($route['route'], $parameters), $parameters);
|
}
|
||||||
|
|
||||||
|
public function buildUrl(string $uri = "", array $parameters = [], bool $appendVersion = false) : string
|
||||||
|
{
|
||||||
|
$this->eventExecute(UrlBuildUrlEvent::class, $uri, $parameters, $appendVersion);
|
||||||
|
|
||||||
|
return $this->setUrlParameters($this->url() . "/" . ltrim($uri, "/"), $appendVersion ? array_replace([ 'v' => $this->assetToken ], $parameters) : $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildAssetUrl(string $uri, array $parameters = [], bool $appendVersion = true) : string
|
||||||
|
{
|
||||||
|
$this->eventExecute(UrlBuildAssetEvent::class, $uri, $parameters, $appendVersion);
|
||||||
|
|
||||||
|
return $this->buildUrl($uri, $parameters, $appendVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildRouteUrl(string $name, array $parameters = [], bool $appendVersion = false) : string
|
||||||
|
{
|
||||||
|
if ( false !== $route = $this->findRoute($name) ) {
|
||||||
|
return $this->buildUrl($this->prepareRoute($route, $parameters), $parameters, $appendVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
$routeList = json_encode($this->routes, \JSON_PRETTY_PRINT);
|
$routeList = json_encode($this->routes, \JSON_PRETTY_PRINT);
|
||||||
@@ -87,14 +131,40 @@ class UrlExtension implements Extension {
|
|||||||
return $this->scheme() . $this->domain() . $this->base();
|
return $this->scheme() . $this->domain() . $this->base();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# src: https://stackoverflow.com/a/14550919
|
||||||
|
public static function slug(\Stringable|string $text, string $separator = '-') : string
|
||||||
|
{
|
||||||
|
$clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', (string) $text);
|
||||||
|
$clean = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $clean);
|
||||||
|
|
||||||
|
return preg_replace("/[\/_| -]+/", $separator, strtolower(trim($clean, '-')));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function registerRoute(string $name, string $route, string $class, string $method, array $routeMethods) : void
|
public function registerRoute(string $name, string $route, string $class, string $method, array $routeMethods) : void
|
||||||
{
|
{
|
||||||
$this->routes[$name] = [
|
$this->routes[] = $array = [
|
||||||
|
'name' => $name,
|
||||||
'route' => $route,
|
'route' => $route,
|
||||||
'routeMethods' => $routeMethods,
|
'routeMethods' => array_map('strtoupper', $routeMethods),
|
||||||
'class' => $class,
|
'class' => $class,
|
||||||
'classMethod' => $method,
|
'classMethod' => $method,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$this->eventExecute(UrlRegisterRouteEvent::class, $name, $array);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function findRoute(string $name, string $method = "*") : false|array
|
||||||
|
{
|
||||||
|
foreach($this->routes as $route) {
|
||||||
|
if ( $route['name'] === $name ) {
|
||||||
|
if ($method === '*' || in_array(strtoupper($method), $route['routeMethods']) ) {
|
||||||
|
return $route;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,11 +178,7 @@ class UrlExtension implements Extension {
|
|||||||
$uri = explode('?', $uri, 2)[0];
|
$uri = explode('?', $uri, 2)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ($base = $this->config['base'] ?? false) && ( stripos($uri, $base) === 0 ) ) {
|
return '/' . ltrim(substr($uri, strlen($this->base())), '/');
|
||||||
$uri = substr($uri, strlen($base));
|
|
||||||
}
|
|
||||||
|
|
||||||
return '/' . ltrim($uri, '/');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -125,7 +191,7 @@ class UrlExtension implements Extension {
|
|||||||
|
|
||||||
protected function scheme() : string
|
protected function scheme() : string
|
||||||
{
|
{
|
||||||
return ( $this->isHttps() ? "https" : "http" ) . "://";
|
return ( $this->forceSSL || $this->isHttps() ? "https" : "http" ) . "://";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function base() : string
|
protected function base() : string
|
||||||
@@ -144,7 +210,31 @@ class UrlExtension implements Extension {
|
|||||||
|
|
||||||
protected function domain() : string
|
protected function domain() : string
|
||||||
{
|
{
|
||||||
return strtolower($_SERVER['HTTP_HOST']);
|
# Forwarded from an SSL connected host
|
||||||
|
if ( ! empty($_SERVER['HTTP_X_FORWARDED_PROTO']) || ! empty($_SERVER['HTTP_X_FORWARDED_SSL']) ) {
|
||||||
|
$port = "";
|
||||||
|
}
|
||||||
|
# Forwarded from another host
|
||||||
|
elseif ( !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
|
||||||
|
$port = explode(':', $_SERVER['HTTP_X_FORWARDED_HOST'])[1] ?? "";
|
||||||
|
}
|
||||||
|
# Default
|
||||||
|
else {
|
||||||
|
$port = $this->isDefaultPort() ? "" : ":" . $_SERVER['SERVER_PORT'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return strtolower($this->appUrl($port) ?: $_SERVER['HTTP_HOST']);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function appUrl(string $port) : string
|
||||||
|
{
|
||||||
|
$domain = strtolower($_SERVER['HTTP_HOST']);
|
||||||
|
|
||||||
|
if (in_array($domain, $this->appUrl)) {
|
||||||
|
return $domain . $port;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->appUrl[0] . $port;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isDefaultPort() : bool
|
protected function isDefaultPort() : bool
|
||||||
@@ -154,52 +244,72 @@ class UrlExtension implements Extension {
|
|||||||
|
|
||||||
protected function isHttps() : bool
|
protected function isHttps() : bool
|
||||||
{
|
{
|
||||||
$header = in_array('https', array_map("strtolower", [
|
$https = in_array('https', array_map("strtolower", [
|
||||||
|
$_SERVER['REQUEST_SCHEME'] ?? "",
|
||||||
$_SERVER['X-Url-Scheme'] ?? "",
|
$_SERVER['X-Url-Scheme'] ?? "",
|
||||||
$_SERVER['Front-End-Https'] ?? "",
|
$_SERVER['Front-End-Https'] ?? "",
|
||||||
$_SERVER['X-Forwarded-Ssl'] ?? "",
|
|
||||||
$_SERVER['X-Forwarded-Proto'] ?? "",
|
$_SERVER['X-Forwarded-Proto'] ?? "",
|
||||||
$_SERVER['X-Forwarded-Protocol'] ?? "",
|
$_SERVER['X-Forwarded-Protocol'] ?? "",
|
||||||
]));
|
$_SERVER['HTTP_X_FORWARDED_PROTO'] ?? "",
|
||||||
|
$_SERVER['HTTP_X_FORWARDED_PROTOCOL'] ?? "",
|
||||||
|
])) || isset($_SERVER['HTTP_X_ARR_SSL']);
|
||||||
|
|
||||||
return $header
|
return $https
|
||||||
|| ( "443" === ( $_SERVER['SERVER_PORT'] ?? "" ) )
|
|| ( "443" === ( $_SERVER['SERVER_PORT'] ?? "" ) ) || ( "443" === ( $_SERVER['HTTP_X_FORWARDED_PORT'] ?? "" ) )
|
||||||
|| ( "https" === ( $_SERVER['REQUEST_SCHEME'] ?? "http") )
|
|| ( "off" !== ( strtolower($_SERVER['HTTPS'] ?? $_SERVER['HTTP_X_FORWARDED_SSL'] ?? $_SERVER['X-Forwarded-Ssl'] ?? "off")) );
|
||||||
|| ( "off" !== ( strtolower($_SERVER['HTTPS'] ?? "off")) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function prepareRoute(string $route, array &$arguments)
|
protected function prepareRoute(array $routeParam, array &$arguments)
|
||||||
{
|
{
|
||||||
|
$route = $routeParam['route'];
|
||||||
|
|
||||||
if ( preg_match_all('~{(.*?)}~si', $route, $matches, PREG_SET_ORDER) ) {
|
if ( preg_match_all('~{(.*?)}~si', $route, $matches, PREG_SET_ORDER) ) {
|
||||||
$search = [];
|
$search = [];
|
||||||
|
|
||||||
foreach($matches as $item) {
|
foreach($matches as $item) {
|
||||||
if (strpos($item[1], "=") !== false) {
|
$default = null;
|
||||||
|
|
||||||
|
$variable = $item[1];
|
||||||
|
|
||||||
|
# Handles default
|
||||||
|
if (strpos($variable, "=") !== false) {
|
||||||
list($variable, $default) = explode('=', $item[1]);
|
list($variable, $default) = explode('=', $item[1]);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
$variable = $item[1];
|
# Handles type
|
||||||
|
if (strpos($variable, ":") !== false) {
|
||||||
|
list($variable, ) = explode(':', $item[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( array_key_exists($variable, $arguments) ) {
|
if ( array_key_exists($variable, $arguments) ) {
|
||||||
$value = $arguments[ $item[1] ];
|
$value = $arguments[ $variable ];
|
||||||
}
|
unset($arguments[ $variable ]);
|
||||||
else if ( isset($default) ) {
|
|
||||||
$value = ""; # $default;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$variable = $item[1];
|
if ($default ?? false) {
|
||||||
|
$value = $default;
|
||||||
|
}
|
||||||
|
elseif ( $this->enforceExistingArguments && ! preg_match(sprintf("/\[\/?%s]/i", $item[0]), $route) ) {
|
||||||
|
throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s ) from %s::%s", $route, $variable, json_encode($arguments), $routeParam['class'], $routeParam['classMethod']));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$search[ $item[0] ] = $value;
|
$search[$item[0]] = $value ?? "";
|
||||||
|
|
||||||
unset($arguments[ $item[1] ]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$route = str_replace(array_keys($search), array_values($search), $route);
|
$route = str_replace(array_keys($search), array_values($search), $route);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $route;
|
/*
|
||||||
|
* @TODO - must also take into account that recursivity is possible here [/test[/another[/still]]],
|
||||||
|
* so the regex must be adjusted (or simply using another method could also be a possiblity)
|
||||||
|
* while(strpos($route, '[') !== false && strpos($route, ']') !== false ) {
|
||||||
|
if ( preg_match_all('~[(.*?)]~si', $route, $matches, PREG_SET_ORDER) ) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
$route = trim(str_replace([ '[', ']' ], '', $route), '/');
|
||||||
|
|
||||||
|
return $route;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+55
-6
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Picea;
|
namespace Picea;
|
||||||
|
|
||||||
|
use RecursiveIteratorIterator, RecursiveDirectoryIterator;
|
||||||
|
|
||||||
class FileFetcher
|
class FileFetcher
|
||||||
{
|
{
|
||||||
protected array $folderList = [];
|
protected array $folderList = [];
|
||||||
@@ -12,13 +14,13 @@ class FileFetcher
|
|||||||
|
|
||||||
public function __construct(?array $folderList = null) {
|
public function __construct(?array $folderList = null) {
|
||||||
if ( $folderList !== null ) {
|
if ( $folderList !== null ) {
|
||||||
$this->folderList = $folderList;
|
$this->addFolders($folderList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addFolder(string $folder, int $order = 100) : void
|
public function addFolder(string $folder, int $order = 100) : void
|
||||||
{
|
{
|
||||||
$folder = rtrim($folder, DIRECTORY_SEPARATOR);
|
$folder = $this->normalizeFolder($folder);
|
||||||
|
|
||||||
$this->folderList[$folder] = [
|
$this->folderList[$folder] = [
|
||||||
'path' => $folder,
|
'path' => $folder,
|
||||||
@@ -28,7 +30,14 @@ class FileFetcher
|
|||||||
|
|
||||||
public function addFolders(array $folderList) : void
|
public function addFolders(array $folderList) : void
|
||||||
{
|
{
|
||||||
$this->folderList = array_replace($this->folderList, $folderList);
|
foreach($folderList as $folder) {
|
||||||
|
$this->addFolder($folder['path'], $folder['order']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function normalizeFolder(string $path) : string
|
||||||
|
{
|
||||||
|
return rtrim($path, '/\\');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function folderList(?array $set = null) : ?array
|
public function folderList(?array $set = null) : ?array
|
||||||
@@ -40,21 +49,31 @@ class FileFetcher
|
|||||||
{
|
{
|
||||||
usort($this->folderList, fn($a, $b) => $a['order'] <=> $b['order']);
|
usort($this->folderList, fn($a, $b) => $a['order'] <=> $b['order']);
|
||||||
|
|
||||||
foreach($this->folderList as $folder) {
|
foreach($this->folderList() as $folder) {
|
||||||
foreach($this->supportedExtensionList as $extension) {
|
foreach($this->supportedExtensionList as $extension) {
|
||||||
$file = $folder['path'] . DIRECTORY_SEPARATOR . "$fileName.$extension";
|
$file = $folder['path'] . DIRECTORY_SEPARATOR . "$fileName.$extension";
|
||||||
|
$file = str_replace([ '\\', '/' ], DIRECTORY_SEPARATOR, $file);
|
||||||
|
|
||||||
if ( file_exists($file) ) {
|
if ( file_exists($file) ) {
|
||||||
|
if ( is_dir($file) ) {
|
||||||
|
throw new \RuntimeException("Given view file `$fileName` is a folder, you must provide a valid filename.");
|
||||||
|
}
|
||||||
|
|
||||||
return $file;
|
return $file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Fallback on full-path
|
# Fallback on full-path
|
||||||
foreach($this->folderList as $folder) {
|
foreach($this->folderList() as $folder) {
|
||||||
$file = $folder['path'] . DIRECTORY_SEPARATOR . $fileName;
|
$file = $folder['path'] . DIRECTORY_SEPARATOR . $fileName;
|
||||||
|
$file = str_replace([ '\\', '/' ], DIRECTORY_SEPARATOR, $file);
|
||||||
|
|
||||||
if ( file_exists($file) ) {
|
if ( file_exists($file) ) {
|
||||||
|
if ( is_dir($file) ) {
|
||||||
|
throw new \RuntimeException("Given view file `$fileName` is a folder, you must provide a valid filename.");
|
||||||
|
}
|
||||||
|
|
||||||
return $file;
|
return $file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,8 +81,38 @@ class FileFetcher
|
|||||||
throw new \RuntimeException("Given view file `$fileName` can not be found within given folder list..");
|
throw new \RuntimeException("Given view file `$fileName` can not be found within given folder list..");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getFileList() : array
|
||||||
|
{
|
||||||
|
usort($this->folderList, fn($a, $b) => $a['order'] <=> $b['order']);
|
||||||
|
|
||||||
|
$list = [];
|
||||||
|
|
||||||
|
foreach($this->folderList() as $folder) {
|
||||||
|
$path = $folder['path'] . "/";
|
||||||
|
$list[$path] = [];
|
||||||
|
|
||||||
|
if ( \file_exists($path) ) {
|
||||||
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD);
|
||||||
|
|
||||||
|
foreach ($iterator as $file) {
|
||||||
|
if ($file->isFile() && in_array($file->getExtension(), $this->supportedExtensionList)) {
|
||||||
|
$list[$path][] = $file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFilePath(string $fileName) : string
|
||||||
|
{
|
||||||
|
return $this->findFile($fileName);
|
||||||
|
}
|
||||||
|
|
||||||
public function getFileContent(string $fileName) : string
|
public function getFileContent(string $fileName) : string
|
||||||
{
|
{
|
||||||
return file_get_contents($this->findFile($fileName));
|
return file_get_contents($this->getFilePath($fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,25 +4,13 @@ namespace Picea\Language;
|
|||||||
|
|
||||||
use Picea\Compiler;
|
use Picea\Compiler;
|
||||||
|
|
||||||
use Picea\Compiler\Context;
|
|
||||||
|
|
||||||
class DefaultRegistrations implements LanguageRegistration
|
class DefaultRegistrations implements LanguageRegistration
|
||||||
{
|
{
|
||||||
protected array $extensions;
|
public function __construct(
|
||||||
|
protected array $extensions,
|
||||||
protected array $syntaxes;
|
protected array $syntaxes,
|
||||||
|
protected array $controlStructures,
|
||||||
protected array $controlStructures;
|
) { }
|
||||||
|
|
||||||
protected ? Context $context;
|
|
||||||
|
|
||||||
public function __construct(? Context $context = null, array $extensions = [], array $syntaxes = [], array $controlStructure = [])
|
|
||||||
{
|
|
||||||
$this->context = $context;
|
|
||||||
$this->extensions = $extensions;
|
|
||||||
$this->syntaxes = $syntaxes;
|
|
||||||
$this->controlStructures = $controlStructure;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function registerAll(Compiler $compiler) : void
|
public function registerAll(Compiler $compiler) : void
|
||||||
{
|
{
|
||||||
@@ -33,7 +21,6 @@ class DefaultRegistrations implements LanguageRegistration
|
|||||||
|
|
||||||
public function registerSyntax(Compiler $compiler) : void
|
public function registerSyntax(Compiler $compiler) : void
|
||||||
{
|
{
|
||||||
$compiler->registerSyntax(new \Picea\Syntax\PhpTagToken());
|
|
||||||
$compiler->registerSyntax(new \Picea\Syntax\CommentToken());
|
$compiler->registerSyntax(new \Picea\Syntax\CommentToken());
|
||||||
$compiler->registerSyntax(new \Picea\Syntax\EchoRawToken());
|
$compiler->registerSyntax(new \Picea\Syntax\EchoRawToken());
|
||||||
$compiler->registerSyntax(new \Picea\Syntax\EchoSafeToken());
|
$compiler->registerSyntax(new \Picea\Syntax\EchoSafeToken());
|
||||||
@@ -45,17 +32,21 @@ class DefaultRegistrations implements LanguageRegistration
|
|||||||
|
|
||||||
public function registerControlStructure(Compiler $compiler) : void
|
public function registerControlStructure(Compiler $compiler) : void
|
||||||
{
|
{
|
||||||
|
$compiler->registerControlStructure(new \Picea\ControlStructure\TryCatchToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\NamespaceToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\NamespaceToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\UseToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\UseToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\IfToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\IfToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\ForeachToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\ForeachToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\ForToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\ForToken());
|
||||||
|
$compiler->registerControlStructure(new \Picea\ControlStructure\WhileToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\OrToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\OrToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\SwitchToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\SwitchToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\DefaultToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\DefaultToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\BreakToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\BreakToken());
|
||||||
|
$compiler->registerControlStructure(new \Picea\ControlStructure\ContinueToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\ExtendsToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\ExtendsToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\SectionToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\SectionToken());
|
||||||
|
$compiler->registerControlStructure(new \Picea\ControlStructure\FunctionToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\BlockToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\BlockToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\IncludeToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\IncludeToken());
|
||||||
$compiler->registerControlStructure(new \Picea\ControlStructure\ViewToken());
|
$compiler->registerControlStructure(new \Picea\ControlStructure\ViewToken());
|
||||||
@@ -68,8 +59,10 @@ class DefaultRegistrations implements LanguageRegistration
|
|||||||
public function registerExtension(Compiler $compiler) : void
|
public function registerExtension(Compiler $compiler) : void
|
||||||
{
|
{
|
||||||
$compiler->registerExtension(new \Picea\Extension\PhpExtension());
|
$compiler->registerExtension(new \Picea\Extension\PhpExtension());
|
||||||
$compiler->registerExtension(new \Picea\Extension\JsonExtension($this->context));
|
$compiler->registerExtension(new \Picea\Extension\PrintExtension());
|
||||||
|
$compiler->registerExtension(new \Picea\Extension\JsonExtension());
|
||||||
|
$compiler->registerExtension(new \Picea\Extension\BatchExtension());
|
||||||
|
|
||||||
foreach($this->extensions ?? [] as $extension) {
|
foreach($this->extensions ?? [] as $extension) {
|
||||||
$compiler->registerExtension($extension);
|
$compiler->registerExtension($extension);
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-19
@@ -5,35 +5,35 @@ namespace Picea\Method;
|
|||||||
use Picea\Extension\Extension,
|
use Picea\Extension\Extension,
|
||||||
Picea\Extension\ExtensionTrait;
|
Picea\Extension\ExtensionTrait;
|
||||||
|
|
||||||
use Picea\Compiler\Context;
|
use Picea\Extension\FunctionExtension;
|
||||||
|
use Psr\Http\Message\RequestInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
class Request implements Extension {
|
class
|
||||||
|
Request implements Extension, FunctionExtension {
|
||||||
use ExtensionTrait;
|
use ExtensionTrait;
|
||||||
|
|
||||||
public array $tokens;
|
public array $tokens;
|
||||||
|
|
||||||
public string $token;
|
public string $token;
|
||||||
|
|
||||||
public ServerRequestInterface $request;
|
|
||||||
|
|
||||||
public function __construct(ServerRequestInterface $request, Context $context) {
|
public function __construct(
|
||||||
$this->request = $request;
|
public ServerRequestInterface $request
|
||||||
$this->register($context);
|
) { }
|
||||||
}
|
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { }
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { }
|
||||||
|
|
||||||
public function register(Context $context) : void
|
public function exportFunctions(): array
|
||||||
{
|
{
|
||||||
$context->pushFunction("cookie", [ $this, 'cookie' ]);
|
return [
|
||||||
$context->pushFunction("get", [ $this, 'get' ]);
|
"cookie" => [ $this, 'cookie' ],
|
||||||
$context->pushFunction("post", [ $this, 'post' ]);
|
"get" => [ $this, 'get' ],
|
||||||
$context->pushFunction("request", [ $this, 'request' ]);
|
"post" => [ $this, 'post' ],
|
||||||
$context->pushFunction("server", [ $this, 'server' ]);
|
"request" => [ $this, 'request' ],
|
||||||
|
"server" => [ $this, 'server' ],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function cookie(? string $variableName = null, $default = null)
|
public function cookie(? string $variableName = null, $default = null)
|
||||||
{
|
{
|
||||||
return $variableName === null ? $this->request->getCookieParams() : static::arrayGet($this->request->getCookieParams(), $variableName) ?? $default;
|
return $variableName === null ? $this->request->getCookieParams() : static::arrayGet($this->request->getCookieParams(), $variableName) ?? $default;
|
||||||
@@ -46,12 +46,12 @@ class Request implements Extension {
|
|||||||
|
|
||||||
public function post(? string $variableName = null, $default = null)
|
public function post(? string $variableName = null, $default = null)
|
||||||
{
|
{
|
||||||
return $variableName === null ? $this->request->getParsedBody() : static::arrayGet($this->request->getParsedBody(), $variableName) ?? $default;
|
return $variableName === null ? $this->_post() : static::arrayGet($this->_post(), $variableName) ?? $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function request(? string $variableName = null, $default = null)
|
public function request(? string $variableName = null, $default = null)
|
||||||
{
|
{
|
||||||
return $variableName === null ? array_merge(get(null), post(null)) : $this->post($variableName) ?? $this->get($variableName) ?? $default;
|
return $variableName === null ? array_merge($this->get(), $this->post()) : $this->post($variableName) ?? $this->get($variableName) ?? $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function server(? string $variableName = null, $default = null)
|
public function server(? string $variableName = null, $default = null)
|
||||||
@@ -75,4 +75,19 @@ class Request implements Extension {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function _post() : array
|
||||||
|
{
|
||||||
|
$post = $this->request->getParsedBody();
|
||||||
|
|
||||||
|
if ( ! $post ) {
|
||||||
|
$content = mb_convert_encoding((string)$this->request->getBody(), 'UTF-8', 'ISO-8859-1');
|
||||||
|
|
||||||
|
if ( $content && ( $json = json_decode($content, true) ) ) {
|
||||||
|
$post = $json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $post ?: [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+66
-41
@@ -3,7 +3,9 @@
|
|||||||
namespace Picea;
|
namespace Picea;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Picea\Language\LanguageRegistration;
|
use Picea\Language\LanguageRegistration,
|
||||||
|
Picea\Exception\RenderingError;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
class Picea implements LanguageRegistration
|
class Picea implements LanguageRegistration
|
||||||
@@ -18,16 +20,19 @@ class Picea implements LanguageRegistration
|
|||||||
|
|
||||||
public string $builderTemplatePath;
|
public string $builderTemplatePath;
|
||||||
|
|
||||||
public Closure $responseHtml;
|
|
||||||
|
|
||||||
public Caching\Cache $cache;
|
public Caching\Cache $cache;
|
||||||
|
|
||||||
public FileFetcher $fileFetcher;
|
public FileFetcher $fileFetcher;
|
||||||
|
|
||||||
public bool $debug;
|
public bool $debug;
|
||||||
|
|
||||||
|
public array $loadedTemplateFile = [];
|
||||||
|
|
||||||
|
public array $globalVariables = [];
|
||||||
|
|
||||||
|
public array $compiled = [];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
? Closure $responseHtml = null,
|
|
||||||
? Compiler\Context $context = null,
|
? Compiler\Context $context = null,
|
||||||
? Caching\Cache $cache = null,
|
? Caching\Cache $cache = null,
|
||||||
? Compiler $compiler = null,
|
? Compiler $compiler = null,
|
||||||
@@ -36,43 +41,71 @@ class Picea implements LanguageRegistration
|
|||||||
? string $builderTemplatePath = null,
|
? string $builderTemplatePath = null,
|
||||||
bool $debug = false
|
bool $debug = false
|
||||||
){
|
){
|
||||||
$this->response = $responseHtml;
|
|
||||||
$this->cache = $cache ?? new Caching\Memory("");
|
$this->cache = $cache ?? new Caching\Memory("");
|
||||||
$this->context = $context ?? new Compiler\BaseContext();
|
$this->context = $context ?? new Compiler\BaseContext();
|
||||||
$this->languageRegistration = $languageRegistration ?? new Language\DefaultRegistrations();
|
$this->languageRegistration = $languageRegistration ?? new Language\DefaultRegistrations([], [], []);
|
||||||
$this->builderTemplatePath = $builderTemplatePath ?? dirname(__FILE__) . static::DEFAULT_BUILDER_TEMPLATE;
|
$this->builderTemplatePath = $builderTemplatePath ?? dirname(__FILE__) . static::DEFAULT_BUILDER_TEMPLATE;
|
||||||
$this->compiler = $compiler ?? $this->instanciateCompiler();
|
$this->compiler = $compiler ?? $this->instanciateCompiler();
|
||||||
$this->fileFetcher = $fileFetcher ?? $this->instanciateFileFetcher();
|
$this->fileFetcher = $fileFetcher ?? $this->instanciateFileFetcher();
|
||||||
$this->debug = $debug;
|
$this->debug = $debug;
|
||||||
|
|
||||||
#$this->context->exportFunctions();
|
$this->compiler->registerExtensionsFunctions($this->context);
|
||||||
|
|
||||||
$this->renderContext($this->context);
|
$this->renderContext($this->context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderHtml(string $viewPath, array $variables = [], ?object $proxy = null) : string
|
public function gatherCompiledObject(string $viewPath, array $variables = [], ? object $proxy = null) : ? object
|
||||||
{
|
{
|
||||||
if ( null === $object = $this->fetchFromCache($viewPath, $variables, $proxy) ) {
|
if ( null === $object = $this->fetchFromCache($viewPath, $this->globalVariables + $variables, $proxy) ) {
|
||||||
throw new \RuntimeException("An error occured while trying to save a compiled template.");
|
throw new \RuntimeException("An error occured while trying to save a compiled template.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $object();
|
return $object;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method used by Block and View tokens
|
|
||||||
* @param object $proxy
|
|
||||||
* @param string $viewPath
|
|
||||||
* @param array $variables
|
|
||||||
* @return type
|
|
||||||
*/
|
|
||||||
public function inlineHtml(? object $proxy, string $viewPath, ... $variables) {
|
|
||||||
return $this->renderHtml($viewPath, [ 'inlineVariables' => $variables ], $proxy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function renderHtml(string $viewPath, array $variables = [], ?object $proxy = null) : ? string
|
||||||
|
{
|
||||||
|
$object = $this->gatherCompiledObject($viewPath, $variables, $proxy);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return call_user_func($object);
|
||||||
|
}
|
||||||
|
catch(\Throwable $ex) {
|
||||||
|
#if (! $ex instanceof Exception\RenderHtmlException ) {
|
||||||
|
# throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex);
|
||||||
|
#}
|
||||||
|
#else {
|
||||||
|
throw $ex;
|
||||||
|
#}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method used by Block and View tokens
|
||||||
|
*/
|
||||||
|
public function inlineHtml(? object $proxy, string $viewPath, array $variables) {
|
||||||
|
return $this->renderHtml($viewPath, $this->globalVariables + $variables, $proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows block to be called from templates
|
||||||
|
*/
|
||||||
|
public function inlineBlock(? object $proxy, string $viewPath, ... $variables) {
|
||||||
|
return $this->renderHtml($viewPath, [ 'inlineVariables' => $variables, 'globalVariables' => $this->globalVariables ], $proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push view content inline
|
||||||
|
*/
|
||||||
public function inlineContent(string $viewPath) {
|
public function inlineContent(string $viewPath) {
|
||||||
return $this->fileFetcher->getFileContent($viewPath);
|
return $this->fileFetcher->getFileContent($viewPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders a compile context
|
||||||
|
*/
|
||||||
public function renderContext(Compiler\Context $context) : object
|
public function renderContext(Compiler\Context $context) : object
|
||||||
{
|
{
|
||||||
if ( null === $object = $this->contextFromCache($context) ) {
|
if ( null === $object = $this->contextFromCache($context) ) {
|
||||||
@@ -82,22 +115,6 @@ class Picea implements LanguageRegistration
|
|||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#public function outputHtml(string $viewPath, array $variables) : ResponseInterface
|
|
||||||
#{
|
|
||||||
# if ( $this->response ?? false ) {
|
|
||||||
# if ( false === $content = $this->cache->handle($viewPath) ) {
|
|
||||||
#
|
|
||||||
# }
|
|
||||||
|
|
||||||
# $source = $this->compileSource($source ?? "test");
|
|
||||||
# $response = $this->response;
|
|
||||||
# return $response("abc");
|
|
||||||
# }
|
|
||||||
|
|
||||||
# throw new \InvalidArgumentException("No \Psr\Http\Message\ResponseInterface closure provided. Please provide one using the constructor or assigning it to the class variable `responseHtml`.");
|
|
||||||
#}
|
|
||||||
|
|
||||||
public function compileSource(string $source) : array
|
public function compileSource(string $source) : array
|
||||||
{
|
{
|
||||||
$this->compiler->loadSourceCode($source);
|
$this->compiler->loadSourceCode($source);
|
||||||
@@ -145,13 +162,15 @@ class Picea implements LanguageRegistration
|
|||||||
|
|
||||||
public function fetchFromCache(string $viewPath, array $variables = [], ?object $proxy = null) : ?object
|
public function fetchFromCache(string $viewPath, array $variables = [], ?object $proxy = null) : ?object
|
||||||
{
|
{
|
||||||
if ( ( true === $this->debug ) || (! $this->cache->compiled($viewPath))) {
|
if ( ! isset($this->compiled[$viewPath]) && ( true === $this->debug ) || (! $this->cache->compiled($viewPath))) {
|
||||||
$context = $this->compileView($viewPath);
|
$context = $this->compileView($viewPath);
|
||||||
$this->cache->save($context);
|
$this->cache->save($context);
|
||||||
|
|
||||||
if ( $context->extendFrom ) {
|
if ( $context->extendFrom ) {
|
||||||
$this->fetchFromCache($context->extendFrom, $variables, $proxy);
|
$this->fetchFromCache($context->extendFrom, $variables, $proxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->compiled[$viewPath] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->cache->load($viewPath, $this, $variables, $proxy);
|
return $this->cache->load($viewPath, $this, $variables, $proxy);
|
||||||
@@ -161,6 +180,7 @@ class Picea implements LanguageRegistration
|
|||||||
{
|
{
|
||||||
if ( $this->debug || ! $this->cache->compiled( $context->cacheFilename() ) ) {
|
if ( $this->debug || ! $this->cache->compiled( $context->cacheFilename() ) ) {
|
||||||
$context = $this->compileContext($context);
|
$context = $this->compileContext($context);
|
||||||
|
|
||||||
$this->cache->save($context);
|
$this->cache->save($context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,6 +224,11 @@ class Picea implements LanguageRegistration
|
|||||||
$this->languageRegistration->registerExtension($compiler);
|
$this->languageRegistration->registerExtension($compiler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function declareLoadedTemplate(string $className, string $filePath) : void
|
||||||
|
{
|
||||||
|
$this->loadedTemplateFile[$className] = $filePath;
|
||||||
|
}
|
||||||
|
|
||||||
protected function compileView(string $viewPath) : Compiler\Context
|
protected function compileView(string $viewPath) : Compiler\Context
|
||||||
{
|
{
|
||||||
$tmpFolder = sys_get_temp_dir();
|
$tmpFolder = sys_get_temp_dir();
|
||||||
@@ -211,9 +236,9 @@ class Picea implements LanguageRegistration
|
|||||||
$compiled = $this->compileSource($this->fileFetcher->getFileContent($viewPath));
|
$compiled = $this->compileSource($this->fileFetcher->getFileContent($viewPath));
|
||||||
$context = $compiled['context'];
|
$context = $compiled['context'];
|
||||||
$context->viewPath = $viewPath;
|
$context->viewPath = $viewPath;
|
||||||
|
$context->filePath = $this->fileFetcher->getFilePath($viewPath);
|
||||||
$context = $builder->build($compiled['context'], $compiled['source']) ;
|
$context = $builder->build($compiled['context'], $compiled['source']) ;
|
||||||
$context->classPath = $tmpFolder . DIRECTORY_SEPARATOR . $context->className . ".php";
|
$context->classPath = $tmpFolder . DIRECTORY_SEPARATOR . $context->className . ".php";
|
||||||
|
|
||||||
|
|
||||||
if ( $context->extendFrom ) {
|
if ( $context->extendFrom ) {
|
||||||
$this->compileView($context->extendFrom);
|
$this->compileView($context->extendFrom);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ class CommentToken implements Syntax {
|
|||||||
|
|
||||||
protected string $tokenClose = "\#\}";
|
protected string $tokenClose = "\#\}";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, string &$sourceCode)
|
public function parse(\Picea\Compiler\Context &$context, string &$sourceCode)
|
||||||
{
|
{
|
||||||
$sourceCode = preg_replace("#({$this->tokenOpen})(.*?)({$this->tokenClose})#s", "", $sourceCode);
|
$sourceCode = preg_replace("#({$this->tokenOpen})(.*?)({$this->tokenClose})#s", "", $sourceCode);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ class EchoRawToken implements Syntax {
|
|||||||
|
|
||||||
public string $tokenClose = "\}\}";
|
public string $tokenClose = "\}\}";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$content, string &$sourceCode)
|
public function parse(\Picea\Compiler\Context &$content, string &$sourceCode)
|
||||||
{
|
{
|
||||||
$sourceCode = preg_replace_callback("#({$this->tokenOpen})(.*?)({$this->tokenClose})#s", function ($matches) {
|
$sourceCode = preg_replace_callback("#({$this->tokenOpen})(.*?)({$this->tokenClose})#s", function ($matches) {
|
||||||
$line = trim($matches[2], " \t\n\r\0\x0B;");
|
$line = trim($matches[2], " \t\n\r\0\x0B;");
|
||||||
|
|||||||
@@ -18,12 +18,12 @@ class EchoSafeToken implements Syntax {
|
|||||||
$this->encoding = ini_get("default_charset");
|
$this->encoding = ini_get("default_charset");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, string &$sourceCode)
|
public function parse(\Picea\Compiler\Context &$context, string &$sourceCode)
|
||||||
{
|
{
|
||||||
$sourceCode = preg_replace_callback("#({$this->tokenOpen})(.*?)({$this->tokenClose})#s", function ($matches) {
|
$sourceCode = preg_replace_callback("#({$this->tokenOpen})(.*?)({$this->tokenClose})#s", function ($matches) {
|
||||||
$line = trim($matches[2], " \t\n\r\0\x0B;");
|
$line = trim($matches[2], " \t\n\r\0\x0B;");
|
||||||
|
|
||||||
return "<?php echo htmlspecialchars({$line}, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
return "<?php echo htmlspecialchars((string) ({$line}), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
||||||
}, $sourceCode);
|
}, $sourceCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Picea\Syntax;
|
|
||||||
|
|
||||||
class PhpTagToken implements Syntax {
|
|
||||||
|
|
||||||
public string $tokenOpen = "\{\?";
|
|
||||||
|
|
||||||
public string $tokenClose = "\?\}";
|
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, string &$sourceCode)
|
|
||||||
{
|
|
||||||
$sourceCode = preg_replace_callback("#({$this->tokenOpen})(.*?)({$this->tokenClose})#s", function ($matches) {
|
|
||||||
return "<?php {$matches[2]} ?>";
|
|
||||||
}, $sourceCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -3,5 +3,5 @@
|
|||||||
namespace Picea\Syntax;
|
namespace Picea\Syntax;
|
||||||
|
|
||||||
interface Syntax {
|
interface Syntax {
|
||||||
public function parse(\Picae\Compiler\Context &$context, string &$sourceCode);
|
public function parse(\Picea\Compiler\Context &$context, string &$sourceCode);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user