40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?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:get' ) {
|
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->container->get($arguments) ?>";
|
|
}
|
|
|
|
if ( $token === 'di:has' ) {
|
|
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->container->has($arguments) ?>";
|
|
}
|
|
|
|
throw new \InvalidArgumentException("You must use di:get/di:has tokens or di_get()/di_has() methods.");
|
|
}
|
|
|
|
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);
|
|
},
|
|
];
|
|
}
|
|
}
|