You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.9 KiB
74 lines
1.9 KiB
3 years ago
|
<?php
|
||
|
/** @license MIT
|
||
|
* Copyright 2021 Dustin Wilson et al.
|
||
3 years ago
|
* See LICENSE file for details */
|
||
3 years ago
|
|
||
|
declare(strict_types=1);
|
||
3 years ago
|
namespace dW\Lit;
|
||
3 years ago
|
|
||
3 years ago
|
/** Static storage for grammars; a map of a scope string and a Grammar object */
|
||
3 years ago
|
class GrammarRegistry implements \IteratorAggregate {
|
||
3 years ago
|
protected static array $storage = [];
|
||
3 years ago
|
|
||
3 years ago
|
public static function clear(): bool {
|
||
3 years ago
|
self::$storage = [];
|
||
3 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
public static function delete(string $scopeName): bool {
|
||
|
try {
|
||
3 years ago
|
unset(self::$storage[$scopeName]);
|
||
3 years ago
|
} catch (\Exception $e) {
|
||
3 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
3 years ago
|
public static function get(string $scopeName): Grammar|bool {
|
||
|
if (array_key_exists($scopeName, self::$storage)) {
|
||
|
return self::$storage[$scopeName];
|
||
3 years ago
|
} else {
|
||
3 years ago
|
$filename = __DIR__ . "/../data/$scopeName.json";
|
||
3 years ago
|
if (file_exists($filename)) {
|
||
|
$grammar = new Grammar();
|
||
|
$grammar->loadJSON($filename);
|
||
3 years ago
|
return $grammar;
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
return false;
|
||
|
}
|
||
3 years ago
|
|
||
|
public function getIterator(): \Traversable {
|
||
|
foreach (self::$storage as $scopeName => $grammar) {
|
||
|
yield $scopeName => $grammar;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static function has(string $scopeName): bool {
|
||
|
return (array_key_exists($scopeName, self::$storage));
|
||
|
}
|
||
|
|
||
|
public static function keys(): \Traversable {
|
||
|
foreach (self::$storage as $scopeName => $_) {
|
||
|
yield $scopeName;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static function set(string $scopeName, Grammar $grammar): bool {
|
||
|
try {
|
||
|
self::$storage[$scopeName] = $grammar;
|
||
|
} catch (\Exception $e) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public function values(): \Traversable {
|
||
|
foreach (self::$storage as $grammar) {
|
||
|
yield $grammar;
|
||
|
}
|
||
|
}
|
||
3 years ago
|
}
|