1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-05 18:41:02 +00:00

twig implementation

This commit is contained in:
Nicolas Lœuillet 2013-08-03 19:26:54 +02:00
parent 2b840e0cfb
commit 4f5b44bd3b
1418 changed files with 108207 additions and 1586 deletions

View file

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\ResourceBundle\Reader;
/**
* Base class for {@link BundleReaderInterface} implementations.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class AbstractBundleReader implements BundleReaderInterface
{
/**
* {@inheritdoc}
*/
public function getLocales($path)
{
$extension = '.' . $this->getFileExtension();
$locales = glob($path . '/*' . $extension);
// Remove file extension and sort
array_walk($locales, function (&$locale) use ($extension) { $locale = basename($locale, $extension); });
sort($locales);
return $locales;
}
/**
* Returns the extension of locale files in this bundle.
*
* @return string The file extension (without leading dot).
*/
abstract protected function getFileExtension();
}

View file

@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\ResourceBundle\Reader;
use Symfony\Component\Intl\Exception\RuntimeException;
use Symfony\Component\Intl\ResourceBundle\Util\ArrayAccessibleResourceBundle;
/**
* Reads binary .res resource bundles.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class BinaryBundleReader extends AbstractBundleReader implements BundleReaderInterface
{
/**
* {@inheritdoc}
*/
public function read($path, $locale)
{
// Point for future extension: Modify this class so that it works also
// if the \ResourceBundle class is not available.
$bundle = new \ResourceBundle($locale, $path);
if (null === $bundle) {
throw new RuntimeException(sprintf(
'Could not load the resource bundle "%s/%s.res".',
$path,
$locale
));
}
return new ArrayAccessibleResourceBundle($bundle);
}
/**
* {@inheritdoc}
*/
protected function getFileExtension()
{
return 'res';
}
}

View file

@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\ResourceBundle\Reader;
use Symfony\Component\Intl\ResourceBundle\Util\RingBuffer;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class BufferedBundleReader implements BundleReaderInterface
{
/**
* @var BundleReaderInterface
*/
private $reader;
private $buffer;
/**
* Buffers a given reader.
*
* @param BundleReaderInterface $reader The reader to buffer.
* @param integer $bufferSize The number of entries to store
* in the buffer.
*/
public function __construct(BundleReaderInterface $reader, $bufferSize)
{
$this->reader = $reader;
$this->buffer = new RingBuffer($bufferSize);
}
/**
* {@inheritdoc}
*/
public function read($path, $locale)
{
$hash = $path . '//' . $locale;
if (!isset($this->buffer[$hash])) {
$this->buffer[$hash] = $this->reader->read($path, $locale);
}
return $this->buffer[$hash];
}
/**
* {@inheritdoc}
*/
public function getLocales($path)
{
return $this->reader->getLocales($path);
}
}

View file

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\ResourceBundle\Reader;
/**
* Reads resource bundle files.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface BundleReaderInterface
{
/**
* Reads a resource bundle.
*
* @param string $path The path to the resource bundle.
* @param string $locale The locale to read.
*
* @return mixed Returns an array or {@link \ArrayAccess} instance for
* complex data, a scalar value otherwise.
*/
public function read($path, $locale);
/**
* Reads the available locales of a resource bundle.
*
* @param string $path The path to the resource bundle.
*
* @return string[] A list of supported locale codes.
*/
public function getLocales($path);
}

View file

@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\ResourceBundle\Reader;
use Symfony\Component\Intl\Exception\InvalidArgumentException;
use Symfony\Component\Intl\Exception\RuntimeException;
/**
* Reads .php resource bundles.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PhpBundleReader extends AbstractBundleReader implements BundleReaderInterface
{
/**
* {@inheritdoc}
*/
public function read($path, $locale)
{
if ('en' !== $locale) {
throw new InvalidArgumentException('Only the locale "en" is supported.');
}
$fileName = $path . '/' . $locale . '.php';
if (!file_exists($fileName)) {
throw new RuntimeException(sprintf(
'The resource bundle "%s/%s.php" does not exist.',
$path,
$locale
));
}
if (!is_file($fileName)) {
throw new RuntimeException(sprintf(
'The resource bundle "%s/%s.php" is not a file.',
$path,
$locale
));
}
return include $fileName;
}
/**
* {@inheritdoc}
*/
protected function getFileExtension()
{
return 'php';
}
}

View file

@ -0,0 +1,113 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\ResourceBundle\Reader;
use Symfony\Component\Intl\ResourceBundle\Util\RecursiveArrayAccess;
/**
* A structured reader wrapping an existing resource bundle reader.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @see StructuredResourceBundleBundleReaderInterface
*/
class StructuredBundleReader implements StructuredBundleReaderInterface
{
/**
* @var BundleReaderInterface
*/
private $reader;
/**
* Creates an entry reader based on the given resource bundle reader.
*
* @param BundleReaderInterface $reader A resource bundle reader to use.
*/
public function __construct(BundleReaderInterface $reader)
{
$this->reader = $reader;
}
/**
* {@inheritdoc}
*/
public function read($path, $locale)
{
return $this->reader->read($path, $locale);
}
/**
* {@inheritdoc}
*/
public function getLocales($path)
{
return $this->reader->getLocales($path);
}
/**
* {@inheritdoc}
*/
public function readEntry($path, $locale, array $indices, $fallback = true)
{
$data = $this->reader->read($path, $locale);
$entry = RecursiveArrayAccess::get($data, $indices);
$multivalued = is_array($entry) || $entry instanceof \Traversable;
if (!($fallback && (null === $entry || $multivalued))) {
return $entry;
}
if (null !== ($fallbackLocale = $this->getFallbackLocale($locale))) {
$parentEntry = $this->readEntry($path, $fallbackLocale, $indices, true);
if ($entry || $parentEntry) {
$multivalued = $multivalued || is_array($parentEntry) || $parentEntry instanceof \Traversable;
if ($multivalued) {
if ($entry instanceof \Traversable) {
$entry = iterator_to_array($entry);
}
if ($parentEntry instanceof \Traversable) {
$parentEntry = iterator_to_array($parentEntry);
}
$entry = array_merge(
$parentEntry ?: array(),
$entry ?: array()
);
} else {
$entry = null === $entry ? $parentEntry : $entry;
}
}
}
return $entry;
}
/**
* Returns the fallback locale for a given locale, if any
*
* @param string $locale The locale to find the fallback for.
*
* @return string|null The fallback locale, or null if no parent exists
*/
private function getFallbackLocale($locale)
{
if (false === $pos = strrpos($locale, '_')) {
return null;
}
return substr($locale, 0, $pos);
}
}

View file

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Intl\ResourceBundle\Reader;
/**
* Reads individual entries of a resource file.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface StructuredBundleReaderInterface extends BundleReaderInterface
{
/**
* Reads an entry from a resource bundle.
*
* An entry can be selected from the resource bundle by passing the path
* to that entry in the bundle. For example, if the bundle is structured
* like this:
*
* TopLevel
* NestedLevel
* Entry: Value
*
* Then the value can be read by calling:
*
* $reader->readEntry('...', 'en', array('TopLevel', 'NestedLevel', 'Entry'));
*
* @param string $path The path to the resource bundle.
* @param string $locale The locale to read.
* @param string[] $indices The indices to read from the bundle.
* @param Boolean $fallback Whether to merge the value with the value from
* the fallback locale (e.g. "en" for "en_GB").
* Only applicable if the result is multivalued
* (i.e. array or \ArrayAccess) or cannot be found
* in the requested locale.
*
* @return mixed Returns an array or {@link \ArrayAccess} instance for
* complex data, a scalar value for simple data and NULL
* if the given path could not be accessed.
*/
public function readEntry($path, $locale, array $indices, $fallback = true);
}