GitHub PullRequestReviewCommentEvent at 2014-04-28 21:21:03
Repository: cakephp
Actor: jadb
Payload: {"comment":{"url":"https://api.github.com/repos/cakephp/cakephp/pulls/comments/12068506","id":12068506,"diff_hunk":"@@ -0,0 +1,233 @@\n+<?php\n+/**\n+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)\n+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)\n+ *\n+ * Licensed under The MIT License\n+ * For full copyright and license information, please see the LICENSE.txt\n+ * Redistributions of files must retain the above copyright notice.\n+ *\n+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)\n+ * @link          http://cakephp.org CakePHP(tm) Project\n+ * @since         3.0.0\n+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License\n+ */\n+namespace Cake\\Controller\\Component;\n+\n+use Cake\\Controller\\Component;\n+use Cake\\Controller\\ComponentRegistry;\n+use Cake\\Error\\InternalErrorException;\n+use Cake\\Event\\Event;\n+use Cake\\Network\\Session;\n+use Cake\\Utility\\Hash;\n+use Cake\\Utility\\String;\n+\n+/**\n+ * The CakePHP FlashComponent provides a way to easily set flash messages of any\n+ * type (i.e. notice, error, success, etc.).\n+ */\n+class FlashComponent extends Component {\n+\n+/**\n+ * The controller.\n+ *\n+ * @var \\Cake\\Controller\\Controller\n+ */\n+\tprotected $_controller;\n+\n+/**\n+ * Default config\n+ *\n+ * - `element` - Element to wrap flash message in.\n+ * - `key` - Message key, default is 'flash'.\n+ * - `log` - Adds associated log entry. If true, uses the flash message. Default to false.\n+ * - `modelName` - Name of the model to use in the default CRUD templates, default is 'record'\n+ *      or `Controller::$modelName` if not empty.\n+ * - `redirect` - A string or array-based URL pointing to another location within the app,\n+ *     or an absolute URL. If true, the result of `Controller::referer()` is used. Default\n+ *     to false.\n+ * - `templates` - Re-usable message templates.\n+ * - `type` - The message type, default is 'notice'.\n+ *\n+ * @var array\n+ */\n+\tprotected $_defaultConfig = [\n+\t\t'element' => 'default',\n+\t\t'key' => 'flash',\n+\t\t'log' => false,\n+\t\t'modelName' => 'record',\n+\t\t'redirect' => false,\n+\t\t'templates' => [],\n+\t\t'type' => 'notice',\n+\t];\n+\n+/**\n+ * Constructor\n+ *\n+ * @param ComponentRegistry $collection A ComponentRegistry for this component\n+ * @param array $config Array of config.\n+ */\n+\tpublic function __construct(ComponentRegistry $registry, array $config = []) {\n+\t\t$this->_defaultConfig['templates'] = [\n+\t\t\t'create' => [\n+\t\t\t\t'success' => [\n+\t\t\t\t\t'message' => __d('cake', 'Successfully created {{modelName}}.'),\n+\t\t\t\t\t'redirect' => ['action' => 'index'],\n+\t\t\t\t],\n+\t\t\t\t'failure' => [\n+\t\t\t\t\t'message' => __d('cake', 'There was a problem creating your {{modelName}}, fix the error(s) and try again.'),\n+\t\t\t\t\t'type' => 'error',\n+\t\t\t\t],\n+\t\t\t],\n+\t\t\t'read' => [\n+\t\t\t\t'failure' => [\n+\t\t\t\t\t'message' => __d('cake', 'Invalid {{modelName}}, please try again.'),\n+\t\t\t\t\t'type' => 'error',\n+\t\t\t\t\t'redirect' => true,\n+\t\t\t\t],\n+\t\t\t],\n+\t\t\t'update' => [\n+\t\t\t\t'success' => [\n+\t\t\t\t\t'message' => __d('cake', 'Successfully updated {{modelName}}.'),\n+\t\t\t\t],\n+\t\t\t\t'failure' => [\n+\t\t\t\t\t'message' => __d('cake', 'There was a problem updating your {{modelName}}, fix the error(s) and try again.'),\n+\t\t\t\t\t'type' => 'error',\n+\t\t\t\t]\n+\t\t\t],\n+\t\t\t'delete' => [\n+\t\t\t\t'success' => [\n+\t\t\t\t\t'message' => __d('cake', 'Successfully deleted {{modelName}}.'),\n+\t\t\t\t],\n+\t\t\t\t'failure' => [\n+\t\t\t\t\t'message' => __d('cake', 'There was a problem deleting your {{modelName}}, please try again.'),\n+\t\t\t\t\t'type' => 'error',\n+\t\t\t\t]\n+\t\t\t],\n+\t\t];\n+\t\tparent::__construct($registry, $config);\n+\t}\n+\n+/**\n+ * Startup callback.\n+ *\n+ * Sets controller to be used for redirects.\n+ *\n+ * @param \\Cake\\Event\\Event $event\n+ * @return void\n+ */\n+\tpublic function startup(Event $event) {\n+\t\t$this->_controller = $event->subject();\n+\t\tif (!empty($this->_controller->modelName)) {\n+\t\t\t$this->_config['modelName'] = $this->_controller->modelName;\n+\t\t}\n+\t}\n+\n+/**\n+ * Used to set a session message that can be used to output messages in the view.\n+ *\n+ * In your controller: $this->Flash->set('This has been saved');\n+ *\n+ * Additional params below can be passed to customize the output, or the Message.[key].\n+ * You can also set additional parameters when rendering flash messages. See SessionHelper::flash()\n+ * for more information on how to do that.\n+ *\n+ * @param string $message Message to be flashed\n+ * @param string|array $options\n+ * @return void\n+ */\n+\tpublic function set($message, $options = []) {\n+\t\tif (is_string($options)) {\n+\t\t\t$options = ['type' => $options];\n+\t\t}\n+\n+\t\tif ($message instanceof \\Exception) {\n+\t\t\t$options += ['type' => 'error'];\n+\t\t\t$message = $message->getMessage();\n+\t\t}\n+\n+\t\tif (Hash::check($this->_config, 'templates.' . $message)) {\n+\t\t\t$template = Hash::extract($this->_config, 'templates.' . $message);\n+\n+\t\t\tif (!is_array($template)) {\n+\t\t\t\t$template = ['message' => $template];\n+\t\t\t}\n+\n+\t\t\t$options += $template;\n+\n+\t\t\tif (!empty($options['message'])) {\n+\t\t\t\t$message = $options['message'];\n+\t\t\t\tunset($options['message']);\n+\t\t\t}\n+\t\t}\n+\n+\t\t$params = $options + $this->_config;\n+\n+\t\tlist($plugin, $element) = pluginSplit($params['element']);\n+\t\tif ($plugin) {\n+\t\t\t$params += compact('plugin');\n+\t\t}\n+\n+\t\t$insertOpts = ['before' => '{{', 'after' => '}}', 'clean' => true];\n+\n+\t\t$message = String::insert($message, array_filter($params, 'is_string'), $insertOpts);\n+\n+\t\t$key = $params['key'];\n+\t\t$redirect = $params['redirect'];\n+\n+\t\tif (!empty($params['log'])) {\n+\t\t\tif (is_string($params['log'])) {\n+\t\t\t\t$params['log'] = ['message' => String::insert($params['log'], $params, $insertOpts)];\n+\t\t\t}\n+\n+\t\t\tif (is_callable($params['log'])) {\n+\t\t\t\tcall_user_func_array($params['log'], compact('message', 'params'));\n+\t\t\t} else {\n+\t\t\t\t$log = ['level' => $params['type'], 'message' => $message, 'scope' => []];\n+\t\t\t\t$log = $params['log'] + $log;\n+\t\t\t\t$this->_controller->log($log['message'], $log['level'], $log['scope']);\n+\t\t\t}\n+\t\t}\n+\n+\t\tunset(\n+\t\t\t$params['element'],\n+\t\t\t$params['key'],\n+\t\t\t$params['log'],\n+\t\t\t$params['modelName'],\n+\t\t\t$params['redirect'],\n+\t\t\t$params['templates']\n+\t\t);\n+\n+\t\tSession::write('Message.' . $key, compact('message', 'element', 'params'));\n+\n+\t\tif (!empty($redirect)) {","path":"src/Controller/Component/FlashComponent.php","position":203,"original_position":203,"commit_id":"a310fe1d5cb3e636e87fe0fb503d45cc21193011","original_commit_id":"a310fe1d5cb3e636e87fe0fb503d45cc21193011","user":{"login":"jadb","id":33527,"avatar_url":"https://avatars.githubusercontent.com/u/33527?","gravatar_id":"ce6ea1cd14b044dd1fb906037cf5e2eb","url":"https://api.github.com/users/jadb","html_url":"https://github.com/jadb","followers_url":"https://api.github.com/users/jadb/followers","following_url":"https://api.github.com/users/jadb/following{/other_user}","gists_url":"https://api.github.com/users/jadb/gists{/gist_id}","starred_url":"https://api.github.com/users/jadb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jadb/subscriptions","organizations_url":"https://api.github.com/users/jadb/orgs","repos_url":"https://api.github.com/users/jadb/repos","events_url":"https://api.github.com/users/jadb/events{/privacy}","received_events_url":"https://api.github.com/users/jadb/received_events","type":"User","site_admin":false},"body":"That's another of the stuff others liked in the original issue. I have a feeling you are against any extra feature to the original `Session::setFlash()` and only liked the `type` feature - which doesn't need a component of itself if that's all that is needed.","created_at":"2014-04-28T21:21:03Z","updated_at":"2014-04-28T21:21:03Z","html_url":"https://github.com/cakephp/cakephp/pull/3409#discussion_r12068506","pull_request_url":"https://api.github.com/repos/cakephp/cakephp/pulls/3409","_links":{"self":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/comments/12068506"},"html":{"href":"https://github.com/cakephp/cakephp/pull/3409#discussion_r12068506"},"pull_request":{"href":"https://api.github.com/repos/cakephp/cakephp/pulls/3409"}}}}