vendor/startplatz/wordpress-integration-bundle/Wordpress/HttpKernel.php line 81

Open in your IDE?
  1. <?php
  2. namespace Startplatz\Bundle\WordpressIntegrationBundle\Wordpress;
  3. use Startplatz\Bundle\WordpressIntegrationBundle\Wordpress\ShortCode;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\HttpKernelInterface;
  8. class HttpKernel implements HttpKernelInterface
  9. {
  10.     protected $wordpressRootDir;
  11.     protected $wordpressGlobalNamesCacheFile;
  12.     protected $catch;
  13.     protected $outputBufferLevel;
  14.     protected $oldGlobals;
  15.     protected $timezone;
  16.     public function __construct($wordpressRootDir$wordpressGlobalNamesCacheFile)
  17.     {
  18.         $this->wordpressRootDir $wordpressRootDir;
  19.         $this->wordpressGlobalNamesCacheFile $wordpressGlobalNamesCacheFile;
  20.     }
  21.     public function handle(Request $request$type self::MASTER_REQUEST$catch true)
  22.     {
  23.         if ($type !== self::MASTER_REQUEST) {
  24.             throw new \LogicException('Wordpress\HttpKernel cannot handle SUB_REQUESTS');
  25.         }
  26.         unset($type);
  27.         $this->catch $catch;
  28.         unset($catch);
  29.         $this->timezone date_default_timezone_get();
  30.         $this->startOutputBuffer();
  31.         try {
  32.             $wp_the_query null;
  33.             $this->storeGlobals();
  34.             $request->overrideGlobals();
  35.             if ($globalNames = @include($this->wordpressGlobalNamesCacheFile)) {
  36.                 foreach ($globalNames ? : array() as $name) {
  37.                     @eval('global $' $name ';');
  38.                 }
  39.             } else {
  40.                 throw new \RuntimeException('The global names cache file has to be generated with "bin/console startplatz:wordpress-integration:build-global-names-cache"');
  41.             }
  42.             define('WP_USE_THEMES'true);
  43.             $time_start microtime(true);
  44.             require("{$this->wordpressRootDir}/wp-blog-header.php");
  45.             require_once("{$this->wordpressRootDir}/wp-load.php");
  46.             global $wp_query;
  47.             $wp_query $wp_the_query;
  48.             \wp();
  49.             require_once("{$this->wordpressRootDir}/wp-includes/template-loader.php");
  50.             $content $this->endOutputBuffer();
  51.             $statusCode is_404() ? 404 200;
  52.             $headers $this->flushHeaders();
  53.             $this->restoreGlobals();
  54.             date_default_timezone_set($this->timezone);
  55.             return new Response($content$statusCode$headers);
  56.         } catch (\Exception $e) {
  57.             $this->endOutputBuffer();
  58.             $this->flushHeaders();
  59.             $this->restoreGlobals();
  60.             date_default_timezone_set($this->timezone);
  61.             if ($this->catch) {
  62.                 return new Response($e->getMessage(), 500);
  63.             } else {
  64.                 throw $e;
  65.             }
  66.         }
  67.     }
  68.     protected function startOutputBuffer() {
  69.         ob_start();
  70.         $this->outputBufferLevel ob_get_level();
  71.     }
  72.     protected function endOutputBuffer() {
  73.         while (ob_get_level() > $this->outputBufferLevel) {
  74.             ob_end_flush();
  75.         }
  76.         $buffer ob_get_contents();
  77.         ob_end_clean();
  78.         return $buffer;
  79.     }
  80.     protected function flushHeaders() {
  81.         $headers = array();
  82.         foreach (headers_list() as $header) {
  83.             if (preg_match('(^([^:]+):\s+(.*)$)'$header$matches)) {
  84.                 $headers[$matches[1]] = $matches[2];
  85.             }
  86.         }
  87.         header_remove();
  88.         return $headers;
  89.     }
  90.     protected function storeGlobals() {
  91.         $this->oldGlobals = array();
  92.         foreach ($GLOBALS as $name => $value) {
  93.             $this->oldGlobals[$name] = $value;
  94.         }
  95.         unset($this->oldGlobals['_SESSION']);
  96.         unset($this->oldGlobals['GLOBALS']);
  97.         unset($this->oldGlobals['HTTP_SESSION_VARS']);
  98.         unset($this->oldGlobals['_COOKIE']);
  99.         unset($this->oldGlobals['HTTP_COOKIE_VARS']);
  100.     }
  101.     protected function restoreGlobals() {
  102.         foreach ($GLOBALS as $name => $value) {
  103.             if (!in_array($name, array('_SESSION''GLOBALS''HTTP_SESSION_VARS''_COOKIE''HTTP_COOKIE_VARS'))) {
  104.                 if (!array_key_exists($name$this->oldGlobals)) {
  105.                     unset($GLOBALS[$name]);
  106.                 }
  107.             }
  108.         }
  109.         foreach ($this->oldGlobals as $name => $value) {
  110.             $GLOBALS[$name] = $value;
  111.         }
  112.     }
  113. }