app/Plugin/NewsPageSelfReliance/EventListener/NpsrPagesListener.php line 37

Open in your IDE?
  1. <?php
  2. namespace Plugin\NewsPageSelfReliance\EventListener;
  3. use Eccube\Request\Context;
  4. use Eccube\Repository\NewsRepository;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  8. class NpsrPagesListener implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var RequestStack
  12.      */
  13.     protected $requestStack;
  14.     /**
  15.      * @var Context
  16.      */
  17.     protected $requestContext;
  18.     /**
  19.      * @var NewsRepository
  20.      */
  21.     protected $newsRepository;
  22.     public function __construct(RequestStack $requestStackContext $requestContextNewsRepository $newsRepository)
  23.     {
  24.         $this->requestStack $requestStack;
  25.         $this->requestContext $requestContext;
  26.         $this->newsRepository $newsRepository;
  27.     }
  28.     public function onKernelRequest(FilterResponseEvent $event)
  29.     {
  30.         if (!$event->isMasterRequest()) {
  31.             return;
  32.         }
  33.         if ($this->requestContext->isFront()) {
  34.             $request $event->getRequest();
  35.             $pathInfo $request->getPathInfo();
  36.             if( strpos($pathInfo,'/news/') === false ){
  37.                 return;
  38.             }
  39.             
  40.             $response $event->getResponse();
  41.             $content $response->getContent();
  42.             
  43.             $News $this->newsRepository->findbasename$pathInfo ) );
  44.   
  45.             $title $News->getNpseoTitle();
  46.             if( $title !== null ){
  47.                 $title "<title>{$title}</title>";
  48.                 preg_match('/\<title\>(.*?)\<\/title\>/s'$content$matches_title);
  49.                 if( $matches_title != false){
  50.                 $content str_replace$matches_title[0] , $title$content);
  51.                 }else{
  52.                 $content str_replace"</head>" $title."\r\n</head>" $content);
  53.                 }
  54.             }
  55.             $description $News->getNpseoDescription();
  56.             if( $description !== null ){
  57.                 $description "<meta name=\"description\" content=\"{$description}\" >";
  58.                 preg_match('/\<meta name=\"description\" (.*?)\>/s'$content$matches_description);
  59.                 if( $matches_description != false){
  60.                 $content str_replace$matches_description[0] , $description$content);
  61.                 }else{
  62.                 $content str_replace"</head>" $description."\r\n</head>"$content);
  63.                 }
  64.             }
  65.             $robots $News->getNpseoRobots();
  66.             if( $robots !== null ){
  67.                 $robots "<meta name=\"robots\" content=\"{$robots}\" >";
  68.                 preg_match('/\<meta name=\"robots\" (.*?)\>/'$content$matches_robots);
  69.                 if( $matches_robots != false){
  70.                 $content str_replace$matches_robots[0] , $robots$content);
  71.                 }else{
  72.                 $content str_replace"</head>" $robots."\r\n</head>"$content);
  73.                 }
  74.             }
  75.             $response->setContent($content);
  76.         }
  77.     }
  78.     public static function getSubscribedEvents()
  79.     {
  80.         return [
  81.             'kernel.response' => ['onKernelRequest'512],
  82.         ];
  83.     }
  84. }