- Fixed 'title' method and extension
This commit is contained in:
parent
e8d81222ff
commit
2df950a054
|
@ -2,12 +2,36 @@
|
||||||
|
|
||||||
namespace Picea\Extension;
|
namespace Picea\Extension;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
class TitleExtension implements Extension {
|
class TitleExtension implements Extension {
|
||||||
|
|
||||||
public string $token = "title";
|
public string $token = "title";
|
||||||
|
|
||||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
public string $title = "";
|
||||||
return "<?php /* echo title ?? */ ?>";
|
|
||||||
|
public function __construct(Context $context) {
|
||||||
|
$this->register($context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function register(Context $context) : void
|
||||||
|
{
|
||||||
|
$context->pushFunction("title", [ $this, 'handleTitle' ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||||
|
return "<?php echo title($arguments) ?>";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleTitle(? string $set = null) : ? string
|
||||||
|
{
|
||||||
|
if ( $set === null ) {
|
||||||
|
return $this->title;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$this->title = $set;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\Method;
|
||||||
|
|
||||||
|
use Picea\Extension\Extension,
|
||||||
|
Picea\Extension\ExtensionTrait;
|
||||||
|
|
||||||
|
use Picea\Compiler\Context;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
class Request implements Extension {
|
||||||
|
use ExtensionTrait;
|
||||||
|
|
||||||
|
public array $tokens;
|
||||||
|
|
||||||
|
public string $token;
|
||||||
|
|
||||||
|
public ServerRequestInterface $request;
|
||||||
|
|
||||||
|
public function __construct(ServerRequestInterface $request, Context $context) {
|
||||||
|
$this->request = $request;
|
||||||
|
$this->register($context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string { }
|
||||||
|
|
||||||
|
public function register(Context $context) : void
|
||||||
|
{
|
||||||
|
$context->pushFunction("cookie", [ $this, 'cookie' ]);
|
||||||
|
$context->pushFunction("get", [ $this, 'get' ]);
|
||||||
|
$context->pushFunction("post", [ $this, 'post' ]);
|
||||||
|
$context->pushFunction("post", [ $this, 'post' ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cookie(string $variableName)
|
||||||
|
{
|
||||||
|
return static::arrayGet($this->request->getCookieParams(), $variableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(string $variableName)
|
||||||
|
{
|
||||||
|
return static::arrayGet($this->request->getQueryParams(), $variableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function post(string $variableName)
|
||||||
|
{
|
||||||
|
return static::arrayGet($this->request->getParsedBody(), $variableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function server(string $variableName)
|
||||||
|
{
|
||||||
|
return static::arrayGet($this->request->getServerParams(), $variableName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function arrayGet(array $array, string $path, string $delimiter = '.')
|
||||||
|
{
|
||||||
|
$pathArr = explode($delimiter, $path);
|
||||||
|
|
||||||
|
if ( isset($array[$pathArr[0]]) ) {
|
||||||
|
if ( isset($pathArr[1]) ) {
|
||||||
|
return static::arrayGet($array[array_shift($pathArr)], implode($delimiter, $pathArr));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return $array[$pathArr[0]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue