Laravel 5.6 updates

Travis config update

Removed HHVM script as Laravel no longer support HHVM after releasing 5.3
This commit is contained in:
Manish Verma
2018-08-06 20:08:55 +05:30
parent 126fbb0255
commit 1ac0f42a58
2464 changed files with 65239 additions and 46734 deletions

View File

@@ -41,11 +41,11 @@ trait InteractsWithContentTypes
*/
public function expectsJson()
{
return ($this->ajax() && ! $this->pjax()) || $this->wantsJson();
return ($this->ajax() && ! $this->pjax() && $this->acceptsAnyContentType()) || $this->wantsJson();
}
/**
* Determine if the current request is asking for JSON in return.
* Determine if the current request is asking for JSON.
*
* @return bool
*/
@@ -118,6 +118,20 @@ trait InteractsWithContentTypes
}
}
/**
* Determine if the current request accepts any content type.
*
* @return bool
*/
public function acceptsAnyContentType()
{
$acceptable = $this->getAcceptableContentTypes();
return count($acceptable) === 0 || (
isset($acceptable[0]) && ($acceptable[0] === '*/*' || $acceptable[0] === '*')
);
}
/**
* Determines whether a request accepts JSON.
*

View File

@@ -94,11 +94,13 @@ trait InteractsWithInput
/**
* Determine if the request contains any of the given inputs.
*
* @param dynamic $key
* @param string|array $keys
* @return bool
*/
public function hasAny(...$keys)
public function hasAny($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
$input = $this->all();
foreach ($keys as $key) {
@@ -129,6 +131,25 @@ trait InteractsWithInput
return true;
}
/**
* Determine if the request contains a non-empty value for any of the given inputs.
*
* @param string|array $keys
* @return bool
*/
public function anyFilled($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
foreach ($keys as $key) {
if ($this->filled($key)) {
return true;
}
}
return false;
}
/**
* Determine if the given input key is an empty string for "has".
*
@@ -178,9 +199,9 @@ trait InteractsWithInput
/**
* Retrieve an input item from the request.
*
* @param string $key
* @param string|null $key
* @param string|array|null $default
* @return string|array
* @return string|array|null
*/
public function input($key = null, $default = null)
{

View File

@@ -0,0 +1,23 @@
<?php
namespace Illuminate\Http\Exceptions;
use Exception;
use Symfony\Component\HttpKernel\Exception\HttpException;
class ThrottleRequestsException extends HttpException
{
/**
* Create a new exception instance.
*
* @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(429, $message, $previous, $headers, $code);
}
}

View File

@@ -86,9 +86,16 @@ class JsonResponse extends BaseJsonResponse
*/
protected function hasValidJson($jsonError)
{
return $jsonError === JSON_ERROR_NONE ||
($jsonError === JSON_ERROR_UNSUPPORTED_TYPE &&
$this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR));
if ($jsonError === JSON_ERROR_NONE) {
return true;
}
return $this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR) &&
in_array($jsonError, [
JSON_ERROR_RECURSION,
JSON_ERROR_INF_OR_NAN,
JSON_ERROR_UNSUPPORTED_TYPE,
]);
}
/**

View File

@@ -0,0 +1,54 @@
<?php
namespace Illuminate\Http\Middleware;
use Closure;
class SetCacheHeaders
{
/**
* Add cache related HTTP headers.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|array $options
* @return \Symfony\Component\HttpFoundation\Response
* @throws \InvalidArgumentException
*/
public function handle($request, Closure $next, $options = [])
{
$response = $next($request);
if (! $request->isMethodCacheable() || ! $response->getContent()) {
return $response;
}
if (is_string($options)) {
$options = $this->parseOptions($options);
}
if (isset($options['etag']) && $options['etag'] === true) {
$options['etag'] = md5($response->getContent());
}
$response->setCache($options);
$response->isNotModified($request);
return $response;
}
/**
* Parse the given header options.
*
* @param string $options
* @return array
*/
protected function parseOptions($options)
{
return collect(explode(';', $options))->mapWithKeys(function ($option) {
$data = explode('=', $option, 2);
return [$data[0] => $data[1] ?? true];
})->all();
}
}

View File

@@ -231,8 +231,8 @@ class RedirectResponse extends BaseRedirectResponse
return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
}
throw new BadMethodCallException(
"Method [$method] does not exist on Redirect."
);
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
}

View File

@@ -309,6 +309,20 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
return $this;
}
/**
* This method belongs to Symfony HttpFoundation and is not usually needed when using Laravel.
*
* Instead, you may use the "input" method.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
return parent::get($key, $default);
}
/**
* Get the JSON payload for the request.
*
@@ -343,6 +357,44 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
return in_array($this->getRealMethod(), ['GET', 'HEAD']) ? $this->query : $this->request;
}
/**
* Create a new request instance from the given Laravel request.
*
* @param \Illuminate\Http\Request $from
* @param \Illuminate\Http\Request|null $to
* @return static
*/
public static function createFrom(self $from, $to = null)
{
$request = $to ?: new static;
$files = $from->files->all();
$files = is_array($files) ? array_filter($files) : $files;
$request->initialize(
$from->query->all(),
$from->request->all(),
$from->attributes->all(),
$from->cookies->all(),
$files,
$from->server->all(),
$from->getContent()
);
$request->setJson($from->json());
if ($session = $from->getSession()) {
$request->setLaravelSession($session);
}
$request->setUserResolver($from->getUserResolver());
$request->setRouteResolver($from->getRouteResolver());
return $request;
}
/**
* Create an Illuminate request from a Symfony instance.
*
@@ -358,12 +410,8 @@ 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;
@@ -419,7 +467,17 @@ class Request extends SymfonyRequest implements Arrayable, ArrayAccess
throw new RuntimeException('Session store not set on request.');
}
return $this->getSession();
return $this->session;
}
/**
* Get the session associated with the request.
*
* @return \Illuminate\Session\Store|null
*/
public function getSession()
{
return $this->session;
}
/**

View File

@@ -16,6 +16,8 @@ trait ConditionallyLoadsAttributes
{
$index = -1;
$numericKeys = array_values($data) === $data;
foreach ($data as $key => $value) {
$index++;
@@ -26,16 +28,7 @@ trait ConditionallyLoadsAttributes
}
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--;
return $this->mergeData($data, $index, $this->filter($value->data), $numericKeys);
}
if ($value instanceof self && is_null($value->resource)) {
@@ -43,7 +36,7 @@ trait ConditionallyLoadsAttributes
}
}
return $data;
return $this->removeMissingValues($data, $numericKeys);
}
/**
@@ -52,20 +45,43 @@ trait ConditionallyLoadsAttributes
* @param array $data
* @param int $index
* @param array $merge
* @param bool $numericKeys
* @return array
*/
protected function merge($data, $index, $merge)
protected function mergeData($data, $index, $merge, $numericKeys)
{
if (array_values($data) === $data) {
return array_merge(
if ($numericKeys) {
return $this->removeMissingValues(array_merge(
array_merge(array_slice($data, 0, $index, true), $merge),
$this->filter(array_slice($data, $index + 1, null, true))
);
$this->filter(array_values(array_slice($data, $index + 1, null, true)))
), $numericKeys);
}
return array_slice($data, 0, $index, true) +
return $this->removeMissingValues(array_slice($data, 0, $index, true) +
$merge +
$this->filter(array_slice($data, $index + 1, null, true));
$this->filter(array_slice($data, $index + 1, null, true)));
}
/**
* Remove the missing values from the filtered data.
*
* @param array $data
* @param bool $numericKeys
* @return array
*/
protected function removeMissingValues($data, $numericKeys = false)
{
foreach ($data as $key => $value) {
if (($value instanceof PotentiallyMissing && $value->isMissing()) ||
($value instanceof self &&
$value->resource instanceof PotentiallyMissing &&
$value->isMissing())) {
unset($data[$key]);
}
}
return ! empty($data) && is_numeric(array_keys($data)[0])
? array_values($data) : $data;
}
/**
@@ -85,12 +101,23 @@ trait ConditionallyLoadsAttributes
return func_num_args() === 3 ? value($default) : new MissingValue;
}
/**
* Merge a value into the array.
*
* @param mixed $value
* @return \Illuminate\Http\Resources\MergeValue|mixed
*/
protected function merge($value)
{
return $this->mergeWhen(true, $value);
}
/**
* Merge a value based on a given condition.
*
* @param bool $condition
* @param mixed $value
* @return \Illuminate\Http\Resources\MissingValue|mixed
* @return \Illuminate\Http\Resources\MergeValue|mixed
*/
protected function mergeWhen($condition, $value)
{
@@ -125,7 +152,7 @@ trait ConditionallyLoadsAttributes
}
if (! $this->resource->relationLoaded($relationship)) {
return $default;
return value($default);
}
if (func_num_args() === 1) {

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 JsonResource 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

@@ -25,6 +25,8 @@ class PaginatedResourceResponse extends ResourceResponse
),
$this->calculateStatus()
), function ($response) use ($request) {
$response->original = $this->resource->resource->pluck('resource');
$this->resource->withResponse($request, $response);
});
}

View File

@@ -2,205 +2,7 @@
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
class Resource extends JsonResource
{
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

@@ -6,7 +6,7 @@ use IteratorAggregate;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Http\Resources\CollectsResources;
class ResourceCollection extends Resource implements IteratorAggregate
class ResourceCollection extends JsonResource implements IteratorAggregate
{
use CollectsResources;

View File

@@ -42,6 +42,8 @@ class ResourceResponse implements Responsable
),
$this->calculateStatus()
), function ($response) use ($request) {
$response->original = $this->resource->resource;
$this->resource->withResponse($request, $response);
});
}

View File

@@ -2,6 +2,8 @@
namespace Illuminate\Http\Testing;
use Illuminate\Support\Str;
class FileFactory
{
/**
@@ -28,7 +30,9 @@ class FileFactory
*/
public function image($name, $width = 10, $height = 10)
{
return new File($name, $this->generateImage($width, $height));
return new File($name, $this->generateImage(
$width, $height, Str::endsWith(Str::lower($name), ['.jpg', '.jpeg']) ? 'jpeg' : 'png'
));
}
/**
@@ -36,14 +40,24 @@ class FileFactory
*
* @param int $width
* @param int $height
* @param string $type
* @return resource
*/
protected function generateImage($width, $height)
protected function generateImage($width, $height, $type)
{
return tap(tmpfile(), function ($temp) use ($width, $height) {
return tap(tmpfile(), function ($temp) use ($width, $height, $type) {
ob_start();
imagepng(imagecreatetruecolor($width, $height));
$image = imagecreatetruecolor($width, $height);
switch ($type) {
case 'jpeg':
imagejpeg($image);
break;
case 'png':
imagepng($image);
break;
}
fwrite($temp, ob_get_clean());
});

View File

@@ -14,11 +14,11 @@
}
],
"require": {
"php": ">=7.0",
"illuminate/session": "5.5.*",
"illuminate/support": "5.5.*",
"symfony/http-foundation": "~3.3",
"symfony/http-kernel": "~3.3"
"php": "^7.1.3",
"illuminate/session": "5.6.*",
"illuminate/support": "5.6.*",
"symfony/http-foundation": "~4.0",
"symfony/http-kernel": "~4.0"
},
"autoload": {
"psr-4": {
@@ -27,7 +27,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.5-dev"
"dev-master": "5.6-dev"
}
},
"config": {