src/Twig/TwigGlobalSubscriber.php line 32

  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. class TwigGlobalSubscriber implements EventSubscriberInterface 
  13. {
  14.     /**
  15.      * @var \Twig\Environment
  16.      */
  17.     private $twig;
  18.     /**
  19.      * @var \Doctrine\ORM\EntityManagerInterface
  20.      */
  21.     private $manager;
  22.     public function __construct(Environment $twigEntityManagerInterface  $manager) {
  23.         $this->twig    $twig;
  24.         $this->manager $manager;        
  25.     }
  26.     public function injectGlobalVariablesControllerEvent  $event ) {
  27.        
  28.         $courierList $this->manager->getRepository(Courier::class)->findAll();
  29.         $this->twig->addGlobal'gloCourierList'$courierList);
  30.         $customerList $this->manager->getRepository(Customer::class)->findBy([], ['name' => 'ASC']);
  31.         $this->twig->addGlobal'gloCustomerList'$customerList);
  32.         $totalBooking $this->manager->getRepository(Shipment::class)->getTotalCountShipment();
  33.         $this->twig->addGlobal'totalBookingMenu'$totalBooking);
  34.         $trackingStatusList $this->manager->getRepository(TrackingStatus::class)->findAll();
  35.         $this->twig->addGlobal'gloTrackingStatusList'$trackingStatusList);
  36.         
  37.     }
  38.     public static function getSubscribedEvents() {
  39.         return [ KernelEvents::CONTROLLER =>  'injectGlobalVariables' ];
  40.     }
  41. }