Skip to content

Commit 68e44a1

Browse files
committed
Improve preprocessor
1 parent db4f3b0 commit 68e44a1

40 files changed

+916
-826
lines changed

src/ContextInterface.php

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

src/Directives/Directive/Directive.php renamed to src/Directive/Directive.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,10 @@
99

1010
declare(strict_types=1);
1111

12-
namespace FFI\Preprocessor\Directives\Directive;
13-
14-
use FFI\Preprocessor\Directives\DirectiveInterface;
15-
use FFI\Preprocessor\Directives\RepositoryProviderInterface;
12+
namespace FFI\Preprocessor\Directive;
1613

1714
abstract class Directive implements DirectiveInterface
1815
{
19-
/**
20-
* @var string
21-
*/
22-
public const DEFAULT_VALUE = RepositoryProviderInterface::DEFAULT_VALUE;
23-
2416
/**
2517
* @var string
2618
*/
@@ -87,12 +79,12 @@ public function getMinArgumentsCount(): int
8779
* @param mixed $result
8880
* @return string
8981
*/
90-
protected function toString(mixed $result): string
82+
public static function render(mixed $result): string
9183
{
9284
return match(true) {
9385
$result === true => '1',
9486
\is_null($result), $result === false => '0',
95-
\is_string($result) => \var_export($result, true),
87+
\is_string($result) => '"' . \addcslashes($result, '"') . '"',
9688
default => (string)$result,
9789
};
9890
}

src/Directives/DirectiveInterface.php renamed to src/Directive/DirectiveInterface.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99

1010
declare(strict_types=1);
1111

12-
namespace FFI\Preprocessor\Directives;
12+
namespace FFI\Preprocessor\Directive;
1313

1414
/**
1515
* @mixin callable
1616
*/
1717
interface DirectiveInterface
1818
{
19+
/**
20+
* @var string
21+
*/
22+
public const DEFAULT_VALUE = '';
23+
1924
/**
2025
* @param string ...$args
2126
* @return string

src/Directives/Directive/FunctionDirective.php renamed to src/Directive/FunctionDirective.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
declare(strict_types=1);
1111

12-
namespace FFI\Preprocessor\Directives\Directive;
12+
namespace FFI\Preprocessor\Directive;
1313

1414
final class FunctionDirective extends Directive
1515
{
@@ -38,6 +38,6 @@ public function __construct(callable $cb)
3838
*/
3939
public function __invoke(string ...$args): string
4040
{
41-
return $this->toString(($this->callback)(...$args));
41+
return $this->render(($this->callback)(...$args));
4242
}
4343
}

src/Directives/Directive/FunctionLikeDirective.php renamed to src/Directive/FunctionLikeDirective.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
declare(strict_types=1);
1111

12-
namespace FFI\Preprocessor\Directives\Directive;
12+
namespace FFI\Preprocessor\Directive;
1313

14-
use FFI\Preprocessor\Directives\Compiler;
14+
use FFI\Preprocessor\Directive\FunctionLikeDirective\Compiler;
1515

1616
final class FunctionLikeDirective extends Directive
1717
{
@@ -64,6 +64,6 @@ public function __invoke(string ...$args): string
6464
$this->compiled = Compiler::compile($this->body, $this->args);
6565
}
6666

67-
return $this->toString(($this->compiled)(...$args));
67+
return $this->render(($this->compiled)(...$args));
6868
}
6969
}

src/Directives/Compiler.php renamed to src/Directive/FunctionLikeDirective/Compiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
declare(strict_types=1);
1111

12-
namespace FFI\Preprocessor\Directives;
12+
namespace FFI\Preprocessor\Directive\FunctionLikeDirective;
1313

14-
use FFI\Preprocessor\Directives\Directive\FunctionLikeDirective;
14+
use FFI\Preprocessor\Directive\FunctionLikeDirective;
1515

1616
/**
1717
* @internal Refers to {@see FunctionLikeDirective}

src/Directives/Directive/ObjectLikeDirective.php renamed to src/Directive/ObjectLikeDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
declare(strict_types=1);
1111

12-
namespace FFI\Preprocessor\Directives\Directive;
12+
namespace FFI\Preprocessor\Directive;
1313

1414
final class ObjectLikeDirective extends Directive
1515
{

src/Environment/EnvironmentProviderInterface.php renamed to src/Directive/ProviderInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
declare(strict_types=1);
1111

12-
namespace FFI\Preprocessor\Environment;
12+
namespace FFI\Preprocessor\Directive;
1313

14-
interface EnvironmentProviderInterface
14+
interface ProviderInterface
1515
{
1616
/**
17-
* @param EnvironmentInterface $env
17+
* @return RepositoryInterface
1818
*/
19-
public function load(EnvironmentInterface $env): void;
19+
public function getDirectives(): RepositoryInterface;
2020
}

src/Directive/RegistrarInterface.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* This file is part of FFI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace FFI\Preprocessor\Directive;
13+
14+
use FFI\Preprocessor\Exception\DirectiveDefinitionException;
15+
16+
interface RegistrarInterface
17+
{
18+
/**
19+
* @param string $directive
20+
* @param mixed $value
21+
* @throws DirectiveDefinitionException
22+
*/
23+
public function define(string $directive, mixed $value = DirectiveInterface::DEFAULT_VALUE): void;
24+
25+
/**
26+
* @param string $directive
27+
* @return bool
28+
*/
29+
public function undef(string $directive): bool;
30+
}

src/Directive/Repository.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
/**
4+
* This file is part of FFI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace FFI\Preprocessor\Directive;
13+
14+
use FFI\Preprocessor\Exception\DirectiveDefinitionException;
15+
16+
final class Repository implements RepositoryInterface, RegistrarInterface
17+
{
18+
/**
19+
* @var array<string, DirectiveInterface>
20+
*/
21+
private array $directives = [];
22+
23+
/**
24+
* @param iterable<string, DirectiveInterface> $directives
25+
* @throws \ReflectionException
26+
*/
27+
public function __construct(iterable $directives = [])
28+
{
29+
foreach ($directives as $directive => $definition) {
30+
$this->define($directive, $definition);
31+
}
32+
}
33+
34+
/**
35+
* @param string|callable|DirectiveInterface $directive
36+
* @return DirectiveInterface
37+
* @throws \ReflectionException
38+
*/
39+
private function cast(mixed $directive): DirectiveInterface
40+
{
41+
return match (true) {
42+
$directive instanceof DirectiveInterface => $directive,
43+
\is_callable($directive) => new FunctionDirective($directive),
44+
\is_scalar($directive), $directive instanceof \Stringable => new ObjectLikeDirective((string)$directive),
45+
default => $directive,
46+
};
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function define(string $directive, mixed $value = DirectiveInterface::DEFAULT_VALUE): void
53+
{
54+
try {
55+
$expr = $this->cast($value);
56+
} catch (\Throwable $e) {
57+
throw new DirectiveDefinitionException($e->getMessage(), (int)$e->getCode(), $e);
58+
}
59+
60+
if ($expr instanceof ObjectLikeDirective) {
61+
$this->directives = \array_merge([$directive => $expr], $this->directives);
62+
} else {
63+
$this->directives[$directive] = $expr;
64+
}
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*/
70+
public function undef(string $directive): bool
71+
{
72+
$exists = $this->defined($directive);
73+
74+
\unset($this->directives[$directive]);
75+
76+
return $exists;
77+
}
78+
79+
/**
80+
* {@inheritDoc}
81+
*/
82+
public function defined(string $directive): bool
83+
{
84+
return isset($this->directives[$directive]);
85+
}
86+
87+
/**
88+
* {@inheritDoc}
89+
*/
90+
public function find(string $directive): ?DirectiveInterface
91+
{
92+
return $this->directives[$directive] ?? null;
93+
}
94+
95+
/**
96+
* {@inheritDoc}
97+
*/
98+
public function getIterator(): \Traversable
99+
{
100+
return new \ArrayIterator($this->directives);
101+
}
102+
103+
/**
104+
* {@inheritDoc}
105+
*/
106+
public function count(): int
107+
{
108+
return \count($this->directives);
109+
}
110+
}

0 commit comments

Comments
 (0)