<commit_before><?php

declare(strict_types=1);

namespace Ray\Compiler;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\CacheProvider;
use Ray\Di\InjectorInterface;

final class CachedInjectorFactory
{
    /**
     * @var array<string, CacheProvider>
     */
    private static $cache = [];

    private function __construct()
    {
    }

    /**
     * @param callable():\Ray\Di\AbstractModule $modules
     * @param array<class-string>               $savedSingletons
     */
    public static function getInstance(string $injectorId, string $scriptDir, callable $modules, CacheProvider $cache = null, array $savedSingletons = []) : InjectorInterface
    {
        if (! isset(self::$cache[$injectorId])) {
            self::$cache[$injectorId] = $cache ?? new ArrayCache;
        }
        $cache = self::$cache[$injectorId];
        $cache->setNamespace($injectorId);
        /** @var ?InjectorInterface $cachedInjector */
        $cachedInjector = $cache->fetch(InjectorInterface::class);
        if ($cachedInjector instanceof InjectorInterface) {
            return $cachedInjector;
        }
        $injector = InjectorFactory::getInstance($modules, $scriptDir);
        self::saveSingletons($injector, $savedSingletons);
        $cache->save(InjectorInterface::class, $injector);

        return $injector;
    }

    /**
     * @param array<class-string> $savedSingletons
     */
    private static function saveSingletons(InjectorInterface $injector, array $savedSingletons) : void
    {
        foreach ($savedSingletons as $singleton) {
            $injector->getInstance($singleton);
        }
    }
}<commit_msg>save injector only when ScriptInjector<commit_after><?php

declare(strict_types=1);

namespace Ray\Compiler;

use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\VoidCache;
use Ray\Di\InjectorInterface;

final class CachedInjectorFactory
{
    /**
     * @var array<string, InjectorInterface>
     */
    private static $injectors = [];

    private function __construct()
    {
    }

    /**
     * @param callable():\Ray\Di\AbstractModule $modules
     * @param array<class-string>               $savedSingletons
     */
    public static function getInstance(string $injectorId, string $scriptDir, callable $modules, CacheProvider $cache = null, array $savedSingletons = []) : InjectorInterface
    {
        if (isset(self::$injectors[$injectorId])) {
            return self::$injectors[$injectorId];
        }
        $cache = $cache ?? new VoidCache;
        $cache->setNamespace($injectorId);
        $cachedInjector = $cache->fetch(InjectorInterface::class);
        if ($cachedInjector instanceof InjectorInterface) {
            return $cachedInjector;
        }
        $injector = self::getInjector($modules, $scriptDir, $savedSingletons);
        if ($injector instanceof ScriptInjector) {
            $cache->save(InjectorInterface::class, $injector);
        }
        self::$injectors[$injectorId] = $injector;

        return $injector;
    }

    /**
     * @param callable():\Ray\Di\AbstractModule $modules
     * @param array<class-string>               $savedSingletons
     */
    private static function getInjector(callable $modules, string $scriptDir, array $savedSingletons) : InjectorInterface
    {
        $injector = InjectorFactory::getInstance($modules, $scriptDir);
        foreach ($savedSingletons as $singleton) {
            $injector->getInstance($singleton);
        }

        return $injector;
    }
}
{'fraction_non_alphanumeric': 0.12300228310502283, 'fraction_numerical': 0.0005707762557077625, 'mean_word_length': 2.9604519774011298, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6412535', 'n_tokens_mistral': 1000, 'n_tokens_neox': 1003, 'n_words': 192}