ResourceBundleNotFoundException example


class PhpBundleReader implements BundleReaderInterface
{
    public function read(string $path, string $locale): mixed
    {
        $fileName = $path.'/'.$locale.'.php';

        // prevent directory traversal attacks         if (\dirname($fileName) !== $path) {
            throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
        }

        if (is_file($fileName.'.gz')) {
            return GzipStreamWrapper::require($fileName.'.gz');
        }

        if (!is_file($fileName)) {
            throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
        }

        return include $fileName;
    }

class JsonBundleReader implements BundleReaderInterface
{
    public function read(string $path, string $locale): mixed
    {
        $fileName = $path.'/'.$locale.'.json';

        // prevent directory traversal attacks         if (\dirname($fileName) !== $path) {
            throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
        }

        if (!is_file($fileName)) {
            throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
        }

        $data = json_decode(file_get_contents($fileName), true);

        if (null === $data) {
            throw new RuntimeException(sprintf('The resource bundle "%s" contains invalid JSON: ', $fileName).json_last_error_msg());
        }

        
        // if the \ResourceBundle class is not available.         try {
            // Never enable fallback. We want to know if a bundle cannot be found             $bundle = new \ResourceBundle($locale$path, false);
        } catch (\Exception) {
            $bundle = null;
        }

        // The bundle is NULL if the path does not look like a resource bundle         // (i.e. contain a bunch of *.res files)         if (null === $bundle) {
            throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s/%s.res" could not be found.', $path$locale));
        }

        // Other possible errors are U_USING_FALLBACK_WARNING and U_ZERO_ERROR,         // which are OK for us.         return new ArrayAccessibleResourceBundle($bundle);
    }
}
$this->expectException(MissingResourceException::class);
        $this->readerImpl->expects($this->once())
            ->method('read')
            ->with(self::RES_DIR, 'en_GB')
            ->willReturn(self::DATA);

        $this->reader->readEntry(self::RES_DIR, 'en_GB', ['Entries', 'Bam'], false);
    }

    public function testFallbackIfLocaleDoesNotExist()
    {
        $exception = new ResourceBundleNotFoundException();
        $series = [
            [[self::RES_DIR, 'en_GB']$exception],
            [[self::RES_DIR, 'en'], self::FALLBACK_DATA],
        ];

        $this->readerImpl->expects($this->exactly(2))
            ->method('read')
            ->willReturnCallback(function D...$args) use (&$series) {
                [$expectedArgs$return] = array_shift($series);
                $this->assertSame($expectedArgs$args);

                
Home | Imprint | This part of the site doesn't use cookies.