1
|
|
|
<?php namespace Arcanedev\LaravelDisqus\Http\Middleware; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelDisqus\Contracts\Disqus; |
4
|
|
|
use Closure; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class DisqusMiddleware |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelDisqus\Http\Middleware |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class DisqusMiddleware |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* The Disqus instance. |
20
|
|
|
* |
21
|
|
|
* @var \Arcanedev\LaravelDisqus\Contracts\Disqus |
22
|
|
|
*/ |
23
|
|
|
protected $disqus; |
24
|
|
|
|
25
|
|
|
/* ------------------------------------------------------------------------------------------------ |
26
|
|
|
| Constructor |
27
|
|
|
| ------------------------------------------------------------------------------------------------ |
28
|
|
|
*/ |
29
|
|
|
/** |
30
|
|
|
* DisqusMiddleware constructor. |
31
|
|
|
* |
32
|
|
|
* @param \Arcanedev\LaravelDisqus\Contracts\Disqus $disqus |
33
|
|
|
*/ |
34
|
12 |
|
public function __construct(Disqus $disqus) |
35
|
|
|
{ |
36
|
12 |
|
$this->disqus = $disqus; |
37
|
12 |
|
} |
38
|
|
|
|
39
|
|
|
/* ------------------------------------------------------------------------------------------------ |
40
|
|
|
| Main Functions |
41
|
|
|
| ------------------------------------------------------------------------------------------------ |
42
|
|
|
*/ |
43
|
|
|
/** |
44
|
|
|
* Handle an incoming request. |
45
|
|
|
* |
46
|
|
|
* @param \Illuminate\Http\Request $request |
47
|
|
|
* @param \Closure $next |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
12 |
|
public function handle($request, Closure $next) |
52
|
|
|
{ |
53
|
12 |
|
if ($this->disqus->isEnabled()) |
54
|
12 |
|
$this->handleDisqus($request); |
55
|
|
|
|
56
|
12 |
|
return $next($request); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/* ------------------------------------------------------------------------------------------------ |
60
|
|
|
| Other Functions |
61
|
|
|
| ------------------------------------------------------------------------------------------------ |
62
|
|
|
*/ |
63
|
|
|
/** |
64
|
|
|
* Handle Disqus. |
65
|
|
|
* |
66
|
|
|
* @param \Illuminate\Http\Request $request |
67
|
|
|
*/ |
68
|
12 |
|
protected function handleDisqus($request) |
69
|
|
|
{ |
70
|
12 |
|
$this->disqus->setPageUrl($request->url()) |
71
|
12 |
|
->setPageId($this->getPageId($request)); |
72
|
12 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the page id. |
76
|
|
|
* |
77
|
|
|
* @param \Illuminate\Http\Request $request |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
12 |
|
protected function getPageId($request) |
82
|
|
|
{ |
83
|
12 |
|
$path = implode('.', explode('/', $request->getPathInfo())); |
84
|
|
|
|
85
|
12 |
|
return 'base'.($path === '.' ? $path.'home' : $path); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|