src/Bidcoz/Bundle/CoreBundle/Twig/Extension/CoreExtension.php line 93

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\CoreBundle\Twig\Extension;
  3. use Bidcoz\Bundle\CoreBundle\BidcozCoreServices;
  4. use Bidcoz\Bundle\CoreBundle\Util\TextManipulator;
  5. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  6. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\Extension\GlobalsInterface;
  9. use Twig\TwigFunction;
  10. abstract class CoreExtension extends AbstractExtension implements GlobalsInterfaceServiceSubscriberInterface
  11. {
  12.     use BidcozCoreServices;
  13.     public function getGlobals(): array
  14.     {
  15.         return [
  16.             'orgParams'      => $this->getOrgParams(),
  17.             'campaignParams' => $this->getCampaignParams(),
  18.             'now'            => $this->getNow(),
  19.             'numMessages'    => $this->getNumberOfFlashMessages(),
  20.         ];
  21.     }
  22.     public function getFunctions()
  23.     {
  24.         return [
  25.             new TwigFunction('validate', [$this'validateObject']),
  26.             new TwigFunction('truncateText', [$this'truncateText']),
  27.         ];
  28.     }
  29.     public function getName()
  30.     {
  31.         return 'bidcoz.core';
  32.     }
  33.     public function validateObject($object$groups null)
  34.     {
  35.         return $this->getValidator()->validate($objectnull$groups);
  36.     }
  37.     public function getNow(): \DateTime
  38.     {
  39.         return new \DateTime();
  40.     }
  41.     public function getNumberOfFlashMessages(): int
  42.     {
  43.         $flashes $this->getFlashes();
  44.         return count($flashesCOUNT_RECURSIVE) - count($flashes);
  45.     }
  46.     public function truncateText(string $textint $maxLenstring $additionalText '...')
  47.     {
  48.         return TextManipulator::truncateText($text$maxLen$additionalText);
  49.     }
  50.     protected function getOrgParams()
  51.     {
  52.         if ($request $this->getRequestStack()->getCurrentRequest()) {
  53.             if ($request->attributes->has('organization')) {
  54.                 $organization $request->attributes->get('organization');
  55.                 return [
  56.                     'orgSlug' => $organization->getSlug(),
  57.                 ];
  58.             }
  59.         }
  60.     }
  61.     protected function getCampaignParams()
  62.     {
  63.         if ($orgParams $this->getOrgParams()) {
  64.             if ($request $this->getRequestStack()->getCurrentRequest()) {
  65.                 if ($request->attributes->has('campaign')) {
  66.                     $campaign $request->attributes->get('campaign');
  67.                     return array_merge($orgParams, [
  68.                         'campaignSlug' => $campaign->getSlug(),
  69.                     ]);
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     private function getFlashes(): array
  75.     {
  76.         try {
  77.             return $this->getSession()->getFlashBag()->peekAll();
  78.         } catch (SessionNotFoundException $e) {
  79.             return [];
  80.         }
  81.     }
  82. }