"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.4.48/core-bundle/src/EventListener/AddToSearchIndexListener.php" (2 Apr 2020, 2064 Bytes) of package /linux/www/contao-4.4.48.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AddToSearchIndexListener.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.4.47_vs_4.4.48.

    1 <?php
    2 
    3 /*
    4  * This file is part of Contao.
    5  *
    6  * (c) Leo Feyer
    7  *
    8  * @license LGPL-3.0-or-later
    9  */
   10 
   11 namespace Contao\CoreBundle\EventListener;
   12 
   13 use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
   14 use Contao\Frontend;
   15 use Symfony\Component\HttpFoundation\Request;
   16 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
   17 
   18 /**
   19  * Adds a page to the search index after the response has been sent.
   20  *
   21  * @author Leo Feyer <https://github.com/leofeyer>
   22  * @author Andreas Schempp <https://github.com/aschempp>
   23  * @author Yanick Witschi <https://github.com/toflar>
   24  */
   25 class AddToSearchIndexListener
   26 {
   27     /**
   28      * @var ContaoFrameworkInterface
   29      */
   30     private $framework;
   31 
   32     /**
   33      * @var string
   34      */
   35     private $fragmentPath;
   36 
   37     /**
   38      * Constructor.
   39      *
   40      * @param ContaoFrameworkInterface $framework
   41      * @param string                   $fragmentPath
   42      */
   43     public function __construct(ContaoFrameworkInterface $framework, $fragmentPath = '_fragment')
   44     {
   45         $this->framework = $framework;
   46         $this->fragmentPath = $fragmentPath;
   47     }
   48 
   49     /**
   50      * Checks if the request can be indexed and forwards it accordingly.
   51      *
   52      * @param PostResponseEvent $event
   53      */
   54     public function onKernelTerminate(PostResponseEvent $event)
   55     {
   56         if (!$this->framework->isInitialized()) {
   57             return;
   58         }
   59 
   60         $request = $event->getRequest();
   61 
   62         // Only index GET requests (see #1194)
   63         if (!$request->isMethod(Request::METHOD_GET)) {
   64             return;
   65         }
   66 
   67         // Do not index fragments
   68         if (preg_match('~(?:^|/)'.preg_quote($this->fragmentPath, '~').'/~', $request->getPathInfo())) {
   69             return;
   70         }
   71 
   72         $response = $event->getResponse();
   73 
   74         // Only index successful responses (see #1091)
   75         if (200 !== $response->getStatusCode()) {
   76             return;
   77         }
   78 
   79         /** @var Frontend $frontend */
   80         $frontend = $this->framework->getAdapter(Frontend::class);
   81         $frontend->indexPageIfApplicable($response);
   82     }
   83 }