Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
9 changed files
with
400 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Ekino\WordpressBundle\Event\Subscriber; | ||
|
||
use Ekino\WordpressBundle\Wordpress\Wordpress; | ||
use Ekino\WordpressBundle\Wordpress\WordpressResponse; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
|
||
class WordpressResponseSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var string|array | ||
*/ | ||
protected $httpHeaderCallback; | ||
|
||
/** | ||
* @var Wordpress | ||
*/ | ||
protected $wordpress; | ||
|
||
/** | ||
* @param string|array $httpHeaderCallback | ||
* @param Wordpress $wordpress | ||
*/ | ||
public function __construct($httpHeaderCallback, Wordpress $wordpress) | ||
{ | ||
$this->httpHeaderCallback = $httpHeaderCallback; | ||
$this->wordpress = $wordpress; | ||
} | ||
|
||
/** | ||
* @param FilterResponseEvent $event | ||
*/ | ||
public function onKernelResponse(FilterResponseEvent $event) | ||
{ | ||
$response = $event->getResponse(); | ||
|
||
if (!$response instanceof WordpressResponse || $event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) { | ||
return; | ||
} | ||
|
||
if (!$wp_query = $this->wordpress->getWpQuery()) { | ||
return; | ||
} | ||
|
||
$callback = $this->getHttpHeadersCallback(); | ||
$wpHeaders = (array) call_user_func_array($callback, array($event->getRequest()->getUri())); | ||
|
||
foreach ($wpHeaders as $name => $value) { | ||
// TODO add cache headers support | ||
if ($name == 'cache-control') { | ||
//$response->setCache($value); | ||
continue; | ||
} | ||
|
||
$response->headers->set($name, $value); | ||
} | ||
|
||
if ($wp_query->is_404()) { | ||
$response->setStatusCode(404); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getHttpHeadersCallback() | ||
{ | ||
return $this->httpHeaderCallback; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
KernelEvents::RESPONSE => array('onKernelResponse'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.