vendor/contao-community-alliance/events-contao-bindings/src/Subscribers/SystemSubscriber.php line 87

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of contao-community-alliance/events-contao-bindings
  4.  *
  5.  * (c) 2014-2018 The Contao Community Alliance
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  *
  10.  * This project is provided in good faith and hope to be usable by anyone.
  11.  *
  12.  * @package    ContaoCommunityAlliance\Contao\Bindings
  13.  * @subpackage Subscribers
  14.  * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
  15.  * @author     Sven Baumann <baumann.sv@gmail.com>
  16.  * @copyright  2018 The Contao Community Alliance.
  17.  * @license    https://github.com/contao-community-alliance/events-contao-bindings/blob/master/LICENSE LGPL-3.0
  18.  * @filesource
  19.  */
  20. namespace ContaoCommunityAlliance\Contao\Bindings\Subscribers;
  21. use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
  22. use Contao\CoreBundle\Monolog\ContaoContext;
  23. use Contao\System;
  24. use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents;
  25. use ContaoCommunityAlliance\Contao\Bindings\Events\System\GetReferrerEvent;
  26. use ContaoCommunityAlliance\Contao\Bindings\Events\System\LoadLanguageFileEvent;
  27. use ContaoCommunityAlliance\Contao\Bindings\Events\System\LogEvent;
  28. use Psr\Log\LoggerInterface;
  29. use Psr\Log\LogLevel;
  30. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  31. /**
  32.  * Subscriber for the System class in Contao.
  33.  */
  34. class SystemSubscriber implements EventSubscriberInterface
  35. {
  36.     /**
  37.      * The contao framework.
  38.      *
  39.      * @var ContaoFrameworkInterface
  40.      */
  41.     protected $framework;
  42.     /**
  43.      * The logger service.
  44.      *
  45.      * @var LoggerInterface
  46.      */
  47.     private $logger;
  48.     /**
  49.      * SystemSubscriber constructor.
  50.      *
  51.      * @param ContaoFrameworkInterface $framework The contao framework.
  52.      * @param LoggerInterface          $logger    The logger service.
  53.      */
  54.     public function __construct(ContaoFrameworkInterface $frameworkLoggerInterface $logger)
  55.     {
  56.         $this->framework $framework;
  57.         $this->logger    $logger;
  58.     }
  59.     /**
  60.      * Returns an array of event names this subscriber wants to listen to.
  61.      *
  62.      * @return array
  63.      */
  64.     public static function getSubscribedEvents()
  65.     {
  66.         return [
  67.             ContaoEvents::SYSTEM_GET_REFERRER       => 'handleGetReferer',
  68.             ContaoEvents::SYSTEM_LOG                => 'handleLog',
  69.             ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE => 'handleLoadLanguageFile',
  70.         ];
  71.     }
  72.     /**
  73.      * Retrieve the current referrer url.
  74.      *
  75.      * @param GetReferrerEvent $event The event.
  76.      *
  77.      * @return void
  78.      */
  79.     public function handleGetReferer(GetReferrerEvent $event)
  80.     {
  81.         /** @var System $systemAdapter */
  82.         $systemAdapter $this->framework->getAdapter(System::class);
  83.         $event->setReferrerUrl($systemAdapter->getReferer($event->isEncodeAmpersands(), $event->getTableName()));
  84.     }
  85.     /**
  86.      * Handle a log event.
  87.      *
  88.      * @param LogEvent $event The event.
  89.      *
  90.      * @return void
  91.      */
  92.     public function handleLog(LogEvent $event)
  93.     {
  94.         $level TL_ERROR === $event->getCategory() ? LogLevel::ERROR LogLevel::INFO;
  95.         $this->logger->log(
  96.             $level,
  97.             $event->getText(),
  98.             ['contao' => new ContaoContext($event->getFunction(), $event->getCategory())]
  99.         );
  100.     }
  101.     /**
  102.      * Handle a load language file event.
  103.      *
  104.      * @param LoadLanguageFileEvent $event The event.
  105.      *
  106.      * @return void
  107.      */
  108.     public function handleLoadLanguageFile(LoadLanguageFileEvent $event)
  109.     {
  110.         /** @var System $systemAdapter */
  111.         $systemAdapter $this->framework->getAdapter(System::class);
  112.         $systemAdapter->loadLanguageFile($event->getFileName(), $event->getLanguage(), $event->isCacheIgnored());
  113.     }
  114. }