Total Complexity | 40 |
Total Lines | 221 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ContentNegotiator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentNegotiator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class ContentNegotiator |
||
36 | { |
||
37 | use Injectable; |
||
38 | use Configurable; |
||
39 | |||
40 | /** |
||
41 | * @config |
||
42 | * @var string |
||
43 | */ |
||
44 | private static $content_type = ''; |
||
|
|||
45 | |||
46 | /** |
||
47 | * @config |
||
48 | * @var string |
||
49 | */ |
||
50 | private static $encoding = 'utf-8'; |
||
51 | |||
52 | /** |
||
53 | * @config |
||
54 | * @var bool |
||
55 | */ |
||
56 | private static $enabled = false; |
||
57 | |||
58 | /** |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected static $current_enabled = null; |
||
62 | |||
63 | /** |
||
64 | * @config |
||
65 | * @var string |
||
66 | */ |
||
67 | private static $default_format = 'html'; |
||
68 | |||
69 | /** |
||
70 | * Returns true if negotiation is enabled for the given response. By default, negotiation is only |
||
71 | * enabled for pages that have the xml header. |
||
72 | * |
||
73 | * @param HTTPResponse $response |
||
74 | * @return bool |
||
75 | */ |
||
76 | public static function enabled_for($response) |
||
77 | { |
||
78 | $contentType = $response->getHeader("Content-Type"); |
||
79 | |||
80 | // Disable content negotiation for other content types |
||
81 | if ($contentType |
||
82 | && substr((string) $contentType, 0, 9) != 'text/html' |
||
83 | && substr((string) $contentType, 0, 21) != 'application/xhtml+xml' |
||
84 | ) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | if (ContentNegotiator::getEnabled()) { |
||
89 | return true; |
||
90 | } else { |
||
91 | return (substr((string) $response->getBody(), 0, 5) == '<' . '?xml'); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Gets the current enabled status, if it is not set this will fallback to config |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public static function getEnabled() |
||
101 | { |
||
102 | if (isset(static::$current_enabled)) { |
||
103 | return static::$current_enabled; |
||
104 | } |
||
105 | return Config::inst()->get(static::class, 'enabled'); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Sets the current enabled status |
||
110 | * |
||
111 | * @param bool $enabled |
||
112 | */ |
||
113 | public static function setEnabled($enabled) |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param HTTPResponse $response |
||
120 | */ |
||
121 | public static function process(HTTPResponse $response) |
||
122 | { |
||
123 | if (!self::enabled_for($response)) { |
||
124 | return; |
||
125 | } |
||
126 | |||
127 | $mimes = [ |
||
128 | "xhtml" => "application/xhtml+xml", |
||
129 | "html" => "text/html", |
||
130 | ]; |
||
131 | $q = []; |
||
132 | if (headers_sent()) { |
||
133 | $chosenFormat = static::config()->get('default_format'); |
||
134 | } elseif (isset($_GET['forceFormat'])) { |
||
135 | $chosenFormat = $_GET['forceFormat']; |
||
136 | } else { |
||
137 | // The W3C validator doesn't send an HTTP_ACCEPT header, but it can support xhtml. We put this |
||
138 | // special case in here so that designers don't get worried that their templates are HTML4. |
||
139 | if (isset($_SERVER['HTTP_USER_AGENT']) && substr($_SERVER['HTTP_USER_AGENT'], 0, 14) == 'W3C_Validator/') { |
||
140 | $chosenFormat = "xhtml"; |
||
141 | } else { |
||
142 | foreach ($mimes as $format => $mime) { |
||
143 | $regExp = '/' . str_replace(['+', '/'], ['\+', '\/'], $mime ?: '') . '(;q=(\d+\.\d+))?/i'; |
||
144 | if (isset($_SERVER['HTTP_ACCEPT']) && preg_match((string) $regExp, $_SERVER['HTTP_ACCEPT'], $matches)) { |
||
145 | $preference = isset($matches[2]) ? $matches[2] : 1; |
||
146 | if (!isset($q[$preference])) { |
||
147 | $q[$preference] = $format; |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | if ($q) { |
||
153 | // Get the preferred format |
||
154 | krsort($q); |
||
155 | $chosenFormat = reset($q); |
||
156 | } else { |
||
157 | $chosenFormat = Config::inst()->get(static::class, 'default_format'); |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | |||
162 | $negotiator = new ContentNegotiator(); |
||
163 | $negotiator->$chosenFormat($response); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Check user defined content type and use it, if it's empty use the strict application/xhtml+xml. |
||
168 | * Replaces a few common tags and entities with their XHTML representations (<br>, <img>, |
||
169 | * <input>, checked, selected). |
||
170 | * |
||
171 | * @param HTTPResponse $response |
||
172 | * |
||
173 | * @todo Search for more xhtml replacement |
||
174 | */ |
||
175 | public function xhtml(HTTPResponse $response) |
||
176 | { |
||
177 | $content = $response->getBody(); |
||
178 | $encoding = Config::inst()->get('SilverStripe\\Control\\ContentNegotiator', 'encoding'); |
||
179 | |||
180 | $contentType = Config::inst()->get('SilverStripe\\Control\\ContentNegotiator', 'content_type'); |
||
181 | if (empty($contentType)) { |
||
182 | $response->addHeader("Content-Type", "application/xhtml+xml; charset=" . $encoding); |
||
183 | } else { |
||
184 | $response->addHeader("Content-Type", $contentType . "; charset=" . $encoding); |
||
185 | } |
||
186 | $response->addHeader("Vary", "Accept"); |
||
187 | |||
188 | // Fix base tag |
||
189 | $content = preg_replace( |
||
190 | '/<base href="([^"]*)"><!--\[if[[^\]*]\] \/><!\[endif\]-->/', |
||
191 | '<base href="$1" />', |
||
192 | $content ?: '' |
||
193 | ); |
||
194 | |||
195 | $content = str_replace(' ', ' ', $content ?: ''); |
||
196 | $content = str_replace('<br>', '<br />', $content ?: ''); |
||
197 | $content = str_replace('<hr>', '<hr />', $content ?: ''); |
||
198 | $content = preg_replace('#(<img[^>]*[^/>])>#i', '\\1/>', $content ?: ''); |
||
199 | $content = preg_replace('#(<input[^>]*[^/>])>#i', '\\1/>', $content ?: ''); |
||
200 | $content = preg_replace('#(<param[^>]*[^/>])>#i', '\\1/>', $content ?: ''); |
||
201 | $content = preg_replace("#(\<option[^>]*[\s]+selected)(?!\s*\=)#si", "$1=\"selected\"$2", $content ?: ''); |
||
202 | $content = preg_replace("#(\<input[^>]*[\s]+checked)(?!\s*\=)#si", "$1=\"checked\"$2", $content ?: ''); |
||
203 | |||
204 | $response->setBody($content); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Performs the following replacements: |
||
209 | * - Check user defined content type and use it, if it's empty use the text/html. |
||
210 | * - If find a XML header replaces it and existing doctypes with HTML4.01 Strict. |
||
211 | * - Replaces self-closing tags like <img /> with unclosed solitary tags like <img>. |
||
212 | * - Replaces all occurrences of "application/xhtml+xml" with "text/html" in the template. |
||
213 | * - Removes "xmlns" attributes and any <?xml> Pragmas. |
||
214 | * |
||
215 | * @param HTTPResponse $response |
||
216 | */ |
||
217 | public function html(HTTPResponse $response) |
||
256 | } |
||
257 | } |
||
258 |