- Some more documentation made / fixed
This commit is contained in:
parent
46535c2734
commit
58f73d914b
|
@ -30,7 +30,7 @@ Using `print` or `echo`, which are, by default, made safer by using PHP's native
|
||||||
|
|
||||||
**[PHP]** Would yield internally:
|
**[PHP]** Would yield internally:
|
||||||
|
|
||||||
```php
|
```html
|
||||||
<?php echo htmlspecialchars((string) "Hello World !", 3, 'UTF-8', true) ?>
|
<?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) ?>
|
||||||
|
@ -63,7 +63,7 @@ Those tokens represents the equivalent of the printf() function from PHP.
|
||||||
|
|
||||||
**[PHP]** Would render internally as :
|
**[PHP]** Would render internally as :
|
||||||
|
|
||||||
```php
|
```html
|
||||||
<?php
|
<?php
|
||||||
$num = 5;
|
$num = 5;
|
||||||
$location = 'tree';
|
$location = 'tree';
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Extension - Creating an extension
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Extension - `json`, `json.pretty`, `json.html`
|
||||||
|
|
||||||
|
This extension
|
||||||
|
|
|
@ -20,7 +20,7 @@ class JsonExtension implements Extension, FunctionExtension {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "json.html":
|
case "json.html":
|
||||||
return "<?php echo htmlentities(json_encode($arguments, {$this->flags}), ENT_QUOTES, 'UTF-8') ?>";
|
return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$cls = static::class;
|
$cls = static::class;
|
||||||
|
@ -32,7 +32,7 @@ class JsonExtension implements Extension, FunctionExtension {
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'json' => function($arguments, ? int $flags = null) {
|
'json' => function($arguments, ? int $flags = null) {
|
||||||
return json_encode($arguments, \JSON_FORCE_OBJECT);
|
return json_encode($arguments, $flags ?? $this->flags);
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ namespace Picea\Extension;
|
||||||
|
|
||||||
use Picea\Compiler\Context;
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
class MoneyExtension implements Extension {
|
class MoneyExtension implements Extension, FunctionExtension {
|
||||||
|
|
||||||
public string $token = "money";
|
public string $token = "money";
|
||||||
|
|
||||||
|
@ -14,17 +14,18 @@ class MoneyExtension implements Extension {
|
||||||
|
|
||||||
public \NumberFormatter $formatter;
|
public \NumberFormatter $formatter;
|
||||||
|
|
||||||
public function __construct(Context $context) {
|
public function __construct() {
|
||||||
$this->register($context);
|
|
||||||
$this->locale = explode('.', \Locale::getDefault())[0];
|
$this->locale = explode('.', \Locale::getDefault())[0];
|
||||||
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
|
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register(Context $context) : void
|
public function exportFunctions(): array
|
||||||
{
|
{
|
||||||
$context->pushFunction("money", [ $this, 'money' ]);
|
return [
|
||||||
|
"money" => [ $this, 'money' ]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||||
return "<?php echo money($arguments) ?>";
|
return "<?php echo money($arguments) ?>";
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,23 +2,19 @@
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
$context->pushFunction("title", [ $this, 'handleTitle' ]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||||
return "<?php echo title($arguments) ?>";
|
return "<?php echo title($arguments) ?>";
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ namespace Picea\Extension;
|
||||||
|
|
||||||
use Picea\Compiler\Context;
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
class UrlExtension implements Extension {
|
class UrlExtension implements Extension, FunctionExtension {
|
||||||
|
|
||||||
public const URLIZE_PATTERN_URL = <<<PATTERN
|
public const URLIZE_PATTERN_URL = <<<PATTERN
|
||||||
~(?<!href=['"])https?://[\w/._\-&?]*(?!</a>)(?=[^\w/._\-&])~s
|
~(?<!href=['"])https?://[\w/._\-&?]*(?!</a>)(?=[^\w/._\-&])~s
|
||||||
|
@ -24,10 +24,9 @@ PATTERN;
|
||||||
|
|
||||||
public array $tokens = [ "url" , "route", "route.cacheless", "asset", "url.current", "url.parameters", "slug" ];
|
public array $tokens = [ "url" , "route", "route.cacheless", "asset", "url.current", "url.parameters", "slug" ];
|
||||||
|
|
||||||
public function __construct(Context $context, string $urlBase = "", string $assetToken = "") {
|
public function __construct(string $urlBase = "", string $assetToken = "") {
|
||||||
$this->urlBase = trim($urlBase, "/");
|
$this->urlBase = trim($urlBase, "/");
|
||||||
$this->assetToken = $assetToken;
|
$this->assetToken = $assetToken;
|
||||||
$this->register($context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : ?string
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : ?string
|
||||||
|
@ -54,15 +53,17 @@ PATTERN;
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register(Context $context) : void
|
public function exportFunctions(): array
|
||||||
{
|
{
|
||||||
$context->pushFunction("url", [ $this, 'buildUrl' ]);
|
return [
|
||||||
$context->pushFunction("current_url", [ $this, 'currentUrl' ]);
|
"url" => [ $this, 'buildUrl' ],
|
||||||
$context->pushFunction("asset", [ $this, 'buildAssetUrl' ]);
|
"current_url" => [ $this, 'currentUrl' ],
|
||||||
$context->pushFunction("route", [ $this, 'buildRouteUrl' ]);
|
"asset" => [ $this, 'buildAssetUrl' ],
|
||||||
$context->pushFunction("slug", [ $this, 'slug' ]);
|
"route" => [ $this, 'buildRouteUrl' ],
|
||||||
$context->pushFunction("urlize", [ $this, 'urlize' ]);
|
"slug" => [ $this, 'slug' ],
|
||||||
|
"urlize" => [ $this, 'urlize' ]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRouteList(bool $full = false) : array
|
public function getRouteList(bool $full = false) : array
|
||||||
|
|
|
@ -7,10 +7,11 @@ use Picea\Extension\Extension,
|
||||||
|
|
||||||
use Picea\Compiler\Context;
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
|
use Picea\Extension\FunctionExtension;
|
||||||
use Psr\Http\Message\RequestInterface;
|
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;
|
||||||
|
@ -21,20 +22,21 @@ class Request implements Extension {
|
||||||
|
|
||||||
public function __construct(ServerRequestInterface $request, Context $context) {
|
public function __construct(ServerRequestInterface $request, Context $context) {
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
$this->register($context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string { }
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : 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;
|
||||||
|
|
Loading…
Reference in New Issue