Completed
Push — master ( 79372b...1f082f )
by recca
02:25
created

TerminalController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 1
b 0
f 1
1
<?php
2
3
namespace Recca0120\Terminal\Http\Controllers;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
use Recca0120\Terminal\Kernel;
8
use Illuminate\Routing\Controller;
9
use Illuminate\Filesystem\Filesystem;
10
use Illuminate\Contracts\Routing\ResponseFactory;
11
12
class TerminalController extends Controller
13
{
14
    /**
15
     * $request.
16
     *
17
     * @var \Illuminate\Http\Request
18
     */
19
    protected $request;
20
21
    /**
22
     * $responseFactory.
23
     *
24
     * @var \Illuminate\Contracts\Routing\ResponseFactory
25
     */
26
    protected $responseFactory;
27
28
    /**
29
     * __construct.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     * @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory
33
     */
34 2
    public function __construct(Request $request, ResponseFactory $responseFactory)
35
    {
36 2
        $this->request = $request;
37 2
        $this->responseFactory = $responseFactory;
38 2
    }
39
40
    /**
41
     * index.
42
     *
43
     * @param \Recca0120\Terminal\Kernel $kernel
44
     * @param string $view
45
     * @return \Illuminate\Http\Response
46
     */
47 1
    public function index(Kernel $kernel, $view = 'index')
48
    {
49 1
        $kernel->call('list --ansi');
50 1
        $options = json_encode(array_merge($kernel->getConfig(), [
51 1
            'helpInfo' => $kernel->output(),
52 1
        ]));
53 1
        $id = ($view === 'panel') ? Str::random(30) : null;
54
55 1
        return $this->responseFactory->view('terminal::'.$view, compact('options', 'id'));
56
    }
57
58
    /**
59
     * rpc response.
60
     *
61
     * @param \Recca0120\Terminal\Kernel $kernel
62
     * @return \Illuminate\Http\JsonResponse
63
     */
64 1
    public function endpoint(Kernel $kernel)
65
    {
66 1
        $error = $kernel->call($this->request->get('command'));
67
68 1
        return $this->responseFactory->json([
69 1
            'jsonrpc' => $this->request->get('jsonrpc'),
70 1
            'id' => $this->request->get('id'),
71 1
            'result' => $kernel->output(),
72 1
            'error' => $error,
73 1
        ]);
74
    }
75
76
    /**
77
     * media.
78
     *
79
     * @param \Illuminate\Filesystem\Filesystem $files
80
     * @param string $file
81
     * @return \Illuminate\Http\Response
82
     */
83
    public function media(Filesystem $files, $file)
84
    {
85
        $filename = __DIR__.'/../../../public/'.$file;
86
        $mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript';
87
        $lastModified = $files->lastModified($filename);
88
        $eTag = sha1_file($filename);
89
        $headers = [
90
            'content-type' => $mimeType,
91
            'last-modified' => date('D, d M Y H:i:s ', $lastModified).'GMT',
92
        ];
93
94
        if (@strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified ||
95
            trim($this->request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag
96
        ) {
97
            $response = $this->responseFactory->make(null, 304, $headers);
98
        } else {
99
            $response = $this->responseFactory->stream(function () use ($filename) {
100
                $out = fopen('php://output', 'wb');
101
                $file = fopen($filename, 'rb');
102
                stream_copy_to_stream($file, $out, filesize($filename));
103
                fclose($out);
104
                fclose($file);
105
            }, 200, $headers);
106
        }
107
108
        return $response->setEtag($eTag);
109
    }
110
}
111