Bug fixes for Flex

This commit is contained in:
Matias Griese
2018-09-13 15:21:47 +03:00
parent 5c81d7863c
commit 871333d3a0
5 changed files with 36 additions and 39 deletions

View File

@@ -191,7 +191,7 @@ class AbstractFile implements FileInterface
}
if ($this->locked) {
flock($this->handle, LOCK_UN);
$this->locked = null;
$this->locked = false;
}
fclose($this->handle);
$this->handle = null;
@@ -317,7 +317,7 @@ class AbstractFile implements FileInterface
protected function isWritableDir($dir) : bool
{
if ($dir && !file_exists($dir)) {
return $this->isWritableDir(dirname($dir));
return $this->isWritableDir(\dirname($dir));
}
return $dir && is_dir($dir) && is_writable($dir);

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Grav\Framework\File;
use Grav\Framework\File\Formatter\FormatterInterface;
use RuntimeException;
class DataFile extends AbstractFile
{
@@ -34,6 +35,7 @@ class DataFile extends AbstractFile
* (Re)Load a file and return RAW file contents.
*
* @return array
* @throws RuntimeException
*/
public function load()
{
@@ -41,8 +43,8 @@ class DataFile extends AbstractFile
try {
return $this->formatter->decode($raw);
} catch (\RuntimeException $e) {
throw new \RuntimeException(sprintf("Failed to load file '%s': %s", $this->getFilePath(), $e->getMessage()), $e->getCode(), $e);
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf("Failed to load file '%s': %s", $this->getFilePath(), $e->getMessage()), $e->getCode(), $e);
}
}
@@ -50,15 +52,15 @@ class DataFile extends AbstractFile
* Save file.
*
* @param string|array $data Data to be saved.
* @throws \RuntimeException
* @throws RuntimeException
*/
public function save($data)
{
if (\is_string($data)) {
try {
$this->formatter->decode($data);
} catch (\RuntimeException $e) {
throw new \RuntimeException(sprintf("Failed to save file '%s': %s", $this->getFilePath(), $e->getMessage()), $e->getCode(), $e);
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf("Failed to save file '%s': %s", $this->getFilePath(), $e->getMessage()), $e->getCode(), $e);
}
$encoded = $data;
} else {

View File

@@ -26,7 +26,7 @@ use RocketTheme\Toolbox\Event\Event;
class FlexCollection extends ObjectCollection implements FlexCollectionInterface
{
/** @var FlexDirectory */
private $flexDirectory;
private $_flexDirectory;
/**
* @return array
@@ -77,7 +77,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*/
protected function createFrom(array $elements)
{
return new static($elements, $this->flexDirectory);
return new static($elements, $this->_flexDirectory);
}
/**
@@ -96,7 +96,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
{
$type = $prefix ? $this->getTypePrefix() : '';
return $type . $this->flexDirectory->getType();
return $type . $this->_flexDirectory->getType();
}
/**
@@ -129,7 +129,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
if ($key !== false) {
$key = md5($this->getCacheKey() . '.' . $layout . json_encode($context));
$cache = $this->flexDirectory->getCache('render');
$cache = $this->_flexDirectory->getCache('render');
}
try {
@@ -183,7 +183,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*/
public function setFlexDirectory(FlexDirectory $type)
{
$this->flexDirectory = $type;
$this->_flexDirectory = $type;
return $this;
}
@@ -193,7 +193,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*/
public function getFlexDirectory() : FlexDirectory
{
return $this->flexDirectory;
return $this->_flexDirectory;
}
/**
@@ -327,7 +327,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
*/
protected function getTemplate($layout)
protected function getTemplate($layout) : \Twig_Template
{
$grav = Grav::instance();
@@ -348,17 +348,12 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
/**
* @param $type
* @return FlexDirectory
* @throws \RuntimeException
*/
protected function getRelatedDirectory($type)
protected function getRelatedDirectory($type) : ?FlexDirectory
{
/** @var Flex $flex */
$flex = Grav::instance()['flex_objects'];
$directory = $flex->getDirectory($type);
if (!$directory) {
throw new \RuntimeException(ucfirst($type). ' directory does not exist!');
}
return $directory;
return $flex->getDirectory($type);
}
}

View File

@@ -22,7 +22,7 @@ use PSR\SimpleCache\InvalidArgumentException;
class FlexIndex extends ObjectIndex implements FlexCollectionInterface
{
/** @var FlexDirectory */
private $flexDirectory;
private $_flexDirectory;
/**
* Initializes a new FlexIndex.
@@ -34,7 +34,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
{
parent::__construct($entries);
$this->flexDirectory = $flexDirectory;
$this->_flexDirectory = $flexDirectory;
}
/**
@@ -42,7 +42,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
*/
public function getFlexDirectory() : FlexDirectory
{
return $this->flexDirectory;
return $this->_flexDirectory;
}
/**
@@ -53,7 +53,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
{
$type = $prefix ? $this->getTypePrefix() : '';
return $type . $this->flexDirectory->getType();
return $type . $this->_flexDirectory->getType();
}
/**
@@ -165,13 +165,13 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
$debugger = Grav::instance()['debugger'];
/** @var FlexCollection $className */
$className = $this->flexDirectory->getCollectionClass();
$className = $this->_flexDirectory->getCollectionClass();
$cachedMethods = $className::getCachedMethods();
if (!empty($cachedMethods[$name])) {
$key = $this->getType(true) . '.' . sha1($name . '.' . json_encode($arguments) . $this->getCacheKey());
$cache = $this->flexDirectory->getCache('object');
$cache = $this->_flexDirectory->getCache('object');
$test = new \stdClass;
try {
@@ -227,7 +227,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
{
$data = unserialize($serialized);
$this->flexDirectory = Grav::instance()['flex_objects']->getDirectory($data['type']);
$this->_flexDirectory = Grav::instance()['flex_objects']->getDirectory($data['type']);
$this->setEntries($data['entries']);
}
@@ -238,7 +238,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
*/
protected function createFrom(array $entries)
{
return new static($entries, $this->flexDirectory);
return new static($entries, $this->_flexDirectory);
}
/**
@@ -256,7 +256,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
*/
protected function loadElement($key, $value) : ?ObjectInterface
{
$objects = $this->flexDirectory->loadObjects([$key => $value]);
$objects = $this->_flexDirectory->loadObjects([$key => $value]);
return $objects ? reset($objects) : null;
}
@@ -267,7 +267,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
*/
protected function loadElements(array $entries = null) : array
{
return $this->flexDirectory->loadObjects($entries ?? $this->getEntries());
return $this->_flexDirectory->loadObjects($entries ?? $this->getEntries());
}
/**
@@ -276,7 +276,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface
*/
protected function loadCollection(array $entries = null) : CollectionInterface
{
return $this->flexDirectory->loadCollection($entries ?? $this->getEntries());
return $this->_flexDirectory->loadCollection($entries ?? $this->getEntries());
}
/**

View File

@@ -44,7 +44,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
use FlexAuthorizeTrait;
/** @var FlexDirectory */
private $flexDirectory;
private $_flexDirectory;
/** @var string */
private $storageKey;
/** @var int */
@@ -93,7 +93,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function __construct(array $elements, $key, FlexDirectory $flexDirectory, $validate = false)
{
$this->flexDirectory = $flexDirectory;
$this->_flexDirectory = $flexDirectory;
if ($validate) {
$blueprint = $this->getFlexDirectory()->getBlueprint();
@@ -148,7 +148,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
{
$type = $prefix ? $this->getTypePrefix() : '';
return $type . $this->flexDirectory->getType();
return $type . $this->_flexDirectory->getType();
}
/**
@@ -156,7 +156,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function getFlexDirectory() : FlexDirectory
{
return $this->flexDirectory;
return $this->_flexDirectory;
}
/**
@@ -177,7 +177,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function getBlueprint()
{
return $this->flexDirectory->getBlueprint();
return $this->_flexDirectory->getBlueprint();
}
/**
@@ -275,7 +275,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
if ($key !== false) {
$key = md5($this->getCacheKey() . '.' . $layout . json_encode($context));
$cache = $this->flexDirectory->getCache('render');
$cache = $this->_flexDirectory->getCache('render');
}
try {
@@ -484,7 +484,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
if (!$directory) {
throw new \InvalidArgumentException("Cannot unserialize '{$type}': Not found");
}
$this->flexDirectory = $directory;
$this->_flexDirectory = $directory;
$this->storageKey = $serialized['storage_key'];
$this->timestamp = $serialized['storage_timestamp'];