mirror of
https://github.com/erusev/parsedown.git
synced 2023-08-10 21:13:06 +03:00
Caching for initial configurable values removes need to seed state
This commit is contained in:
parent
bc3c1544c5
commit
48c0c34470
|
@ -32,6 +32,7 @@
|
|||
<referencedMethod name="Erusev\Parsedown\Configurables\BlockTypes::settingUnmarked" />
|
||||
<referencedMethod name="Erusev\Parsedown\Configurables\BlockTypes::removing" />
|
||||
<referencedMethod name="Erusev\Parsedown\Configurables\Breaks::enabled" />
|
||||
<referencedMethod name="Erusev\Parsedown\State::mergingWith" />
|
||||
</errorLevel>
|
||||
</PossiblyUnusedMethod>
|
||||
</issueHandlers>
|
||||
|
|
|
@ -28,12 +28,6 @@ final class Parsedown
|
|||
public function __construct(State $State = null)
|
||||
{
|
||||
$this->State = $State ?: new State;
|
||||
|
||||
// ensure we cache the initial value if these weren't explicitly set
|
||||
$this->State = $this->State->mergingWith(new State([
|
||||
$this->State->get(BlockTypes::class),
|
||||
$this->State->get(InlineTypes::class),
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,9 +9,14 @@ final class State
|
|||
{
|
||||
/**
|
||||
* @var array<class-string<Configurable>, Configurable>
|
||||
* */
|
||||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @var array<class-string<Configurable>, Configurable>
|
||||
*/
|
||||
private static $initialCache;
|
||||
|
||||
/**
|
||||
* @param Configurable[] $Configurables
|
||||
*/
|
||||
|
@ -48,13 +53,18 @@ final class State
|
|||
* @template-typeof T $configurableClass
|
||||
* @param class-string<Configurable> $configurableClass
|
||||
* @return T
|
||||
* */
|
||||
*/
|
||||
public function get($configurableClass)
|
||||
{
|
||||
return (isset($this->state[$configurableClass])
|
||||
? $this->state[$configurableClass]
|
||||
: $configurableClass::initial()
|
||||
);
|
||||
if (isset($this->state[$configurableClass])) {
|
||||
return $this->state[$configurableClass];
|
||||
}
|
||||
|
||||
if (! isset(self::$initialCache[$configurableClass])) {
|
||||
self::$initialCache[$configurableClass] = $configurableClass::initial();
|
||||
}
|
||||
|
||||
return self::$initialCache[$configurableClass];
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
|
|
Loading…
Reference in New Issue
Block a user