1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spiral\LogViewer\Controllers; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use Spiral\Core\Controller; |
7
|
|
|
use Spiral\Core\Traits\AuthorizesTrait; |
8
|
|
|
use Spiral\Http\Exceptions\ClientExceptions\NotFoundException; |
9
|
|
|
use Spiral\Http\Request\InputManager; |
10
|
|
|
use Spiral\Http\Response\ResponseWrapper; |
11
|
|
|
use Spiral\LogViewer\Services\LogService; |
12
|
|
|
use Spiral\LogViewer\Helpers\Timestamps; |
13
|
|
|
use Spiral\Translator\Traits\TranslatorTrait; |
14
|
|
|
use Spiral\Vault\Vault; |
15
|
|
|
use Spiral\Views\ViewManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @property InputManager $input |
20
|
|
|
* @property ViewManager $views |
21
|
|
|
* @property Vault $vault |
22
|
|
|
* @property ResponseWrapper $response |
23
|
|
|
*/ |
24
|
|
|
class LogViewerController extends Controller |
25
|
|
|
{ |
26
|
|
|
use AuthorizesTrait, TranslatorTrait; |
27
|
|
|
|
28
|
|
|
const GUARD_NAMESPACE = 'vault.logs'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param LogService $source |
32
|
|
|
* @param Timestamps $timestamps |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function indexAction(LogService $source, Timestamps $timestamps) |
36
|
|
|
{ |
37
|
|
|
return $this->views->render('log-viewer:list', [ |
38
|
|
|
'selector' => $source->getLogs(), |
39
|
|
|
'lastLog' => $source->lastLog(), |
40
|
|
|
'timestamps' => $timestamps |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param LogService $source |
46
|
|
|
* @param Timestamps $timestamps |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function viewAction(LogService $source, Timestamps $timestamps) |
50
|
|
|
{ |
51
|
|
|
$filename = $this->input->input('filename'); |
52
|
|
|
$log = $source->getLogByName($filename); |
53
|
|
|
|
54
|
|
|
if (empty($log)) { |
55
|
|
|
throw new NotFoundException; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->authorize('view', compact('log')); |
59
|
|
|
|
60
|
|
|
return $this->views->render('log-viewer:log', compact('log', 'timestamps')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param LogService $source |
65
|
|
|
* @return array|\Psr\Http\Message\ResponseInterface |
66
|
|
|
*/ |
67
|
|
|
public function removeAllAction(LogService $source) |
68
|
|
|
{ |
69
|
|
|
$this->authorize('remove'); |
70
|
|
|
|
71
|
|
|
$source->removeAll(); |
72
|
|
|
|
73
|
|
|
$uri = $this->vault->uri('logs'); |
74
|
|
View Code Duplication |
if ($this->input->isAjax()) { |
75
|
|
|
return [ |
76
|
|
|
'status' => 200, |
77
|
|
|
'message' => $this->say('Logs deleted.'), |
78
|
|
|
'action' => ['redirect' => $uri] |
79
|
|
|
]; |
80
|
|
|
} else { |
81
|
|
|
return $this->response->redirect($uri); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param LogService $source |
87
|
|
|
* @param ServerRequestInterface $request |
88
|
|
|
* @return array|\Psr\Http\Message\ResponseInterface |
89
|
|
|
*/ |
90
|
|
|
public function removeAction(LogService $source, ServerRequestInterface $request) |
91
|
|
|
{ |
92
|
|
|
$filename = $this->input->input('filename'); |
93
|
|
|
$log = $source->getLogByName($filename); |
94
|
|
|
|
95
|
|
|
if (empty($log)) { |
96
|
|
|
throw new NotFoundException; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->authorize('remove', compact('log')); |
100
|
|
|
|
101
|
|
|
$source->removeLog($log); |
102
|
|
|
|
103
|
|
|
$uri = $this->removeBackURI($request); |
104
|
|
|
|
105
|
|
View Code Duplication |
if ($this->input->isAjax()) { |
106
|
|
|
return [ |
107
|
|
|
'status' => 200, |
108
|
|
|
'message' => $this->say('Log rotations deleted.'), |
109
|
|
|
'action' => ['redirect' => $uri] |
110
|
|
|
]; |
111
|
|
|
} else { |
112
|
|
|
return $this->response->redirect($uri); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Build redirect URI for removal operation. |
118
|
|
|
* |
119
|
|
|
* @param ServerRequestInterface $request |
120
|
|
|
* @return \Psr\Http\Message\UriInterface |
121
|
|
|
*/ |
122
|
|
|
protected function removeBackURI(ServerRequestInterface $request) |
123
|
|
|
{ |
124
|
|
|
$query = $request->getQueryParams(); |
125
|
|
|
if (array_key_exists('backToList', $query)) { |
126
|
|
|
$uri = $this->vault->uri('logs'); |
127
|
|
|
} else { |
128
|
|
|
$uri = $request->getServerParams()['HTTP_REFERER']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $uri; |
132
|
|
|
} |
133
|
|
|
} |