<?php
namespace Cms\CoreBundle\Service\Twig;
use Cms\ContainerBundle\Entity\Container;
use Cms\ContainerBundle\Entity\Containers\IntranetContainer;
use Cms\CoreBundle\Service\ContextManager;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Cms\FrontendBundle\Model\FrontendGlobals;
use Cms\FrontendBundle\Service\Resolvers\LocaleResolver;
use Cms\ModuleBundle\Entity\ModuleEntity;
use Cms\ModuleBundle\Model\ModuleConfig;
use Cms\ModuleBundle\Service\ModuleManager;
use Cms\TenantBundle\Entity\Tenant;
use Platform\SecurityBundle\Entity\Identity\Account;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Kernel;
/**
* Provides a "cms" variable in Twig templates.
*
* Class GlobalVariables
* @package Cms\CoreBundle\Util\Templating
*/
final class GlobalVariables
{
/**
* @var ParameterBagInterface
*/
private ParameterBagInterface $params;
/**
* @var ContextManager
*/
private ContextManager $contextManager;
/**
* @var ModuleManager
*/
private ModuleManager $moduleManager;
/**
* @var EntityManager
*/
private EntityManager $em;
/**
* @var RequestStack
*/
private RequestStack $requestStack;
/**
* @var LocaleResolver
*/
private LocaleResolver $localeResolver;
/**
* @param ParameterBagInterface $params
* @param ContextManager $contextManager
* @param ModuleManager $moduleManager
* @param EntityManager $em
* @param RequestStack $requestStack
* @param LocaleResolver $localeResolver
*/
public function __construct(
ParameterBagInterface $params,
ContextManager $contextManager,
ModuleManager $moduleManager,
EntityManager $em,
RequestStack $requestStack,
LocaleResolver $localeResolver
)
{
$this->params = $params;
$this->contextManager = $contextManager;
$this->moduleManager = $moduleManager;
$this->em = $em;
$this->requestStack = $requestStack;
$this->localeResolver = $localeResolver;
}
/**
* @return Container|null
*/
public function getDepartment(): ?Container
{
/** @var Request $request */
// TODO: should we be getting master or current?
$request = $this->requestStack->getMasterRequest();
if ($request->attributes->has('_route_params')) {
$params = $request->attributes->get('_route_params', []);
$container = null;
if (isset($params['containerId'])) {
$container = $this->em->getRepository(Container::class)->find($params['containerId']);
} else if (isset($params['container'])) {
$container = $this->em->getRepository(Container::class)->find($params['container']);
}
if ( ! empty($container)) {
return $container;
}
}
return null;
}
/**
* @return string
*/
public function getEnvironment(): string
{
return $this->contextManager->getGlobalContext()->getMode();
}
/**
* @return Tenant|null
*/
public function getTenant(): ?Tenant
{
return $this->contextManager->getGlobalContext()->getTenant();
}
/**
* @return ParameterBagInterface
*/
public function getParams(): ParameterBagInterface
{
return $this->params;
}
/**
* @return Account|null
*/
public function getAccount(): ?Account
{
return $this->getEffectiveAccount();
}
/**
* @return Account|null
*/
public function getEffectiveAccount(): ?Account
{
return $this->contextManager->getGlobalContext()->getEffectiveAccount();
}
/**
* @return Account|null
*/
public function getImpersonatedAccount(): ?Account
{
return $this->contextManager->getGlobalContext()->getImpersonatedAccount();
}
/**
* @return Account|null
*/
public function getAuthenticatedAccount(): ?Account
{
return $this->contextManager->getGlobalContext()->getAuthenticatedAccount();
}
/**
* @param string|ModuleEntity $module
* @return ModuleConfig
*/
public function getModuleConfig($module): ModuleConfig
{
if ($module instanceof ModuleEntity) {
return $this->moduleManager->getModuleConfigurationForEntity($module);
}
return $this->moduleManager->getModuleConfiguration($module);
}
/**
* @return ContextManager
*/
public function getContextManager(): ContextManager
{
return $this->contextManager;
}
/**
* @param FrontendGlobals|null $globals
* @return array
*/
public function getState(FrontendGlobals $globals = null): array
{
$state = array(
'environment' => $this->getEnvironment(),
'tenant' => array(
'id' => $this->getTenant()->getId(),
'uid' => $this->getTenant()->getUid()->toString(),
'slug' => $this->getTenant()->getSlug(),
),
'dashboard' => $this->getParams()->get('dashboard.hostname'),
'container' => null,
);
if ($globals) {
$state = array_merge($state, array(
'container' => ($globals->getContainer()) ? $globals->getContainer()->getId() : null,
));
if ($globals->getContainer() instanceof IntranetContainer && $this->getEffectiveAccount()) {
$state = array_merge($state, [
'user' => [
'name' => $this->getEffectiveAccount()->getDisplayName(),
'email' => $this->getEffectiveAccount()->getEmail(),
],
]);
}
}
return $state;
}
/**
* @return array
*/
public function getTranslationLanguages(): array
{
return $this->localeResolver->resolveSecondaryLanguagesByTenant($this->getTenant());
}
/**
* @return string
*/
public function getUserEffectiveLanguage(): string
{
return $this->localeResolver->resolveUserEffectiveLanguage($this->getTenant());
}
/**
* @return array
*/
public function getDebugging(): array
{
return array(
// php and core info
'environment' => $this->getParams()->get('kernel.environment'),
'debug' => $this->getParams()->get('kernel.debug'),
'symfony_version' => Kernel::VERSION,
'php_version' => PHP_VERSION,
'xdebug_enabled' => extension_loaded('xdebug'),
'eaccel_enabled' => extension_loaded('eaccelerator') && ini_get('eaccelerator.enable'),
'apc_enabled' => extension_loaded('apcu') && ini_get('apc.enabled'),
'xcache_enabled' => extension_loaded('xcache') && ini_get('xcache.cacher'),
'wincache_enabled' => extension_loaded('wincache') && ini_get('wincache.ocenabled'),
'zend_opcache_enabled' => extension_loaded('Zend OPcache') && ini_get('opcache.enable'),
'sapi_name' => PHP_SAPI,
// key parameters
'app.routing.apexes.system' => $this->params->get('app.routing.apexes.system'),
'app.routing.apexes.portal' => $this->params->get('app.routing.apexes.portal'),
'app.routing.cluster' => $this->params->get('app.routing.cluster'),
);
}
}