From ea0900201240887dbb851f1bc50adda2a72380aa Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 21 Jun 2019 13:19:01 +0300 Subject: [PATCH] Changed `FormFlashInterface` constructor to take `$config` array --- CHANGELOG.md | 2 +- system/src/Grav/Framework/Form/FormFlash.php | 36 +++++++++++++++---- .../Form/Interfaces/FormFlashInterface.php | 6 ++-- .../Grav/Framework/Form/Traits/FormTrait.php | 25 +++++++++---- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b90fbb37a..701bbff08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/src/Grav/Framework/Form/FormFlash.php b/system/src/Grav/Framework/Form/FormFlash.php index eb35a72d1..bd354fdc9 100644 --- a/system/src/Grav/Framework/Form/FormFlash.php +++ b/system/src/Grav/Framework/Form/FormFlash.php @@ -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 */ diff --git a/system/src/Grav/Framework/Form/Interfaces/FormFlashInterface.php b/system/src/Grav/Framework/Form/Interfaces/FormFlashInterface.php index 470e845c6..8eb85254d 100644 --- a/system/src/Grav/Framework/Form/Interfaces/FormFlashInterface.php +++ b/system/src/Grav/Framework/Form/Interfaces/FormFlashInterface.php @@ -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. diff --git a/system/src/Grav/Framework/Form/Traits/FormTrait.php b/system/src/Grav/Framework/Form/Traits/FormTrait.php index af42ca0b1..2e92a24ca 100644 --- a/system/src/Grav/Framework/Form/Traits/FormTrait.php +++ b/system/src/Grav/Framework/Form/Traits/FormTrait.php @@ -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; } }