Skip to content

Commit

Permalink
Add Module::getTwig and Module::has
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Dec 5, 2024
1 parent 861ec23 commit 3ab29eb
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions classes/module/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Filesystem\Filesystem as SfFileSystem;
use Symfony\Component\Finder\Finder;
use Twig\Environment;

abstract class ModuleCore implements ModuleInterface
{
Expand Down Expand Up @@ -3459,12 +3460,37 @@ public function isSymfonyContext()
* @throws ServiceNotFoundException When the service is not defined
* @throws Exception
*/
public function get($serviceName)
public function get(string $serviceName): ?object
{
if ($serviceName === 'twig' && method_exists($this->context->controller, 'getTwig')) {
trigger_deprecation('prestashop/prestashop', '9.0', 'Load Twig using $this->context->controller->getTwig().', 'getTwig');
if ($serviceName === 'twig') {
trigger_deprecation('prestashop/prestashop', '9.0', 'Load Twig using $this->getTwig().');

return $this->context->controller->getTwig();
return $this->getTwig();
}

try {
$container = $this->getContainer();
} catch (ContainerNotFoundException) {
return null;
}

return $container->get($serviceName);
}

/**
* Check if the container has the requested service, it prevents throwing an ecception when
* trying to get a service not defined.
*
* @param string $serviceName
*
* @return bool
*/
public function has(string $serviceName): bool
{
if ($serviceName === 'twig') {
trigger_deprecation('prestashop/prestashop', '9.0', 'Load Twig using $this->getTwig().');

return $this->getTwig() !== null;
}

try {
Expand All @@ -3473,7 +3499,7 @@ public function get($serviceName)
return false;
}

return $container->get($serviceName);
return $container->has($serviceName);
}

/**
Expand All @@ -3498,6 +3524,15 @@ public function getContainer(): ContainerInterface
return $this->container;
}

public function getTwig(): ?Environment
{
if (method_exists($this->context->controller, 'getTwig')) {
return $this->context->controller->getTwig();
}

return null;
}

/**
* Save dashboard configuration
*
Expand Down

0 comments on commit 3ab29eb

Please sign in to comment.