Changed FormFlashInterface constructor to take $config array

This commit is contained in:
Matias Griese
2019-06-21 13:19:01 +03:00
parent 601ec5cb7a
commit ea09002012
4 changed files with 50 additions and 19 deletions

View File

@@ -5,7 +5,7 @@
* Added `FormTrait::getAllFlashes()` method to get all the available form flash objects for the form
* Added creation and update timestamps to `FormFlash` objects
1. [](#improved)
* Added `FormFlashInterface`
* Added `FormFlashInterface`, changed constructor to take `$config` array
1. [](#bugfix)
* Fixed error in `ImageMedium::url()` if the image cache folder does not exist
* Fixed empty form flash name after file upload or form state update

View File

@@ -17,6 +17,8 @@ use RocketTheme\Toolbox\File\YamlFile;
class FormFlash implements FormFlashInterface
{
/** @var bool */
protected $exists;
/** @var string */
protected $sessionId;
/** @var string */
@@ -39,8 +41,6 @@ class FormFlash implements FormFlashInterface
protected $uploadedFiles;
/** @var string[] */
protected $uploadObjects;
/** @var bool */
protected $exists;
/**
* @param string $sessionId
@@ -58,10 +58,22 @@ class FormFlash implements FormFlashInterface
/**
* @inheritDoc
*/
public function __construct(string $sessionId, string $uniqueId, string $formName = null)
public function __construct($config)
{
$this->sessionId = $sessionId;
$this->uniqueId = $uniqueId;
// Backwards compatibility with Grav 1.6 plugins.
if (!is_array($config)) {
user_error(__CLASS__ . '::' . __FUNCTION__ . '($sessionId, $uniqueId, $formName) is deprecated since Grav 1.6.11, use $config parameter instead', E_USER_DEPRECATED);
$args = func_get_args();
$config = [
'session_id' => $args[0],
'unique_id' => $args[1] ?? null,
'form_name' => $args[2] ?? null,
];
}
$this->sessionId = $config['session_id'] ?? '';
$this->uniqueId = $config['unique_id'] ?? '';
$file = $this->getTmpIndex();
$this->exists = $file->exists();
@@ -72,7 +84,7 @@ class FormFlash implements FormFlashInterface
} catch (\Exception $e) {
$data = [];
}
$this->formName = $content['form'] ?? $formName;
$this->formName = $content['form'] ?? $config['form_name'] ?? '';
$this->url = $data['url'] ?? '';
$this->user = $data['user'] ?? null;
$this->updatedTimestamp = $data['timestamps']['updated'] ?? time();
@@ -80,7 +92,7 @@ class FormFlash implements FormFlashInterface
$this->data = $data['data'] ?? null;
$this->files = $data['files'] ?? [];
} else {
$this->formName = $formName;
$this->formName = $config['form_name'] ?? '';
$this->url = '';
$this->createdTimestamp = $this->updatedTimestamp = time();
$this->files = [];
@@ -103,6 +115,16 @@ class FormFlash implements FormFlashInterface
return $this->uniqueId;
}
/**
* @deprecated 1.6.11 Use '->getUniqueId()' method instead.
*/
public function getUniqieId(): string
{
user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.6.11, use ->getUniqueId() method instead', E_USER_DEPRECATED);
return $this->getUniqueId();
}
/**
* @inheritDoc
*/

View File

@@ -14,11 +14,9 @@ use Psr\Http\Message\UploadedFileInterface;
interface FormFlashInterface extends \JsonSerializable
{
/**
* @param string $sessionId
* @param string $uniqueId
* @param string|null $formName
* @param array $config Available configuration keys: session_id, unique_id, form_name
*/
public function __construct(string $sessionId, string $uniqueId, string $formName = null);
public function __construct($config);
/**
* Get session Id associated to this form instance.

View File

@@ -337,9 +337,14 @@ trait FormTrait
{
if (null === $this->flash) {
$grav = Grav::instance();
$id = $this->getFlashId();
$config = [
'session_id' => $this->getFlashId() ?? '',
'unique_id' => $this->getUniqueId(),
'form_name' => $this->getName()
];
$this->flash = new FormFlash($id ?? '', $this->getUniqueId(), $this->getName());
$this->flash = new FormFlash($config);
$this->flash->setUrl($grav['uri']->url)->setUser($grav['user'] ?? null);
}
@@ -364,7 +369,12 @@ trait FormTrait
$list = [];
/** @var \SplFileInfo $file */
foreach (new \FilesystemIterator($folder) as $file) {
$flash = new FormFlash($id, $file->getFilename(), $name);
$config = [
'session_id' => $id,
'unique_id' => $file->getFilename(),
'form_name' => $name
];
$flash = new FormFlash($config);
if ($flash->exists() && $flash->getFormName() === $name) {
$list[] = $flash;
}
@@ -403,10 +413,11 @@ trait FormTrait
/** @var Grav $grav */
$grav = Grav::instance();
$user = $grav['user'] ?? null;
if (isset($user)) {
$rememberState = $this->getBlueprint()->get('form/remember_state');
if ($rememberState === 'user') {
$rememberState = $this->getBlueprint()->get('form/remember_state');
if ($rememberState === 'user') {
$user = $grav['user'] ?? null;
if (isset($user)) {
return $user->username;
}
}