vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/MVC/Symfony/Templating/GlobalHelper.php line 20

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Publish\Core\MVC\Symfony\Templating;
  7. use eZ\Publish\API\Repository\LocationService;
  8. use eZ\Publish\Core\Helper\TranslationHelper;
  9. use eZ\Publish\Core\MVC\ConfigResolverInterface;
  10. use eZ\Publish\Core\MVC\Symfony\RequestStackAware;
  11. use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
  12. use Symfony\Component\Routing\RouterInterface;
  13. /**
  14.  * Templating helper object globally accessible, through the "ezpublish" variable (in Twig).
  15.  * Container is injected to be sure to lazy load underlying services and to avoid scope conflict.
  16.  */
  17. class GlobalHelper
  18. {
  19.     use RequestStackAware;
  20.     /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
  21.     protected $configResolver;
  22.     /** @var \eZ\Publish\API\Repository\LocationService */
  23.     protected $locationService;
  24.     /** @var \Symfony\Component\Routing\RouterInterface */
  25.     protected $router;
  26.     /** @var \eZ\Publish\Core\Helper\TranslationHelper */
  27.     protected $translationHelper;
  28.     public function __construct(
  29.         ConfigResolverInterface $configResolver,
  30.         LocationService $locationService,
  31.         RouterInterface $router,
  32.         TranslationHelper $translationHelper
  33.     ) {
  34.         $this->configResolver $configResolver;
  35.         $this->locationService $locationService;
  36.         $this->router $router;
  37.         $this->translationHelper $translationHelper;
  38.     }
  39.     /**
  40.      * Returns the current siteaccess.
  41.      *
  42.      * @return \eZ\Publish\Core\MVC\Symfony\SiteAccess|null
  43.      */
  44.     public function getSiteaccess()
  45.     {
  46.         $request $this->getCurrentRequest();
  47.         if ($request) {
  48.             return $request->attributes->get('siteaccess');
  49.         }
  50.     }
  51.     /**
  52.      * Returns the view parameters as a hash.
  53.      *
  54.      * @return array|null
  55.      */
  56.     public function getViewParameters()
  57.     {
  58.         $request $this->getCurrentRequest();
  59.         if ($request) {
  60.             return $request->attributes->get('viewParameters');
  61.         }
  62.     }
  63.     /**
  64.      * Returns the view parameters as a string.
  65.      * e.g. /(foo)/bar.
  66.      *
  67.      * @return string
  68.      */
  69.     public function getViewParametersString()
  70.     {
  71.         $request $this->getCurrentRequest();
  72.         if ($request) {
  73.             return $request->attributes->get('viewParametersString');
  74.         }
  75.     }
  76.     /**
  77.      * Returns the requested URI string (aka semanticPathInfo).
  78.      *
  79.      * @return string
  80.      */
  81.     public function getRequestedUriString()
  82.     {
  83.         $request $this->getCurrentRequest();
  84.         if ($request) {
  85.             return $request->attributes->get('semanticPathinfo');
  86.         }
  87.     }
  88.     /**
  89.      * Returns the "system" URI string.
  90.      * System URI is the URI for internal content controller.
  91.      * E.g. /content/location/123/full.
  92.      *
  93.      * If current route is not an URLAlias, then the current Pathinfo is returned.
  94.      *
  95.      * @return string|null
  96.      */
  97.     public function getSystemUriString()
  98.     {
  99.         $request $this->getCurrentRequest();
  100.         if ($request) {
  101.             if ($request->attributes->get('_route') === UrlAliasRouter::URL_ALIAS_ROUTE_NAME) {
  102.                 return $this->router
  103.                     ->generate(
  104.                         '_ez_content_view',
  105.                         [
  106.                             'contentId' => $request->attributes->get('contentId'),
  107.                             'locationId' => $request->attributes->get('locationId'),
  108.                             'viewType' => $request->attributes->get('viewType'),
  109.                         ]
  110.                     );
  111.             }
  112.             return $this->getRequestedUriString();
  113.         }
  114.     }
  115.     /**
  116.      * Returns the root location.
  117.      *
  118.      * @return \eZ\Publish\API\Repository\Values\Content\Location
  119.      */
  120.     public function getRootLocation()
  121.     {
  122.         return $this->locationService->loadLocation(
  123.             $this->configResolver->getParameter('content.tree_root.location_id')
  124.         );
  125.     }
  126.     /**
  127.      * Returns the translation SiteAccess for $language, or null if it cannot be found.
  128.      *
  129.      * @param string $language
  130.      *
  131.      * @return string|null
  132.      */
  133.     public function getTranslationSiteAccess($language)
  134.     {
  135.         return $this->translationHelper->getTranslationSiteAccess($language);
  136.     }
  137.     /**
  138.      * Returns the list of available languages.
  139.      *
  140.      * @return array
  141.      */
  142.     public function getAvailableLanguages()
  143.     {
  144.         return $this->translationHelper->getAvailableLanguages();
  145.     }
  146.     /**
  147.      * Returns the config resolver.
  148.      *
  149.      * @return \eZ\Publish\Core\MVC\ConfigResolverInterface
  150.      */
  151.     public function getConfigResolver()
  152.     {
  153.         return $this->configResolver;
  154.     }
  155. }