Skip to content

Commit 8649874

Browse files
committed
Merge #571 - Allow using ::class keyword to load a context
Pull-request: #571 Signed-off-by: William Desportes <[email protected]>
2 parents 456d946 + 91357ad commit 8649874

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/Context.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,13 @@ public static function load($context = '')
599599
}
600600

601601
if ($context[0] !== '\\') {
602-
// Short context name (must be formatted into class name).
603-
$context = self::$contextPrefix . $context;
602+
// Could be the fully qualified class name was given, like `ContextDBMS::class`.
603+
if (class_exists('\\' . $context)) {
604+
$context = '\\' . $context;
605+
} else {
606+
// Short context name (must be formatted into class name).
607+
$context = self::$contextPrefix . $context;
608+
}
604609
}
605610

606611
if (! class_exists($context)) {

tests/Lexer/ContextTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\SqlParser\Tests\Lexer;
66

77
use PhpMyAdmin\SqlParser\Context;
8+
use PhpMyAdmin\SqlParser\Contexts;
89
use PhpMyAdmin\SqlParser\Tests\TestCase;
910
use Throwable;
1011

@@ -110,10 +111,73 @@ public static function contextNamesProvider(): array
110111
['MySql50600'],
111112
['MySql50700'],
112113
['MySql80000'],
114+
['MySql80100'],
115+
['MySql80200'],
116+
['MySql80300'],
113117
['MariaDb100000'],
114118
['MariaDb100100'],
115119
['MariaDb100200'],
116120
['MariaDb100300'],
121+
['MariaDb100400'],
122+
['MariaDb100500'],
123+
['MariaDb100600'],
124+
['MariaDb100700'],
125+
['MariaDb100800'],
126+
['MariaDb100900'],
127+
['MariaDb101000'],
128+
['MariaDb101100'],
129+
['MariaDb110000'],
130+
['MariaDb110100'],
131+
['MariaDb110200'],
132+
['MariaDb110300'],
133+
['MariaDb110400'],
134+
];
135+
}
136+
137+
/**
138+
* @dataProvider contextClassesProvider
139+
*/
140+
public function testLoadAllByClass(string $context): void
141+
{
142+
Context::load($context);
143+
$this->assertEquals('\\' . $context, Context::$loadedContext);
144+
145+
// Restoring context.
146+
Context::load('');
147+
}
148+
149+
/**
150+
* @return string[][]
151+
*/
152+
public function contextClassesProvider(): array
153+
{
154+
return [
155+
[Contexts\ContextMySql50000::class],
156+
[Contexts\ContextMySql50100::class],
157+
[Contexts\ContextMySql50500::class],
158+
[Contexts\ContextMySql50600::class],
159+
[Contexts\ContextMySql50700::class],
160+
[Contexts\ContextMySql80000::class],
161+
[Contexts\ContextMySql80100::class],
162+
[Contexts\ContextMySql80200::class],
163+
[Contexts\ContextMySql80300::class],
164+
[Contexts\ContextMariaDb100000::class],
165+
[Contexts\ContextMariaDb100100::class],
166+
[Contexts\ContextMariaDb100200::class],
167+
[Contexts\ContextMariaDb100300::class],
168+
[Contexts\ContextMariaDb100400::class],
169+
[Contexts\ContextMariaDb100500::class],
170+
[Contexts\ContextMariaDb100600::class],
171+
[Contexts\ContextMariaDb100700::class],
172+
[Contexts\ContextMariaDb100800::class],
173+
[Contexts\ContextMariaDb100900::class],
174+
[Contexts\ContextMariaDb101000::class],
175+
[Contexts\ContextMariaDb101100::class],
176+
[Contexts\ContextMariaDb110000::class],
177+
[Contexts\ContextMariaDb110100::class],
178+
[Contexts\ContextMariaDb110200::class],
179+
[Contexts\ContextMariaDb110300::class],
180+
[Contexts\ContextMariaDb110400::class],
117181
];
118182
}
119183

0 commit comments

Comments
 (0)