<commit_before><?php
declare(strict_types=1);

namespace Neos\Cache\Backend;

/*
 * This file is part of the Neos.Cache package.
 *
 * (c) Contributors of the Neos Project - www.neos.io
 *
 * This package is Open Source Software. For the full copyright and license
 * information, please view the LICENSE file which was distributed with this
 * source code.
 */

use Neos\Cache\BackendInstantiationTrait;
use Neos\Cache\EnvironmentConfiguration;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Psr\Log\LoggerInterface;
use Throwable;

/**
 * A multi backend, falling back to multiple backends if errors occur.
 */
class MultiBackend extends AbstractBackend
{
    use BackendInstantiationTrait;

    protected array $backendConfigurations = [];
    protected array $backends = [];
    protected bool $setInAllBackends = true;
    protected bool $debug = false;
    protected bool $logErrors = true;
    protected bool $removeUnhealthyBackends = true;
    protected bool $initialized = false;

    protected ?LoggerInterface $logger = null;
    protected ?ThrowableStorageInterface $throwableStorage = null;

    public function __construct(EnvironmentConfiguration $environmentConfiguration = null, array $options = [])
    {
        parent::__construct($environmentConfiguration, $options);

        if ($this->logErrors && class_exists(Bootstrap::class) && Bootstrap::$staticObjectManager instanceof ObjectManagerInterface) {
            $logger = Bootstrap::$staticObjectManager->get(LoggerInterface::class);
            assert($logger instanceof LoggerInterface);
            $this->logger = $logger;
            $throwableStorage = Bootstrap::$staticObjectManager->get(ThrowableStorageInterface::class);
            assert($throwableStorage instanceof ThrowableStorageInterface);
            $this->throwableStorage = $throwableStorage;
        }
    }

    /**
     * @throws Throwable
     */
    protected function prepareBackends(): void
    {
        if ($this->initialized === true) {
            return;
        }
        foreach ($this->backendConfigurations as $backendConfiguration) {
            $backendOptions = $backendConfiguration['backendOptions'] ?? [];
            $backend = $this->buildSubBackend($backendConfiguration['backend'], $backendOptions);
            if ($backend !== null) {
                $this->backends[] = $backend;
            }
        }
        $this->initialized = true;
    }

    /**
     * @throws Throwable
     */
    protected function buildSubBackend(string $backendClassName, array $backendOptions): ?BackendInterface<commit_msg>BUGFIX: Fix logging in MultiBackend<commit_after><?php
declare(strict_types=1);

namespace Neos\Cache\Backend;

/*
 * This file is part of the Neos.Cache package.
 *
 * (c) Contributors of the Neos Project - www.neos.io
 *
 * This package is Open Source Software. For the full copyright and license
 * information, please view the LICENSE file which was distributed with this
 * source code.
 */

use Neos\Cache\BackendInstantiationTrait;
use Neos\Cache\EnvironmentConfiguration;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\ObjectManagement\Exception\UnknownObjectException;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Psr\Log\LoggerInterface;
use Throwable;

/**
 * A multi backend, falling back to multiple backends if errors occur.
 */
class MultiBackend extends AbstractBackend
{
    use BackendInstantiationTrait;

    protected array $backendConfigurations = [];
    protected array $backends = [];
    protected bool $setInAllBackends = true;
    protected bool $debug = false;
    protected bool $logErrors = true;
    protected bool $removeUnhealthyBackends = true;
    protected bool $initialized = false;

    protected ?LoggerInterface $logger = null;
    protected ?ThrowableStorageInterface $throwableStorage = null;

    public function __construct(EnvironmentConfiguration $environmentConfiguration = null, array $options = [])
    {
        parent::__construct($environmentConfiguration, $options);

        if ($this->logErrors && class_exists(Bootstrap::class) && Bootstrap::$staticObjectManager instanceof ObjectManagerInterface) {
            try {
                $logger = Bootstrap::$staticObjectManager->get(LoggerInterface::class);
                assert($logger instanceof LoggerInterface);
                $this->logger = $logger;
                $throwableStorage = Bootstrap::$staticObjectManager->get(ThrowableStorageInterface::class);
                assert($throwableStorage instanceof ThrowableStorageInterface);
                $this->throwableStorage = $throwableStorage;
            } catch (UnknownObjectException) {
                // Logging might not be available during compile time
            }
        }
    }

    /**
     * @throws Throwable
     */
    protected function prepareBackends(): void
    {
        if ($this->initialized === true) {
            return;
        }
        foreach ($this->backendConfigurations as $backendConfiguration) {
            $backendOptions = $backendConfiguration['backendOptions'] ?? [];
            $backend = $this->buildSubBackend($backendConfiguration['backend'], $backendOptions);
            if ($backend !== null) {
                $this->backends[] = $backend;
            }
        }
        $this->initialized = true;
    }

    /**
     * @throws Throwable
     */
    protected function buildSubBackend(string $backendClassName, array $backendOptions): ?BackendInterface
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.00035211267605633805, 'mean_word_length': 3.2940287226001512, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1358175', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1412, 'n_words': 366}