Skip to content

Commit 353a5c4

Browse files
committed
WIP debug logger
1 parent bf09946 commit 353a5c4

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

app/config.php

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Colors\Color;
66
use PhpSchool\PhpWorkshop\Listener\InitialCodeListener;
7+
use PhpSchool\PhpWorkshop\Logger\ConsoleLogger;
78
use PhpSchool\PhpWorkshop\Logger\Logger;
89
use Psr\Log\LoggerInterface;
910
use function DI\create;
@@ -96,6 +97,10 @@
9697
$appName = $c->get('appName');
9798
$globalDir = $c->get('phpschoolGlobalDir');
9899

100+
if ($c->get('debugMode')) {
101+
return new ConsoleLogger($c->get(OutputInterface::class));
102+
}
103+
99104
return new Logger("$globalDir/logs/$appName.log");
100105
},
101106
ExerciseDispatcher::class => function (ContainerInterface $c) {

src/Application.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ public function setBgColour(string $colour): void
169169
$this->bgColour = $colour;
170170
}
171171

172-
public function configure(): ContainerInterface
172+
public function configure(bool $debugMode = false): ContainerInterface
173173
{
174174
if ($this->container instanceof ContainerInterface) {
175175
return $this->container;
176176
}
177177

178-
$container = $this->getContainer();
178+
$container = $this->getContainer($debugMode);
179179

180180
foreach ($this->exercises as $exercise) {
181181
if (false === $container->has($exercise)) {
@@ -221,10 +221,20 @@ public function configure(): ContainerInterface
221221
*/
222222
public function run(): int
223223
{
224-
$container = $this->configure();
224+
$args = $_SERVER['argv'] ?? [];
225+
226+
$debug = array_reduce($args, function (bool $debugEnabled, string $arg) {
227+
return $debugEnabled || $arg === '--debug';
228+
}, false);
229+
230+
$args = array_values(array_filter($args, function (string $arg) {
231+
return $arg !== '--debug';
232+
}));
233+
234+
$container = $this->configure($debug);
225235

226236
try {
227-
$exitCode = $container->get(CommandRouter::class)->route();
237+
$exitCode = $container->get(CommandRouter::class)->route($args);
228238
} catch (MissingArgumentException $e) {
229239
$container
230240
->get(OutputInterface::class)
@@ -261,9 +271,10 @@ public function run(): int
261271
}
262272

263273
/**
274+
* @param bool $debugMode
264275
* @return Container
265276
*/
266-
private function getContainer(): Container
277+
private function getContainer(bool $debugMode): Container
267278
{
268279
$containerBuilder = new ContainerBuilder();
269280
$containerBuilder->addDefinitions(
@@ -276,6 +287,7 @@ private function getContainer(): Container
276287
$containerBuilder->addDefinitions(
277288
[
278289
'workshopTitle' => $this->workshopTitle,
290+
'debugMode' => $debugMode,
279291
'exercises' => $this->exercises,
280292
'workshopLogo' => $this->logo,
281293
'bgColour' => $this->bgColour,

src/CommandRouter.php

-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public function addCommand(CommandDefinition $c): void
100100
*/
101101
public function route(array $args = null): int
102102
{
103-
104103
if (null === $args) {
105104
$args = $_SERVER['argv'] ?? [];
106105
}

src/Logger/ConsoleLogger.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshop\Logger;
6+
7+
use PhpSchool\PhpWorkshop\Output\OutputInterface;
8+
use Psr\Log\AbstractLogger;
9+
use Psr\Log\LoggerInterface;
10+
11+
class ConsoleLogger extends AbstractLogger implements LoggerInterface
12+
{
13+
/**
14+
* @var OutputInterface
15+
*/
16+
private $output;
17+
18+
public function __construct(OutputInterface $output)
19+
{
20+
$this->output = $output;
21+
}
22+
23+
public function log($level, $message, array $context = []): void
24+
{
25+
$this->output->writeLine(sprintf(
26+
"Time: %s, Level: %s, Message: %s, Context: %s",
27+
(new \DateTime())->format('d-m-y H:i:s'),
28+
$level,
29+
$message,
30+
json_encode($context)
31+
));
32+
}
33+
}

0 commit comments

Comments
 (0)