Skip to content

Commit 66961f0

Browse files
committed
Moved creating instances for the renderes into a factory class.
1 parent aef1758 commit 66961f0

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

src/Console/Debug.php

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
namespace Madewithlove\LaravelDebugConsole\Console;
44

55
use Illuminate\Console\Command;
6-
use Madewithlove\LaravelDebugConsole\Renderers\Exception;
7-
use Madewithlove\LaravelDebugConsole\Renderers\General;
8-
use Madewithlove\LaravelDebugConsole\Renderers\Message;
9-
use Madewithlove\LaravelDebugConsole\Renderers\Query;
10-
use Madewithlove\LaravelDebugConsole\Renderers\Request;
11-
use Madewithlove\LaravelDebugConsole\Renderers\Route;
12-
use Madewithlove\LaravelDebugConsole\Renderers\Timeline;
6+
use Madewithlove\LaravelDebugConsole\Renderers\RenderersFactory;
137
use Madewithlove\LaravelDebugConsole\StorageRepository;
148
use React\EventLoop\Factory;
159

@@ -44,6 +38,16 @@ class Debug extends Command
4438
*/
4539
private $loop;
4640

41+
/**
42+
* @var string
43+
*/
44+
private $section;
45+
46+
/**
47+
* @var array
48+
*/
49+
private $sections;
50+
4751
/**
4852
* @param \Madewithlove\LaravelDebugConsole\StorageRepository $repository
4953
*/
@@ -60,8 +64,10 @@ public function __construct(StorageRepository $repository)
6064
*/
6165
public function handle()
6266
{
63-
$section = $this->argument('section');
64-
$this->loop->addPeriodicTimer(1, function () use ($section) {
67+
$this->section = $this->argument('section');
68+
$this->sections = RenderersFactory::create($this->input, $this->output);
69+
70+
$this->loop->addPeriodicTimer(1, function () {
6571
$data = $this->repository->latest();
6672

6773
// Checks if its a new request
@@ -70,37 +76,21 @@ public function handle()
7076
}
7177

7278
$this->refresh();
73-
74-
(new General($this->input, $this->output))->render($data);
75-
76-
switch ($section) {
77-
case 'messages':
78-
(new Message($this->input, $this->output))->render($data);
79-
break;
80-
case 'timeline':
81-
(new Timeline($this->input, $this->output))->render($data);
82-
break;
83-
case 'exceptions':
84-
(new Exception($this->input, $this->output))->render($data);
85-
break;
86-
case 'route':
87-
(new Route($this->input, $this->output))->render($data);
88-
break;
89-
case 'queries':
90-
(new Query($this->input, $this->output))->render($data);
91-
break;
92-
case 'request':
93-
(new Request($this->input, $this->output))->render($data);
94-
break;
95-
}
79+
$this->renderScreen($data);
9680
});
9781

9882
$this->loop->run();
9983
}
10084

101-
private function refresh()
85+
/**
86+
* @param array $data
87+
*/
88+
public function renderScreen(array $data)
10289
{
103-
system('clear');
90+
array_get($this->sections, 'general')->render($data);
91+
if (array_has($this->sections, $this->section)) {
92+
array_get($this->sections, $this->section)->render($data);
93+
}
10494
}
10595

10696
/**
@@ -119,4 +109,9 @@ private function isNewRequest(array $data)
119109

120110
return false;
121111
}
112+
113+
private function refresh()
114+
{
115+
system('clear');
116+
}
122117
}

src/Renderers/RenderersFactory.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Madewithlove\LaravelDebugConsole\Renderers;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class RenderersFactory
9+
{
10+
/**
11+
* @param \Symfony\Component\Console\Input\InputInterface $input
12+
* @param \Symfony\Component\Console\Output\OutputInterface $output
13+
*
14+
* @return array
15+
*/
16+
public static function create(InputInterface $input, OutputInterface $output)
17+
{
18+
return [
19+
'general' => new General($input, $output),
20+
'messages' => new Message($input, $output),
21+
'timeline' => new Timeline($input, $output),
22+
'exceptions' => new Exception($input, $output),
23+
'route' => new Route($input, $output),
24+
'queries' => new Query($input, $output),
25+
'request' => new Request($input, $output),
26+
];
27+
}
28+
}

0 commit comments

Comments
 (0)