Skip to content

Commit 582e1ce

Browse files
committed
A little refactoring
1 parent 0fe21a9 commit 582e1ce

12 files changed

+135
-46
lines changed

app/config.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpSchool\PhpWorkshop\Listener\TearDownListener;
1414
use PhpSchool\PhpWorkshop\Logger\ConsoleLogger;
1515
use PhpSchool\PhpWorkshop\Logger\Logger;
16+
use PhpSchool\PhpWorkshop\Markdown\Parser\ContextSpecificBlockParser;
1617
use PhpSchool\PhpWorkshop\Markdown\ProblemFileExtension;
1718
use PhpSchool\PhpWorkshop\Markdown\Renderer\ContextSpecificRenderer;
1819
use PhpSchool\PhpWorkshop\Markdown\Shorthands\AppName;
@@ -327,10 +328,10 @@
327328
);
328329
},
329330
ContextSpecificRenderer::class => function () {
330-
return new ContextSpecificRenderer('cli');
331+
return new ContextSpecificRenderer(ContextSpecificBlockParser::CLI_TYPE);
331332
},
332333
Context::class => function () {
333-
return new Context('cli');
334+
return new Context(ContextSpecificBlockParser::CLI_TYPE);
334335
},
335336
Environment::class => function (ContainerInterface $c) {
336337
$terminal = $c->get(Terminal::class);

src/Markdown/Block/ContextSpecificBlock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class ContextSpecificBlock extends AbstractStringContainerBlock
1818
*/
1919
private $type;
2020

21-
public function __construct(string $type = ContextSpecificBlockParser::CLI_TYPE)
21+
public function __construct(string $type)
2222
{
2323
if (!in_array($type, ContextSpecificBlockParser::TYPES, true)) {
2424
throw InvalidArgumentException::notValidParameter('type', ContextSpecificBlockParser::TYPES, $type);

src/Markdown/Context.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Markdown;
4+
5+
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
6+
7+
class Context
8+
{
9+
public const CONTEXT_CLI = 'cli';
10+
public const CONTEXT_CLOUD = 'cloud';
11+
12+
/**
13+
* @var string
14+
*/
15+
private $context;
16+
17+
public function __construct(string $context)
18+
{
19+
if (!in_array($context, [self::CONTEXT_CLI, self::CONTEXT_CLOUD], true)) {
20+
throw InvalidArgumentException::notValidParameter(
21+
'context',
22+
[self::CONTEXT_CLI, self::CONTEXT_CLOUD],
23+
$context
24+
);
25+
}
26+
$this->context = $context;
27+
}
28+
29+
public function getCurrentContext(): string
30+
{
31+
return $this->context;
32+
}
33+
}

src/Markdown/Parser/ContextSpecificBlockParser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
namespace PhpSchool\PhpWorkshop\Markdown\Parser;
66

7+
use League\CommonMark\Block\Parser\BlockParserInterface;
78
use PhpSchool\PhpWorkshop\Markdown\Block\ContextSpecificBlock;
89
use League\CommonMark\ContextInterface;
910
use League\CommonMark\Cursor;
11+
use PhpSchool\PhpWorkshop\Markdown\Context;
1012

11-
final class ContextSpecificBlockParser implements \League\CommonMark\Block\Parser\BlockParserInterface
13+
final class ContextSpecificBlockParser implements BlockParserInterface
1214
{
13-
public const CLI_TYPE = 'cli';
14-
public const CLOUD_TYPE = 'cloud';
15-
public const TYPES = [self::CLI_TYPE, self::CLOUD_TYPE];
15+
public const TYPES = [Context::CONTEXT_CLI, Context::CONTEXT_CLOUD];
1616

1717
public static function getParserRegex(): string
1818
{

src/Markdown/Renderer/CliSpecificRenderer.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Markdown/Renderer/ContextSpecificRenderer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
use League\CommonMark\ElementRendererInterface;
1010
use League\CommonMark\Inline\Element\AbstractInline;
1111
use PhpSchool\PhpWorkshop\Markdown\Block\ContextSpecificBlock;
12+
use PhpSchool\PhpWorkshop\Markdown\Context;
1213

1314
final class ContextSpecificRenderer implements BlockRendererInterface
1415
{
1516
/**
16-
* @var string
17+
* @var Context
1718
*/
18-
private $type;
19+
private $currentContext;
1920

20-
public function __construct(string $type)
21+
public function __construct(Context $currentContext)
2122
{
22-
$this->type = $type;
23+
$this->currentContext = $currentContext;
2324
}
2425

2526
public function render(AbstractBlock $block, ElementRendererInterface $renderer, bool $inTightList = false): string
@@ -30,7 +31,7 @@ public function render(AbstractBlock $block, ElementRendererInterface $renderer,
3031
return $renderer->renderInlines($children);
3132
}
3233

33-
if ($this->type !== $block->getType()) {
34+
if ($this->currentContext->getCurrentContext() !== $block->getType()) {
3435
return '';
3536
}
3637

src/Markdown/Shorthands/AppName.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@ public function __construct(string $appName)
2222
$this->appName = $appName;
2323
}
2424

25+
public function cli(array $callArgs): array
26+
{
27+
return $this->getBlocks($callArgs);
28+
}
29+
30+
public function cloud(array $callArgs): array
31+
{
32+
return $this->getBlocks($callArgs);
33+
}
34+
2535
/**
2636
* @param array<string> $callArgs
2737
* @return array<Node>
2838
*/
29-
public function __invoke(array $callArgs): array
39+
public function getBlocks(array $callArgs): array
3040
{
3141
$wrapped = isset($callArgs[0]);
3242

src/Markdown/Shorthands/Context.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ public function __construct(string $type)
1818
$this->type = $type;
1919
}
2020

21+
public function cli(array $callArgs): array
22+
{
23+
return $this->getBlocks($callArgs);
24+
}
25+
26+
public function cloud(array $callArgs): array
27+
{
28+
return $this->getBlocks($callArgs);
29+
}
30+
2131
/**
2232
* @param array<string> $callArgs
2333
* @return Text[]
2434
*/
25-
public function __invoke(array $callArgs): array
35+
public function getBlocks(array $callArgs): array
2636
{
2737
$offset = array_search($this->type, $callArgs, true);
2838

src/Markdown/Shorthands/Documentation.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,21 @@
1212

1313
final class Documentation implements ShorthandInterface
1414
{
15+
public function cli(array $callArgs): array
16+
{
17+
return $this->getBlocks($callArgs);
18+
}
19+
20+
public function cloud(array $callArgs): array
21+
{
22+
return $this->getBlocks($callArgs);
23+
}
24+
1525
/**
1626
* @param array<string> $callArgs
1727
* @return array<Node>
1828
*/
19-
public function __invoke(array $callArgs): array
29+
public function getBlocks(array $callArgs): array
2030
{
2131
if (count($callArgs) < 3) {
2232
return [];

src/Markdown/Shorthands/Run.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@ public function __invoke(array $callArgs): array
1919
new Text('Run XXXX '),
2020
];
2121
}
22+
23+
public function cli(array $callArgs): array
24+
{
25+
// TODO: Implement cli() method.
26+
}
27+
28+
public function cloud(array $callArgs): array
29+
{
30+
// TODO: Implement cloud() method.
31+
}
2232
}

src/Markdown/Shorthands/ShorthandInterface.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@
99
interface ShorthandInterface
1010
{
1111
/**
12+
* When running via the CLI
13+
*
1214
* @param array<string> $callArgs
1315
* @return array<Node>
1416
*/
15-
public function __invoke(array $callArgs): array;
17+
public function cli(array $callArgs): array;
18+
19+
/**
20+
* When running via Cloud
21+
*
22+
* @param array<string> $callArgs
23+
* @return array<Node>
24+
*/
25+
public function cloud(array $callArgs): array;
1626
}

src/Markdown/Shorthands/Verify.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,50 @@
66

77
use League\CommonMark\Inline\Element\Text;
88
use League\CommonMark\Node\Node;
9+
use PhpSchool\PhpWorkshop\Exception\RuntimeException;
910

1011
final class Verify implements ShorthandInterface
1112
{
13+
/**
14+
* @var string
15+
*/
16+
private $appName;
17+
18+
public function __construct(string $appName)
19+
{
20+
$this->appName = $appName;
21+
}
22+
1223
/**
1324
* @param array<string> $callArgs
1425
* @return Text[]
1526
*/
1627
public function __invoke(array $callArgs): array
28+
{
29+
if (!isset($callArgs[1])) {
30+
throw new RuntimeException('The solution file must be specific');
31+
}
32+
33+
return [
34+
new Text($this->appName . ' verify ' . $callArgs[1]),
35+
];
36+
}
37+
38+
public function cli(array $callArgs): array
39+
{
40+
if (!isset($callArgs[1])) {
41+
throw new RuntimeException('The solution file must be specific');
42+
}
43+
44+
return [
45+
new Text($this->appName . ' verify ' . $callArgs[1]),
46+
];
47+
}
48+
49+
public function cloud(array $callArgs): array
1750
{
1851
return [
19-
new Text('Verify XXXX '),
52+
new Text('Click the Verify button in the bottom right'),
2053
];
2154
}
2255
}

0 commit comments

Comments
 (0)