- Added a Picea's reference into templates. This allows extensions to call themselves after compilation.

This commit is contained in:
Dave Mc Nicoll 2019-12-20 11:43:00 -05:00
parent 423d74aa6c
commit a56b64cf49
3 changed files with 79 additions and 28 deletions

View File

@ -22,11 +22,11 @@ class %CLASSNAME% %EXTENDS% {
public function output(array $variablesList = []) : void
{
( function($___class__template, $___global_variables, $___variables) {
extract($___global_variables);
extract($___variables, \EXTR_OVERWRITE);
( function($___class__template, $___global_variables, $___variables, $___picea) {
extract($___global_variables); unset($___global_variables);
extract($___variables, \EXTR_OVERWRITE); unset($___variables);
?>%CONTENT%<?php
} )->call($this->thisProxy ?? new class(){}, $this, $this->variableList, $variablesList);
} )->call($this->thisProxy ?? new class(){}, $this, $this->variableList, $variablesList, $this->picea);
%PARENT_OUTPUT%
}

View File

@ -97,4 +97,15 @@ class Compiler
return $this;
}
public function getExtensionFromToken(string $name) : Extension\Extension
{
if ( false === $this->extensionList[$name] ?? false ) {
throw new \InvalidArgumentException(<<<MSG
Requested extension from token `$name` could not be found from loaded Picea compiler's extension.
MSG);
}
return $this->extensionList[$name];
}
}

View File

@ -11,56 +11,96 @@ class UrlExtension implements Extension {
public function __construct(string $urlBase = "") {
$this->urlBase = $urlBase;
}
public function addRoute(string $routeName, string $url, array $parameters = [])
{
}
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
return "<?php /* echo url ?? */ ?>";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : ?string
{
switch($token) {
case "url":
return "<?php echo \$___picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>";
}
return null;
}
public function buildUrl(string $uri, array $parameters = []) : string
{
return $this->url() . $uri . ( $parameters ? "?" . http_build_query($parameters) : "" );
}
public function url() : string
{
return $this->scheme() . $this->domain() . $this->base();
}
/**
* Return URI formatted
*/
public function uri($query_string = false) {
$uri = $_SERVER['REQUEST_URI'] ?? $this->php_self() . ($query_string ? $this->query_string() : "");
protected function uri(bool $queryString = false) : string
{
$uri = ( $_SERVER['REQUEST_URI'] ?? $_SERVER['PHP_SELF'] ?? "" ) . ( $queryString ? $this->queryString() : "" );
if ( ! $query_string ) {
if ( ! $queryString ) {
$uri = explode('?', $uri, 2)[0];
}
if (($base = $this->config['base'] ?? false) && ( stripos($uri, $base) === 0 )) {
if ( ($base = $this->config['base'] ?? false) && ( stripos($uri, $base) === 0 ) ) {
$uri = substr($uri, strlen($base));
}
return '/' . ltrim($uri, '/');
}
public function php_self() {
return isset($_SERVER['PHP_SELF']) ? str_replace(NEX . '/', '', $_SERVER['PHP_SELF']) : null;
}
/**
* Return query string
*/
public function query_string() {
protected function queryString() : string
{
return isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : "";
}
protected function scheme() : string
{
return ( $this->isHttps() ? "https" : "http" ) . "://";
}
protected function base() : string
{
return $this->urlBase ? $this->urlBase . "/" : "";
}
/**
* Check if Uri's segments are valid
* @param array $segments The uri's segments
*/
public function secure($segments = []) {
return array_diff($segments, ['..', '://']);
protected function secure($segments = []) : array
{
return array_diff($segments, [ '..', '://' ]);
}
public function domain() {
$domain_list = (array) $this->config('Eckinox.system.url.root');
$current = strtolower($_SERVER['HTTP_HOST']);
return $domain = in_array($current, $domain_list) ? $current : $domain_list[0];
protected function domain() : string
{
return strtolower($_SERVER['HTTP_HOST']);
}
protected function isDefaultPort() : bool
{
return in_array($_SERVER['SERVER_PORT'], [ 80, 443 ]);
}
protected function isHttps() : bool
{
$header = in_array('https', array_map("strtolower", [
$_SERVER['X-Url-Scheme'] ?? "",
$_SERVER['Front-End-Https'] ?? "",
$_SERVER['X-Forwarded-Ssl'] ?? "",
$_SERVER['X-Forwarded-Proto'] ?? "",
$_SERVER['X-Forwarded-Protocol'] ?? "",
]));
return $header
|| ( "443" === $_SERVER['SERVER_PORT'] ?? "" )
|| ( "https" === $_SERVER['REQUEST_SCHEME'] ?? "http" )
|| ( "off" !== strtolower($_SERVER['HTTPS'] ?? "off") );
}
}