Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

View File

@@ -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;
}
}

View File

@@ -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->hasSession() ? $this->session()->getOldInput($key, $default) : $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([]);
}
}

View File

@@ -0,0 +1,375 @@
<?php
namespace Illuminate\Http\Concerns;
use stdClass;
use SplFileInfo;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\UploadedFile;
trait InteractsWithInput
{
/**
* Retrieve a server variable from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function server($key = null, $default = null)
{
return $this->retrieveItem('server', $key, $default);
}
/**
* Determine if a header is set on the request.
*
* @param string $key
* @return bool
*/
public function hasHeader($key)
{
return ! is_null($this->header($key));
}
/**
* Retrieve a header from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function header($key = null, $default = null)
{
return $this->retrieveItem('headers', $key, $default);
}
/**
* Get the bearer token from the request headers.
*
* @return string|null
*/
public function bearerToken()
{
$header = $this->header('Authorization', '');
if (Str::startsWith($header, 'Bearer ')) {
return Str::substr($header, 7);
}
}
/**
* Determine if the request contains a given input item key.
*
* @param string|array $key
* @return bool
*/
public function exists($key)
{
return $this->has($key);
}
/**
* Determine if the request contains a given input item key.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();
$input = $this->all();
foreach ($keys as $value) {
if (! Arr::has($input, $value)) {
return false;
}
}
return true;
}
/**
* Determine if the request contains any of the given inputs.
*
* @param dynamic $key
* @return bool
*/
public function hasAny(...$keys)
{
$input = $this->all();
foreach ($keys as $key) {
if (Arr::has($input, $key)) {
return true;
}
}
return false;
}
/**
* Determine if the request contains a non-empty value for an input item.
*
* @param string|array $key
* @return bool
*/
public function filled($key)
{
$keys = is_array($key) ? $key : func_get_args();
foreach ($keys as $value) {
if ($this->isEmptyString($value)) {
return false;
}
}
return true;
}
/**
* Determine if the given input key is an empty string for "has".
*
* @param string $key
* @return bool
*/
protected function isEmptyString($key)
{
$value = $this->input($key);
return ! is_bool($value) && ! is_array($value) && trim((string) $value) === '';
}
/**
* Get the keys for all of the input and files.
*
* @return array
*/
public function keys()
{
return array_merge(array_keys($this->input()), $this->files->keys());
}
/**
* Get all of the input and files for the request.
*
* @param array|mixed $keys
* @return array
*/
public function all($keys = null)
{
$input = array_replace_recursive($this->input(), $this->allFiles());
if (! $keys) {
return $input;
}
$results = [];
foreach (is_array($keys) ? $keys : func_get_args() as $key) {
Arr::set($results, $key, Arr::get($input, $key));
}
return $results;
}
/**
* Retrieve an input item from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function input($key = null, $default = null)
{
return data_get(
$this->getInputSource()->all() + $this->query->all(), $key, $default
);
}
/**
* Get a subset containing the provided keys with values from the input data.
*
* @param array|mixed $keys
* @return array
*/
public function only($keys)
{
$results = [];
$input = $this->all();
$placeholder = new stdClass;
foreach (is_array($keys) ? $keys : func_get_args() as $key) {
$value = data_get($input, $key, $placeholder);
if ($value !== $placeholder) {
Arr::set($results, $key, $value);
}
}
return $results;
}
/**
* Get all of the input except for a specified array of items.
*
* @param array|mixed $keys
* @return array
*/
public function except($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
$results = $this->all();
Arr::forget($results, $keys);
return $results;
}
/**
* Retrieve a query string item from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function query($key = null, $default = null)
{
return $this->retrieveItem('query', $key, $default);
}
/**
* Retrieve a request payload item from the request.
*
* @param string $key
* @param string|array|null $default
*
* @return string|array
*/
public function post($key = null, $default = null)
{
return $this->retrieveItem('request', $key, $default);
}
/**
* Determine if a cookie is set on the request.
*
* @param string $key
* @return bool
*/
public function hasCookie($key)
{
return ! is_null($this->cookie($key));
}
/**
* Retrieve a cookie from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function cookie($key = null, $default = null)
{
return $this->retrieveItem('cookies', $key, $default);
}
/**
* Get an array of all of the files on the request.
*
* @return array
*/
public function allFiles()
{
$files = $this->files->all();
return $this->convertedFiles
? $this->convertedFiles
: $this->convertedFiles = $this->convertUploadedFiles($files);
}
/**
* Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
*
* @param array $files
* @return array
*/
protected function convertUploadedFiles(array $files)
{
return array_map(function ($file) {
if (is_null($file) || (is_array($file) && empty(array_filter($file)))) {
return $file;
}
return is_array($file)
? $this->convertUploadedFiles($file)
: UploadedFile::createFromBase($file);
}, $files);
}
/**
* Determine if the uploaded data contains a file.
*
* @param string $key
* @return bool
*/
public function hasFile($key)
{
if (! is_array($files = $this->file($key))) {
$files = [$files];
}
foreach ($files as $file) {
if ($this->isValidFile($file)) {
return true;
}
}
return false;
}
/**
* Check that the given file is a valid file instance.
*
* @param mixed $file
* @return bool
*/
protected function isValidFile($file)
{
return $file instanceof SplFileInfo && $file->getPath() !== '';
}
/**
* Retrieve a file from the request.
*
* @param string $key
* @param mixed $default
* @return \Illuminate\Http\UploadedFile|array|null
*/
public function file($key = null, $default = null)
{
return data_get($this->allFiles(), $key, $default);
}
/**
* Retrieve a parameter item from a given source.
*
* @param string $source
* @param string $key
* @param string|array|null $default
* @return string|array
*/
protected function retrieveItem($source, $key, $default)
{
if (is_null($key)) {
return $this->$source->all();
}
return $this->$source->get($key, $default);
}
}

View File

@@ -1,10 +0,0 @@
<?php
namespace Illuminate\Http\Exception;
use Exception;
class PostTooLargeException extends Exception
{
//
}

View File

@@ -1,6 +1,6 @@
<?php
namespace Illuminate\Http\Exception;
namespace Illuminate\Http\Exceptions;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;

View File

@@ -0,0 +1,23 @@
<?php
namespace Illuminate\Http\Exceptions;
use Exception;
use Symfony\Component\HttpKernel\Exception\HttpException;
class PostTooLargeException extends HttpException
{
/**
* PostTooLargeException constructor.
*
* @param string|null $message
* @param \Exception|null $previous
* @param array $headers
* @param int $code
* @return void
*/
public function __construct($message = null, Exception $previous = null, array $headers = [], $code = 0)
{
parent::__construct(413, $message, $previous, $headers, $code);
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Illuminate\Http;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
class File extends SymfonyFile
{
use FileHelpers;
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Illuminate\Http;
use Illuminate\Support\Str;
trait FileHelpers
{
/**
* The cache copy of the file's hash name.
*
* @var string
*/
protected $hashName = null;
/**
* Get the fully qualified path to the file.
*
* @return string
*/
public function path()
{
return $this->getRealPath();
}
/**
* Get the file's extension.
*
* @return string
*/
public function extension()
{
return $this->guessExtension();
}
/**
* Get the file's extension supplied by the client.
*
* @return string
*/
public function clientExtension()
{
return $this->guessClientExtension();
}
/**
* Get a filename for the file.
*
* @param string $path
* @return string
*/
public function hashName($path = null)
{
if ($path) {
$path = rtrim($path, '/').'/';
}
$hash = $this->hashName ?: $this->hashName = Str::random(40);
return $path.$hash.'.'.$this->guessExtension();
}
}

View File

@@ -4,13 +4,16 @@ namespace Illuminate\Http;
use JsonSerializable;
use InvalidArgumentException;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Symfony\Component\HttpFoundation\JsonResponse as BaseJsonResponse;
class JsonResponse extends BaseJsonResponse
{
use ResponseTrait;
use ResponseTrait, Macroable {
Macroable::__call as macroCall;
}
/**
* Constructor.
@@ -19,6 +22,7 @@ class JsonResponse extends BaseJsonResponse
* @param int $status
* @param array $headers
* @param int $options
* @return void
*/
public function __construct($data = null, $status = 200, $headers = [], $options = 0)
{
@@ -27,11 +31,22 @@ class JsonResponse extends BaseJsonResponse
parent::__construct($data, $status, $headers);
}
/**
* Sets the JSONP callback.
*
* @param string|null $callback
* @return $this
*/
public function withCallback($callback = null)
{
return $this->setCallback($callback);
}
/**
* Get the json_decoded data from the response.
*
* @param bool $assoc
* @param int $depth
* @param int $depth
* @return mixed
*/
public function getData($assoc = false, $depth = 512)
@@ -44,17 +59,19 @@ class JsonResponse extends BaseJsonResponse
*/
public function setData($data = [])
{
if ($data instanceof Arrayable) {
$this->data = json_encode($data->toArray(), $this->encodingOptions);
} elseif ($data instanceof Jsonable) {
$this->original = $data;
if ($data instanceof Jsonable) {
$this->data = $data->toJson($this->encodingOptions);
} elseif ($data instanceof JsonSerializable) {
$this->data = json_encode($data->jsonSerialize(), $this->encodingOptions);
} elseif ($data instanceof Arrayable) {
$this->data = json_encode($data->toArray(), $this->encodingOptions);
} else {
$this->data = json_encode($data, $this->encodingOptions);
}
if (JSON_ERROR_NONE !== json_last_error()) {
if (! $this->hasValidJson(json_last_error())) {
throw new InvalidArgumentException(json_last_error_msg());
}
@@ -62,33 +79,36 @@ class JsonResponse extends BaseJsonResponse
}
/**
* Get the JSON encoding options.
* Determine if an error occurred during JSON encoding.
*
* @return int
* @param int $jsonError
* @return bool
*/
public function getJsonOptions()
protected function hasValidJson($jsonError)
{
return $this->getEncodingOptions();
return $jsonError === JSON_ERROR_NONE ||
($jsonError === JSON_ERROR_UNSUPPORTED_TYPE &&
$this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR));
}
/**
* {@inheritdoc}
*/
public function setEncodingOptions($encodingOptions)
{
return $this->setJsonOptions($encodingOptions);
}
/**
* Set the JSON encoding options.
*
* @param int $options
* @return mixed
*/
public function setJsonOptions($options)
public function setEncodingOptions($options)
{
$this->encodingOptions = (int) $options;
return $this->setData($this->getData());
}
/**
* Determine if a JSON encoding option is set.
*
* @param int $option
* @return bool
*/
public function hasEncodingOption($option)
{
return (bool) ($this->encodingOptions & $option);
}
}

View File

@@ -11,7 +11,7 @@ class FrameGuard
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle($request, Closure $next)
{

View File

@@ -6,6 +6,7 @@ use BadMethodCallException;
use Illuminate\Support\Str;
use Illuminate\Support\MessageBag;
use Illuminate\Support\ViewErrorBag;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Session\Store as SessionStore;
use Illuminate\Contracts\Support\MessageProvider;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
@@ -13,7 +14,9 @@ use Symfony\Component\HttpFoundation\RedirectResponse as BaseRedirectResponse;
class RedirectResponse extends BaseRedirectResponse
{
use ResponseTrait;
use ResponseTrait, Macroable {
Macroable::__call as macroCall;
}
/**
* The request instance.
@@ -70,9 +73,9 @@ class RedirectResponse extends BaseRedirectResponse
*/
public function withInput(array $input = null)
{
$input = $input ?: $this->request->input();
$this->session->flashInput($this->removeFilesFromInput($input));
$this->session->flashInput($this->removeFilesFromInput(
! is_null($input) ? $input : $this->request->input()
));
return $this;
}
@@ -101,7 +104,6 @@ class RedirectResponse extends BaseRedirectResponse
/**
* Flash an array of input to the session.
*
* @param mixed string
* @return $this
*/
public function onlyInput()
@@ -112,7 +114,6 @@ class RedirectResponse extends BaseRedirectResponse
/**
* Flash an array of input to the session.
*
* @param mixed string
* @return \Illuminate\Http\RedirectResponse
*/
public function exceptInput()
@@ -131,8 +132,14 @@ class RedirectResponse extends BaseRedirectResponse
{
$value = $this->parseErrors($provider);
$errors = $this->session->get('errors', new ViewErrorBag);
if (! $errors instanceof ViewErrorBag) {
$errors = new ViewErrorBag;
}
$this->session->flash(
'errors', $this->session->get('errors', new ViewErrorBag)->put($key, $value)
'errors', $errors->put($key, $value)
);
return $this;
@@ -153,6 +160,16 @@ class RedirectResponse extends BaseRedirectResponse
return new MessageBag((array) $provider);
}
/**
* Get the original response content.
*
* @return null
*/
public function getOriginalContent()
{
//
}
/**
* Get the request instance.
*
@@ -206,10 +223,16 @@ class RedirectResponse extends BaseRedirectResponse
*/
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
if (Str::startsWith($method, 'with')) {
return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
}
throw new BadMethodCallException("Method [$method] does not exist on Redirect.");
throw new BadMethodCallException(
"Method [$method] does not exist on Redirect."
);
}
}

View File

@@ -4,7 +4,6 @@ namespace Illuminate\Http;
use Closure;
use ArrayAccess;
use SplFileInfo;
use RuntimeException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
@@ -15,12 +14,15 @@ use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
class Request extends SymfonyRequest implements Arrayable, ArrayAccess
{
use Macroable;
use Concerns\InteractsWithContentTypes,
Concerns\InteractsWithFlashData,
Concerns\InteractsWithInput,
Macroable;
/**
* The decoded JSON content for the request.
*
* @var string
* @var \Symfony\Component\HttpFoundation\ParameterBag|null
*/
protected $json;
@@ -119,9 +121,11 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function fullUrlWithQuery(array $query)
{
$question = $this->getBaseUrl().$this->getPathInfo() == '/' ? '/?' : '?';
return count($this->query()) > 0
? $this->url().'/?'.http_build_query(array_merge($this->query(), $query))
: $this->fullUrl().'?'.http_build_query($query);
? $this->url().$question.http_build_query(array_merge($this->query(), $query))
: $this->fullUrl().$question.http_build_query($query);
}
/**
@@ -137,7 +141,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
}
/**
* Get the current encoded path info for the request.
* Get the current decoded path info for the request.
*
* @return string
*/
@@ -165,23 +169,23 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function segments()
{
$segments = explode('/', $this->path());
$segments = explode('/', $this->decodedPath());
return array_values(array_filter($segments, function ($v) {
return $v != '';
return array_values(array_filter($segments, function ($value) {
return $value !== '';
}));
}
/**
* Determine if the current request URI matches a pattern.
*
* @param mixed string
* @param dynamic $patterns
* @return bool
*/
public function is()
public function is(...$patterns)
{
foreach (func_get_args() as $pattern) {
if (Str::is($pattern, urldecode($this->path()))) {
foreach ($patterns as $pattern) {
if (Str::is($pattern, $this->decodedPath())) {
return true;
}
}
@@ -190,16 +194,27 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
}
/**
* Determine if the current request URL and query string matches a pattern.
* Determine if the route name matches a given pattern.
*
* @param mixed string
* @param dynamic $patterns
* @return bool
*/
public function fullUrlIs()
public function routeIs(...$patterns)
{
return $this->route() && $this->route()->named(...$patterns);
}
/**
* Determine if the current request URL and query string matches a pattern.
*
* @param dynamic $patterns
* @return bool
*/
public function fullUrlIs(...$patterns)
{
$url = $this->fullUrl();
foreach (func_get_args() as $pattern) {
foreach ($patterns as $pattern) {
if (Str::is($pattern, $url)) {
return true;
}
@@ -239,7 +254,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
}
/**
* Returns the client IP address.
* Get the client IP address.
*
* @return string
*/
@@ -249,7 +264,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
}
/**
* Returns the client IP addresses.
* Get the client IP addresses.
*
* @return array
*/
@@ -259,379 +274,39 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
}
/**
* Determine if the request contains a given input item key.
* Get the client user agent.
*
* @param string|array $key
* @return bool
* @return string
*/
public function exists($key)
public function userAgent()
{
$keys = is_array($key) ? $key : func_get_args();
$input = $this->all();
foreach ($keys as $value) {
if (! Arr::has($input, $value)) {
return false;
}
}
return true;
}
/**
* Determine if the request contains a non-empty value for an input item.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();
foreach ($keys as $value) {
if ($this->isEmptyString($value)) {
return false;
}
}
return true;
}
/**
* Determine if the given input key is an empty string for "has".
*
* @param string $key
* @return bool
*/
protected function isEmptyString($key)
{
$value = $this->input($key);
$boolOrArray = is_bool($value) || is_array($value);
return ! $boolOrArray && trim((string) $value) === '';
}
/**
* Get all of the input and files for the request.
*
* @return array
*/
public function all()
{
return array_replace_recursive($this->input(), $this->allFiles());
}
/**
* Retrieve an input item from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function input($key = null, $default = null)
{
$input = $this->getInputSource()->all() + $this->query->all();
return data_get($input, $key, $default);
}
/**
* Get a subset containing the provided keys with values from the input data.
*
* @param array|mixed $keys
* @return array
*/
public function only($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
$results = [];
$input = $this->all();
foreach ($keys as $key) {
Arr::set($results, $key, data_get($input, $key));
}
return $results;
}
/**
* Get all of the input except for a specified array of items.
*
* @param array|mixed $keys
* @return array
*/
public function except($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
$results = $this->all();
Arr::forget($results, $keys);
return $results;
}
/**
* Intersect an array of items with the input data.
*
* @param array|mixed $keys
* @return array
*/
public function intersect($keys)
{
return array_filter($this->only(is_array($keys) ? $keys : func_get_args()));
}
/**
* Retrieve a query string item from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function query($key = null, $default = null)
{
return $this->retrieveItem('query', $key, $default);
}
/**
* Determine if a cookie is set on the request.
*
* @param string $key
* @return bool
*/
public function hasCookie($key)
{
return ! is_null($this->cookie($key));
}
/**
* Retrieve a cookie from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function cookie($key = null, $default = null)
{
return $this->retrieveItem('cookies', $key, $default);
}
/**
* Get an array of all of the files on the request.
*
* @return array
*/
public function allFiles()
{
$files = $this->files->all();
return $this->convertedFiles
? $this->convertedFiles
: $this->convertedFiles = $this->convertUploadedFiles($files);
}
/**
* Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
*
* @param array $files
* @return array
*/
protected function convertUploadedFiles(array $files)
{
return array_map(function ($file) {
if (is_null($file) || (is_array($file) && empty(array_filter($file)))) {
return $file;
}
return is_array($file)
? $this->convertUploadedFiles($file)
: UploadedFile::createFromBase($file);
}, $files);
}
/**
* Retrieve a file from the request.
*
* @param string $key
* @param mixed $default
* @return \Illuminate\Http\UploadedFile|array|null
*/
public function file($key = null, $default = null)
{
return data_get($this->allFiles(), $key, $default);
}
/**
* Determine if the uploaded data contains a file.
*
* @param string $key
* @return bool
*/
public function hasFile($key)
{
if (! is_array($files = $this->file($key))) {
$files = [$files];
}
foreach ($files as $file) {
if ($this->isValidFile($file)) {
return true;
}
}
return false;
}
/**
* Check that the given file is a valid file instance.
*
* @param mixed $file
* @return bool
*/
protected function isValidFile($file)
{
return $file instanceof SplFileInfo && $file->getPath() != '';
}
/**
* Determine if a header is set on the request.
*
* @param string $key
* @return bool
*/
public function hasHeader($key)
{
return ! is_null($this->header($key));
}
/**
* Retrieve a header from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function header($key = null, $default = null)
{
return $this->retrieveItem('headers', $key, $default);
}
/**
* Retrieve a server variable from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function server($key = null, $default = null)
{
return $this->retrieveItem('server', $key, $default);
}
/**
* 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.
*
* @param string $filter
* @param array $keys
* @return void
*/
public function flash($filter = null, $keys = [])
{
$flash = (! is_null($filter)) ? $this->$filter($keys) : $this->input();
$this->session()->flashInput($flash);
}
/**
* Flash only some of the input to the session.
*
* @param array|mixed $keys
* @return void
*/
public function flashOnly($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
return $this->flash('only', $keys);
}
/**
* Flash only some of the input to the session.
*
* @param array|mixed $keys
* @return void
*/
public function flashExcept($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
return $this->flash('except', $keys);
}
/**
* Flush all of the old input from the session.
*
* @return void
*/
public function flush()
{
$this->session()->flashInput([]);
}
/**
* Retrieve a parameter item from a given source.
*
* @param string $source
* @param string $key
* @param string|array|null $default
* @return string|array
*/
protected function retrieveItem($source, $key, $default)
{
if (is_null($key)) {
return $this->$source->all();
}
return $this->$source->get($key, $default);
return $this->headers->get('User-Agent');
}
/**
* Merge new input into the current request's input array.
*
* @param array $input
* @return void
* @return \Illuminate\Http\Request
*/
public function merge(array $input)
{
$this->getInputSource()->add($input);
return $this;
}
/**
* Replace the input for the current request.
*
* @param array $input
* @return void
* @return \Illuminate\Http\Request
*/
public function replace(array $input)
{
$this->getInputSource()->replace($input);
return $this;
}
/**
@@ -639,7 +314,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*
* @param string $key
* @param mixed $default
* @return mixed
* @return \Symfony\Component\HttpFoundation\ParameterBag|mixed
*/
public function json($key = null, $default = null)
{
@@ -665,160 +340,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
return $this->json();
}
return $this->getMethod() == 'GET' ? $this->query : $this->request;
}
/**
* 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 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;
}
/**
* Get the bearer token from the request headers.
*
* @return string|null
*/
public function bearerToken()
{
$header = $this->header('Authorization', '');
if (Str::startsWith($header, 'Bearer ')) {
return Str::substr($header, 7);
}
return in_array($this->getRealMethod(), ['GET', 'HEAD']) ? $this->query : $this->request;
}
/**
@@ -836,10 +358,12 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
$content = $request->content;
$request = (new static)->duplicate(
$request->query->all(), $request->request->all(), $request->attributes->all(),
$request->cookies->all(), $request->files->all(), $request->server->all()
$request->query->all(),
$request->request->all(),
$request->attributes->all(),
$request->cookies->all(),
$request->files->all(),
$request->server->all()
);
$request->content = $content;
@@ -854,7 +378,32 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
{
return parent::duplicate($query, $request, $attributes, $cookies, array_filter((array) $files), $server);
return parent::duplicate($query, $request, $attributes, $cookies, $this->filterFiles($files), $server);
}
/**
* Filter the given array of files, removing any empty values.
*
* @param mixed $files
* @return mixed
*/
protected function filterFiles($files)
{
if (! $files) {
return;
}
foreach ($files as $key => $file) {
if (is_array($file)) {
$files[$key] = $this->filterFiles($files[$key]);
}
if (empty($files[$key])) {
unset($files[$key]);
}
}
return $files;
}
/**
@@ -873,6 +422,17 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
return $this->getSession();
}
/**
* Set the session instance on the request.
*
* @param \Illuminate\Contracts\Session\Session $session
* @return void
*/
public function setLaravelSession($session)
{
$this->session = $session;
}
/**
* Get the user making the request.
*
@@ -887,7 +447,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
/**
* Get the route handling the request.
*
* @param string|null $param
* @param string|null $param
*
* @return \Illuminate\Routing\Route|object|string
*/
@@ -897,9 +457,9 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
if (is_null($route) || is_null($param)) {
return $route;
} else {
return $route->parameter($param);
}
return $route->parameter($param);
}
/**
@@ -911,16 +471,27 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function fingerprint()
{
if (! $this->route()) {
if (! $route = $this->route()) {
throw new RuntimeException('Unable to generate fingerprint. Route unavailable.');
}
return sha1(
implode('|', $this->route()->methods()).
'|'.$this->route()->domain().
'|'.$this->route()->uri().
'|'.$this->ip()
);
return sha1(implode('|', array_merge(
$route->methods(),
[$route->getDomain(), $route->uri(), $this->ip()]
)));
}
/**
* Set the JSON payload for the request.
*
* @param \Symfony\Component\HttpFoundation\ParameterBag $json
* @return $this
*/
public function setJson($json)
{
$this->json = $json;
return $this;
}
/**
@@ -991,7 +562,10 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->all());
return array_key_exists(
$offset,
$this->all() + $this->route()->parameters()
);
}
/**
@@ -1002,7 +576,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function offsetGet($offset)
{
return data_get($this->all(), $offset);
return $this->__get($offset);
}
/**
@@ -1014,7 +588,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function offsetSet($offset, $value)
{
return $this->getInputSource()->set($offset, $value);
$this->getInputSource()->set($offset, $value);
}
/**
@@ -1025,7 +599,7 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function offsetUnset($offset)
{
return $this->getInputSource()->remove($offset);
$this->getInputSource()->remove($offset);
}
/**
@@ -1047,12 +621,10 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
*/
public function __get($key)
{
$all = $this->all();
if (array_key_exists($key, $all)) {
return $all[$key];
} else {
return $this->route($key);
if (array_key_exists($key, $this->all())) {
return data_get($this->all(), $key);
}
return $this->route($key);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Illuminate\Http\Resources;
use Illuminate\Support\Str;
use Illuminate\Pagination\AbstractPaginator;
trait CollectsResources
{
/**
* Map the given collection resource into its individual resources.
*
* @param mixed $resource
* @return mixed
*/
protected function collectResource($resource)
{
if ($resource instanceof MissingValue) {
return $resource;
}
$collects = $this->collects();
$this->collection = $collects && ! $resource->first() instanceof $collects
? $resource->mapInto($collects)
: $resource->toBase();
return $resource instanceof AbstractPaginator
? $resource->setCollection($this->collection)
: $this->collection;
}
/**
* Get the resource that this resource collects.
*
* @return string|null
*/
protected function collects()
{
if ($this->collects) {
return $this->collects;
}
if (Str::endsWith(class_basename($this), 'Collection') &&
class_exists($class = Str::replaceLast('Collection', '', get_class($this)))) {
return $class;
}
}
/**
* Get an iterator for the resource collection.
*
* @return \ArrayIterator
*/
public function getIterator()
{
return $this->collection->getIterator();
}
}

View File

@@ -0,0 +1,178 @@
<?php
namespace Illuminate\Http\Resources;
use Illuminate\Support\Arr;
trait ConditionallyLoadsAttributes
{
/**
* Filter the given data, removing any optional values.
*
* @param array $data
* @return array
*/
protected function filter($data)
{
$index = -1;
foreach ($data as $key => $value) {
$index++;
if (is_array($value)) {
$data[$key] = $this->filter($value);
continue;
}
if (is_numeric($key) && $value instanceof MergeValue) {
return $this->merge($data, $index, $this->filter($value->data));
}
if (($value instanceof PotentiallyMissing && $value->isMissing()) ||
($value instanceof self &&
$value->resource instanceof PotentiallyMissing &&
$value->isMissing())) {
unset($data[$key]);
$index--;
}
if ($value instanceof self && is_null($value->resource)) {
$data[$key] = null;
}
}
return $data;
}
/**
* Merge the given data in at the given index.
*
* @param array $data
* @param int $index
* @param array $merge
* @return array
*/
protected function merge($data, $index, $merge)
{
if (array_values($data) === $data) {
return array_merge(
array_merge(array_slice($data, 0, $index, true), $merge),
$this->filter(array_slice($data, $index + 1, null, true))
);
}
return array_slice($data, 0, $index, true) +
$merge +
$this->filter(array_slice($data, $index + 1, null, true));
}
/**
* Retrieve a value based on a given condition.
*
* @param bool $condition
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function when($condition, $value, $default = null)
{
if ($condition) {
return value($value);
}
return func_num_args() === 3 ? value($default) : new MissingValue;
}
/**
* Merge a value based on a given condition.
*
* @param bool $condition
* @param mixed $value
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function mergeWhen($condition, $value)
{
return $condition ? new MergeValue(value($value)) : new MissingValue;
}
/**
* Merge the given attributes.
*
* @param array $attributes
* @return \Illuminate\Http\Resources\MergeValue
*/
protected function attributes($attributes)
{
return new MergeValue(
Arr::only($this->resource->toArray(), $attributes)
);
}
/**
* Retrieve a relationship if it has been loaded.
*
* @param string $relationship
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenLoaded($relationship, $value = null, $default = null)
{
if (func_num_args() < 3) {
$default = new MissingValue;
}
if (! $this->resource->relationLoaded($relationship)) {
return $default;
}
if (func_num_args() === 1) {
return $this->resource->{$relationship};
}
if ($this->resource->{$relationship} === null) {
return null;
}
return value($value);
}
/**
* Execute a callback if the given pivot table has been loaded.
*
* @param string $table
* @param mixed $value
* @param mixed $default
* @return \Illuminate\Http\Resources\MissingValue|mixed
*/
protected function whenPivotLoaded($table, $value, $default = null)
{
if (func_num_args() === 2) {
$default = new MissingValue;
}
return $this->when(
$this->resource->pivot &&
($this->resource->pivot instanceof $table ||
$this->resource->pivot->getTable() === $table),
...[$value, $default]
);
}
/**
* Transform the given value if it is present.
*
* @param mixed $value
* @param callable $callback
* @param mixed $default
* @return mixed
*/
protected function transform($value, callable $callback, $default = null)
{
return transform(
$value, $callback, func_num_args() === 3 ? $default : new MissingValue
);
}
}

View File

@@ -0,0 +1,130 @@
<?php
namespace Illuminate\Http\Resources;
use Exception;
trait DelegatesToResource
{
/**
* Get the value of the resource's route key.
*
* @return mixed
*/
public function getRouteKey()
{
return $this->resource->getRouteKey();
}
/**
* Get the route key for the resource.
*
* @return string
*/
public function getRouteKeyName()
{
return $this->resource->getRouteKeyName();
}
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @return void
* @throws \Exception
*/
public function resolveRouteBinding($value)
{
throw new Exception('Resources may not be implicitly resolved from route bindings.');
}
/**
* Determine if the given attribute exists.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->resource);
}
/**
* Get the value for a given offset.
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->resource[$offset];
}
/**
* Set the value for a given offset.
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value)
{
$this->resource[$offset] = $value;
}
/**
* Unset the value for a given offset.
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset)
{
unset($this->resource[$offset]);
}
/**
* Determine if an attribute exists on the resource.
*
* @param string $key
* @return bool
*/
public function __isset($key)
{
return isset($this->resource->{$key});
}
/**
* Unset an attribute on the resource.
*
* @param string $key
* @return void
*/
public function __unset($key)
{
unset($this->resource->{$key});
}
/**
* Dynamically get properties from the underlying resource.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->resource->{$key};
}
/**
* Dynamically pass method calls to the underlying resource.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->resource->{$method}(...$parameters);
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Illuminate\Http\Resources\Json;
class AnonymousResourceCollection extends ResourceCollection
{
/**
* The name of the resource being collected.
*
* @var string
*/
public $collects;
/**
* Create a new anonymous resource collection.
*
* @param mixed $resource
* @param string $collects
* @return void
*/
public function __construct($resource, $collects)
{
$this->collects = $collects;
parent::__construct($resource);
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace Illuminate\Http\Resources\Json;
use Illuminate\Support\Arr;
class PaginatedResourceResponse extends ResourceResponse
{
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function toResponse($request)
{
return tap(response()->json(
$this->wrap(
$this->resource->resolve($request),
array_merge_recursive(
$this->paginationInformation($request),
$this->resource->with($request),
$this->resource->additional
)
),
$this->calculateStatus()
), function ($response) use ($request) {
$this->resource->withResponse($request, $response);
});
}
/**
* Add the pagination information to the response.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function paginationInformation($request)
{
$paginated = $this->resource->resource->toArray();
return [
'links' => $this->paginationLinks($paginated),
'meta' => $this->meta($paginated),
];
}
/**
* Get the pagination links for the response.
*
* @param array $paginated
* @return array
*/
protected function paginationLinks($paginated)
{
return [
'first' => $paginated['first_page_url'] ?? null,
'last' => $paginated['last_page_url'] ?? null,
'prev' => $paginated['prev_page_url'] ?? null,
'next' => $paginated['next_page_url'] ?? null,
];
}
/**
* Gather the meta data for the response.
*
* @param array $paginated
* @return array
*/
protected function meta($paginated)
{
return Arr::except($paginated, [
'data',
'first_page_url',
'last_page_url',
'prev_page_url',
'next_page_url',
]);
}
}

View File

@@ -0,0 +1,206 @@
<?php
namespace Illuminate\Http\Resources\Json;
use ArrayAccess;
use JsonSerializable;
use Illuminate\Support\Collection;
use Illuminate\Container\Container;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Resources\DelegatesToResource;
use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
class Resource implements ArrayAccess, JsonSerializable, Responsable, UrlRoutable
{
use ConditionallyLoadsAttributes, DelegatesToResource;
/**
* The resource instance.
*
* @var mixed
*/
public $resource;
/**
* The additional data that should be added to the top-level resource array.
*
* @var array
*/
public $with = [];
/**
* The additional meta data that should be added to the resource response.
*
* Added during response construction by the developer.
*
* @var array
*/
public $additional = [];
/**
* The "data" wrapper that should be applied.
*
* @var string
*/
public static $wrap = 'data';
/**
* Create a new resource instance.
*
* @param mixed $resource
* @return void
*/
public function __construct($resource)
{
$this->resource = $resource;
}
/**
* Create a new resource instance.
*
* @param dynamic $parameters
* @return static
*/
public static function make(...$parameters)
{
return new static(...$parameters);
}
/**
* Create new anonymous resource collection.
*
* @param mixed $resource
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public static function collection($resource)
{
return new AnonymousResourceCollection($resource, get_called_class());
}
/**
* Resolve the resource to an array.
*
* @param \Illuminate\Http\Request|null $request
* @return array
*/
public function resolve($request = null)
{
$data = $this->toArray(
$request = $request ?: Container::getInstance()->make('request')
);
if (is_array($data)) {
$data = $data;
} elseif ($data instanceof Arrayable || $data instanceof Collection) {
$data = $data->toArray();
} elseif ($data instanceof JsonSerializable) {
$data = $data->jsonSerialize();
}
return $this->filter((array) $data);
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->resource->toArray();
}
/**
* Get any additional data that should be returned with the resource array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function with($request)
{
return $this->with;
}
/**
* Add additional meta data to the resource response.
*
* @param array $data
* @return $this
*/
public function additional(array $data)
{
$this->additional = $data;
return $this;
}
/**
* Customize the response for a request.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\JsonResponse $response
* @return void
*/
public function withResponse($request, $response)
{
//
}
/**
* Set the string that should wrap the outer-most resource array.
*
* @param string $value
* @return void
*/
public static function wrap($value)
{
static::$wrap = $value;
}
/**
* Disable wrapping of the outer-most resource array.
*
* @return void
*/
public static function withoutWrapping()
{
static::$wrap = null;
}
/**
* Transform the resource into an HTTP response.
*
* @param \Illuminate\Http\Request|null $request
* @return \Illuminate\Http\JsonResponse
*/
public function response($request = null)
{
return $this->toResponse(
$request ?: Container::getInstance()->make('request')
);
}
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function toResponse($request)
{
return (new ResourceResponse($this))->toResponse($request);
}
/**
* Prepare the resource for JSON serialization.
*
* @return array
*/
public function jsonSerialize()
{
return $this->resolve(Container::getInstance()->make('request'));
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Illuminate\Http\Resources\Json;
use IteratorAggregate;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Http\Resources\CollectsResources;
class ResourceCollection extends Resource implements IteratorAggregate
{
use CollectsResources;
/**
* The resource that this resource collects.
*
* @var string
*/
public $collects;
/**
* The mapped collection instance.
*
* @var \Illuminate\Support\Collection
*/
public $collection;
/**
* Create a new resource instance.
*
* @param mixed $resource
* @return void
*/
public function __construct($resource)
{
parent::__construct($resource);
$this->resource = $this->collectResource($resource);
}
/**
* Transform the resource into a JSON array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection->map->toArray($request)->all();
}
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function toResponse($request)
{
return $this->resource instanceof AbstractPaginator
? (new PaginatedResourceResponse($this))->toResponse($request)
: parent::toResponse($request);
}
}

View File

@@ -0,0 +1,118 @@
<?php
namespace Illuminate\Http\Resources\Json;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Support\Responsable;
class ResourceResponse implements Responsable
{
/**
* The underlying resource.
*
* @var mixed
*/
public $resource;
/**
* Create a new resource response.
*
* @param mixed $resource
* @return void
*/
public function __construct($resource)
{
$this->resource = $resource;
}
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function toResponse($request)
{
return tap(response()->json(
$this->wrap(
$this->resource->resolve($request),
$this->resource->with($request),
$this->resource->additional
),
$this->calculateStatus()
), function ($response) use ($request) {
$this->resource->withResponse($request, $response);
});
}
/**
* Wrap the given data if necessary.
*
* @param array $data
* @param array $with
* @param array $additional
* @return array
*/
protected function wrap($data, $with = [], $additional = [])
{
if ($data instanceof Collection) {
$data = $data->all();
}
if ($this->haveDefaultWrapperAndDataIsUnwrapped($data)) {
$data = [$this->wrapper() => $data];
} elseif ($this->haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)) {
$data = [($this->wrapper() ?? 'data') => $data];
}
return array_merge_recursive($data, $with, $additional);
}
/**
* Determine if we have a default wrapper and the given data is unwrapped.
*
* @param array $data
* @return bool
*/
protected function haveDefaultWrapperAndDataIsUnwrapped($data)
{
return $this->wrapper() && ! array_key_exists($this->wrapper(), $data);
}
/**
* Determine if "with" data has been added and our data is unwrapped.
*
* @param array $data
* @param array $with
* @param array $additional
* @return bool
*/
protected function haveAdditionalInformationAndDataIsUnwrapped($data, $with, $additional)
{
return (! empty($with) || ! empty($additional)) &&
(! $this->wrapper() ||
! array_key_exists($this->wrapper(), $data));
}
/**
* Get the default data wrapper for the resource.
*
* @return string
*/
protected function wrapper()
{
return get_class($this->resource)::$wrap;
}
/**
* Calculate the appropriate status code for the response.
*
* @return int
*/
protected function calculateStatus()
{
return $this->resource->resource instanceof Model &&
$this->resource->resource->wasRecentlyCreated ? 201 : 200;
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Illuminate\Http\Resources;
use Illuminate\Support\Collection;
class MergeValue
{
/**
* The data to be merged.
*
* @var array
*/
public $data;
/**
* Create new merge value instance.
*
* @param \Illuminate\Support\Collection|array $data
* @return void
*/
public function __construct($data)
{
$this->data = $data instanceof Collection ? $data->all() : $data;
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Illuminate\Http\Resources;
class MissingValue implements PotentiallyMissing
{
/**
* Determine if the object should be considered "missing".
*
* @return bool
*/
public function isMissing()
{
return true;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Illuminate\Http\Resources;
interface PotentiallyMissing
{
/**
* Determine if the object should be considered "missing".
*
* @return bool
*/
public function isMissing();
}

View File

@@ -2,30 +2,19 @@
namespace Illuminate\Http;
use Exception;
use ArrayObject;
use JsonSerializable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Renderable;
use Symfony\Component\HttpFoundation\Response as BaseResponse;
class Response extends BaseResponse
{
use ResponseTrait;
/**
* The original content of the response.
*
* @var mixed
*/
public $original;
/**
* The exception that triggered the error response (if applicable).
*
* @var \Exception
*/
public $exception;
use ResponseTrait, Macroable {
Macroable::__call as macroCall;
}
/**
* Set the content on the response.
@@ -53,7 +42,24 @@ class Response extends BaseResponse
$content = $content->render();
}
return parent::setContent($content);
parent::setContent($content);
return $this;
}
/**
* Determine if the given content should be turned into JSON.
*
* @param mixed $content
* @return bool
*/
protected function shouldBeJson($content)
{
return $content instanceof Arrayable ||
$content instanceof Jsonable ||
$content instanceof ArrayObject ||
$content instanceof JsonSerializable ||
is_array($content);
}
/**
@@ -66,45 +72,10 @@ class Response extends BaseResponse
{
if ($content instanceof Jsonable) {
return $content->toJson();
} elseif ($content instanceof Arrayable) {
return json_encode($content->toArray());
}
return json_encode($content);
}
/**
* Determine if the given content should be turned into JSON.
*
* @param mixed $content
* @return bool
*/
protected function shouldBeJson($content)
{
return $content instanceof Jsonable ||
$content instanceof ArrayObject ||
$content instanceof JsonSerializable ||
is_array($content);
}
/**
* Get the original response content.
*
* @return mixed
*/
public function getOriginalContent()
{
return $this->original;
}
/**
* Set the exception to attach to the response.
*
* @param \Exception $e
* @return $this
*/
public function withException(Exception $e)
{
$this->exception = $e;
return $this;
}
}

View File

@@ -2,10 +2,26 @@
namespace Illuminate\Http;
use Illuminate\Http\Exception\HttpResponseException;
use Exception;
use Symfony\Component\HttpFoundation\HeaderBag;
use Illuminate\Http\Exceptions\HttpResponseException;
trait ResponseTrait
{
/**
* The original content of the response.
*
* @var mixed
*/
public $original;
/**
* The exception that triggered the error response (if applicable).
*
* @var \Exception|null
*/
public $exception;
/**
* Get the status code for the response.
*
@@ -26,17 +42,29 @@ trait ResponseTrait
return $this->getContent();
}
/**
* Get the original response content.
*
* @return mixed
*/
public function getOriginalContent()
{
$original = $this->original;
return $original instanceof self ? $original->{__FUNCTION__}() : $original;
}
/**
* Set a header on the Response.
*
* @param string $key
* @param string $value
* @param array|string $values
* @param bool $replace
* @return $this
*/
public function header($key, $value, $replace = true)
public function header($key, $values, $replace = true)
{
$this->headers->set($key, $value, $replace);
$this->headers->set($key, $values, $replace);
return $this;
}
@@ -44,11 +72,15 @@ trait ResponseTrait
/**
* Add an array of headers to the response.
*
* @param array $headers
* @param \Symfony\Component\HttpFoundation\HeaderBag|array $headers
* @return $this
*/
public function withHeaders(array $headers)
public function withHeaders($headers)
{
if ($headers instanceof HeaderBag) {
$headers = $headers->all();
}
foreach ($headers as $key => $value) {
$this->headers->set($key, $value);
}
@@ -84,10 +116,23 @@ trait ResponseTrait
return $this;
}
/**
* Set the exception to attach to the response.
*
* @param \Exception $e
* @return $this
*/
public function withException(Exception $e)
{
$this->exception = $e;
return $this;
}
/**
* Throws the response in a HttpResponseException instance.
*
* @throws \Illuminate\Http\Exception\HttpResponseException
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
public function throwResponse()
{

View File

@@ -0,0 +1,115 @@
<?php
namespace Illuminate\Http\Testing;
use Illuminate\Http\UploadedFile;
class File extends UploadedFile
{
/**
* The name of the file.
*
* @var string
*/
public $name;
/**
* The temporary file resource.
*
* @var resource
*/
public $tempFile;
/**
* The "size" to report.
*
* @var int
*/
public $sizeToReport;
/**
* Create a new file instance.
*
* @param string $name
* @param resource $tempFile
* @return void
*/
public function __construct($name, $tempFile)
{
$this->name = $name;
$this->tempFile = $tempFile;
parent::__construct(
$this->tempFilePath(), $name, $this->getMimeType(),
filesize($this->tempFilePath()), null, true
);
}
/**
* Create a new fake file.
*
* @param string $name
* @param int $kilobytes
* @return \Illuminate\Http\Testing\File
*/
public static function create($name, $kilobytes = 0)
{
return (new FileFactory)->create($name, $kilobytes);
}
/**
* Create a new fake image.
*
* @param string $name
* @param int $width
* @param int $height
* @return \Illuminate\Http\Testing\File
*/
public static function image($name, $width = 10, $height = 10)
{
return (new FileFactory)->image($name, $width, $height);
}
/**
* Set the "size" of the file in kilobytes.
*
* @param int $kilobytes
* @return $this
*/
public function size($kilobytes)
{
$this->sizeToReport = $kilobytes * 1024;
return $this;
}
/**
* Get the size of the file.
*
* @return int
*/
public function getSize()
{
return $this->sizeToReport ?: parent::getSize();
}
/**
* Get the MIME type for the file.
*
* @return string
*/
public function getMimeType()
{
return MimeType::from($this->name);
}
/**
* Get the path to the temporary file.
*
* @return string
*/
protected function tempFilePath()
{
return stream_get_meta_data($this->tempFile)['uri'];
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Illuminate\Http\Testing;
class FileFactory
{
/**
* Create a new fake file.
*
* @param string $name
* @param int $kilobytes
* @return \Illuminate\Http\Testing\File
*/
public function create($name, $kilobytes = 0)
{
return tap(new File($name, tmpfile()), function ($file) use ($kilobytes) {
$file->sizeToReport = $kilobytes * 1024;
});
}
/**
* Create a new fake image.
*
* @param string $name
* @param int $width
* @param int $height
* @return \Illuminate\Http\Testing\File
*/
public function image($name, $width = 10, $height = 10)
{
return new File($name, $this->generateImage($width, $height));
}
/**
* Generate a dummy image of the given width and height.
*
* @param int $width
* @param int $height
* @return resource
*/
protected function generateImage($width, $height)
{
return tap(tmpfile(), function ($temp) use ($width, $height) {
ob_start();
imagepng(imagecreatetruecolor($width, $height));
fwrite($temp, ob_get_clean());
});
}
}

View File

@@ -0,0 +1,827 @@
<?php
namespace Illuminate\Http\Testing;
class MimeType
{
/**
* An array of extension to MIME types.
*
* @var array
*/
protected static $mimes = [
'ez' => 'application/andrew-inset',
'aw' => 'application/applixware',
'atom' => 'application/atom+xml',
'atomcat' => 'application/atomcat+xml',
'atomsvc' => 'application/atomsvc+xml',
'ccxml' => 'application/ccxml+xml',
'cdmia' => 'application/cdmi-capability',
'cdmic' => 'application/cdmi-container',
'cdmid' => 'application/cdmi-domain',
'cdmio' => 'application/cdmi-object',
'cdmiq' => 'application/cdmi-queue',
'cu' => 'application/cu-seeme',
'davmount' => 'application/davmount+xml',
'dbk' => 'application/docbook+xml',
'dssc' => 'application/dssc+der',
'xdssc' => 'application/dssc+xml',
'ecma' => 'application/ecmascript',
'emma' => 'application/emma+xml',
'epub' => 'application/epub+zip',
'exi' => 'application/exi',
'pfr' => 'application/font-tdpfr',
'gml' => 'application/gml+xml',
'gpx' => 'application/gpx+xml',
'gxf' => 'application/gxf',
'stk' => 'application/hyperstudio',
'ink' => 'application/inkml+xml',
'ipfix' => 'application/ipfix',
'jar' => 'application/java-archive',
'ser' => 'application/java-serialized-object',
'class' => 'application/java-vm',
'js' => 'application/javascript',
'json' => 'application/json',
'jsonml' => 'application/jsonml+json',
'lostxml' => 'application/lost+xml',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'mads' => 'application/mads+xml',
'mrc' => 'application/marc',
'mrcx' => 'application/marcxml+xml',
'ma' => 'application/mathematica',
'mathml' => 'application/mathml+xml',
'mbox' => 'application/mbox',
'mscml' => 'application/mediaservercontrol+xml',
'metalink' => 'application/metalink+xml',
'meta4' => 'application/metalink4+xml',
'mets' => 'application/mets+xml',
'mods' => 'application/mods+xml',
'm21' => 'application/mp21',
'mp4s' => 'application/mp4',
'doc' => 'application/msword',
'mxf' => 'application/mxf',
'bin' => 'application/octet-stream',
'oda' => 'application/oda',
'opf' => 'application/oebps-package+xml',
'ogx' => 'application/ogg',
'omdoc' => 'application/omdoc+xml',
'onetoc' => 'application/onenote',
'oxps' => 'application/oxps',
'xer' => 'application/patch-ops-error+xml',
'pdf' => 'application/pdf',
'pgp' => 'application/pgp-encrypted',
'asc' => 'application/pgp-signature',
'prf' => 'application/pics-rules',
'p10' => 'application/pkcs10',
'p7m' => 'application/pkcs7-mime',
'p7s' => 'application/pkcs7-signature',
'p8' => 'application/pkcs8',
'ac' => 'application/pkix-attr-cert',
'cer' => 'application/pkix-cert',
'crl' => 'application/pkix-crl',
'pkipath' => 'application/pkix-pkipath',
'pki' => 'application/pkixcmp',
'pls' => 'application/pls+xml',
'ai' => 'application/postscript',
'cww' => 'application/prs.cww',
'pskcxml' => 'application/pskc+xml',
'rdf' => 'application/rdf+xml',
'rif' => 'application/reginfo+xml',
'rnc' => 'application/relax-ng-compact-syntax',
'rl' => 'application/resource-lists+xml',
'rld' => 'application/resource-lists-diff+xml',
'rs' => 'application/rls-services+xml',
'gbr' => 'application/rpki-ghostbusters',
'mft' => 'application/rpki-manifest',
'roa' => 'application/rpki-roa',
'rsd' => 'application/rsd+xml',
'rss' => 'application/rss+xml',
'sbml' => 'application/sbml+xml',
'scq' => 'application/scvp-cv-request',
'scs' => 'application/scvp-cv-response',
'spq' => 'application/scvp-vp-request',
'spp' => 'application/scvp-vp-response',
'sdp' => 'application/sdp',
'setpay' => 'application/set-payment-initiation',
'setreg' => 'application/set-registration-initiation',
'shf' => 'application/shf+xml',
'smi' => 'application/smil+xml',
'rq' => 'application/sparql-query',
'srx' => 'application/sparql-results+xml',
'gram' => 'application/srgs',
'grxml' => 'application/srgs+xml',
'sru' => 'application/sru+xml',
'ssdl' => 'application/ssdl+xml',
'ssml' => 'application/ssml+xml',
'tei' => 'application/tei+xml',
'tfi' => 'application/thraud+xml',
'tsd' => 'application/timestamped-data',
'plb' => 'application/vnd.3gpp.pic-bw-large',
'psb' => 'application/vnd.3gpp.pic-bw-small',
'pvb' => 'application/vnd.3gpp.pic-bw-var',
'tcap' => 'application/vnd.3gpp2.tcap',
'pwn' => 'application/vnd.3m.post-it-notes',
'aso' => 'application/vnd.accpac.simply.aso',
'imp' => 'application/vnd.accpac.simply.imp',
'acu' => 'application/vnd.acucobol',
'atc' => 'application/vnd.acucorp',
'air' => 'application/vnd.adobe.air-application-installer-package+zip',
'fcdt' => 'application/vnd.adobe.formscentral.fcdt',
'fxp' => 'application/vnd.adobe.fxp',
'xdp' => 'application/vnd.adobe.xdp+xml',
'xfdf' => 'application/vnd.adobe.xfdf',
'ahead' => 'application/vnd.ahead.space',
'azf' => 'application/vnd.airzip.filesecure.azf',
'azs' => 'application/vnd.airzip.filesecure.azs',
'azw' => 'application/vnd.amazon.ebook',
'acc' => 'application/vnd.americandynamics.acc',
'ami' => 'application/vnd.amiga.ami',
'apk' => 'application/vnd.android.package-archive',
'cii' => 'application/vnd.anser-web-certificate-issue-initiation',
'fti' => 'application/vnd.anser-web-funds-transfer-initiation',
'atx' => 'application/vnd.antix.game-component',
'mpkg' => 'application/vnd.apple.installer+xml',
'm3u8' => 'application/vnd.apple.mpegurl',
'swi' => 'application/vnd.aristanetworks.swi',
'iota' => 'application/vnd.astraea-software.iota',
'aep' => 'application/vnd.audiograph',
'mpm' => 'application/vnd.blueice.multipass',
'bmi' => 'application/vnd.bmi',
'rep' => 'application/vnd.businessobjects',
'cdxml' => 'application/vnd.chemdraw+xml',
'mmd' => 'application/vnd.chipnuts.karaoke-mmd',
'cdy' => 'application/vnd.cinderella',
'cla' => 'application/vnd.claymore',
'rp9' => 'application/vnd.cloanto.rp9',
'c4g' => 'application/vnd.clonk.c4group',
'c11amc' => 'application/vnd.cluetrust.cartomobile-config',
'c11amz' => 'application/vnd.cluetrust.cartomobile-config-pkg',
'csp' => 'application/vnd.commonspace',
'cdbcmsg' => 'application/vnd.contact.cmsg',
'cmc' => 'application/vnd.cosmocaller',
'clkx' => 'application/vnd.crick.clicker',
'clkk' => 'application/vnd.crick.clicker.keyboard',
'clkp' => 'application/vnd.crick.clicker.palette',
'clkt' => 'application/vnd.crick.clicker.template',
'clkw' => 'application/vnd.crick.clicker.wordbank',
'wbs' => 'application/vnd.criticaltools.wbs+xml',
'pml' => 'application/vnd.ctc-posml',
'ppd' => 'application/vnd.cups-ppd',
'car' => 'application/vnd.curl.car',
'pcurl' => 'application/vnd.curl.pcurl',
'dart' => 'application/vnd.dart',
'rdz' => 'application/vnd.data-vision.rdz',
'uvf' => 'application/vnd.dece.data',
'uvt' => 'application/vnd.dece.ttml+xml',
'uvx' => 'application/vnd.dece.unspecified',
'uvz' => 'application/vnd.dece.zip',
'fe_launch' => 'application/vnd.denovo.fcselayout-link',
'dna' => 'application/vnd.dna',
'mlp' => 'application/vnd.dolby.mlp',
'dpg' => 'application/vnd.dpgraph',
'dfac' => 'application/vnd.dreamfactory',
'kpxx' => 'application/vnd.ds-keypoint',
'ait' => 'application/vnd.dvb.ait',
'svc' => 'application/vnd.dvb.service',
'geo' => 'application/vnd.dynageo',
'mag' => 'application/vnd.ecowin.chart',
'nml' => 'application/vnd.enliven',
'esf' => 'application/vnd.epson.esf',
'msf' => 'application/vnd.epson.msf',
'qam' => 'application/vnd.epson.quickanime',
'slt' => 'application/vnd.epson.salt',
'ssf' => 'application/vnd.epson.ssf',
'es3' => 'application/vnd.eszigno3+xml',
'ez2' => 'application/vnd.ezpix-album',
'ez3' => 'application/vnd.ezpix-package',
'fdf' => 'application/vnd.fdf',
'mseed' => 'application/vnd.fdsn.mseed',
'seed' => 'application/vnd.fdsn.seed',
'gph' => 'application/vnd.flographit',
'ftc' => 'application/vnd.fluxtime.clip',
'fm' => 'application/vnd.framemaker',
'fnc' => 'application/vnd.frogans.fnc',
'ltf' => 'application/vnd.frogans.ltf',
'fsc' => 'application/vnd.fsc.weblaunch',
'oas' => 'application/vnd.fujitsu.oasys',
'oa2' => 'application/vnd.fujitsu.oasys2',
'oa3' => 'application/vnd.fujitsu.oasys3',
'fg5' => 'application/vnd.fujitsu.oasysgp',
'bh2' => 'application/vnd.fujitsu.oasysprs',
'ddd' => 'application/vnd.fujixerox.ddd',
'xdw' => 'application/vnd.fujixerox.docuworks',
'xbd' => 'application/vnd.fujixerox.docuworks.binder',
'fzs' => 'application/vnd.fuzzysheet',
'txd' => 'application/vnd.genomatix.tuxedo',
'ggb' => 'application/vnd.geogebra.file',
'ggt' => 'application/vnd.geogebra.tool',
'gex' => 'application/vnd.geometry-explorer',
'gxt' => 'application/vnd.geonext',
'g2w' => 'application/vnd.geoplan',
'g3w' => 'application/vnd.geospace',
'gmx' => 'application/vnd.gmx',
'kml' => 'application/vnd.google-earth.kml+xml',
'kmz' => 'application/vnd.google-earth.kmz',
'gqf' => 'application/vnd.grafeq',
'gac' => 'application/vnd.groove-account',
'ghf' => 'application/vnd.groove-help',
'gim' => 'application/vnd.groove-identity-message',
'grv' => 'application/vnd.groove-injector',
'gtm' => 'application/vnd.groove-tool-message',
'tpl' => 'application/vnd.groove-tool-template',
'vcg' => 'application/vnd.groove-vcard',
'hal' => 'application/vnd.hal+xml',
'zmm' => 'application/vnd.handheld-entertainment+xml',
'hbci' => 'application/vnd.hbci',
'les' => 'application/vnd.hhe.lesson-player',
'hpgl' => 'application/vnd.hp-hpgl',
'hpid' => 'application/vnd.hp-hpid',
'hps' => 'application/vnd.hp-hps',
'jlt' => 'application/vnd.hp-jlyt',
'pcl' => 'application/vnd.hp-pcl',
'pclxl' => 'application/vnd.hp-pclxl',
'sfd-hdstx' => 'application/vnd.hydrostatix.sof-data',
'mpy' => 'application/vnd.ibm.minipay',
'afp' => 'application/vnd.ibm.modcap',
'irm' => 'application/vnd.ibm.rights-management',
'sc' => 'application/vnd.ibm.secure-container',
'icc' => 'application/vnd.iccprofile',
'igl' => 'application/vnd.igloader',
'ivp' => 'application/vnd.immervision-ivp',
'ivu' => 'application/vnd.immervision-ivu',
'igm' => 'application/vnd.insors.igm',
'xpw' => 'application/vnd.intercon.formnet',
'i2g' => 'application/vnd.intergeo',
'qbo' => 'application/vnd.intu.qbo',
'qfx' => 'application/vnd.intu.qfx',
'rcprofile' => 'application/vnd.ipunplugged.rcprofile',
'irp' => 'application/vnd.irepository.package+xml',
'xpr' => 'application/vnd.is-xpr',
'fcs' => 'application/vnd.isac.fcs',
'jam' => 'application/vnd.jam',
'rms' => 'application/vnd.jcp.javame.midlet-rms',
'jisp' => 'application/vnd.jisp',
'joda' => 'application/vnd.joost.joda-archive',
'ktz' => 'application/vnd.kahootz',
'karbon' => 'application/vnd.kde.karbon',
'chrt' => 'application/vnd.kde.kchart',
'kfo' => 'application/vnd.kde.kformula',
'flw' => 'application/vnd.kde.kivio',
'kon' => 'application/vnd.kde.kontour',
'kpr' => 'application/vnd.kde.kpresenter',
'ksp' => 'application/vnd.kde.kspread',
'kwd' => 'application/vnd.kde.kword',
'htke' => 'application/vnd.kenameaapp',
'kia' => 'application/vnd.kidspiration',
'kne' => 'application/vnd.kinar',
'skp' => 'application/vnd.koan',
'sse' => 'application/vnd.kodak-descriptor',
'lasxml' => 'application/vnd.las.las+xml',
'lbd' => 'application/vnd.llamagraphics.life-balance.desktop',
'lbe' => 'application/vnd.llamagraphics.life-balance.exchange+xml',
'123' => 'application/vnd.lotus-1-2-3',
'apr' => 'application/vnd.lotus-approach',
'pre' => 'application/vnd.lotus-freelance',
'nsf' => 'application/vnd.lotus-notes',
'org' => 'application/vnd.lotus-organizer',
'scm' => 'application/vnd.lotus-screencam',
'lwp' => 'application/vnd.lotus-wordpro',
'portpkg' => 'application/vnd.macports.portpkg',
'mcd' => 'application/vnd.mcd',
'mc1' => 'application/vnd.medcalcdata',
'cdkey' => 'application/vnd.mediastation.cdkey',
'mwf' => 'application/vnd.mfer',
'mfm' => 'application/vnd.mfmp',
'flo' => 'application/vnd.micrografx.flo',
'igx' => 'application/vnd.micrografx.igx',
'mif' => 'application/vnd.mif',
'daf' => 'application/vnd.mobius.daf',
'dis' => 'application/vnd.mobius.dis',
'mbk' => 'application/vnd.mobius.mbk',
'mqy' => 'application/vnd.mobius.mqy',
'msl' => 'application/vnd.mobius.msl',
'plc' => 'application/vnd.mobius.plc',
'txf' => 'application/vnd.mobius.txf',
'mpn' => 'application/vnd.mophun.application',
'mpc' => 'application/vnd.mophun.certificate',
'xul' => 'application/vnd.mozilla.xul+xml',
'cil' => 'application/vnd.ms-artgalry',
'cab' => 'application/vnd.ms-cab-compressed',
'xls' => 'application/vnd.ms-excel',
'xlam' => 'application/vnd.ms-excel.addin.macroenabled.12',
'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroenabled.12',
'xlsm' => 'application/vnd.ms-excel.sheet.macroenabled.12',
'xltm' => 'application/vnd.ms-excel.template.macroenabled.12',
'eot' => 'application/vnd.ms-fontobject',
'chm' => 'application/vnd.ms-htmlhelp',
'ims' => 'application/vnd.ms-ims',
'lrm' => 'application/vnd.ms-lrm',
'thmx' => 'application/vnd.ms-officetheme',
'cat' => 'application/vnd.ms-pki.seccat',
'stl' => 'application/vnd.ms-pki.stl',
'ppt' => 'application/vnd.ms-powerpoint',
'ppam' => 'application/vnd.ms-powerpoint.addin.macroenabled.12',
'pptm' => 'application/vnd.ms-powerpoint.presentation.macroenabled.12',
'sldm' => 'application/vnd.ms-powerpoint.slide.macroenabled.12',
'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroenabled.12',
'potm' => 'application/vnd.ms-powerpoint.template.macroenabled.12',
'mpp' => 'application/vnd.ms-project',
'docm' => 'application/vnd.ms-word.document.macroenabled.12',
'dotm' => 'application/vnd.ms-word.template.macroenabled.12',
'wps' => 'application/vnd.ms-works',
'wpl' => 'application/vnd.ms-wpl',
'xps' => 'application/vnd.ms-xpsdocument',
'mseq' => 'application/vnd.mseq',
'mus' => 'application/vnd.musician',
'msty' => 'application/vnd.muvee.style',
'taglet' => 'application/vnd.mynfc',
'nlu' => 'application/vnd.neurolanguage.nlu',
'ntf' => 'application/vnd.nitf',
'nnd' => 'application/vnd.noblenet-directory',
'nns' => 'application/vnd.noblenet-sealer',
'nnw' => 'application/vnd.noblenet-web',
'ngdat' => 'application/vnd.nokia.n-gage.data',
'n-gage' => 'application/vnd.nokia.n-gage.symbian.install',
'rpst' => 'application/vnd.nokia.radio-preset',
'rpss' => 'application/vnd.nokia.radio-presets',
'edm' => 'application/vnd.novadigm.edm',
'edx' => 'application/vnd.novadigm.edx',
'ext' => 'application/vnd.novadigm.ext',
'odc' => 'application/vnd.oasis.opendocument.chart',
'otc' => 'application/vnd.oasis.opendocument.chart-template',
'odb' => 'application/vnd.oasis.opendocument.database',
'odf' => 'application/vnd.oasis.opendocument.formula',
'odft' => 'application/vnd.oasis.opendocument.formula-template',
'odg' => 'application/vnd.oasis.opendocument.graphics',
'otg' => 'application/vnd.oasis.opendocument.graphics-template',
'odi' => 'application/vnd.oasis.opendocument.image',
'oti' => 'application/vnd.oasis.opendocument.image-template',
'odp' => 'application/vnd.oasis.opendocument.presentation',
'otp' => 'application/vnd.oasis.opendocument.presentation-template',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
'odt' => 'application/vnd.oasis.opendocument.text',
'odm' => 'application/vnd.oasis.opendocument.text-master',
'ott' => 'application/vnd.oasis.opendocument.text-template',
'oth' => 'application/vnd.oasis.opendocument.text-web',
'xo' => 'application/vnd.olpc-sugar',
'dd2' => 'application/vnd.oma.dd2+xml',
'oxt' => 'application/vnd.openofficeorg.extension',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
'mgp' => 'application/vnd.osgeo.mapguide.package',
'dp' => 'application/vnd.osgi.dp',
'esa' => 'application/vnd.osgi.subsystem',
'pdb' => 'application/vnd.palm',
'paw' => 'application/vnd.pawaafile',
'str' => 'application/vnd.pg.format',
'ei6' => 'application/vnd.pg.osasli',
'efif' => 'application/vnd.picsel',
'wg' => 'application/vnd.pmi.widget',
'plf' => 'application/vnd.pocketlearn',
'pbd' => 'application/vnd.powerbuilder6',
'box' => 'application/vnd.previewsystems.box',
'mgz' => 'application/vnd.proteus.magazine',
'qps' => 'application/vnd.publishare-delta-tree',
'ptid' => 'application/vnd.pvi.ptid1',
'qxd' => 'application/vnd.quark.quarkxpress',
'bed' => 'application/vnd.realvnc.bed',
'mxl' => 'application/vnd.recordare.musicxml',
'musicxml' => 'application/vnd.recordare.musicxml+xml',
'cryptonote' => 'application/vnd.rig.cryptonote',
'cod' => 'application/vnd.rim.cod',
'rm' => 'application/vnd.rn-realmedia',
'rmvb' => 'application/vnd.rn-realmedia-vbr',
'link66' => 'application/vnd.route66.link66+xml',
'st' => 'application/vnd.sailingtracker.track',
'see' => 'application/vnd.seemail',
'sema' => 'application/vnd.sema',
'semd' => 'application/vnd.semd',
'semf' => 'application/vnd.semf',
'ifm' => 'application/vnd.shana.informed.formdata',
'itp' => 'application/vnd.shana.informed.formtemplate',
'iif' => 'application/vnd.shana.informed.interchange',
'ipk' => 'application/vnd.shana.informed.package',
'twd' => 'application/vnd.simtech-mindmapper',
'mmf' => 'application/vnd.smaf',
'teacher' => 'application/vnd.smart.teacher',
'sdkm' => 'application/vnd.solent.sdkm+xml',
'dxp' => 'application/vnd.spotfire.dxp',
'sfs' => 'application/vnd.spotfire.sfs',
'sdc' => 'application/vnd.stardivision.calc',
'sda' => 'application/vnd.stardivision.draw',
'sdd' => 'application/vnd.stardivision.impress',
'smf' => 'application/vnd.stardivision.math',
'sdw' => 'application/vnd.stardivision.writer',
'sgl' => 'application/vnd.stardivision.writer-global',
'smzip' => 'application/vnd.stepmania.package',
'sm' => 'application/vnd.stepmania.stepchart',
'sxc' => 'application/vnd.sun.xml.calc',
'stc' => 'application/vnd.sun.xml.calc.template',
'sxd' => 'application/vnd.sun.xml.draw',
'std' => 'application/vnd.sun.xml.draw.template',
'sxi' => 'application/vnd.sun.xml.impress',
'sti' => 'application/vnd.sun.xml.impress.template',
'sxm' => 'application/vnd.sun.xml.math',
'sxw' => 'application/vnd.sun.xml.writer',
'sxg' => 'application/vnd.sun.xml.writer.global',
'stw' => 'application/vnd.sun.xml.writer.template',
'sus' => 'application/vnd.sus-calendar',
'svd' => 'application/vnd.svd',
'sis' => 'application/vnd.symbian.install',
'xsm' => 'application/vnd.syncml+xml',
'bdm' => 'application/vnd.syncml.dm+wbxml',
'xdm' => 'application/vnd.syncml.dm+xml',
'tao' => 'application/vnd.tao.intent-module-archive',
'pcap' => 'application/vnd.tcpdump.pcap',
'tmo' => 'application/vnd.tmobile-livetv',
'tpt' => 'application/vnd.trid.tpt',
'mxs' => 'application/vnd.triscape.mxs',
'tra' => 'application/vnd.trueapp',
'ufd' => 'application/vnd.ufdl',
'utz' => 'application/vnd.uiq.theme',
'umj' => 'application/vnd.umajin',
'unityweb' => 'application/vnd.unity',
'uoml' => 'application/vnd.uoml+xml',
'vcx' => 'application/vnd.vcx',
'vsd' => 'application/vnd.visio',
'vis' => 'application/vnd.visionary',
'vsf' => 'application/vnd.vsf',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'wtb' => 'application/vnd.webturbo',
'nbp' => 'application/vnd.wolfram.player',
'wpd' => 'application/vnd.wordperfect',
'wqd' => 'application/vnd.wqd',
'stf' => 'application/vnd.wt.stf',
'xar' => 'application/vnd.xara',
'xfdl' => 'application/vnd.xfdl',
'hvd' => 'application/vnd.yamaha.hv-dic',
'hvs' => 'application/vnd.yamaha.hv-script',
'hvp' => 'application/vnd.yamaha.hv-voice',
'osf' => 'application/vnd.yamaha.openscoreformat',
'osfpvg' => 'application/vnd.yamaha.openscoreformat.osfpvg+xml',
'saf' => 'application/vnd.yamaha.smaf-audio',
'spf' => 'application/vnd.yamaha.smaf-phrase',
'cmp' => 'application/vnd.yellowriver-custom-menu',
'zir' => 'application/vnd.zul',
'zaz' => 'application/vnd.zzazz.deck+xml',
'vxml' => 'application/voicexml+xml',
'wgt' => 'application/widget',
'hlp' => 'application/winhlp',
'wsdl' => 'application/wsdl+xml',
'wspolicy' => 'application/wspolicy+xml',
'7z' => 'application/x-7z-compressed',
'abw' => 'application/x-abiword',
'ace' => 'application/x-ace-compressed',
'dmg' => 'application/x-apple-diskimage',
'aab' => 'application/x-authorware-bin',
'aam' => 'application/x-authorware-map',
'aas' => 'application/x-authorware-seg',
'bcpio' => 'application/x-bcpio',
'torrent' => 'application/x-bittorrent',
'blb' => 'application/x-blorb',
'bz' => 'application/x-bzip',
'bz2' => 'application/x-bzip2',
'cbr' => 'application/x-cbr',
'vcd' => 'application/x-cdlink',
'cfs' => 'application/x-cfs-compressed',
'chat' => 'application/x-chat',
'pgn' => 'application/x-chess-pgn',
'nsc' => 'application/x-conference',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'deb' => 'application/x-debian-package',
'dgc' => 'application/x-dgc-compressed',
'dir' => 'application/x-director',
'wad' => 'application/x-doom',
'ncx' => 'application/x-dtbncx+xml',
'dtb' => 'application/x-dtbook+xml',
'res' => 'application/x-dtbresource+xml',
'dvi' => 'application/x-dvi',
'evy' => 'application/x-envoy',
'eva' => 'application/x-eva',
'bdf' => 'application/x-font-bdf',
'gsf' => 'application/x-font-ghostscript',
'psf' => 'application/x-font-linux-psf',
'otf' => 'application/x-font-otf',
'pcf' => 'application/x-font-pcf',
'snf' => 'application/x-font-snf',
'ttf' => 'application/x-font-ttf',
'pfa' => 'application/x-font-type1',
'woff' => 'application/x-font-woff',
'arc' => 'application/x-freearc',
'spl' => 'application/x-futuresplash',
'gca' => 'application/x-gca-compressed',
'ulx' => 'application/x-glulx',
'gnumeric' => 'application/x-gnumeric',
'gramps' => 'application/x-gramps-xml',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'install' => 'application/x-install-instructions',
'iso' => 'application/x-iso9660-image',
'jnlp' => 'application/x-java-jnlp-file',
'latex' => 'application/x-latex',
'lzh' => 'application/x-lzh-compressed',
'mie' => 'application/x-mie',
'prc' => 'application/x-mobipocket-ebook',
'application' => 'application/x-ms-application',
'lnk' => 'application/x-ms-shortcut',
'wmd' => 'application/x-ms-wmd',
'wmz' => 'application/x-ms-wmz',
'xbap' => 'application/x-ms-xbap',
'mdb' => 'application/x-msaccess',
'obd' => 'application/x-msbinder',
'crd' => 'application/x-mscardfile',
'clp' => 'application/x-msclip',
'exe' => 'application/x-msdownload',
'mvb' => 'application/x-msmediaview',
'wmf' => 'application/x-msmetafile',
'mny' => 'application/x-msmoney',
'pub' => 'application/x-mspublisher',
'scd' => 'application/x-msschedule',
'trm' => 'application/x-msterminal',
'wri' => 'application/x-mswrite',
'nc' => 'application/x-netcdf',
'nzb' => 'application/x-nzb',
'p12' => 'application/x-pkcs12',
'p7b' => 'application/x-pkcs7-certificates',
'p7r' => 'application/x-pkcs7-certreqresp',
'rar' => 'application/x-rar',
'ris' => 'application/x-research-info-systems',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'xap' => 'application/x-silverlight-app',
'sql' => 'application/x-sql',
'sit' => 'application/x-stuffit',
'sitx' => 'application/x-stuffitx',
'srt' => 'application/x-subrip',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
't3' => 'application/x-t3vm-image',
'gam' => 'application/x-tads',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'tfm' => 'application/x-tex-tfm',
'texinfo' => 'application/x-texinfo',
'obj' => 'application/x-tgif',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'der' => 'application/x-x509-ca-cert',
'fig' => 'application/x-xfig',
'xlf' => 'application/x-xliff+xml',
'xpi' => 'application/x-xpinstall',
'xz' => 'application/x-xz',
'z1' => 'application/x-zmachine',
'xaml' => 'application/xaml+xml',
'xdf' => 'application/xcap-diff+xml',
'xenc' => 'application/xenc+xml',
'xhtml' => 'application/xhtml+xml',
'xml' => 'application/xml',
'dtd' => 'application/xml-dtd',
'xop' => 'application/xop+xml',
'xpl' => 'application/xproc+xml',
'xslt' => 'application/xslt+xml',
'xspf' => 'application/xspf+xml',
'mxml' => 'application/xv+xml',
'yang' => 'application/yang',
'yin' => 'application/yin+xml',
'zip' => 'application/zip',
'adp' => 'audio/adpcm',
'au' => 'audio/basic',
'mid' => 'audio/midi',
'mp3' => 'audio/mpeg',
'mp4a' => 'audio/mp4',
'mpga' => 'audio/mpeg',
'oga' => 'audio/ogg',
's3m' => 'audio/s3m',
'sil' => 'audio/silk',
'uva' => 'audio/vnd.dece.audio',
'eol' => 'audio/vnd.digital-winds',
'dra' => 'audio/vnd.dra',
'dts' => 'audio/vnd.dts',
'dtshd' => 'audio/vnd.dts.hd',
'lvp' => 'audio/vnd.lucent.voice',
'pya' => 'audio/vnd.ms-playready.media.pya',
'ecelp4800' => 'audio/vnd.nuera.ecelp4800',
'ecelp7470' => 'audio/vnd.nuera.ecelp7470',
'ecelp9600' => 'audio/vnd.nuera.ecelp9600',
'rip' => 'audio/vnd.rip',
'weba' => 'audio/webm',
'aac' => 'audio/x-aac',
'aif' => 'audio/x-aiff',
'caf' => 'audio/x-caf',
'flac' => 'audio/x-flac',
'mka' => 'audio/x-matroska',
'm3u' => 'audio/x-mpegurl',
'wax' => 'audio/x-ms-wax',
'wma' => 'audio/x-ms-wma',
'ram' => 'audio/x-pn-realaudio',
'rmp' => 'audio/x-pn-realaudio-plugin',
'wav' => 'audio/x-wav',
'xm' => 'audio/xm',
'cdx' => 'chemical/x-cdx',
'cif' => 'chemical/x-cif',
'cmdf' => 'chemical/x-cmdf',
'cml' => 'chemical/x-cml',
'csml' => 'chemical/x-csml',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'cgm' => 'image/cgm',
'g3' => 'image/g3fax',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'ktx' => 'image/ktx',
'png' => 'image/png',
'btif' => 'image/prs.btif',
'sgi' => 'image/sgi',
'svg' => 'image/svg+xml',
'tiff' => 'image/tiff',
'psd' => 'image/vnd.adobe.photoshop',
'uvi' => 'image/vnd.dece.graphic',
'djvu' => 'image/vnd.djvu',
'dwg' => 'image/vnd.dwg',
'dxf' => 'image/vnd.dxf',
'fbs' => 'image/vnd.fastbidsheet',
'fpx' => 'image/vnd.fpx',
'fst' => 'image/vnd.fst',
'mmr' => 'image/vnd.fujixerox.edmics-mmr',
'rlc' => 'image/vnd.fujixerox.edmics-rlc',
'mdi' => 'image/vnd.ms-modi',
'wdp' => 'image/vnd.ms-photo',
'npx' => 'image/vnd.net-fpx',
'wbmp' => 'image/vnd.wap.wbmp',
'xif' => 'image/vnd.xiff',
'webp' => 'image/webp',
'3ds' => 'image/x-3ds',
'ras' => 'image/x-cmu-raster',
'cmx' => 'image/x-cmx',
'fh' => 'image/x-freehand',
'ico' => 'image/x-icon',
'sid' => 'image/x-mrsid-image',
'pcx' => 'image/x-pcx',
'pic' => 'image/x-pict',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'tga' => 'image/x-tga',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'eml' => 'message/rfc822',
'igs' => 'model/iges',
'msh' => 'model/mesh',
'dae' => 'model/vnd.collada+xml',
'dwf' => 'model/vnd.dwf',
'gdl' => 'model/vnd.gdl',
'gtw' => 'model/vnd.gtw',
'mts' => 'model/vnd.mts',
'vtu' => 'model/vnd.vtu',
'wrl' => 'model/vrml',
'x3db' => 'model/x3d+binary',
'x3dv' => 'model/x3d+vrml',
'x3d' => 'model/x3d+xml',
'appcache' => 'text/cache-manifest',
'ics' => 'text/calendar',
'css' => 'text/css',
'csv' => 'text/csv',
'html' => 'text/html',
'n3' => 'text/n3',
'txt' => 'text/plain',
'dsc' => 'text/prs.lines.tag',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
't' => 'text/troff',
'ttl' => 'text/turtle',
'uri' => 'text/uri-list',
'vcard' => 'text/vcard',
'curl' => 'text/vnd.curl',
'dcurl' => 'text/vnd.curl.dcurl',
'scurl' => 'text/vnd.curl.scurl',
'mcurl' => 'text/vnd.curl.mcurl',
'sub' => 'text/vnd.dvb.subtitle',
'fly' => 'text/vnd.fly',
'flx' => 'text/vnd.fmi.flexstor',
'gv' => 'text/vnd.graphviz',
'3dml' => 'text/vnd.in3d.3dml',
'spot' => 'text/vnd.in3d.spot',
'jad' => 'text/vnd.sun.j2me.app-descriptor',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
's' => 'text/x-asm',
'c' => 'text/x-c',
'f' => 'text/x-fortran',
'p' => 'text/x-pascal',
'java' => 'text/x-java-source',
'opml' => 'text/x-opml',
'nfo' => 'text/x-nfo',
'etx' => 'text/x-setext',
'sfv' => 'text/x-sfv',
'uu' => 'text/x-uuencode',
'vcs' => 'text/x-vcalendar',
'vcf' => 'text/x-vcard',
'3gp' => 'video/3gpp',
'3g2' => 'video/3gpp2',
'h261' => 'video/h261',
'h263' => 'video/h263',
'h264' => 'video/h264',
'jpgv' => 'video/jpeg',
'jpm' => 'video/jpm',
'mj2' => 'video/mj2',
'mp4' => 'video/mp4',
'mpeg' => 'video/mpeg',
'ogv' => 'video/ogg',
'mov' => 'video/quicktime',
'qt' => 'video/quicktime',
'uvh' => 'video/vnd.dece.hd',
'uvm' => 'video/vnd.dece.mobile',
'uvp' => 'video/vnd.dece.pd',
'uvs' => 'video/vnd.dece.sd',
'uvv' => 'video/vnd.dece.video',
'dvb' => 'video/vnd.dvb.file',
'fvt' => 'video/vnd.fvt',
'mxu' => 'video/vnd.mpegurl',
'pyv' => 'video/vnd.ms-playready.media.pyv',
'uvu' => 'video/vnd.uvvu.mp4',
'viv' => 'video/vnd.vivo',
'webm' => 'video/webm',
'f4v' => 'video/x-f4v',
'fli' => 'video/x-fli',
'flv' => 'video/x-flv',
'm4v' => 'video/x-m4v',
'mkv' => 'video/x-matroska',
'mng' => 'video/x-mng',
'asf' => 'video/x-ms-asf',
'vob' => 'video/x-ms-vob',
'wm' => 'video/x-ms-wm',
'wmv' => 'video/x-ms-wmv',
'wmx' => 'video/x-ms-wmx',
'wvx' => 'video/x-ms-wvx',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'smv' => 'video/x-smv',
'ice' => 'x-conference/x-cooltalk',
];
/**
* Get the MIME type for a file based on the file's extension.
*
* @param string $filename
* @return string
*/
public static function from($filename)
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);
return self::getMimeTypeFromExtension($extension);
}
/**
* Get the MIME type for a given extension or return all mimes.
*
* @param string $extension
* @return string|array
*/
public static function get($extension = null)
{
return $extension ? self::getMimeTypeFromExtension($extension) : self::$mimes;
}
/**
* Search for the extension of a given MIME type.
*
* @param string $mimeType
* @return string|null
*/
public static function search($mimeType)
{
return array_search($mimeType, self::$mimes) ?: null;
}
/**
* Get the MIME type for a given extension.
*
* @param string $extension
* @return string
*/
protected static function getMimeTypeFromExtension($extension)
{
return self::$mimes[$extension] ?? 'application/octet-stream';
}
}

View File

@@ -2,56 +2,88 @@
namespace Illuminate\Http;
use Illuminate\Support\Arr;
use Illuminate\Container\Container;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
class UploadedFile extends SymfonyUploadedFile
{
use Macroable;
use FileHelpers, Macroable;
/**
* Get the fully qualified path to the file.
* Begin creating a new file fake.
*
* @return string
* @return \Illuminate\Http\Testing\FileFactory
*/
public function path()
public static function fake()
{
return $this->getRealPath();
return new Testing\FileFactory;
}
/**
* Get the file's extension.
*
* @return string
*/
public function extension()
{
return $this->guessExtension();
}
/**
* Get the file's extension supplied by the client.
*
* @return string
*/
public function clientExtension()
{
return $this->guessClientExtension();
}
/**
* Get a filename for the file that is the MD5 hash of the contents.
* Store the uploaded file on a filesystem disk.
*
* @param string $path
* @return string
* @param array|string $options
* @return string|false
*/
public function hashName($path = null)
public function store($path, $options = [])
{
if ($path) {
$path = rtrim($path, '/').'/';
}
return $this->storeAs($path, $this->hashName(), $this->parseOptions($options));
}
return $path.md5_file($this->path()).'.'.$this->extension();
/**
* Store the uploaded file on a filesystem disk with public visibility.
*
* @param string $path
* @param array|string $options
* @return string|false
*/
public function storePublicly($path, $options = [])
{
$options = $this->parseOptions($options);
$options['visibility'] = 'public';
return $this->storeAs($path, $this->hashName(), $options);
}
/**
* Store the uploaded file on a filesystem disk with public visibility.
*
* @param string $path
* @param string $name
* @param array|string $options
* @return string|false
*/
public function storePubliclyAs($path, $name, $options = [])
{
$options = $this->parseOptions($options);
$options['visibility'] = 'public';
return $this->storeAs($path, $name, $options);
}
/**
* Store the uploaded file on a filesystem disk.
*
* @param string $path
* @param string $name
* @param array|string $options
* @return string|false
*/
public function storeAs($path, $name, $options = [])
{
$options = $this->parseOptions($options);
$disk = Arr::pull($options, 'disk');
return Container::getInstance()->make(FilesystemFactory::class)->disk($disk)->putFileAs(
$path, $this, $name, $options
);
}
/**
@@ -72,4 +104,19 @@ class UploadedFile extends SymfonyUploadedFile
$test
);
}
/**
* Parse and format the given options.
*
* @param array|string $options
* @return array
*/
protected function parseOptions($options)
{
if (is_string($options)) {
$options = ['disk' => $options];
}
return $options;
}
}

View File

@@ -2,7 +2,7 @@
"name": "illuminate/http",
"description": "The Illuminate Http package.",
"license": "MIT",
"homepage": "http://laravel.com",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
@@ -14,11 +14,11 @@
}
],
"require": {
"php": ">=5.5.9",
"illuminate/session": "5.2.*",
"illuminate/support": "5.2.*",
"symfony/http-foundation": "2.8.*|3.0.*",
"symfony/http-kernel": "2.8.*|3.0.*"
"php": ">=7.0",
"illuminate/session": "5.5.*",
"illuminate/support": "5.5.*",
"symfony/http-foundation": "~3.3",
"symfony/http-kernel": "~3.3"
},
"autoload": {
"psr-4": {
@@ -27,8 +27,11 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.2-dev"
"dev-master": "5.5-dev"
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}