Browse Source

Update FlashComponent to use FlashMessage

ADmad 5 years ago
parent
commit
e664b61dce

+ 61 - 44
src/Controller/Component/FlashComponent.php

@@ -18,7 +18,7 @@ namespace Cake\Controller\Component;
 
 use Cake\Controller\Component;
 use Cake\Http\Exception\InternalErrorException;
-use Cake\Http\Session;
+use Cake\Http\FlashMessage;
 use Cake\Utility\Inflector;
 use Throwable;
 
@@ -68,48 +68,75 @@ class FlashComponent extends Component
      */
     public function set($message, array $options = []): void
     {
-        $options += (array)$this->getConfig();
-
         if ($message instanceof Throwable) {
-            if (!isset($options['params']['code'])) {
-                $options['params']['code'] = $message->getCode();
-            }
-            $message = $message->getMessage();
+            $this->flash()->setExceptionMessage($message, $options);
+        } else {
+            $this->flash()->set($message, $options);
         }
+    }
 
-        if (isset($options['escape']) && !isset($options['params']['escape'])) {
-            $options['params']['escape'] = $options['escape'];
-        }
+    /**
+     * Get flash message utility instance.
+     *
+     * @return \Cake\Http\FlashMessage
+     */
+    protected function flash(): FlashMessage
+    {
+        return $this->getController()->getRequest()->getFlash();
+    }
 
-        [$plugin, $element] = pluginSplit($options['element']);
+    /**
+     * Proxy method to FlashMessage instance.
+     *
+     * @param string|array $key The key to set, or a complete array of configs.
+     * @param mixed|null $value The value to set.
+     * @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
+     * @return $this
+     * @throws \Cake\Core\Exception\CakeException When trying to set a key that is invalid.
+     */
+    public function setConfig($key, $value = null, $merge = true)
+    {
+        $this->flash()->setConfig($key, $value, $merge);
 
-        if ($plugin) {
-            $options['element'] = $plugin . '.flash/' . $element;
-        } else {
-            $options['element'] = 'flash/' . $element;
-        }
+        return $this;
+    }
 
-        $messages = [];
-        if (!$options['clear']) {
-            $messages = (array)$this->getSession()->read('Flash.' . $options['key']);
-        }
+    /**
+     * Proxy method to FlashMessage instance.
+     *
+     * @param string|null $key The key to get or null for the whole config.
+     * @param mixed $default The return value when the key does not exist.
+     * @return mixed Configuration data at the named key or null if the key does not exist.
+     */
+    public function getConfig(?string $key = null, $default = null)
+    {
+        return $this->flash()->getConfig($key, $default);
+    }
 
-        if (!$options['duplicate']) {
-            foreach ($messages as $existingMessage) {
-                if ($existingMessage['message'] === $message) {
-                    return;
-                }
-            }
-        }
+    /**
+     * Proxy method to FlashMessage instance.
+     *
+     * @param string $key The key to get.
+     * @return mixed Configuration data at the named key
+     * @throws \InvalidArgumentException
+     */
+    public function getConfigOrFail(string $key)
+    {
+        return $this->flash()->getConfigOrFail($key);
+    }
 
-        $messages[] = [
-            'message' => $message,
-            'key' => $options['key'],
-            'element' => $options['element'],
-            'params' => $options['params'],
-        ];
+    /**
+     * Proxy method to FlashMessage instance.
+     *
+     * @param string|array $key The key to set, or a complete array of configs.
+     * @param mixed|null $value The value to set.
+     * @return $this
+     */
+    public function configShallow($key, $value = null)
+    {
+        $this->flash()->configShallow($key, $value);
 
-        $this->getSession()->write('Flash.' . $options['key'], $messages);
+        return $this;
     }
 
     /**
@@ -154,14 +181,4 @@ class FlashComponent extends Component
 
         $this->set($args[0], $options);
     }
-
-    /**
-     * Returns current session object from a controller request.
-     *
-     * @return \Cake\Http\Session
-     */
-    protected function getSession(): Session
-    {
-        return $this->getController()->getRequest()->getSession();
-    }
 }

+ 1 - 1
tests/TestCase/Controller/Component/FlashComponentTest.php

@@ -210,7 +210,7 @@ class FlashComponentTest extends TestCase
             [
                 'message' => 'This is a test message',
                 'key' => 'flash',
-                'element' => 'flash/default',
+                'element' => 'flash/error',
                 'params' => ['code' => 404],
             ],
         ];