src/Twig/TwigGlobalSubscriber.php line 36

  1. <?php
  2. namespace App\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Twig\Environment;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use App\Entity\Courier;
  9. use App\Entity\Shipment;
  10. use App\Entity\TrackingStatus;
  11. use App\Entity\Customer;
  12. use Symfony\Component\Security\Core\Security;
  13. class TwigGlobalSubscriber implements EventSubscriberInterface 
  14. {
  15.     /**
  16.      * @var \Twig\Environment
  17.      */
  18.     private $twig;
  19.     /**
  20.      * @var \Doctrine\ORM\EntityManagerInterface
  21.      */
  22.     private $manager;
  23.     private $security;
  24.     public function __construct(Environment $twigEntityManagerInterface  $managerSecurity $security) {
  25.         $this->twig    $twig;
  26.         $this->manager $manager;     
  27.         $this->security $security;   
  28.     }
  29.     public function injectGlobalVariablesControllerEvent  $event ) {
  30.        
  31.         $courierList $this->manager->getRepository(Courier::class)->findAll();
  32.         $this->twig->addGlobal'gloCourierList'$courierList);
  33.         if($this->security->getUser() !== null)
  34.         {
  35.             $customerList $this->manager->getRepository(Customer::class)->findBy(['userCompany' => $this->security->getUser()->getUserCompany()->getId()], ['name' => 'ASC']);
  36.             $this->twig->addGlobal'gloCustomerList'$customerList);
  37.         }
  38.         $totalBooking $this->manager->getRepository(Shipment::class)->getTotalCountShipment();
  39.         $this->twig->addGlobal'totalBookingMenu'$totalBooking);
  40.         $trackingStatusList $this->manager->getRepository(TrackingStatus::class)->findAll();
  41.         $this->twig->addGlobal'gloTrackingStatusList'$trackingStatusList);
  42.         
  43.     }
  44.     public static function getSubscribedEvents() {
  45.         return [ KernelEvents::CONTROLLER =>  'injectGlobalVariables' ];
  46.     }
  47. }