HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/kommabasic.nl/vendor/komma/kms/src/Core/Translator.php
<?php declare(strict_types=1);


namespace Komma\KMS\Core;


class Translator extends \Illuminate\Translation\Translator
{

    private static bool $manualDisableThrowingTranslationException = false;

    /**
     * Manual disable throwing translation exception.
     * For example during unit test.
     *
     * NOTE: This should globally be disabled by setting your env key APP_THROW_TRANSLATION_EXCEPTIONS on false.
     */
    public static function disableThrowingTranslationException()
    {
        if (config('app.throw_translation_exceptions')) {
            self::$manualDisableThrowingTranslationException = true;
            config()->set('app.throw_translation_exceptions', false);
        }
    }

    /**
     * Manual enable throwing translation exception
     * For example during unit test.
     *
     * NOTE: This should globally be disabled by setting your env key APP_THROW_TRANSLATION_EXCEPTIONS on true.
     */
    public static function enableThrowingTranslationException()
    {
        if(self::$manualDisableThrowingTranslationException) {
            self::$manualDisableThrowingTranslationException = false;
            config()->set('app.throw_translation_exceptions', true);
        }
    }

    /**
     * Throw an exception when a translation key does not exist.
     * But only if it did not occur in laravel's package itself (sometimes it happens!).
     *
     * @throws \Exception
     *
     * @param array  $replace
     * @param null   $locale
     * @param bool   $fallback
     * @param string $key
     *
     * @return array|string
     */
    public function get($key, array $replace = [], $locale = null, $fallback = true)
    {
        $translation = parent::get($key, $replace, $locale, $fallback);
        if($this->shouldIgnoreError() || $key !== $translation || config('app.throw_translation_exceptions') == false) return $translation;

        throw new \Exception('Translation key for locale "'.($locale ?? app()->getLocale()).'" not found: "'. $key .'". Please make sure it exists.');
    }

    /**
     * Use the orignal get method to prevent the throwing of the exception.
     *
     * @param string $key
     * @param null   $locale
     * @param bool   $fallback
     *
     * @return bool
     */
    public function has($key, $locale = null, $fallback = true)
    {
        //Make sure we use the original get function to prevent the throwing of the exception.
        return parent::get($key, [], $locale, $fallback) !== $key;
    }

    /**
     * Ignore the error when it originated from the illuminate namespace.
     * That is the laravel frameworks main namespace.
     *
     * @return bool
     */
    public function shouldIgnoreError() {
        if(count(debug_backtrace()) < 3) return false;
        $relevantStackItem = debug_backtrace()[2];
        $calledFromClass = array_key_exists('class', $relevantStackItem);
        return ($calledFromClass && strpos($relevantStackItem['class'], 'Illuminate') !== false);
    }
}