aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/sprinkles/core/src/Http
diff options
context:
space:
mode:
authormarvin-borner@live.com2018-04-16 21:09:05 +0200
committermarvin-borner@live.com2018-04-16 21:09:05 +0200
commitcf14306c2b3f82a81f8d56669a71633b4d4b5fce (patch)
tree86700651aa180026e89a66064b0364b1e4346f3f /main/app/sprinkles/core/src/Http
parent619b01b3615458c4ed78bfaeabb6b1a47cc8ad8b (diff)
Main merge to user management system - files are now at /main/public/
Diffstat (limited to 'main/app/sprinkles/core/src/Http')
-rwxr-xr-xmain/app/sprinkles/core/src/Http/Concerns/DeterminesContentType.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/main/app/sprinkles/core/src/Http/Concerns/DeterminesContentType.php b/main/app/sprinkles/core/src/Http/Concerns/DeterminesContentType.php
new file mode 100755
index 0000000..e963afa
--- /dev/null
+++ b/main/app/sprinkles/core/src/Http/Concerns/DeterminesContentType.php
@@ -0,0 +1,76 @@
+<?php
+/**
+ * UserFrosting (http://www.userfrosting.com)
+ *
+ * @link https://github.com/userfrosting/UserFrosting
+ * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
+ */
+namespace UserFrosting\Sprinkle\Core\Http\Concerns;
+
+use Psr\Http\Message\ServerRequestInterface;
+
+/**
+ * Trait for classes that need to determine a request's accepted content type(s).
+ *
+ * @author Alex Weissman (https://alexanderweissman.com)
+ */
+trait DeterminesContentType
+{
+ /**
+ * Known handled content types
+ *
+ * @var array
+ */
+ protected $knownContentTypes = [
+ 'application/json',
+ 'application/xml',
+ 'text/xml',
+ 'text/html',
+ 'text/plain'
+ ];
+
+ /**
+ * Determine which content type we know about is wanted using Accept header
+ *
+ * Note: This method is a bare-bones implementation designed specifically for
+ * Slim's error handling requirements. Consider a fully-feature solution such
+ * as willdurand/negotiation for any other situation.
+ *
+ * @param ServerRequestInterface $request
+ * @return string
+ */
+ protected function determineContentType(ServerRequestInterface $request, $ajaxDebug = false)
+ {
+ // For AJAX requests, if AJAX debugging is turned on, always return html
+ if ($ajaxDebug && $request->isXhr()) {
+ return 'text/html';
+ }
+
+ $acceptHeader = $request->getHeaderLine('Accept');
+ $selectedContentTypes = array_intersect(explode(',', $acceptHeader), $this->knownContentTypes);
+ $count = count($selectedContentTypes);
+
+ if ($count) {
+ $current = current($selectedContentTypes);
+
+ /**
+ * Ensure other supported content types take precedence over text/plain
+ * when multiple content types are provided via Accept header.
+ */
+ if ($current === 'text/plain' && $count > 1) {
+ return next($selectedContentTypes);
+ }
+
+ return $current;
+ }
+
+ if (preg_match('/\+(json|xml)/', $acceptHeader, $matches)) {
+ $mediaType = 'application/' . $matches[1];
+ if (in_array($mediaType, $this->knownContentTypes)) {
+ return $mediaType;
+ }
+ }
+
+ return 'text/html';
+ }
+}