From 664d2aa007a9a8f1a86dcfd651fbe44966a25aef Mon Sep 17 00:00:00 2001
From: Qiang Xue <qiang.xue@gmail.com>
Date: Wed, 16 Apr 2014 14:55:33 -0400
Subject: [PATCH] Supported adding a new response formatter without the need to reconfigure existing formatters

---
 framework/CHANGELOG.md     |  1 +
 framework/web/Response.php | 28 +++++++++++++++++++---------
 framework/web/View.php     |  3 +++
 3 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md
index 5848029..1a7297c 100644
--- a/framework/CHANGELOG.md
+++ b/framework/CHANGELOG.md
@@ -13,6 +13,7 @@ Yii Framework 2 Change Log
 - Enh #3108: Added `yii\debug\Module::enableDebugLogs` to disable logging debug logs by default (qiangxue)
 - Enh #3132: `yii\rbac\PhpManager` now supports more compact data file format (qiangxue)
 - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
+- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
 - Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
 
 
diff --git a/framework/web/Response.php b/framework/web/Response.php
index 406c9f8..52306dd 100644
--- a/framework/web/Response.php
+++ b/framework/web/Response.php
@@ -118,15 +118,7 @@ class Response extends \yii\base\Response
      * for creating the formatter objects.
      * @see format
      */
-    public $formatters = [
-        self::FORMAT_HTML => 'yii\web\HtmlResponseFormatter',
-        self::FORMAT_XML => 'yii\web\XmlResponseFormatter',
-        self::FORMAT_JSON => 'yii\web\JsonResponseFormatter',
-        self::FORMAT_JSONP => [
-            'class' => 'yii\web\JsonResponseFormatter',
-            'useJsonp' => true,
-        ],
-    ];
+    public $formatters = [];
     /**
      * @var mixed the original response data. When this is not null, it will be converted into [[content]]
      * according to [[format]] when the response is being sent out.
@@ -259,6 +251,8 @@ class Response extends \yii\base\Response
         if ($this->charset === null) {
             $this->charset = Yii::$app->charset;
         }
+        $formatters = $this->defaultFormatters();
+        $this->formatters = empty($this->formatters) ? $formatters : array_merge($formatters, $this->formatters);
     }
 
     /**
@@ -841,6 +835,22 @@ class Response extends \yii\base\Response
     }
 
     /**
+     * @return array the formatters that are supported by default
+     */
+    protected function defaultFormatters()
+    {
+        return [
+            self::FORMAT_HTML => 'yii\web\HtmlResponseFormatter',
+            self::FORMAT_XML => 'yii\web\XmlResponseFormatter',
+            self::FORMAT_JSON => 'yii\web\JsonResponseFormatter',
+            self::FORMAT_JSONP => [
+                'class' => 'yii\web\JsonResponseFormatter',
+                'useJsonp' => true,
+            ],
+        ];
+    }
+
+    /**
      * Prepares for sending the response.
      * The default implementation will convert [[data]] into [[content]] and set headers accordingly.
      * @throws InvalidConfigException if the formatter for the specified format is invalid or [[format]] is not supported
diff --git a/framework/web/View.php b/framework/web/View.php
index bb0febf..902493d 100644
--- a/framework/web/View.php
+++ b/framework/web/View.php
@@ -41,6 +41,9 @@ use yii\helpers\Url;
  */
 class View extends \yii\base\View
 {
+    /**
+     * @event Event an event that is triggered by [[beginBody()]].
+     */
     const EVENT_BEGIN_BODY = 'beginBody';
     /**
      * @event Event an event that is triggered by [[endBody()]].
--
libgit2 0.27.1