- Work on Language Handler and extension.
- Added an URL extension which allows routes and URLs. - Fixed some bug within ClassTemplate
This commit is contained in:
1 parent
a64989af9f
commit
ddc3ae704a
10 files changed
+267
-82
No files matched your search
@@ -2,12 +2,50 @@
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
class LanguageExtension implements Extension {
|
||||
|
||||
public array $tokens = [ "lang", "_" ];
|
||||
public array $tokens = [ "lang", "_", "language.set" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
return "<?php /* echo lang($arguments) */ ?>";
|
||||
public string $currentLanguage = "";
|
||||
|
||||
protected LanguageHandler $languageHandler;
|
||||
|
||||
public function __construct(Context $context, LanguageHandler $handler) {
|
||||
$this->register($context);
|
||||
$this->languageHandler = $handler;
|
||||
}
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
|
||||
{
|
||||
switch($token) {
|
||||
case "language.set":
|
||||
return "<?php \$picea->compiler->getExtensionFromToken('$token')->currentLanguage = $arguments; ?>";
|
||||
|
||||
case "lang":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>";
|
||||
|
||||
case "_":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public function register(Context $context) : void
|
||||
{
|
||||
$context->pushFunction("_", [ $this, 'relativeLang' ]);
|
||||
$context->pushFunction("lang", [ $this, 'absoluteLang' ]);
|
||||
}
|
||||
|
||||
public function relativeLang(string $key, array $variables = []) : string
|
||||
{
|
||||
return $this->languageHandler->languageFromKey("{$this->currentLanguage}.{$key}", $variables);
|
||||
}
|
||||
|
||||
public function absoluteLang(string $key, array $variables = []) : string
|
||||
{
|
||||
return $this->languageHandler->languageFromKey($key, $variables);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
interface LanguageHandler
|
||||
{
|
||||
public function languageFromKey(string $key, array $variables = []) : string;
|
||||
}
|
||||
@@ -2,50 +2,81 @@
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
class UrlExtension implements Extension {
|
||||
|
||||
protected string $urlBase;
|
||||
|
||||
|
||||
protected string $assetToken;
|
||||
|
||||
|
||||
protected array $routes;
|
||||
|
||||
public array $tokens = [ "url" , "route", "asset" ];
|
||||
|
||||
public function __construct(string $urlBase = "", string $assetToken = "") {
|
||||
public function __construct(Context $context, string $urlBase = "", string $assetToken = "") {
|
||||
$this->urlBase = $urlBase;
|
||||
$this->assetToken = $assetToken;
|
||||
}
|
||||
$this->register($context);
|
||||
}
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : ?string
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : ?string
|
||||
{
|
||||
switch($token) {
|
||||
case "asset":
|
||||
return "<?php echo \$___picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>";
|
||||
|
||||
case "route":
|
||||
return "<?php echo \$___picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>";
|
||||
|
||||
case "url":
|
||||
return "<?php echo \$___picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>";
|
||||
case "asset":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>";
|
||||
|
||||
case "route":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>";
|
||||
|
||||
case "url":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>";
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function buildUrl(string $uri, array $parameters = []) : string
|
||||
public function register(Context $context) : void
|
||||
{
|
||||
return $this->url() . $uri . ( $parameters ? "?" . http_build_query($parameters) : "" );
|
||||
$context->pushFunction("url", [ $this, 'buildUrl' ]);
|
||||
$context->pushFunction("asset", [ $this, 'buildAssetUrl' ]);
|
||||
$context->pushFunction("route", [ $this, 'buildRouteUrl' ]);
|
||||
}
|
||||
|
||||
|
||||
public function buildUrl(string $uri = "", array $parameters = []) : string
|
||||
{
|
||||
return $this->url() . "/" . ltrim($uri, "/") . ( $parameters ? "?" . http_build_query($parameters) : "" );
|
||||
}
|
||||
|
||||
public function buildAssetUrl(string $uri, array $parameters = []) : string
|
||||
{
|
||||
return $this->url() . $uri . "?" . http_build_query( array_replace([ 'token' => $this->assetToken ], $parameters) );
|
||||
return $this->buildUrl($uri, array_replace([ 'v' => $this->assetToken ], $parameters));
|
||||
}
|
||||
|
||||
|
||||
public function buildRouteUrl(string $name, array $parameters = []) : string
|
||||
{
|
||||
if ( false !== ( $route = $this->routes[$name] ?? false ) ) {
|
||||
return $this->buildUrl($this->prepareRoute($route, $parameters), $parameters);
|
||||
}
|
||||
|
||||
$routeList = json_encode($this->routes, \JSON_PRETTY_PRINT);
|
||||
|
||||
throw new \RuntimeException("
|
||||
Given route `$name` could not be found within current route list:
|
||||
$routeList
|
||||
");
|
||||
}
|
||||
|
||||
public function url() : string
|
||||
{
|
||||
return $this->scheme() . $this->domain() . $this->base();
|
||||
}
|
||||
|
||||
|
||||
public function registerRoute(string $name, string $route) : void
|
||||
{
|
||||
$this->routes[$name] = $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return URI formatted
|
||||
*/
|
||||
@@ -60,33 +91,33 @@ class UrlExtension implements Extension {
|
||||
if ( ($base = $this->config['base'] ?? false) && ( stripos($uri, $base) === 0 ) ) {
|
||||
$uri = substr($uri, strlen($base));
|
||||
}
|
||||
|
||||
|
||||
return '/' . ltrim($uri, '/');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return query string
|
||||
*/
|
||||
protected function queryString() : string
|
||||
protected function queryString() : string
|
||||
{
|
||||
return isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : "";
|
||||
}
|
||||
|
||||
protected function scheme() : string
|
||||
|
||||
protected function scheme() : string
|
||||
{
|
||||
return ( $this->isHttps() ? "https" : "http" ) . "://";
|
||||
}
|
||||
|
||||
protected function base() : string
|
||||
|
||||
protected function base() : string
|
||||
{
|
||||
return $this->urlBase ? $this->urlBase . "/" : "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if Uri's segments are valid
|
||||
* @param array $segments The uri's segments
|
||||
*/
|
||||
protected function secure($segments = []) : array
|
||||
protected function secure($segments = []) : array
|
||||
{
|
||||
return array_diff($segments, [ '..', '://' ]);
|
||||
}
|
||||
@@ -95,25 +126,46 @@ class UrlExtension implements Extension {
|
||||
{
|
||||
return strtolower($_SERVER['HTTP_HOST']);
|
||||
}
|
||||
|
||||
protected function isDefaultPort() : bool
|
||||
|
||||
protected function isDefaultPort() : bool
|
||||
{
|
||||
return in_array($_SERVER['SERVER_PORT'], [ 80, 443 ]);
|
||||
}
|
||||
|
||||
protected function isHttps() : bool
|
||||
|
||||
protected function isHttps() : bool
|
||||
{
|
||||
$header = in_array('https', array_map("strtolower", [
|
||||
$header = in_array('https', array_map("strtolower", [
|
||||
$_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'] ?? "",
|
||||
]));
|
||||
|
||||
return $header
|
||||
|| ( "443" === $_SERVER['SERVER_PORT'] ?? "" )
|
||||
|| ( "https" === $_SERVER['REQUEST_SCHEME'] ?? "http" )
|
||||
|
||||
return $header
|
||||
|| ( "443" === $_SERVER['SERVER_PORT'] ?? "" )
|
||||
|| ( "https" === $_SERVER['REQUEST_SCHEME'] ?? "http" )
|
||||
|| ( "off" !== strtolower($_SERVER['HTTPS'] ?? "off") );
|
||||
}
|
||||
}
|
||||
|
||||
protected function prepareRoute(string $route, array &$arguments)
|
||||
{
|
||||
if ( preg_match_all('~{(.*?)}~si', $route, $matches, PREG_SET_ORDER) ) {
|
||||
$search = [];
|
||||
|
||||
foreach($matches as $item) {
|
||||
if ( ! array_key_exists($item[1], $arguments) ) {
|
||||
throw new \InvalidArgumentException("Argument `{$item[1]}` is missing within required route parameter(s)");
|
||||
}
|
||||
|
||||
$search[ $item[0] ] = $arguments[ $item[1] ];
|
||||
unset($arguments[ $item[1] ]);
|
||||
}
|
||||
|
||||
$route = str_replace(array_keys($search), array_values($search), $route);
|
||||
}
|
||||
|
||||
return $route;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user