Skip to content

Commit

Permalink
Merge pull request #5 from ntzm/master
Browse files Browse the repository at this point in the history
Update formatting for PSR-2
  • Loading branch information
JacobBennett committed Nov 20, 2015
2 parents cf593cb + 4cfbac6 commit 811ae93
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions src/JacobBennett/Pjax/PjaxMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,52 @@
<?php namespace JacobBennett\Pjax;
<?php

namespace JacobBennett\Pjax;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\DomCrawler\Crawler;

class PjaxMiddleware {

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
class PjaxMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
/** @var $response Response */
$response = $next($request);

// Only handle non-redirections
if (!$response->isRedirection()) {
// Must be a pjax-request
if ($request->pjax()) {
$crawler = new Crawler($response->getContent());

// Filter to title (in order to update the browser title bar)
$response_title = $crawler->filter('head > title');

// Filter to given container
$response_container = $crawler->filter($request->header('X-PJAX-CONTAINER'));
// Only handle non-redirections and must be a pjax-request
if (!$response->isRedirection() && $request->pjax()) {
$crawler = new Crawler($response->getContent());

// Container must exist
if ($response_container->count() != 0) {
// Filter to title (in order to update the browser title bar)
$response_title = $crawler->filter('head > title');

$title = '';
// If a title-attribute exists
if ($response_title->count() != 0) {
$title = '<title>' . $response_title->html() . '</title>';
}
// Filter to given container
$response_container = $crawler->filter($request->header('X-PJAX-CONTAINER'));

// Set new content for the response
$response->setContent($title . $response_container->html());
// Container must exist
if ($response_container->count() != 0) {
$title = '';
// If a title-attribute exists
if ($response_title->count() != 0) {
$title = '<title>' . $response_title->html() . '</title>';
}

// Updating address bar with the last URL in case there were redirects
$response->header('X-PJAX-URL', $request->getRequestUri());
// Set new content for the response
$response->setContent($title . $response_container->html());
}

// Updating address bar with the last URL in case there were redirects
$response->header('X-PJAX-URL', $request->getRequestUri());
}

return $response;
}

}
}

0 comments on commit 811ae93

Please sign in to comment.