mirror of
https://github.com/cerbero90/json-parser.git
synced 2025-06-11 02:40:58 +02:00
Reduce number of token instances
This commit is contained in:
@ -9,11 +9,11 @@ namespace Cerbero\JsonParser\Tokens;
|
|||||||
class Tokenizer
|
class Tokenizer
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The map of token instances.
|
* The map of token instances by type.
|
||||||
*
|
*
|
||||||
* @var array<int, Token>
|
* @var array<int, Token>
|
||||||
*/
|
*/
|
||||||
protected static array $tokensMap = [];
|
protected static array $tokensMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiate the class.
|
* Instantiate the class.
|
||||||
@ -21,23 +21,23 @@ class Tokenizer
|
|||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->hydrateTokens();
|
static::$tokensMap ??= $this->hydrateTokensMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the hydrated tokens
|
* Retrieve the hydrated tokens map
|
||||||
*
|
*
|
||||||
* @return void
|
* @return array<int, Token>
|
||||||
*/
|
*/
|
||||||
protected function hydrateTokens(): void
|
protected function hydrateTokensMap(): array
|
||||||
{
|
{
|
||||||
if (static::$tokensMap) {
|
$map = $instances = [];
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Tokens::MAP as $type => $class) {
|
foreach (Tokens::MAP as $type => $class) {
|
||||||
static::$tokensMap[$type] = new $class();
|
$map[$type] = $instances[$class] ??= new $class();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $map;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user