- Added a new DiExtension and proceed to some code cleaning
This commit is contained in:
parent
778f84116b
commit
1f771b1932
@ -12,7 +12,7 @@ use Picea\{
|
||||
Method\Request
|
||||
};
|
||||
|
||||
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
|
||||
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension, DiExtension };
|
||||
|
||||
return [
|
||||
Picea\Picea::class => function($c) {
|
||||
@ -41,6 +41,7 @@ return [
|
||||
$c->get(LanguageExtension::class),
|
||||
$c->get(TitleExtension::class),
|
||||
$c->get(NumberExtension::class),
|
||||
$c->get(DiExtension::class),
|
||||
$c->get(UrlExtension::class),
|
||||
$c->get(Request::class),
|
||||
], class_exists(\Taxus\Picea\Extension::class) ? [ $c->get(\Taxus\Picea\Extension::class) ] : [],
|
||||
@ -52,6 +53,8 @@ return [
|
||||
|
||||
NumberExtension::class => autowire(NumberExtension::class),
|
||||
|
||||
DiExtension::class => autowire(DiExtension::class),
|
||||
|
||||
UrlExtension::class => create(UrlExtension::class)->constructor(getenv("URL_BASE"), get('git.commit'), explode(',', getenv('APP_URL')), (bool) getenv('FORCE_SSL')),
|
||||
|
||||
Cache::class => create(Opcache::class)->constructor(getenv("CACHE_PATH"), get(Context::class)),
|
||||
|
||||
39
src/Extension/DiExtension.php
Normal file
39
src/Extension/DiExtension.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\Extension;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class DiExtension implements Extension, FunctionExtension {
|
||||
|
||||
public array $token = [ "di:has", "di:get" ];
|
||||
|
||||
public function __construct(public readonly ContainerInterface $container) {}
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
if ( $token === 'di:has' ) {
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->container->has($arguments) ?>";
|
||||
}
|
||||
|
||||
if ( $token === 'di:get' ) {
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->container->get($arguments) ?>";
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException("You must use has() or get() method.");
|
||||
}
|
||||
|
||||
public function exportFunctions(): array
|
||||
{
|
||||
return [
|
||||
'di_get' => function(string $classname) {
|
||||
return $this->container->get($classname);
|
||||
},
|
||||
|
||||
'di_has' => function(string $classname) {
|
||||
return $this->container->has($classname);
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -11,11 +11,9 @@ class LanguageExtension implements Extension, FunctionExtension {
|
||||
|
||||
public string $currentLanguage = "";
|
||||
|
||||
protected LanguageHandlerInterface $languageHandler;
|
||||
|
||||
public function __construct(LanguageHandlerInterface $handler) {
|
||||
$this->languageHandler = $handler;
|
||||
}
|
||||
public function __construct(
|
||||
protected LanguageHandlerInterface $languageHandler
|
||||
) {}
|
||||
|
||||
public function exportFunctions(): array
|
||||
{
|
||||
|
||||
@ -16,29 +16,23 @@ class UrlExtension implements Extension, FunctionExtension {
|
||||
/(?:[a-z0-9!#$%&'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+\\/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])/
|
||||
PATTERN;
|
||||
|
||||
protected string $urlBase;
|
||||
|
||||
protected array $appUrl;
|
||||
|
||||
protected string $assetToken;
|
||||
|
||||
protected array $routes;
|
||||
|
||||
protected array $routesTarget;
|
||||
|
||||
protected bool $forceSSL = false;
|
||||
|
||||
public array $tokens = [ "url" , "route", "asset", "slug" ];
|
||||
|
||||
##[\Deprecated]
|
||||
public bool $enforceExistingArguments = true;
|
||||
|
||||
public function __construct(string $urlBase = "", string $assetToken = "", array $appUrl = [], bool $forceSSL = false) {
|
||||
public function __construct(
|
||||
protected string $urlBase = "",
|
||||
protected string $assetToken = "",
|
||||
protected array $appUrl = [],
|
||||
protected bool $forceSSL = false
|
||||
) {
|
||||
$this->urlBase = trim($urlBase, "/");
|
||||
$this->assetToken = $assetToken;
|
||||
$this->appUrl = $appUrl;
|
||||
$this->forceSSL = $forceSSL;
|
||||
}
|
||||
}
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
|
||||
@ -5,8 +5,6 @@ namespace Picea\Method;
|
||||
use Picea\Extension\Extension,
|
||||
Picea\Extension\ExtensionTrait;
|
||||
|
||||
use Picea\Compiler\Context;
|
||||
|
||||
use Picea\Extension\FunctionExtension;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
@ -17,12 +15,10 @@ class Request implements Extension, FunctionExtension {
|
||||
public array $tokens;
|
||||
|
||||
public string $token;
|
||||
|
||||
public ServerRequestInterface $request;
|
||||
|
||||
public function __construct(ServerRequestInterface $request, Context $context) {
|
||||
$this->request = $request;
|
||||
}
|
||||
public function __construct(
|
||||
public ServerRequestInterface $request
|
||||
) { }
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { }
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user