From 10586c4e60dd6a02515b8e5f01b047ab8cc08d6e Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:43:59 +0100 Subject: [PATCH 01/13] Tidy up environment object --- src/Adapters/Laravel/Facades/Phiki.php | 2 +- src/Contracts/ExtensionInterface.php | 2 +- src/Environment.php | 50 ++++++++++++ src/Environment/Environment.php | 85 -------------------- src/Extensions/DefaultExtension.php | 2 +- src/Phiki.php | 11 ++- src/TextMate/Tokenizer.php | 8 +- tests/Adapters/Laravel/Facades/PhikiTest.php | 7 +- tests/Fixtures/EmptyExtension.php | 2 +- tests/Pest.php | 10 +-- 10 files changed, 69 insertions(+), 110 deletions(-) create mode 100644 src/Environment.php delete mode 100644 src/Environment/Environment.php diff --git a/src/Adapters/Laravel/Facades/Phiki.php b/src/Adapters/Laravel/Facades/Phiki.php index eb18f285..34a258b2 100644 --- a/src/Adapters/Laravel/Facades/Phiki.php +++ b/src/Adapters/Laravel/Facades/Phiki.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\Facade; /** - * @method static \Phiki\Environment\Environment environment() + * @method static \Phiki\Environment environment() * @method static array> codeToTokens(string $code, string|\Phiki\Grammar\Grammar|\Phiki\Grammar\ParsedGrammar $grammar) * @method static array> tokensToHighlightedTokens(array> $tokens, string|array|\Phiki\Theme\Theme $theme) * @method static array> codeToHighlightedTokens(string $code, string|\Phiki\Grammar\Grammar $grammar, string|array|\Phiki\Theme\Theme $theme) diff --git a/src/Contracts/ExtensionInterface.php b/src/Contracts/ExtensionInterface.php index d48ccb71..8a3fe8a6 100644 --- a/src/Contracts/ExtensionInterface.php +++ b/src/Contracts/ExtensionInterface.php @@ -2,7 +2,7 @@ namespace Phiki\Contracts; -use Phiki\Environment\Environment; +use Phiki\Environment; interface ExtensionInterface { diff --git a/src/Environment.php b/src/Environment.php new file mode 100644 index 00000000..755355fb --- /dev/null +++ b/src/Environment.php @@ -0,0 +1,50 @@ +grammars = new GrammarRepository; + $this->themes = new ThemeRepository; + } + + public function addExtension(ExtensionInterface $extension): static + { + $extension->register($this); + + return $this; + } + + public function resolveGrammar(string|Grammar|ParsedGrammar $grammar): ParsedGrammar + { + return match (true) { + is_string($grammar) => $this->grammars->get($grammar), + $grammar instanceof Grammar => $grammar->toParsedGrammar($this->grammars), + $grammar instanceof ParsedGrammar => $grammar, + }; + } + + public function resolveTheme(string|Theme|ParsedTheme $theme): ParsedTheme + { + return match (true) { + is_string($theme) => $this->themes->get($theme), + $theme instanceof Theme => $theme->toParsedTheme($this->themes), + $theme instanceof ParsedTheme => $theme, + }; + } +} diff --git a/src/Environment/Environment.php b/src/Environment/Environment.php deleted file mode 100644 index 56e6a542..00000000 --- a/src/Environment/Environment.php +++ /dev/null @@ -1,85 +0,0 @@ -register($this); - - return $this; - } - - public function useGrammarRepository(GrammarRepositoryInterface $grammarRepository): static - { - $this->grammarRepository = $grammarRepository; - - return $this; - } - - public function useThemeRepository(ThemeRepositoryInterface $themeRepository): static - { - $this->themeRepository = $themeRepository; - - return $this; - } - - public function getGrammarRepository(): GrammarRepositoryInterface - { - return $this->grammarRepository; - } - - public function resolveGrammar(string|Grammar|ParsedGrammar $grammar): ParsedGrammar - { - return match (true) { - is_string($grammar) => $this->grammarRepository->get($grammar), - $grammar instanceof Grammar => $grammar->toParsedGrammar($this->grammarRepository), - $grammar instanceof ParsedGrammar => $grammar, - }; - } - - public function getThemeRepository(): ThemeRepositoryInterface - { - return $this->themeRepository; - } - - public function resolveTheme(string|Theme|ParsedTheme $theme): ParsedTheme - { - return match (true) { - is_string($theme) => $this->themeRepository->get($theme), - $theme instanceof Theme => $theme->toParsedTheme($this->themeRepository), - $theme instanceof ParsedTheme => $theme, - }; - } - - public function validate(): void - { - if (! isset($this->grammarRepository)) { - throw EnvironmentException::missingGrammarRepository(); - } - - if (! isset($this->themeRepository)) { - throw EnvironmentException::missingThemeRepository(); - } - } - - final public static function default(): self - { - return (new self)->addExtension(new DefaultExtension); - } -} diff --git a/src/Extensions/DefaultExtension.php b/src/Extensions/DefaultExtension.php index f22bc608..bff2faf2 100644 --- a/src/Extensions/DefaultExtension.php +++ b/src/Extensions/DefaultExtension.php @@ -3,7 +3,7 @@ namespace Phiki\Extensions; use Phiki\Contracts\ExtensionInterface; -use Phiki\Environment\Environment; +use Phiki\Environment; use Phiki\Grammar\GrammarRepository; use Phiki\Theme\ThemeRepository; diff --git a/src/Phiki.php b/src/Phiki.php index 4384368a..45a531cc 100644 --- a/src/Phiki.php +++ b/src/Phiki.php @@ -3,7 +3,7 @@ namespace Phiki; use Phiki\Contracts\ExtensionInterface; -use Phiki\Environment\Environment; +use Phiki\Environment; use Phiki\Grammar\Grammar; use Phiki\Grammar\ParsedGrammar; use Phiki\Highlighting\Highlighter; @@ -17,10 +17,9 @@ class Phiki { protected Environment $environment; - public function __construct(?Environment $environment = null) + public function __construct() { - $this->environment = $environment ?? Environment::default(); - $this->environment->validate(); + $this->environment = new Environment; } public function environment(): Environment @@ -78,14 +77,14 @@ public function addExtension(ExtensionInterface $extension): static public function registerGrammar(string $name, string|ParsedGrammar $pathOrGrammar): static { - $this->environment->getGrammarRepository()->register($name, $pathOrGrammar); + $this->environment->grammars->register($name, $pathOrGrammar); return $this; } public function registerTheme(string $name, string|ParsedTheme $pathOrTheme): static { - $this->environment->getThemeRepository()->register($name, $pathOrTheme); + $this->environment->themes->register($name, $pathOrTheme); return $this; } diff --git a/src/TextMate/Tokenizer.php b/src/TextMate/Tokenizer.php index adf872d7..c2f03f00 100644 --- a/src/TextMate/Tokenizer.php +++ b/src/TextMate/Tokenizer.php @@ -2,7 +2,7 @@ namespace Phiki\TextMate; -use Phiki\Environment\Environment; +use Phiki\Environment; use Phiki\Grammar\BeginEndPattern; use Phiki\Grammar\BeginWhilePattern; use Phiki\Grammar\EndPattern; @@ -311,7 +311,7 @@ protected function matchRuleOrInjections(ParsedGrammar $grammar, string $lineTex */ protected function matchRule(ParsedGrammar $grammar, string $lineText, bool $isFirstLine, int $linePos, StateStack &$stack, int $anchorPosition): ?MatchedPattern { - return (new PatternSearcher($stack->pattern, $grammar, $this->environment->getGrammarRepository(), $isFirstLine, $linePos === $anchorPosition)) + return (new PatternSearcher($stack->pattern, $grammar, $this->environment->grammars, $isFirstLine, $linePos === $anchorPosition)) ->findNextMatch($lineText, $linePos); } @@ -338,7 +338,7 @@ protected function matchInjections(array $injections, ParsedGrammar $grammar, st continue; } - $searcher = new PatternSearcher($injection, $grammar, $this->environment->getGrammarRepository(), $isFirstLine, $linePos === $anchorPosition); + $searcher = new PatternSearcher($injection, $grammar, $this->environment->grammars, $isFirstLine, $linePos === $anchorPosition); $matched = $searcher->findNextMatch($lineText, $linePos); if (! $matched) { @@ -384,7 +384,7 @@ protected function checkWhileConditions(ParsedGrammar $grammar, string $lineText // Process and check each while pattern. for ($whileRule = array_pop($whileRules); $whileRule; $whileRule = array_pop($whileRules)) { - $searcher = new PatternSearcher($whileRule->rule, $grammar, $this->environment->getGrammarRepository(), $isFirstLine, $linePos === $anchorPosition); + $searcher = new PatternSearcher($whileRule->rule, $grammar, $this->environment->grammars, $isFirstLine, $linePos === $anchorPosition); $r = $searcher->findNextMatch($lineText, $linePos, while: true); if (! $r) { diff --git a/tests/Adapters/Laravel/Facades/PhikiTest.php b/tests/Adapters/Laravel/Facades/PhikiTest.php index 40991f41..95d31c19 100644 --- a/tests/Adapters/Laravel/Facades/PhikiTest.php +++ b/tests/Adapters/Laravel/Facades/PhikiTest.php @@ -1,11 +1,10 @@ getGrammarRepository()->has('custom'))->toBeTrue(); + expect(Phiki::environment()->grammars->has('custom'))->toBeTrue(); }); it('can register a custom theme', function () { Phiki::registerTheme('custom', __DIR__ . '/../../../Fixtures/theme.json'); - expect(Phiki::environment()->getThemeRepository()->has('custom'))->toBeTrue(); + expect(Phiki::environment()->themes->has('custom'))->toBeTrue(); }); it('can register an extension', function () { diff --git a/tests/Fixtures/EmptyExtension.php b/tests/Fixtures/EmptyExtension.php index cd7cebf0..f10856ea 100644 --- a/tests/Fixtures/EmptyExtension.php +++ b/tests/Fixtures/EmptyExtension.php @@ -3,7 +3,7 @@ namespace Phiki\Tests\Fixtures; use Phiki\Contracts\ExtensionInterface; -use Phiki\Environment\Environment; +use Phiki\Environment; class EmptyExtension implements ExtensionInterface { diff --git a/tests/Pest.php b/tests/Pest.php index bf8b6001..d10c5138 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,12 +1,10 @@ uses(LaravelTestCase::class)->in('Adapters/Laravel'); @@ -23,9 +21,7 @@ function tokenize(string $input, array|Grammar $grammar): array $parsedGrammar = $grammar->toParsedGrammar(new GrammarRepository); } - $tokenizer = new Tokenizer($parsedGrammar, Environment::default()); - - return $tokenizer->tokenize($input); + return (new Phiki)->codeToTokens($input, $parsedGrammar); } function highlight(array $tokens, array $theme): array @@ -49,5 +45,5 @@ function highlight(array $tokens, array $theme): array $value = Theme::parse($value); } - return (new Highlighter($theme))->highlight($tokens); + return (new Phiki)->tokensToHighlightedTokens($tokens, $theme); } From 1c9572c681610941288b2ae434c6d3a4c14bfd45 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:45:38 +0100 Subject: [PATCH 02/13] Move resolution methods --- src/Environment.php | 18 ------------------ src/Grammar/GrammarRepository.php | 12 ++++++++++++ src/Phiki.php | 6 +++--- src/Theme/ThemeRepository.php | 12 ++++++++++++ 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 755355fb..afba28b6 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -29,22 +29,4 @@ public function addExtension(ExtensionInterface $extension): static return $this; } - - public function resolveGrammar(string|Grammar|ParsedGrammar $grammar): ParsedGrammar - { - return match (true) { - is_string($grammar) => $this->grammars->get($grammar), - $grammar instanceof Grammar => $grammar->toParsedGrammar($this->grammars), - $grammar instanceof ParsedGrammar => $grammar, - }; - } - - public function resolveTheme(string|Theme|ParsedTheme $theme): ParsedTheme - { - return match (true) { - is_string($theme) => $this->themes->get($theme), - $theme instanceof Theme => $theme->toParsedTheme($this->themes), - $theme instanceof ParsedTheme => $theme, - }; - } } diff --git a/src/Grammar/GrammarRepository.php b/src/Grammar/GrammarRepository.php index 0dcd525b..26f5970b 100644 --- a/src/Grammar/GrammarRepository.php +++ b/src/Grammar/GrammarRepository.php @@ -66,4 +66,16 @@ public function register(string $name, string|ParsedGrammar $pathOrGrammar): voi { $this->grammars[$name] = $pathOrGrammar; } + + public function resolve(string|Grammar|ParsedGrammar $grammar): ParsedGrammar + { + if ($grammar instanceof ParsedGrammar) { + return $grammar; + } + + return match (true) { + is_string($grammar) => $this->get($grammar), + $grammar instanceof Grammar => $grammar->toParsedGrammar($this), + }; + } } diff --git a/src/Phiki.php b/src/Phiki.php index 45a531cc..ad852b72 100644 --- a/src/Phiki.php +++ b/src/Phiki.php @@ -29,7 +29,7 @@ public function environment(): Environment public function codeToTokens(string $code, string|Grammar|ParsedGrammar $grammar): array { - $grammar = $this->environment->resolveGrammar($grammar); + $grammar = $this->environment->grammars->resolve($grammar); $tokenizer = new Tokenizer($grammar, $this->environment); return $tokenizer->tokenize($code); @@ -54,7 +54,7 @@ public function codeToHighlightedTokens(string $code, string|Grammar $grammar, s public function codeToHtml(string $code, string|Grammar $grammar, string|array|Theme $theme): PendingHtmlOutput { - return (new PendingHtmlOutput($code, $this->environment->resolveGrammar($grammar), $this->wrapThemes($theme))) + return (new PendingHtmlOutput($code, $this->environment->grammars->resolve($grammar), $this->wrapThemes($theme))) ->generateTokensUsing(fn (string $code, ParsedGrammar $grammar) => $this->codeToTokens($code, $grammar)) ->highlightTokensUsing(fn (array $tokens, array $themes) => $this->tokensToHighlightedTokens($tokens, $themes)); } @@ -65,7 +65,7 @@ protected function wrapThemes(string|array|Theme $themes): array $themes = ['default' => $themes]; } - return Arr::map($themes, fn (string|Theme|ParsedTheme $theme): ParsedTheme => $this->environment->resolveTheme($theme)); + return Arr::map($themes, fn (string|Theme|ParsedTheme $theme): ParsedTheme => $this->environment->themes->resolve($theme)); } public function addExtension(ExtensionInterface $extension): static diff --git a/src/Theme/ThemeRepository.php b/src/Theme/ThemeRepository.php index caa69d29..ae225bd8 100644 --- a/src/Theme/ThemeRepository.php +++ b/src/Theme/ThemeRepository.php @@ -42,4 +42,16 @@ public function register(string $name, string|ParsedTheme $pathOrTheme): void { $this->themes[$name] = $pathOrTheme; } + + public function resolve(string|Theme|ParsedTheme $theme): ParsedTheme + { + if ($theme instanceof ParsedTheme) { + return $theme; + } + + return match (true) { + is_string($theme) => $this->get($theme), + $theme instanceof Theme => $theme->toParsedTheme($this), + }; + } } From fc2f8b756762a6f5f9909824f4324a478dbb6741 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:48:19 +0100 Subject: [PATCH 03/13] Simplify method names --- src/Adapters/Laravel/Facades/Phiki.php | 6 +++--- src/Environment.php | 2 +- src/Phiki.php | 8 ++++---- tests/Adapters/Laravel/Facades/PhikiTest.php | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Adapters/Laravel/Facades/Phiki.php b/src/Adapters/Laravel/Facades/Phiki.php index 34a258b2..c5b5c5c6 100644 --- a/src/Adapters/Laravel/Facades/Phiki.php +++ b/src/Adapters/Laravel/Facades/Phiki.php @@ -10,9 +10,9 @@ * @method static array> tokensToHighlightedTokens(array> $tokens, string|array|\Phiki\Theme\Theme $theme) * @method static array> codeToHighlightedTokens(string $code, string|\Phiki\Grammar\Grammar $grammar, string|array|\Phiki\Theme\Theme $theme) * @method static \Phiki\Output\Html\PendingHtmlOutput codeToHtml(string $code, string|\Phiki\Grammar\Grammar $grammar, string|array|\Phiki\Theme\Theme $theme) - * @method static \Phiki\Phiki addExtension(\Phiki\Contracts\ExtensionInterface $extension) - * @method static \Phiki\Phiki registerGrammar(string $name, string|\Phiki\Grammar\ParsedGrammar $pathOrGrammar) - * @method static \Phiki\Phiki registerTheme(string $name, string|\Phiki\Theme\ParsedTheme $pathOrTheme) + * @method static \Phiki\Phiki extend(\Phiki\Contracts\ExtensionInterface $extension) + * @method static \Phiki\Phiki grammar(string $name, string|\Phiki\Grammar\ParsedGrammar $pathOrGrammar) + * @method static \Phiki\Phiki theme(string $name, string|\Phiki\Theme\ParsedTheme $pathOrTheme) * * @see \Phiki\Phiki */ diff --git a/src/Environment.php b/src/Environment.php index afba28b6..3f1168ed 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -23,7 +23,7 @@ public function __construct() $this->themes = new ThemeRepository; } - public function addExtension(ExtensionInterface $extension): static + public function extend(ExtensionInterface $extension): static { $extension->register($this); diff --git a/src/Phiki.php b/src/Phiki.php index ad852b72..feea0c6d 100644 --- a/src/Phiki.php +++ b/src/Phiki.php @@ -68,21 +68,21 @@ protected function wrapThemes(string|array|Theme $themes): array return Arr::map($themes, fn (string|Theme|ParsedTheme $theme): ParsedTheme => $this->environment->themes->resolve($theme)); } - public function addExtension(ExtensionInterface $extension): static + public function extend(ExtensionInterface $extension): static { - $this->environment->addExtension($extension); + $this->environment->extend($extension); return $this; } - public function registerGrammar(string $name, string|ParsedGrammar $pathOrGrammar): static + public function grammar(string $name, string|ParsedGrammar $pathOrGrammar): static { $this->environment->grammars->register($name, $pathOrGrammar); return $this; } - public function registerTheme(string $name, string|ParsedTheme $pathOrTheme): static + public function theme(string $name, string|ParsedTheme $pathOrTheme): static { $this->environment->themes->register($name, $pathOrTheme); diff --git a/tests/Adapters/Laravel/Facades/PhikiTest.php b/tests/Adapters/Laravel/Facades/PhikiTest.php index 95d31c19..dbd119cc 100644 --- a/tests/Adapters/Laravel/Facades/PhikiTest.php +++ b/tests/Adapters/Laravel/Facades/PhikiTest.php @@ -12,13 +12,13 @@ }); it('can register a custom grammar', function () { - Phiki::registerGrammar('custom', __DIR__ . '/../../../Fixtures/example.json'); + Phiki::grammar('custom', __DIR__ . '/../../../Fixtures/example.json'); expect(Phiki::environment()->grammars->has('custom'))->toBeTrue(); }); it('can register a custom theme', function () { - Phiki::registerTheme('custom', __DIR__ . '/../../../Fixtures/theme.json'); + Phiki::theme('custom', __DIR__ . '/../../../Fixtures/theme.json'); expect(Phiki::environment()->themes->has('custom'))->toBeTrue(); }); @@ -26,7 +26,7 @@ it('can register an extension', function () { $extension = new EmptyExtension(); - Phiki::addExtension($extension); + Phiki::extend($extension); expect($extension->registered)->toBeTrue(); }); From d62049c1e015421a9ac35dddfdec6346fe0dc9e4 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:50:05 +0100 Subject: [PATCH 04/13] Remove default extension --- src/Environment.php | 1 - src/Extensions/DefaultExtension.php | 18 ------------------ 2 files changed, 19 deletions(-) delete mode 100644 src/Extensions/DefaultExtension.php diff --git a/src/Environment.php b/src/Environment.php index 3f1168ed..0f8f92b0 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -3,7 +3,6 @@ namespace Phiki; use Phiki\Contracts\ExtensionInterface; -use Phiki\Extensions\DefaultExtension; use Phiki\Grammar\Grammar; use Phiki\Grammar\GrammarRepository; use Phiki\Grammar\ParsedGrammar; diff --git a/src/Extensions/DefaultExtension.php b/src/Extensions/DefaultExtension.php deleted file mode 100644 index bff2faf2..00000000 --- a/src/Extensions/DefaultExtension.php +++ /dev/null @@ -1,18 +0,0 @@ -useGrammarRepository(new GrammarRepository) - ->useThemeRepository(new ThemeRepository); - } -} From b0c3056289f4cf58c8807f8e493deb21746292cb Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:51:38 +0100 Subject: [PATCH 05/13] Add resolve methods to interfaces --- src/Contracts/GrammarRepositoryInterface.php | 6 ++++++ src/Contracts/ThemeRepositoryInterface.php | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/Contracts/GrammarRepositoryInterface.php b/src/Contracts/GrammarRepositoryInterface.php index 17f61988..611ac952 100644 --- a/src/Contracts/GrammarRepositoryInterface.php +++ b/src/Contracts/GrammarRepositoryInterface.php @@ -2,6 +2,7 @@ namespace Phiki\Contracts; +use Phiki\Grammar\Grammar; use Phiki\Grammar\ParsedGrammar; interface GrammarRepositoryInterface @@ -40,4 +41,9 @@ public function has(string $name): bool; * @param string|ParsedGrammar $pathOrGrammar The path to the grammar file or the grammar itself. */ public function register(string $name, string|ParsedGrammar $pathOrGrammar): void; + + /** + * Resolve the given grammar from the repository. + */ + public function resolve(string|Grammar|ParsedGrammar $theme): ParsedGrammar; } diff --git a/src/Contracts/ThemeRepositoryInterface.php b/src/Contracts/ThemeRepositoryInterface.php index c5e8ba73..5d04da51 100644 --- a/src/Contracts/ThemeRepositoryInterface.php +++ b/src/Contracts/ThemeRepositoryInterface.php @@ -3,6 +3,7 @@ namespace Phiki\Contracts; use Phiki\Theme\ParsedTheme; +use Phiki\Theme\Theme; interface ThemeRepositoryInterface { @@ -31,4 +32,9 @@ public function has(string $name): bool; * @param string|ParsedTheme $pathOrTheme The path to the theme file or the theme itself. */ public function register(string $name, string|ParsedTheme $pathOrTheme): void; + + /** + * Resolve the given theme from the repository. + */ + public function resolve(string|Theme|ParsedTheme $theme): ParsedTheme; } From a8f744a11f15924f12433bef71cd96683019a5e4 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:52:38 +0100 Subject: [PATCH 06/13] Simplify comments --- src/Contracts/GrammarRepositoryInterface.php | 13 +------------ src/Contracts/ThemeRepositoryInterface.php | 7 ------- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/src/Contracts/GrammarRepositoryInterface.php b/src/Contracts/GrammarRepositoryInterface.php index 611ac952..752efd65 100644 --- a/src/Contracts/GrammarRepositoryInterface.php +++ b/src/Contracts/GrammarRepositoryInterface.php @@ -10,10 +10,6 @@ interface GrammarRepositoryInterface /** * Get a grammar from the repository. * - * If the grammar is not already loaded, it will be loaded and cached. - * - * @param string $name The name of the grammar. - * * @throws \Phiki\Exceptions\UnrecognisedGrammarException If the grammar is not registered. */ public function get(string $name): ParsedGrammar; @@ -21,24 +17,17 @@ public function get(string $name): ParsedGrammar; /** * Get a grammar from the repository by scope name. * - * @param string $scope The name of the scope. - * * @throws \Phiki\Exceptions\UnrecognisedGrammarException If the grammar is not registered. */ public function getFromScope(string $scope): ParsedGrammar; /** * Check whether a grammar exists in the repository. - * - * @param string $name The name of the grammar. */ public function has(string $name): bool; /** - * Register a new Grammar to use when highlighting. - * - * @param string $name The name of the grammar. - * @param string|ParsedGrammar $pathOrGrammar The path to the grammar file or the grammar itself. + * Register a new grammar to use when highlighting. */ public function register(string $name, string|ParsedGrammar $pathOrGrammar): void; diff --git a/src/Contracts/ThemeRepositoryInterface.php b/src/Contracts/ThemeRepositoryInterface.php index 5d04da51..95234f90 100644 --- a/src/Contracts/ThemeRepositoryInterface.php +++ b/src/Contracts/ThemeRepositoryInterface.php @@ -12,24 +12,17 @@ interface ThemeRepositoryInterface * * If the theme is not already loaded, it will be loaded and cached. * - * @param string $name The name of the theme. - * * @throws \Phiki\Exceptions\UnrecognisedThemeException If the theme is not registered. */ public function get(string $name): ParsedTheme; /** * Check whether a theme exists in the repository. - * - * @param string $name The name of the theme. */ public function has(string $name): bool; /** * Register a new theme to use when highlighting. - * - * @param string $name The name of the theme. - * @param string|ParsedTheme $pathOrTheme The path to the theme file or the theme itself. */ public function register(string $name, string|ParsedTheme $pathOrTheme): void; From 78ca18fe4bda2ebe370c684f7329800ff037fba9 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:53:08 +0100 Subject: [PATCH 07/13] Remove unused exception --- src/Exceptions/EnvironmentException.php | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 src/Exceptions/EnvironmentException.php diff --git a/src/Exceptions/EnvironmentException.php b/src/Exceptions/EnvironmentException.php deleted file mode 100644 index e9a2ca23..00000000 --- a/src/Exceptions/EnvironmentException.php +++ /dev/null @@ -1,18 +0,0 @@ - Date: Sat, 23 Aug 2025 17:53:42 +0100 Subject: [PATCH 08/13] Remove another unused exception --- src/Exceptions/IndeterminateStateException.php | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/Exceptions/IndeterminateStateException.php diff --git a/src/Exceptions/IndeterminateStateException.php b/src/Exceptions/IndeterminateStateException.php deleted file mode 100644 index 392946ca..00000000 --- a/src/Exceptions/IndeterminateStateException.php +++ /dev/null @@ -1,10 +0,0 @@ - Date: Sat, 23 Aug 2025 17:54:43 +0100 Subject: [PATCH 09/13] Move CommonMark into adapters --- src/{ => Adapters}/CommonMark/CodeBlockRenderer.php | 2 +- src/{ => Adapters}/CommonMark/PhikiExtension.php | 2 +- .../it_can_be_configured_using_environment_config_array.snap | 2 ++ tests/{Unit => Adapters}/CommonMark/PhikiExtensionTest.php | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) rename src/{ => Adapters}/CommonMark/CodeBlockRenderer.php (97%) rename src/{ => Adapters}/CommonMark/PhikiExtension.php (97%) create mode 100644 tests/.pest/snapshots/Adapters/CommonMark/PhikiExtensionTest/it_can_be_configured_using_environment_config_array.snap rename tests/{Unit => Adapters}/CommonMark/PhikiExtensionTest.php (96%) diff --git a/src/CommonMark/CodeBlockRenderer.php b/src/Adapters/CommonMark/CodeBlockRenderer.php similarity index 97% rename from src/CommonMark/CodeBlockRenderer.php rename to src/Adapters/CommonMark/CodeBlockRenderer.php index ee5e5bb1..642c6e8a 100644 --- a/src/CommonMark/CodeBlockRenderer.php +++ b/src/Adapters/CommonMark/CodeBlockRenderer.php @@ -1,6 +1,6 @@ class A {} + diff --git a/tests/Unit/CommonMark/PhikiExtensionTest.php b/tests/Adapters/CommonMark/PhikiExtensionTest.php similarity index 96% rename from tests/Unit/CommonMark/PhikiExtensionTest.php rename to tests/Adapters/CommonMark/PhikiExtensionTest.php index 8cbd06ee..e9505480 100644 --- a/tests/Unit/CommonMark/PhikiExtensionTest.php +++ b/tests/Adapters/CommonMark/PhikiExtensionTest.php @@ -3,7 +3,7 @@ use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\MarkdownConverter; -use Phiki\CommonMark\PhikiExtension; +use Phiki\Adapters\CommonMark\PhikiExtension; use Phiki\Theme\Theme; it('registers renderers', function () { From 0e34573fba2ef8d69e90d75489ea2f69b1bd606a Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:55:56 +0100 Subject: [PATCH 10/13] Tidy up test cases --- src/Tests/Adapters/Laravel/TestCase.php | 11 +++++++++++ tests/LaravelTestCase.php | 11 ----------- tests/Pest.php | 3 +-- 3 files changed, 12 insertions(+), 13 deletions(-) create mode 100644 src/Tests/Adapters/Laravel/TestCase.php delete mode 100644 tests/LaravelTestCase.php diff --git a/src/Tests/Adapters/Laravel/TestCase.php b/src/Tests/Adapters/Laravel/TestCase.php new file mode 100644 index 00000000..35c5e418 --- /dev/null +++ b/src/Tests/Adapters/Laravel/TestCase.php @@ -0,0 +1,11 @@ +uses(LaravelTestCase::class)->in('Adapters/Laravel'); +pest()->uses(\Phiki\Tests\Adapters\Laravel\TestCase::class)->in('Adapters/Laravel'); function tokenize(string $input, array|Grammar $grammar): array { From caac8f446492f79e121aeb81461a1aaa8ff4b8a4 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:56:38 +0100 Subject: [PATCH 11/13] Update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b693daa3..aa0ea56c 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ All you need to do is register the extension through a CommonMark `Environment` use League\CommonMark\Environment\Environment; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\MarkdownConverter; -use Phiki\CommonMark\PhikiExtension; +use Phiki\Adapters\CommonMark\PhikiExtension; $environment = new Environment; $environment @@ -73,7 +73,7 @@ $output = $converter->convert(<<<'MD' If you're using Laravel's `Str::markdown()` or `str()->markdown()` methods, you can use the same CommonMark extension by passing it through to the method. ```php -use Phiki\CommonMark\PhikiExtension; +use Phiki\Adapters\CommonMark\PhikiExtension; Str::markdown('...', extensions: [ new PhikiExtension('github-dark'), From 22119a72e4b4e86d963486f5cd65dc4dcd33910d Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 17:57:58 +0100 Subject: [PATCH 12/13] Update README --- README.md | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index aa0ea56c..b1da2fe3 100644 --- a/README.md +++ b/README.md @@ -87,20 +87,11 @@ To use a language or theme that Phiki doesn't support, you need to register it w This can be done by building a custom `Environment` object and telling Phiki to use this instead of the default one. ```php -use Phiki\Environment\Environment; - -$environment = Environment::default(); - -// Register a custom language. -$environment - ->getGrammarRepository() - ->register('my-language', __DIR__ . '/../path/to/grammar.json'); - -$environment - ->getThemeRepository() - ->register('my-theme', __DIR__ . '/../path/to/theme.json'); +use Phiki\Phiki; -$phiki = new Phiki($environment); +$phiki = (new Phiki) + ->grammar('my-language', __DIR__ . '/../path/to/grammar.json') + ->theme('my-theme', __DIR__ . '/../path/to/theme.json'); $phiki->codeToHtml('...', 'my-language', 'my-theme'); ``` From e051919cd910b86806eb44eec0ce27aa8ba7acd3 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Sat, 23 Aug 2025 18:01:05 +0100 Subject: [PATCH 13/13] Make environment public and readonly --- src/Adapters/Laravel/Facades/Phiki.php | 1 - src/Phiki.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Adapters/Laravel/Facades/Phiki.php b/src/Adapters/Laravel/Facades/Phiki.php index c5b5c5c6..4a86606f 100644 --- a/src/Adapters/Laravel/Facades/Phiki.php +++ b/src/Adapters/Laravel/Facades/Phiki.php @@ -5,7 +5,6 @@ use Illuminate\Support\Facades\Facade; /** - * @method static \Phiki\Environment environment() * @method static array> codeToTokens(string $code, string|\Phiki\Grammar\Grammar|\Phiki\Grammar\ParsedGrammar $grammar) * @method static array> tokensToHighlightedTokens(array> $tokens, string|array|\Phiki\Theme\Theme $theme) * @method static array> codeToHighlightedTokens(string $code, string|\Phiki\Grammar\Grammar $grammar, string|array|\Phiki\Theme\Theme $theme) diff --git a/src/Phiki.php b/src/Phiki.php index feea0c6d..49693928 100644 --- a/src/Phiki.php +++ b/src/Phiki.php @@ -15,7 +15,7 @@ class Phiki { - protected Environment $environment; + public readonly Environment $environment; public function __construct() {