src/Cms/CoreBundle/Service/Twig/GlobalVariables.php line 91

Open in your IDE?
  1. <?php
  2. namespace Cms\CoreBundle\Service\Twig;
  3. use Cms\ContainerBundle\Entity\Container;
  4. use Cms\ContainerBundle\Entity\Containers\IntranetContainer;
  5. use Cms\CoreBundle\Service\ContextManager;
  6. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  7. use Cms\FrontendBundle\Model\FrontendGlobals;
  8. use Cms\FrontendBundle\Service\Resolvers\LocaleResolver;
  9. use Cms\ModuleBundle\Entity\ModuleEntity;
  10. use Cms\ModuleBundle\Model\ModuleConfig;
  11. use Cms\ModuleBundle\Service\ModuleManager;
  12. use Cms\TenantBundle\Entity\Tenant;
  13. use Platform\SecurityBundle\Entity\Identity\Account;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpKernel\Kernel;
  18. /**
  19.  * Provides a "cms" variable in Twig templates.
  20.  *
  21.  * Class GlobalVariables
  22.  * @package Cms\CoreBundle\Util\Templating
  23.  */
  24. final class GlobalVariables
  25. {
  26.     /**
  27.      * @var ParameterBagInterface
  28.      */
  29.     private ParameterBagInterface $params;
  30.     /**
  31.      * @var ContextManager
  32.      */
  33.     private ContextManager $contextManager;
  34.     /**
  35.      * @var ModuleManager
  36.      */
  37.     private ModuleManager $moduleManager;
  38.     /**
  39.      * @var EntityManager
  40.      */
  41.     private EntityManager $em;
  42.     /**
  43.      * @var RequestStack
  44.      */
  45.     private RequestStack $requestStack;
  46.     /**
  47.      * @var LocaleResolver
  48.      */
  49.     private LocaleResolver $localeResolver;
  50.     /**
  51.      * @param ParameterBagInterface $params
  52.      * @param ContextManager $contextManager
  53.      * @param ModuleManager $moduleManager
  54.      * @param EntityManager $em
  55.      * @param RequestStack $requestStack
  56.      * @param LocaleResolver $localeResolver
  57.      */
  58.     public function __construct(
  59.         ParameterBagInterface $params,
  60.         ContextManager $contextManager,
  61.         ModuleManager $moduleManager,
  62.         EntityManager $em,
  63.         RequestStack $requestStack,
  64.         LocaleResolver $localeResolver
  65.     )
  66.     {
  67.         $this->params $params;
  68.         $this->contextManager $contextManager;
  69.         $this->moduleManager $moduleManager;
  70.         $this->em $em;
  71.         $this->requestStack $requestStack;
  72.         $this->localeResolver $localeResolver;
  73.     }
  74.     /**
  75.      * @return Container|null
  76.      */
  77.     public function getDepartment(): ?Container
  78.     {
  79.         /** @var Request $request */
  80.         // TODO: should we be getting master or current?
  81.         $request $this->requestStack->getMasterRequest();
  82.         if ($request->attributes->has('_route_params')) {
  83.             $params $request->attributes->get('_route_params', []);
  84.             $container null;
  85.             if (isset($params['containerId'])) {
  86.                 $container $this->em->getRepository(Container::class)->find($params['containerId']);
  87.             } else if (isset($params['container'])) {
  88.                 $container =  $this->em->getRepository(Container::class)->find($params['container']);
  89.             }
  90.             if ( ! empty($container)) {
  91.                 return $container;
  92.             }
  93.         }
  94.         return null;
  95.     }
  96.     /**
  97.      * @return string
  98.      */
  99.     public function getEnvironment(): string
  100.     {
  101.         return $this->contextManager->getGlobalContext()->getMode();
  102.     }
  103.     /**
  104.      * @return Tenant|null
  105.      */
  106.     public function getTenant(): ?Tenant
  107.     {
  108.         return $this->contextManager->getGlobalContext()->getTenant();
  109.     }
  110.     /**
  111.      * @return ParameterBagInterface
  112.      */
  113.     public function getParams(): ParameterBagInterface
  114.     {
  115.         return $this->params;
  116.     }
  117.     /**
  118.      * @return Account|null
  119.      */
  120.     public function getAccount(): ?Account
  121.     {
  122.         return $this->getEffectiveAccount();
  123.     }
  124.     /**
  125.      * @return Account|null
  126.      */
  127.     public function getEffectiveAccount(): ?Account
  128.     {
  129.         return $this->contextManager->getGlobalContext()->getEffectiveAccount();
  130.     }
  131.     /**
  132.      * @return Account|null
  133.      */
  134.     public function getImpersonatedAccount(): ?Account
  135.     {
  136.         return $this->contextManager->getGlobalContext()->getImpersonatedAccount();
  137.     }
  138.     /**
  139.      * @return Account|null
  140.      */
  141.     public function getAuthenticatedAccount(): ?Account
  142.     {
  143.         return $this->contextManager->getGlobalContext()->getAuthenticatedAccount();
  144.     }
  145.     /**
  146.      * @param string|ModuleEntity $module
  147.      * @return ModuleConfig
  148.      */
  149.     public function getModuleConfig($module): ModuleConfig
  150.     {
  151.         if ($module instanceof ModuleEntity) {
  152.             return $this->moduleManager->getModuleConfigurationForEntity($module);
  153.         }
  154.         return $this->moduleManager->getModuleConfiguration($module);
  155.     }
  156.     /**
  157.      * @return ContextManager
  158.      */
  159.     public function getContextManager(): ContextManager
  160.     {
  161.         return $this->contextManager;
  162.     }
  163.     /**
  164.      * @param FrontendGlobals|null $globals
  165.      * @return array
  166.      */
  167.     public function getState(FrontendGlobals $globals null): array
  168.     {
  169.         $state = array(
  170.             'environment' => $this->getEnvironment(),
  171.             'tenant' => array(
  172.                 'id' => $this->getTenant()->getId(),
  173.                 'uid' => $this->getTenant()->getUid()->toString(),
  174.                 'slug' => $this->getTenant()->getSlug(),
  175.             ),
  176.             'dashboard' => $this->getParams()->get('dashboard.hostname'),
  177.             'container' => null,
  178.         );
  179.         if ($globals) {
  180.             $state array_merge($state, array(
  181.                 'container' => ($globals->getContainer()) ? $globals->getContainer()->getId() : null,
  182.             ));
  183.             if ($globals->getContainer() instanceof IntranetContainer && $this->getEffectiveAccount()) {
  184.                 $state array_merge($state, [
  185.                     'user' => [
  186.                         'name' => $this->getEffectiveAccount()->getDisplayName(),
  187.                         'email' => $this->getEffectiveAccount()->getEmail(),
  188.                     ],
  189.                 ]);
  190.             }
  191.         }
  192.         return $state;
  193.     }
  194.     /**
  195.      * @return array
  196.      */
  197.     public function getTranslationLanguages(): array
  198.     {
  199.         return $this->localeResolver->resolveSecondaryLanguagesByTenant($this->getTenant());
  200.     }
  201.     /**
  202.      * @return string
  203.      */
  204.     public function getUserEffectiveLanguage(): string
  205.     {
  206.         return $this->localeResolver->resolveUserEffectiveLanguage($this->getTenant());
  207.     }
  208.     /**
  209.      * @return array
  210.      */
  211.     public function getDebugging(): array
  212.     {
  213.         return array(
  214.             // php and core info
  215.             'environment' => $this->getParams()->get('kernel.environment'),
  216.             'debug' => $this->getParams()->get('kernel.debug'),
  217.             'symfony_version' => Kernel::VERSION,
  218.             'php_version' => PHP_VERSION,
  219.             'xdebug_enabled' => extension_loaded('xdebug'),
  220.             'eaccel_enabled' => extension_loaded('eaccelerator') && ini_get('eaccelerator.enable'),
  221.             'apc_enabled' => extension_loaded('apcu') && ini_get('apc.enabled'),
  222.             'xcache_enabled' => extension_loaded('xcache') && ini_get('xcache.cacher'),
  223.             'wincache_enabled' => extension_loaded('wincache') && ini_get('wincache.ocenabled'),
  224.             'zend_opcache_enabled' => extension_loaded('Zend OPcache') && ini_get('opcache.enable'),
  225.             'sapi_name' => PHP_SAPI,
  226.             // key parameters
  227.             'app.routing.apexes.system' => $this->params->get('app.routing.apexes.system'),
  228.             'app.routing.apexes.portal' => $this->params->get('app.routing.apexes.portal'),
  229.             'app.routing.cluster' => $this->params->get('app.routing.cluster'),
  230.         );
  231.     }
  232. }