- 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 public function output(array $variablesList = []) : void
{ {
( function($___class__template, $___global_variables, $___variables) { ( function($___class__template, $___global_variables, $___variables, $___picea) {
extract($___global_variables); extract($___global_variables); unset($___global_variables);
extract($___variables, \EXTR_OVERWRITE); extract($___variables, \EXTR_OVERWRITE); unset($___variables);
?>%CONTENT%<?php ?>%CONTENT%<?php
} )->call($this->thisProxy ?? new class(){}, $this, $this->variableList, $variablesList); } )->call($this->thisProxy ?? new class(){}, $this, $this->variableList, $variablesList, $this->picea);
%PARENT_OUTPUT% %PARENT_OUTPUT%
} }

View File

@ -97,4 +97,15 @@ class Compiler
return $this; 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

@ -12,55 +12,95 @@ class UrlExtension implements Extension {
$this->urlBase = $urlBase; $this->urlBase = $urlBase;
} }
public function addRoute(string $routeName, string $url, array $parameters = []) 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) ?>";
} }
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) { return null;
return "<?php /* echo url ?? */ ?>"; }
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 * Return URI formatted
*/ */
public function uri($query_string = false) { protected function uri(bool $queryString = false) : string
$uri = $_SERVER['REQUEST_URI'] ?? $this->php_self() . ($query_string ? $this->query_string() : ""); {
$uri = ( $_SERVER['REQUEST_URI'] ?? $_SERVER['PHP_SELF'] ?? "" ) . ( $queryString ? $this->queryString() : "" );
if ( ! $query_string ) { if ( ! $queryString ) {
$uri = explode('?', $uri, 2)[0]; $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)); $uri = substr($uri, strlen($base));
} }
return '/' . ltrim($uri, '/');
}
public function php_self() { return '/' . ltrim($uri, '/');
return isset($_SERVER['PHP_SELF']) ? str_replace(NEX . '/', '', $_SERVER['PHP_SELF']) : null;
} }
/** /**
* Return query string * Return query string
*/ */
public function query_string() { protected function queryString() : string
{
return isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_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 * Check if Uri's segments are valid
* @param array $segments The uri's segments * @param array $segments The uri's segments
*/ */
public function secure($segments = []) { protected function secure($segments = []) : array
{
return array_diff($segments, [ '..', '://' ]); return array_diff($segments, [ '..', '://' ]);
} }
public function domain() { protected function domain() : string
$domain_list = (array) $this->config('Eckinox.system.url.root'); {
$current = strtolower($_SERVER['HTTP_HOST']); return strtolower($_SERVER['HTTP_HOST']);
return $domain = in_array($current, $domain_list) ? $current : $domain_list[0];
} }
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") );
}
} }