Merge branch '1.6' of github.com:getgrav/grav into 1.6

This commit is contained in:
Andy Miller
2018-11-13 11:29:36 -07:00
8 changed files with 55 additions and 31 deletions

View File

@@ -1,3 +1,9 @@
# v1.6.0-beta.7
## mm/dd/2018
1. [](#improved)
* Improve Flex storage
# v1.6.0-beta.6
## 11/12/2018
@@ -101,6 +107,7 @@
1. [](#improved)
* Propogate error code between 400 and 600 for production sites [#2181](https://github.com/getgrav/grav/pull/2181)
1. [](#bugfix)
* Remove hardcoded `302` when redirecting trailing slash [#2155](https://github.com/getgrav/grav/pull/2155)
# v1.5.4
## 11/05/2018

View File

@@ -222,7 +222,6 @@ class FlexDirectory implements FlexAuthorizeInterface
}
$object->save();
//$rows = $storage->updateRows([$newKey => $object->triggerEvent('onSave')->prepareStorage()]);
}
try {
@@ -235,13 +234,6 @@ class FlexDirectory implements FlexAuthorizeInterface
// Caching failed, but we can ignore that for now.
}
/** @var FlexObject $class */
//$class = $this->getObjectClass();
//
//$row = $object;
//$index = $class::createIndex([key($rows) => time()]);
//$object = $this->createObject($row, key($index), false);
return $object;
}

View File

@@ -218,7 +218,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function getStorageKey()
{
return $this->_storageKey;
return $this->_storageKey ?? $this->__toString();
}
/**
@@ -422,7 +422,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function save()
{
$this->getFlexDirectory()->getStorage()->replaceRows([$this->getStorageKey() => $this->prepareStorage()]);
$result = $this->getFlexDirectory()->getStorage()->replaceRows([$this->getStorageKey() => $this->prepareStorage()]);
try {
$this->getFlexDirectory()->clearCache();
@@ -437,6 +437,12 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
// Caching failed, but we can ignore that for now.
}
$value = reset($result);
$storageKey = key($result);
if ($value && $storageKey) {
$this->setStorageKey($storageKey);
}
return $this;
}

View File

@@ -115,7 +115,7 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface
*/
protected function generateKey() : string
{
return Base32::encode(Utils::generateRandomString(10));
return substr(hash('sha256', random_bytes(32)), 0, 32);
}
/**

View File

@@ -22,7 +22,7 @@ class FileStorage extends FolderStorage
*/
public function __construct(array $options)
{
$this->dataPattern = '%1s/%2s';
$this->dataPattern = '{FOLDER}/{KEY}';
if (!isset($options['formatter']) && isset($options['pattern'])) {
$options['formatter'] = $this->detectDataFormatter($options['pattern']);

View File

@@ -26,7 +26,9 @@ class FolderStorage extends AbstractFilesystemStorage
/** @var string */
protected $dataFolder;
/** @var string */
protected $dataPattern = '%1s/%2s/item';
protected $dataPattern = '{FOLDER}/{KEY}/item';
/** @var bool */
protected $indexed;
/**
* {@inheritdoc}
@@ -64,7 +66,7 @@ class FolderStorage extends AbstractFilesystemStorage
*/
public function hasKey(string $key) : bool
{
return $key && file_exists($this->getPathFromKey($key));
return $key && !strpos($key, '@@') && file_exists($this->getPathFromKey($key));
}
/**
@@ -93,9 +95,13 @@ class FolderStorage extends AbstractFilesystemStorage
foreach ($rows as $key => $row) {
if (null === $row || (!\is_object($row) && !\is_array($row))) {
// Only load rows which haven't been loaded before.
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->hasKey($key) ? $this->loadFile($file) : null;
if (!$this->hasKey($key)) {
$list[$key] = null;
} else {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->loadFile($file);
}
if (null !== $fetched) {
$fetched[$key] = $list[$key];
}
@@ -115,9 +121,13 @@ class FolderStorage extends AbstractFilesystemStorage
{
$list = [];
foreach ($rows as $key => $row) {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->hasKey($key) ? $this->saveFile($file, $row) : null;
if (!$this->hasKey($key)) {
$list[$key] = null;
} else {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->saveFile($file, $row);
}
}
return $list;
@@ -130,15 +140,19 @@ class FolderStorage extends AbstractFilesystemStorage
{
$list = [];
foreach ($rows as $key => $row) {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->hasKey($key) ? $this->deleteFile($file) : null;
if (!$this->hasKey($key)) {
$list[$key] = null;
} else {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->deleteFile($file);
$storage = $this->getStoragePath($key);
$media = $this->getMediaPath($key);
$storage = $this->getStoragePath($key);
$media = $this->getMediaPath($key);
$this->deleteFolder($storage, true);
$media && $this->deleteFolder($media, true);
$this->deleteFolder($storage, true);
$media && $this->deleteFolder($media, true);
}
}
return $list;
@@ -151,6 +165,9 @@ class FolderStorage extends AbstractFilesystemStorage
{
$list = [];
foreach ($rows as $key => $row) {
if (strpos($key, '@@')) {
$key = $this->getNewKey();
}
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->saveFile($file, $row);
@@ -183,7 +200,7 @@ class FolderStorage extends AbstractFilesystemStorage
if (null === $key) {
$path = $this->dataFolder;
} else {
$path = sprintf($this->dataPattern, $this->dataFolder, $key);
$path = sprintf($this->dataPattern, $this->dataFolder, $key, substr($key, 0, 2));
}
return $path;
@@ -205,7 +222,7 @@ class FolderStorage extends AbstractFilesystemStorage
*/
public function getPathFromKey(string $key) : string
{
return sprintf($this->dataPattern, $this->dataFolder, $key);
return sprintf($this->dataPattern, $this->dataFolder, $key, substr($key, 0, 2));
}
/**
@@ -364,8 +381,10 @@ class FolderStorage extends AbstractFilesystemStorage
{
$extension = $this->dataFormatter->getDefaultFileExtension();
$pattern = !empty($options['pattern']) ? $options['pattern'] : $this->dataPattern;
$pattern = preg_replace(['/{FOLDER}/', '/{KEY}/', '/{KEY:2}/'], ['%1$s', '%2$s', '%3$s'], $pattern);
$this->dataPattern = \dirname($pattern) . '/' . basename($pattern, $extension) . $extension;
$this->dataFolder = $options['folder'];
$this->indexed = (bool)($options['indexed'] ?? false);
}
}

View File

@@ -68,7 +68,7 @@ class SimpleStorage extends AbstractFilesystemStorage
*/
public function hasKey(string $key) : bool
{
return isset($this->data[$key]);
return $key && !strpos($key, '@@') && isset($this->data[$key]);
}
/**

View File

@@ -53,7 +53,7 @@ trait ObjectTrait
*/
public function getKey()
{
return $this->_key ?: $this->getType() . '@' . spl_object_hash($this);
return $this->_key ?: $this->getType() . '@@' . spl_object_hash($this);
}
/**