Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use Illuminate\Support\Reflector;
use Illuminate\Support\Util;
use LogicException;
use Psr\Container\ContainerInterface;
use Illuminate\Contracts\Container\Container as ContainerContract;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;

class Container implements ArrayAccess, ContainerInterface {
class Container implements ArrayAccess, ContainerContract {

private static ?Container $instance;

Expand Down Expand Up @@ -699,7 +699,7 @@ public function call($callback, array $parameters = [], $defaultMethod = null)
*
* @return \Closure
*/
public function factory(string $abstract): Closure
public function factory($abstract): Closure
{
return function () use ($abstract) {
return $this->make($abstract);
Expand Down
71 changes: 71 additions & 0 deletions src/Illuminate/Contracts/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Illuminate\Contracts\Routing;

interface ResponseFactory
{
/**
* Create a new response instance.
*
* @param mixed $content
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
public function make($content = '', $status = 200, array $headers = []);

/**
* Create a new response for a given view.
*
* @param string|array $view
* @param array $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
public function view($view, $data = [], $status = 200, array $headers = []);

/**
* Create a new JSON response instance.
*
* @param mixed $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
public function json($data = [], $status = 200, array $headers = [], $options = 0);

/**
* Create a new JSONP response instance.
*
* @param string $callback
* @param mixed $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
public function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0);

/**
* Create a new streamed response instance.
*
* @param \Closure $callback
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function stream($callback, $status = 200, array $headers = []);

/**
* Create a new file download response.
*
* @param \SplFileInfo|string $file
* @param string|null $name
* @param array $headers
* @param string|null $disposition
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function download($file, $name = null, array $headers = [], $disposition = 'attachment');
}
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Capsule/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Manager {
*/
public function __construct(?Container $container = null)
{
$this->setupContainer($container);
$this->setupContainer($container ?: new Container);

// Once we have the container setup, we will setup the default configuration
// options in the container "config" binding. This will make the database
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Capsule/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Manager {
*/
public function __construct(?Container $container = null)
{
$this->setupContainer($container);
$this->setupContainer($container ?: new Container);

// Once we have the container setup, we will setup the default configuration
// options in the container "config" bindings. This just makes this queue
Expand Down
80 changes: 80 additions & 0 deletions src/Illuminate/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php namespace Illuminate\Routing;

use Illuminate\Support\Str;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Routing\ResponseFactory as FactoryContract;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ResponseFactory implements FactoryContract {

use Macroable;

/**
* The container / application instance.
*
* ponytail: hold the container and resolve 'view' lazily (like the old
* static facade) so make()/json() work when 'view' isn't bound. Swap to
* constructor-injected Contracts\View\Factory once View implements it.
*
* @var \ArrayAccess
*/
protected $container;

/**
* Create a new response factory instance.
*
* @param \ArrayAccess $container
* @return void
*/
public function __construct($container)
{
$this->container = $container;
}

public function make($content = '', $status = 200, array $headers = [])
{
return new Response($content, $status, $headers);
}

public function view($view, $data = [], $status = 200, array $headers = [])
{
return $this->make($this->container['view']->make($view, $data), $status, $headers);
}

public function json($data = [], $status = 200, array $headers = [], $options = 0)
{
if ($data instanceof Arrayable)
{
$data = $data->toArray();
}

return new JsonResponse($data, $status, $headers, $options);
}

public function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0)
{
return $this->json($data, $status, $headers, $options)->setCallback($callback);
}

public function stream($callback, $status = 200, array $headers = [])
{
return new StreamedResponse($callback, $status, $headers);
}

public function download($file, $name = null, array $headers = [], $disposition = 'attachment')
{
$response = new BinaryFileResponse($file, 200, $headers, true, $disposition);

if ( ! is_null($name))
{
return $response->setContentDisposition($disposition, $name, str_replace('%', '', Str::ascii($name)));
}

return $response;
}

}
15 changes: 15 additions & 0 deletions src/Illuminate/Routing/RoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function register()
$this->registerUrlGenerator();

$this->registerRedirector();

$this->registerResponseFactory();
}

/**
Expand Down Expand Up @@ -85,4 +87,17 @@ protected function registerRedirector()
});
}

/**
* Register the response factory service.
*
* @return void
*/
protected function registerResponseFactory()
{
$this->app[\Illuminate\Contracts\Routing\ResponseFactory::class] = $this->app->share(function($app)
{
return new ResponseFactory($app);
});
}

}
119 changes: 17 additions & 102 deletions src/Illuminate/Support/Facades/Response.php
Original file line number Diff line number Diff line change
@@ -1,112 +1,27 @@
<?php namespace Illuminate\Support\Facades;

use Illuminate\Support\Str;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Traits\MacroableTrait;
use Illuminate\Http\Response as IlluminateResponse;
use Illuminate\Support\Contracts\ArrayableInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class Response {

use MacroableTrait;

/**
* Return a new response from the application.
*
* @param string $content
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
public static function make($content = '', $status = 200, array $headers = array())
{
return new IlluminateResponse($content, $status, $headers);
}

/**
* Return a new view response from the application.
*
* @param string $view
* @param array $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
public static function view($view, $data = array(), $status = 200, array $headers = array())
{
$app = Facade::getFacadeApplication();

return static::make($app['view']->make($view, $data), $status, $headers);
}

/**
* Return a new JSON response from the application.
*
* @param string|array $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
public static function json($data = array(), $status = 200, array $headers = array(), $options = 0)
{
if ($data instanceof ArrayableInterface)
{
$data = $data->toArray();
}

return new JsonResponse($data, $status, $headers, $options);
}

/**
* Return a new JSONP response from the application.
*
* @param string $callback
* @param string|array $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
public static function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0)
{
return static::json($data, $status, $headers, $options)->setCallback($callback);
}
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;

/**
* @method static \Illuminate\Http\Response make($content = '', $status = 200, array $headers = [])
* @method static \Illuminate\Http\Response view($view, $data = [], $status = 200, array $headers = [])
* @method static \Illuminate\Http\JsonResponse json($data = [], $status = 200, array $headers = [], $options = 0)
* @method static \Illuminate\Http\JsonResponse jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0)
* @method static \Symfony\Component\HttpFoundation\StreamedResponse stream($callback, $status = 200, array $headers = [])
* @method static \Symfony\Component\HttpFoundation\BinaryFileResponse download($file, $name = null, array $headers = [], $disposition = 'attachment')
*
* @see \Illuminate\Routing\ResponseFactory
*/
class Response extends Facade {

/**
* Return a new streamed response from the application.
* Get the registered name of the component.
*
* @param \Closure $callback
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\StreamedResponse
* @return string
*/
public static function stream($callback, $status = 200, array $headers = array())
protected static function getFacadeAccessor()
{
return new StreamedResponse($callback, $status, $headers);
}

/**
* Create a new file download response.
*
* @param \SplFileInfo|string $file
* @param string $name
* @param array $headers
* @param null|string $disposition
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public static function download($file, $name = null, array $headers = array(), $disposition = 'attachment')
{
$response = new BinaryFileResponse($file, 200, $headers, true, $disposition);

if ( ! is_null($name))
{
return $response->setContentDisposition($disposition, $name, str_replace('%', '', Str::ascii($name)));
}

return $response;
return ResponseFactoryContract::class;
}

}
Loading
Loading