forked from TopFuel/laravel-pjax
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ntzm/master
Update formatting for PSR-2
- Loading branch information
Showing
1 changed file
with
33 additions
and
36 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
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; | ||
} | ||
|
||
} | ||
} |