- Added a new DiExtension and proceed to some code cleaning
This commit is contained in:
1 parent
778f84116b
commit
1f771b1932
5 files changed
+56
-26
No files matched your search
@@ -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);
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user