Skip to content

Commit

Permalink
Work on cleaning up HTTP component.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 20, 2016
1 parent e689b2a commit 4810e9d
Show file tree
Hide file tree
Showing 6 changed files with 576 additions and 542 deletions.
157 changes: 157 additions & 0 deletions src/Illuminate/Http/Concerns/InteractsWithContentTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Illuminate\Http\Concerns;

use Illuminate\Support\Str;

trait InteractsWithContentTypes
{
/**
* Determine if the given content types match.
*
* @param string $actual
* @param string $type
* @return bool
*/
public static function matchesType($actual, $type)
{
if ($actual === $type) {
return true;
}

$split = explode('/', $actual);

return isset($split[1]) && preg_match('#'.preg_quote($split[0], '#').'/.+\+'.preg_quote($split[1], '#').'#', $type);
}

/**
* Determine if the request is sending JSON.
*
* @return bool
*/
public function isJson()
{
return Str::contains($this->header('CONTENT_TYPE'), ['/json', '+json']);
}

/**
* Determine if the current request probably expects a JSON response.
*
* @return bool
*/
public function expectsJson()
{
return ($this->ajax() && ! $this->pjax()) || $this->wantsJson();
}

/**
* Determine if the current request is asking for JSON in return.
*
* @return bool
*/
public function wantsJson()
{
$acceptable = $this->getAcceptableContentTypes();

return isset($acceptable[0]) && Str::contains($acceptable[0], ['/json', '+json']);
}

/**
* Determines whether the current requests accepts a given content type.
*
* @param string|array $contentTypes
* @return bool
*/
public function accepts($contentTypes)
{
$accepts = $this->getAcceptableContentTypes();

if (count($accepts) === 0) {
return true;
}

$types = (array) $contentTypes;

foreach ($accepts as $accept) {
if ($accept === '*/*' || $accept === '*') {
return true;
}

foreach ($types as $type) {
if ($this->matchesType($accept, $type) || $accept === strtok($type, '/').'/*') {
return true;
}
}
}

return false;
}

/**
* Return the most suitable content type from the given array based on content negotiation.
*
* @param string|array $contentTypes
* @return string|null
*/
public function prefers($contentTypes)
{
$accepts = $this->getAcceptableContentTypes();

$contentTypes = (array) $contentTypes;

foreach ($accepts as $accept) {
if (in_array($accept, ['*/*', '*'])) {
return $contentTypes[0];
}

foreach ($contentTypes as $contentType) {
$type = $contentType;

if (! is_null($mimeType = $this->getMimeType($contentType))) {
$type = $mimeType;
}

if ($this->matchesType($type, $accept) || $accept === strtok($type, '/').'/*') {
return $contentType;
}
}
}
}

/**
* Determines whether a request accepts JSON.
*
* @return bool
*/
public function acceptsJson()
{
return $this->accepts('application/json');
}

/**
* Determines whether a request accepts HTML.
*
* @return bool
*/
public function acceptsHtml()
{
return $this->accepts('text/html');
}

/**
* Get the data format expected in the response.
*
* @param string $default
* @return string
*/
public function format($default = 'html')
{
foreach ($this->getAcceptableContentTypes() as $type) {
if ($format = $this->getFormat($type)) {
return $format;
}
}

return $default;
}
}
64 changes: 64 additions & 0 deletions src/Illuminate/Http/Concerns/InteractsWithFlashData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Illuminate\Http\Concerns;

trait InteractsWithFlashData
{
/**
* Retrieve an old input item.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function old($key = null, $default = null)
{
return $this->session()->getOldInput($key, $default);
}

/**
* Flash the input for the current request to the session.
*
* @return void
*/
public function flash()
{
$this->session()->flashInput($this->input());
}

/**
* Flash only some of the input to the session.
*
* @param array|mixed $keys
* @return void
*/
public function flashOnly($keys)
{
$this->session()->flashInput(
$this->only(is_array($keys) ? $keys : func_get_args())
);
}

/**
* Flash only some of the input to the session.
*
* @param array|mixed $keys
* @return void
*/
public function flashExcept($keys)
{
$this->session()->flashInput(
$this->except(is_array($keys) ? $keys : func_get_args())
);
}

/**
* Flush all of the old input from the session.
*
* @return void
*/
public function flush()
{
$this->session()->flashInput([]);
}
}
Loading

0 comments on commit 4810e9d

Please sign in to comment.