Initial import

This commit is contained in:
Nextcloud Team 2021-11-30 11:20:42 +01:00 committed by Lukas Reschke
commit 2295a33590
884 changed files with 93939 additions and 0 deletions

294
php/vendor/slim/twig-view/src/Twig.php vendored Normal file
View file

@ -0,0 +1,294 @@
<?php
/**
* Slim Framework (http://slimframework.com)
*
* @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Views;
use ArrayAccess;
use ArrayIterator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Throwable;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Extension\ExtensionInterface;
use Twig\Loader\FilesystemLoader;
use Twig\Loader\LoaderInterface;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
/**
* Twig View
*
* This class is a Slim Framework view helper built on top of the Twig templating component.
* Twig is a PHP component created by Fabien Potencier.
*
* @link https://twig.symfony.com/
*/
class Twig implements ArrayAccess
{
/**
* Twig loader
*
* @var LoaderInterface
*/
protected $loader;
/**
* Twig environment
*
* @var Environment
*/
protected $environment;
/**
* Default view variables
*
* @var array<string, mixed>
*/
protected $defaultVariables = [];
/**
* @param ServerRequestInterface $request
* @param string $attributeName
*
* @return Twig
*/
public static function fromRequest(ServerRequestInterface $request, string $attributeName = 'view'): self
{
$twig = $request->getAttribute($attributeName);
if ($twig === null || !($twig instanceof self)) {
throw new RuntimeException(
'Twig could not be found in the server request attributes using the key "'. $attributeName .'".'
);
}
return $twig;
}
/**
* @param string|string[] $path Path(s) to templates directory
* @param array<string, mixed> $settings Twig environment settings
*
* @throws LoaderError When the template cannot be found
*
* @return Twig
*/
public static function create($path, array $settings = []): self
{
$loader = new FilesystemLoader();
$paths = is_array($path) ? $path : [$path];
foreach ($paths as $namespace => $path) {
if (is_string($namespace)) {
$loader->setPaths($path, $namespace);
} else {
$loader->addPath($path);
}
}
return new self($loader, $settings);
}
/**
* @param LoaderInterface $loader Twig loader
* @param array<string, mixed> $settings Twig environment settings
*/
public function __construct(LoaderInterface $loader, array $settings = [])
{
$this->loader = $loader;
$this->environment = new Environment($this->loader, $settings);
}
/**
* Proxy method to add an extension to the Twig environment
*
* @param ExtensionInterface $extension A single extension instance or an array of instances
*/
public function addExtension(ExtensionInterface $extension): void
{
$this->environment->addExtension($extension);
}
/**
* Proxy method to add a runtime loader to the Twig environment
*
* @param RuntimeLoaderInterface $runtimeLoader
*/
public function addRuntimeLoader(RuntimeLoaderInterface $runtimeLoader): void
{
$this->environment->addRuntimeLoader($runtimeLoader);
}
/**
* Fetch rendered template
*
* @param string $template Template pathname relative to templates directory
* @param array<string, mixed> $data Associative array of template variables
*
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
* @throws RuntimeError When an error occurred during rendering
*
* @return string
*/
public function fetch(string $template, array $data = []): string
{
$data = array_merge($this->defaultVariables, $data);
return $this->environment->render($template, $data);
}
/**
* Fetch rendered block
*
* @param string $template Template pathname relative to templates directory
* @param string $block Name of the block within the template
* @param array<string, mixed> $data Associative array of template variables
*
* @throws Throwable When an error occurred during rendering
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
*
* @return string
*/
public function fetchBlock(string $template, string $block, array $data = []): string
{
$data = array_merge($this->defaultVariables, $data);
return $this->environment->resolveTemplate($template)->renderBlock($block, $data);
}
/**
* Fetch rendered string
*
* @param string $string String
* @param array<string, mixed> $data Associative array of template variables
*
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
*
* @return string
*/
public function fetchFromString(string $string = '', array $data = []): string
{
$data = array_merge($this->defaultVariables, $data);
return $this->environment->createTemplate($string)->render($data);
}
/**
* Output rendered template
*
* @param ResponseInterface $response
* @param string $template Template pathname relative to templates directory
* @param array<string, mixed> $data Associative array of template variables
*
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
* @throws RuntimeError When an error occurred during rendering
*
* @return ResponseInterface
*/
public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface
{
$response->getBody()->write($this->fetch($template, $data));
return $response;
}
/**
* Return Twig loader
*
* @return LoaderInterface
*/
public function getLoader(): LoaderInterface
{
return $this->loader;
}
/**
* Return Twig environment
*
* @return Environment
*/
public function getEnvironment(): Environment
{
return $this->environment;
}
/**
* Does this collection have a given key?
*
* @param string $key The data key
*
* @return bool
*/
public function offsetExists($key): bool
{
return array_key_exists($key, $this->defaultVariables);
}
/**
* Get collection item for key
*
* @param string $key The data key
*
* @return mixed The key's value, or the default value
*/
public function offsetGet($key)
{
if (!$this->offsetExists($key)) {
return null;
}
return $this->defaultVariables[$key];
}
/**
* Set collection item
*
* @param string $key The data key
* @param mixed $value The data value
*/
public function offsetSet($key, $value): void
{
$this->defaultVariables[$key] = $value;
}
/**
* Remove item from collection
*
* @param string $key The data key
*/
public function offsetUnset($key): void
{
unset($this->defaultVariables[$key]);
}
/**
* Get number of items in collection
*
* @return int
*/
public function count(): int
{
return count($this->defaultVariables);
}
/**
* Get collection iterator
*
* @return ArrayIterator<string, mixed>
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->defaultVariables);
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* Slim Framework (http://slimframework.com)
*
* @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Views;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class TwigExtension extends AbstractExtension
{
/**
* @return string
*/
public function getName(): string
{
return 'slim';
}
/**
* @return TwigFunction[]
*/
public function getFunctions(): array
{
return [
new TwigFunction('url_for', [TwigRuntimeExtension::class, 'urlFor']),
new TwigFunction('full_url_for', [TwigRuntimeExtension::class, 'fullUrlFor']),
new TwigFunction('is_current_url', [TwigRuntimeExtension::class, 'isCurrentUrl']),
new TwigFunction('current_url', [TwigRuntimeExtension::class, 'getCurrentUrl']),
new TwigFunction('get_uri', [TwigRuntimeExtension::class, 'getUri']),
new TwigFunction('base_path', [TwigRuntimeExtension::class, 'getBasePath']),
];
}
}

View file

@ -0,0 +1,127 @@
<?php
/**
* Slim Framework (http://slimframework.com)
*
* @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Views;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;
use Slim\App;
use Slim\Interfaces\RouteParserInterface;
class TwigMiddleware implements MiddlewareInterface
{
/**
* @var Twig
*/
protected $twig;
/**
* @var RouteParserInterface
*/
protected $routeParser;
/**
* @var string
*/
protected $basePath;
/**
* @var string|null
*/
protected $attributeName;
/**
* @param App $app
* @param string $containerKey
*
* @return TwigMiddleware
*/
public static function createFromContainer(App $app, string $containerKey = 'view'): self
{
$container = $app->getContainer();
if ($container === null) {
throw new RuntimeException('The app does not have a container.');
}
if (!$container->has($containerKey)) {
throw new RuntimeException(
"The specified container key does not exist: $containerKey"
);
}
$twig = $container->get($containerKey);
if (!($twig instanceof Twig)) {
throw new RuntimeException(
"Twig instance could not be resolved via container key: $containerKey"
);
}
return new self(
$twig,
$app->getRouteCollector()->getRouteParser(),
$app->getBasePath()
);
}
/**
* @param App $app
* @param Twig $twig
* @param string $attributeName
*
* @return TwigMiddleware
*/
public static function create(App $app, Twig $twig, string $attributeName = 'view'): self
{
return new self(
$twig,
$app->getRouteCollector()->getRouteParser(),
$app->getBasePath(),
$attributeName
);
}
/**
* @param Twig $twig
* @param RouteParserInterface $routeParser
* @param string $basePath
* @param string|null $attributeName
*/
public function __construct(
Twig $twig,
RouteParserInterface $routeParser,
string $basePath = '',
?string $attributeName = null
) {
$this->twig = $twig;
$this->routeParser = $routeParser;
$this->basePath = $basePath;
$this->attributeName = $attributeName;
}
/**
* {@inheritdoc}
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$runtimeLoader = new TwigRuntimeLoader($this->routeParser, $request->getUri(), $this->basePath);
$this->twig->addRuntimeLoader($runtimeLoader);
$extension = new TwigExtension();
$this->twig->addExtension($extension);
if ($this->attributeName !== null) {
$request = $request->withAttribute($this->attributeName, $this->twig);
}
return $handler->handle($request);
}
}

View file

@ -0,0 +1,152 @@
<?php
/**
* Slim Framework (http://slimframework.com)
*
* @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Views;
use Psr\Http\Message\UriInterface;
use Slim\Interfaces\RouteParserInterface;
class TwigRuntimeExtension
{
/**
* @var RouteParserInterface
*/
protected $routeParser;
/**
* @var string
*/
protected $basePath = '';
/**
* @var UriInterface
*/
protected $uri;
/**
* @param RouteParserInterface $routeParser Route parser
* @param UriInterface $uri Uri
* @param string $basePath Base path
*/
public function __construct(RouteParserInterface $routeParser, UriInterface $uri, string $basePath = '')
{
$this->routeParser = $routeParser;
$this->uri = $uri;
$this->basePath = $basePath;
}
/**
* Get the url for a named route
*
* @param string $routeName Route name
* @param array<string, string> $data Route placeholders
* @param array<string, string> $queryParams Query parameters
*
* @return string
*/
public function urlFor(string $routeName, array $data = [], array $queryParams = []): string
{
return $this->routeParser->urlFor($routeName, $data, $queryParams);
}
/**
* Get the full url for a named route
*
* @param string $routeName Route name
* @param array<string, string> $data Route placeholders
* @param array<string, string> $queryParams Query parameters
*
* @return string
*/
public function fullUrlFor(string $routeName, array $data = [], array $queryParams = []): string
{
return $this->routeParser->fullUrlFor($this->uri, $routeName, $data, $queryParams);
}
/**
* @param string $routeName Route name
* @param array<string, string> $data Route placeholders
*
* @return bool
*/
public function isCurrentUrl(string $routeName, array $data = []): bool
{
$currentUrl = $this->basePath.$this->uri->getPath();
$result = $this->routeParser->urlFor($routeName, $data);
return $result === $currentUrl;
}
/**
* Get current path on given Uri
*
* @param bool $withQueryString
*
* @return string
*/
public function getCurrentUrl(bool $withQueryString = false): string
{
$currentUrl = $this->basePath.$this->uri->getPath();
$query = $this->uri->getQuery();
if ($withQueryString && !empty($query)) {
$currentUrl .= '?'.$query;
}
return $currentUrl;
}
/**
* Get the uri
*
* @return UriInterface
*/
public function getUri(): UriInterface
{
return $this->uri;
}
/**
* Set the uri
*
* @param UriInterface $uri
*
* @return self
*/
public function setUri(UriInterface $uri): self
{
$this->uri = $uri;
return $this;
}
/**
* Get the base path
*
* @return string
*/
public function getBasePath(): string
{
return $this->basePath;
}
/**
* Set the base path
*
* @param string $basePath
*
* @return self
*/
public function setBasePath(string $basePath): self
{
$this->basePath = $basePath;
return $this;
}
}

View file

@ -0,0 +1,58 @@
<?php
/**
* Slim Framework (http://slimframework.com)
*
* @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Views;
use Psr\Http\Message\UriInterface;
use Slim\Interfaces\RouteParserInterface;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
class TwigRuntimeLoader implements RuntimeLoaderInterface
{
/**
* @var RouteParserInterface
*/
protected $routeParser;
/**
* @var UriInterface
*/
protected $uri;
/**
* @var string
*/
protected $basePath = '';
/**
* TwigRuntimeLoader constructor.
*
* @param RouteParserInterface $routeParser
* @param UriInterface $uri
* @param string $basePath
*/
public function __construct(RouteParserInterface $routeParser, UriInterface $uri, string $basePath = '')
{
$this->routeParser = $routeParser;
$this->uri = $uri;
$this->basePath = $basePath;
}
/**
* {@inheritdoc}
*/
public function load(string $class)
{
if (TwigRuntimeExtension::class === $class) {
return new $class($this->routeParser, $this->uri, $this->basePath);
}
return null;
}
}