From 1f771b193224d4ea0ebfff2ca96ee172dfad0132 Mon Sep 17 00:00:00 2001 From: Dave M Date: Mon, 6 Jul 2026 16:35:06 +0000 Subject: [PATCH] - Added a new DiExtension and proceed to some code cleaning --- meta/definitions.php | 5 +++- src/Extension/DiExtension.php | 39 +++++++++++++++++++++++++++++ src/Extension/LanguageExtension.php | 8 +++--- src/Extension/UrlExtension.php | 20 ++++++--------- src/Method/Request.php | 10 +++----- 5 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 src/Extension/DiExtension.php diff --git a/meta/definitions.php b/meta/definitions.php index 2045a18..b39414f 100644 --- a/meta/definitions.php +++ b/meta/definitions.php @@ -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)), diff --git a/src/Extension/DiExtension.php b/src/Extension/DiExtension.php new file mode 100644 index 0000000..ee36590 --- /dev/null +++ b/src/Extension/DiExtension.php @@ -0,0 +1,39 @@ +compiler->getExtensionFromToken('$token')->container->has($arguments) ?>"; + } + + if ( $token === 'di:get' ) { + return "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); + }, + ]; + } +} diff --git a/src/Extension/LanguageExtension.php b/src/Extension/LanguageExtension.php index 3df7c7b..3f56229 100644 --- a/src/Extension/LanguageExtension.php +++ b/src/Extension/LanguageExtension.php @@ -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 { diff --git a/src/Extension/UrlExtension.php b/src/Extension/UrlExtension.php index 8788560..b2361a2 100644 --- a/src/Extension/UrlExtension.php +++ b/src/Extension/UrlExtension.php @@ -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 { diff --git a/src/Method/Request.php b/src/Method/Request.php index 76d93fd..9bb3228 100644 --- a/src/Method/Request.php +++ b/src/Method/Request.php @@ -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 { }