aboutsummaryrefslogtreecommitdiffhomepage
path: root/login/app/sprinkles/core/src/Http
diff options
context:
space:
mode:
authormarvin-borner@live.com2018-04-14 21:01:44 +0200
committermarvin-borner@live.com2018-04-14 21:01:44 +0200
commit937100e9bb2a2f5ab035e283e01e6d96e569ee51 (patch)
tree48256c7d39bcc00030027ddbe4b0b69c059b1e0e /login/app/sprinkles/core/src/Http
parent7ac4371989ac19ebbb753402319882c9c49d32dd (diff)
Added login things
Diffstat (limited to 'login/app/sprinkles/core/src/Http')
-rwxr-xr-xlogin/app/sprinkles/core/src/Http/Concerns/DeterminesContentType.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/login/app/sprinkles/core/src/Http/Concerns/DeterminesContentType.php b/login/app/sprinkles/core/src/Http/Concerns/DeterminesContentType.php
new file mode 100755
index 0000000..e963afa
--- /dev/null
+++ b/login/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';
+ }
+}