test appel img twig

This commit is contained in:
2021-03-19 16:07:04 +01:00
parent bc84eb05d5
commit 80be13743f
63 changed files with 999 additions and 1167 deletions
@@ -14,11 +14,13 @@ namespace Grav\Framework\Controller\Traits;
use Grav\Common\Config\Config;
use Grav\Common\Debugger;
use Grav\Common\Grav;
use Grav\Common\Utils;
use Grav\Framework\Psr7\Response;
use Grav\Framework\RequestHandler\Exception\RequestException;
use Grav\Framework\Route\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Throwable;
use function get_class;
use function in_array;
@@ -76,6 +78,55 @@ trait ControllerResponseTrait
return new Response($code, $headers, json_encode($content));
}
/**
* @param string $filename
* @param string|resource|StreamInterface $resource
* @param array|null $headers
* @param array|null $options
* @return ResponseInterface
*/
protected function createDownloadResponse(string $filename, $resource, array $headers = null, array $options = null): ResponseInterface
{
// Required for IE, otherwise Content-Disposition may be ignored
if (ini_get('zlib.output_compression')) {
@ini_set('zlib.output_compression', 'Off');
}
$headers = $headers ?? [];
$options = $options ?? ['force_download' => true];
$file_parts = pathinfo($filename);
if (!isset($headers['Content-Type'])) {
$mimetype = Utils::getMimeByExtension($file_parts['extension']);
$headers['Content-Type'] = $mimetype;
}
// TODO: add multipart download support.
//$headers['Accept-Ranges'] = 'bytes';
if (!empty($options['force_download'])) {
$headers['Content-Disposition'] = 'attachment; filename="' . $file_parts['basename'] . '"';
}
if (!isset($headers['Content-Length'])) {
$realpath = realpath($filename);
if ($realpath) {
$headers['Content-Length'] = filesize($realpath);
}
}
$headers += [
'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT',
'Cache-Control' => 'no-store, no-cache, must-revalidate',
'Pragma' => 'no-cache'
];
return new Response(200, $headers, $resource);
}
/**
* @param string $url
* @param int|null $code
+1 -1
View File
@@ -380,7 +380,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
// Handle primary key alias.
$keyField = $this->getFlexDirectory()->getStorage()->getKeyField();
if (isset($orderings[$keyField])) {
if ($keyField !== 'key' && $keyField !== 'storage_key' && isset($orderings[$keyField])) {
$orderings['key'] = $orderings[$keyField];
unset($orderings[$keyField]);
}
@@ -885,6 +885,14 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
];
}
/**
* Clone object.
*/
public function __clone()
{
// Allows future compatibility as parent::__clone() works.
}
protected function markAsCopy(): void
{
$meta = $this->getMetaData();
@@ -16,7 +16,6 @@ use Grav\Common\Grav;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Traits\PageFormTrait;
use Grav\Common\User\Interfaces\UserCollectionInterface;
use Grav\Framework\File\Formatter\YamlFormatter;
use Grav\Framework\Flex\FlexObject;
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
use Grav\Framework\Flex\Interfaces\FlexTranslateInterface;
@@ -50,6 +49,20 @@ class FlexPageObject extends FlexObject implements PageInterface, FlexTranslateI
/** @var array|null */
protected $_reorder;
/** @var FlexPageObject|null */
protected $_original;
/**
* Clone page.
*/
public function __clone()
{
parent::__clone();
if (isset($this->header)) {
$this->header = clone($this->header);
}
}
/**
* @return array
@@ -242,6 +255,32 @@ class FlexPageObject extends FlexObject implements PageInterface, FlexTranslateI
return parent::save();
}
/**
* Gets the Page Unmodified (original) version of the page.
*
* Assumes that object has been cloned before modifying it.
*
* @return FlexPageObject|null The original version of the page.
*/
public function getOriginal()
{
return $this->_original;
}
/**
* Store the Page Unmodified (original) version of the page.
*
* Can be called multiple times, only the first call matters.
*
* @return void
*/
public function storeOriginal(): void
{
if (null === $this->_original) {
$this->_original = clone $this;
}
}
/**
* Get display order for the associated media.
*
@@ -398,23 +437,6 @@ class FlexPageObject extends FlexObject implements PageInterface, FlexTranslateI
unset($elements['content']);
}
// TODO: Remove: RAW frontmatter support has been moved to Flex-Objects v1.0.2 controller.
if (isset($elements['frontmatter'])) {
$formatter = new YamlFormatter();
try {
// Replace the whole header except for media order, which is used in admin.
$media_order = $elements['media_order'] ?? null;
$elements['header'] = $formatter->decode($elements['frontmatter']);
if ($media_order) {
$elements['header']['media_order'] = $media_order;
}
} catch (RuntimeException $e) {
throw new RuntimeException('Badly formatted markdown');
}
unset($elements['frontmatter']);
}
if (!$extended) {
$folder = !empty($elements['folder']) ? trim($elements['folder']) : '';
@@ -27,6 +27,8 @@ trait PageAuthorsTrait
{
/** @var array<int,UserInterface> */
private $_authors;
/** @var array|null */
private $_permissionsCache;
/**
* Returns true if object has the named author.
@@ -70,15 +72,19 @@ trait PageAuthorsTrait
*/
public function getPermissions(bool $inherit = false)
{
$permissions = [];
if ($inherit && $this->getNestedProperty('header.permissions.inherit', true)) {
$parent = $this->parent();
if ($parent && method_exists($parent, 'getPermissions')) {
$permissions = $parent->getPermissions($inherit);
if (null === $this->_permissionsCache) {
$permissions = [];
if ($inherit && $this->getNestedProperty('header.permissions.inherit', true)) {
$parent = $this->parent();
if ($parent && method_exists($parent, 'getPermissions')) {
$permissions = $parent->getPermissions($inherit);
}
}
$this->_permissionsCache = $this->loadPermissions($permissions);
}
return $this->loadPermissions($permissions);
return $this->_permissionsCache;
}
/**
@@ -277,6 +277,8 @@ trait PageLegacyTrait
throw new RuntimeException('Failed: Cannot set page parent to a child of current page');
}
$this->storeOriginal();
// TODO:
throw new RuntimeException(__METHOD__ . '(): Not Implemented');
}
@@ -292,6 +294,8 @@ trait PageLegacyTrait
*/
public function copy(PageInterface $parent = null)
{
$this->storeOriginal();
$filesystem = Filesystem::getInstance(false);
$parentStorageKey = ltrim($filesystem->dirname("/{$this->getMasterKey()}"), '/');
@@ -715,8 +719,9 @@ trait PageLegacyTrait
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$folder = $locator->isStream($folder) ? $locator->getResource($folder) : GRAV_ROOT . "/{$folder}";
return $locator->findResource($folder, true, true) . '/' . ($this->isPage() ? $this->name() : 'default.md');
return $folder . '/' . ($this->isPage() ? $this->name() : 'default.md');
}
/**
@@ -733,8 +738,9 @@ trait PageLegacyTrait
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$folder = $locator->isStream($folder) ? $locator->getResource($folder, false) : $folder;
return $locator->findResource($folder, false, true) . '/' . ($this->isPage() ? $this->name() : 'default.md');
return $folder . '/' . ($this->isPage() ? $this->name() : 'default.md');
}
/**
@@ -1085,18 +1091,6 @@ trait PageLegacyTrait
return $this->exists() || is_dir($this->getStorageFolder() ?? '');
}
/**
* Gets the Page Unmodified (original) version of the page.
*
* Assumes that object has been cloned before modifying it.
*
* @return PageInterface|null The original version of the page.
*/
public function getOriginal()
{
return $this->getFlexDirectory()->getObject($this->getKey());
}
/**
* Gets the action.
*
@@ -18,6 +18,7 @@ use Grav\Common\Uri;
use Grav\Framework\Filesystem\Filesystem;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use RuntimeException;
use function dirname;
use function is_string;
/**
@@ -32,6 +33,8 @@ trait PageRoutableTrait
private $_route;
/** @var string|null */
private $_path;
/** @var PageInterface|null */
private $_parentCache;
/**
* Returns the page extension, got from the page `url_extension` config and falls back to the
@@ -317,7 +320,7 @@ trait PageRoutableTrait
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$path = $locator->findResource($folder, false);
$path = $locator->isStream($folder) ? $locator->findResource($folder, false) : $folder;
return is_string($path) ? $path : null;
}
@@ -350,7 +353,7 @@ trait PageRoutableTrait
if ($folder) {
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$folder = $locator($folder);
$folder = $locator->isStream($folder) ? $locator->getResource($folder) : GRAV_ROOT . "/{$folder}";
}
return $this->_path = is_string($folder) ? $folder : null;
@@ -413,13 +416,12 @@ trait PageRoutableTrait
throw new RuntimeException(__METHOD__ . '(PageInterface): Not Implemented');
}
if ($this->root()) {
return null;
if ($this->_parentCache || $this->root()) {
return $this->_parentCache;
}
$filesystem = Filesystem::getInstance(false);
$directory = $this->getFlexDirectory();
$parentKey = ltrim($filesystem->dirname("/{$this->getKey()}"), '/');
$parentKey = ltrim(dirname("/{$this->getKey()}"), '/');
if ($parentKey) {
$parent = $directory->getObject($parentKey);
$language = $this->getLanguage();
@@ -427,12 +429,14 @@ trait PageRoutableTrait
$parent = $parent->getTranslation($language) ?? $parent;
}
return $parent;
$this->_parentCache = $parent;
} else {
$index = $directory->getIndex();
$this->_parentCache = method_exists($index, 'getRoot') ? $index->getRoot() : null;
}
$index = $directory->getIndex();
return method_exists($index, 'getRoot') ? $index->getRoot() : null;
return $this->_parentCache;
}
/**
@@ -186,10 +186,10 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface
$locator = Grav::instance()['locator'];
if (!$locator->isStream($path)) {
return $path;
return GRAV_ROOT . "/{$path}";
}
return (string)($locator->findResource($path) ?: $locator->findResource($path, true, true));
return $locator->getResource($path);
}
/**
@@ -83,6 +83,8 @@ class FileStorage extends FolderStorage
$path = $this->getPathFromKey($src);
$file = $this->getFile($path);
$file->delete();
$file->free();
unset($file);
return true;
}
@@ -377,12 +377,14 @@ class FolderStorage extends AbstractFilesystemStorage
$file = $this->getFile($path);
try {
$data = (array)$file->content();
$file->free();
if (isset($data[0])) {
throw new RuntimeException('Broken object file');
}
} catch (RuntimeException $e) {
$data = ['__ERROR' => $e->getMessage()];
} finally {
$file->free();
unset($file);
}
$data['__META'] = $this->getObjectMeta($key);
@@ -426,13 +428,17 @@ class FolderStorage extends AbstractFilesystemStorage
$file->save($row);
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
if ($locator->isStream($path)) {
$locator->clearCache();
}
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf('Flex saveFile(%s): %s', $path ?? $key, $e->getMessage()));
} finally {
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$locator->clearCache();
if (isset($file)) {
$file->free();
unset($file);
}
}
$row['__META'] = $this->getObjectMeta($key, true);
@@ -452,14 +458,14 @@ class FolderStorage extends AbstractFilesystemStorage
if ($file->exists()) {
$file->delete();
}
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
if ($locator->isStream($filename)) {
$locator->clearCache($filename);
}
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf('Flex deleteFile(%s): %s', $filename, $e->getMessage()));
} finally {
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$locator->clearCache();
$file->free();
}
return $data;
@@ -474,14 +480,12 @@ class FolderStorage extends AbstractFilesystemStorage
{
try {
Folder::copy($this->resolvePath($src), $this->resolvePath($dst));
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
if ($locator->isStream($src) || $locator->isStream($dst)) {
$locator->clearCache();
}
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf('Flex copyFolder(%s, %s): %s', $src, $dst, $e->getMessage()));
} finally {
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$locator->clearCache();
}
return true;
@@ -496,14 +500,12 @@ class FolderStorage extends AbstractFilesystemStorage
{
try {
Folder::move($this->resolvePath($src), $this->resolvePath($dst));
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
if ($locator->isStream($src) || $locator->isStream($dst)) {
$locator->clearCache();
}
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf('Flex moveFolder(%s, %s): %s', $src, $dst, $e->getMessage()));
} finally {
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$locator->clearCache();
}
return true;
@@ -517,17 +519,13 @@ class FolderStorage extends AbstractFilesystemStorage
protected function deleteFolder(string $path, bool $include_target = false): bool
{
try {
$success = Folder::delete($this->resolvePath($path), $include_target);
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
if ($locator->isStream($path)) {
$locator->clearCache();
}
return $success;
return Folder::delete($this->resolvePath($path), $include_target);
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf('Flex deleteFolder(%s): %s', $path, $e->getMessage()));
} finally {
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$locator->clearCache();
}
}
@@ -669,7 +667,14 @@ class FolderStorage extends AbstractFilesystemStorage
/** @var string $pattern */
$pattern = !empty($options['pattern']) ? $options['pattern'] : $this->dataPattern;
$this->dataFolder = $options['folder'];
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$folder = $options['folder'];
if ($locator->isStream($folder)) {
$folder = $locator->getResource($folder, false);
}
$this->dataFolder = $folder;
$this->dataFile = $options['file'] ?? 'item';
$this->dataExt = $extension;
if (mb_strpos($pattern, '{FILE}') === false && mb_strpos($pattern, '{EXT}') === false) {
@@ -414,9 +414,13 @@ class SimpleStorage extends AbstractFilesystemStorage
}
$file->save($content);
$this->modified = (int)$file->modified(); // cast false to 0
$file->free();
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf('Flex save(): %s', $e->getMessage()));
} finally {
if (isset($file)) {
$file->free();
unset($file);
}
}
}
@@ -453,6 +457,10 @@ class SimpleStorage extends AbstractFilesystemStorage
$data = new Data($content);
$content = $data->get($this->prefix);
}
$file->free();
unset($file);
$this->data = $content;
$list = [];