Elgg  Version master
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Elgg\Http\ResponseFactory Class Reference

HTTP response service. More...

Public Member Functions

 __construct (protected Request $request, protected AjaxService $ajax, protected EventsService $events)
 Constructor. More...
 
 setHeader (string $name, string $value, bool $replace=true)
 Sets headers to apply to all responses being sent. More...
 
 setCookie (\ElggCookie $cookie)
 Set a cookie, but allow plugins to customize it first. More...
 
 getHeaders (bool $remove_existing=true)
 Get headers set to apply to all responses. More...
 
 prepareResponse (?string $content='', int $status=200, array $headers=[])
 Creates an HTTP response. More...
 
 prepareRedirectResponse (string $url, int $status=302, array $headers=[])
 Creates a redirect response. More...
 
 prepareJsonResponse ($content='', int $status=200, array $headers=[])
 Creates an JSON response. More...
 
 send (SymfonyResponse $response)
 Send a response. More...
 
 getSentResponse ()
 Returns a response that was sent to the client. More...
 
 respond (ResponseBuilder $response)
 Send HTTP response. More...
 
 respondWithError (ResponseBuilder $response)
 Send error HTTP response. More...
 
 respondFromContent (ResponseBuilder $response)
 Send OK response. More...
 
 wrapAjaxResponse ($content='', ?string $forward_url=null)
 Wraps response content in an Ajax2 compatible format. More...
 
 redirect (string $forward_url=REFERRER, $status_code=ELGG_HTTP_FOUND)
 Prepares a redirect response. More...
 
 normalize ($content='')
 Normalizes content into serializable data by walking through arrays and objectifying Elgg entities. More...
 
 stringify ($content='')
 Stringify/serialize response data. More...
 
 setTransport (ResponseTransport $transport)
 Replaces response transport. More...
 

Protected Member Functions

 getResponseType ()
 Returns the response type based on the route for use in events. More...
 
 getSiteRefererUrl ()
 Ensures the referer header is a site url. More...
 
 makeSecureForwardUrl (string $url)
 Ensure the url has a valid protocol for browser use. More...
 
 closeSession ()
 Closes the session. More...
 

Protected Attributes

ResponseTransport $transport
 
ResponseHeaderBag $headers
 
SymfonyResponse $response_sent = null
 

Detailed Description

HTTP response service.

Since
2.3

Definition at line 21 of file ResponseFactory.php.

Constructor & Destructor Documentation

◆ __construct()

Elgg\Http\ResponseFactory::__construct ( protected Request  $request,
protected AjaxService  $ajax,
protected EventsService  $events 
)

Constructor.

Parameters
Request$requestHTTP request
AjaxService$ajaxAJAX service
EventsService$eventsEvents service

Definition at line 38 of file ResponseFactory.php.

Member Function Documentation

◆ closeSession()

Elgg\Http\ResponseFactory::closeSession ( )
protected

Closes the session.

Force closing the session so session is saved to the database before headers are sent preventing race conditions with session data

See also
https://github.com/Elgg/Elgg/issues/12348
Returns
void

Definition at line 575 of file ResponseFactory.php.

◆ getHeaders()

Elgg\Http\ResponseFactory::getHeaders ( bool  $remove_existing = true)

Get headers set to apply to all responses.

Parameters
bool$remove_existingRemove existing headers found in headers_list()
Returns
ResponseHeaderBag

Definition at line 95 of file ResponseFactory.php.

◆ getResponseType()

Elgg\Http\ResponseFactory::getResponseType ( )
protected

Returns the response type based on the route for use in events.

Returns
string

Definition at line 228 of file ResponseFactory.php.

◆ getSentResponse()

Elgg\Http\ResponseFactory::getSentResponse ( )

Returns a response that was sent to the client.

Returns
SymfonyResponse|null

Definition at line 219 of file ResponseFactory.php.

◆ getSiteRefererUrl()

Elgg\Http\ResponseFactory::getSiteRefererUrl ( )
protected

Ensures the referer header is a site url.

Returns
string

Definition at line 545 of file ResponseFactory.php.

◆ makeSecureForwardUrl()

Elgg\Http\ResponseFactory::makeSecureForwardUrl ( string  $url)
protected

Ensure the url has a valid protocol for browser use.

Parameters
string$urlurl the secure
Returns
string

Definition at line 556 of file ResponseFactory.php.

◆ normalize()

Elgg\Http\ResponseFactory::normalize (   $content = '')

Normalizes content into serializable data by walking through arrays and objectifying Elgg entities.

Parameters
mixed$contentData to normalize
Returns
mixed

Definition at line 487 of file ResponseFactory.php.

◆ prepareJsonResponse()

Elgg\Http\ResponseFactory::prepareJsonResponse (   $content = '',
int  $status = 200,
array  $headers = [] 
)

Creates an JSON response.

Parameters
mixed$contentThe response content
integer$statusThe response status code
array$headersAn array of response headers
Returns
JsonResponse

Removing Content-Type header because in some cases content-type headers were already set This is a problem when serving a cachable view (for example a .css) in ajax/view

See also
https://github.com/Elgg/Elgg/issues/9794

Definition at line 159 of file ResponseFactory.php.

◆ prepareRedirectResponse()

Elgg\Http\ResponseFactory::prepareRedirectResponse ( string  $url,
int  $status = 302,
array  $headers = [] 
)

Creates a redirect response.

Parameters
string$urlURL to redirect to
integer$statusThe status code (302 by default)
array$headersAn array of response headers (Location is always set to the given URL)
Returns
SymfonyRedirectResponse

Definition at line 141 of file ResponseFactory.php.

◆ prepareResponse()

Elgg\Http\ResponseFactory::prepareResponse ( ?string  $content = '',
int  $status = 200,
array  $headers = [] 
)

Creates an HTTP response.

Parameters
string$contentThe response content
integer$statusThe response status code
array$headersAn array of response headers
Returns
SymfonyResponse

Definition at line 123 of file ResponseFactory.php.

◆ redirect()

Elgg\Http\ResponseFactory::redirect ( string  $forward_url = REFERRER,
  $status_code = ELGG_HTTP_FOUND 
)

Prepares a redirect response.

Parameters
string$forward_urlRedirection URL
mixed$status_codeHTTP status code or forward reason
Returns
false|SymfonyResponse
Exceptions
UnexpectedValueException

Definition at line 418 of file ResponseFactory.php.

◆ respond()

Elgg\Http\ResponseFactory::respond ( ResponseBuilder  $response)

Send HTTP response.

Parameters
ResponseBuilder$responseResponseBuilder instance An instance of an ErrorResponse, OkResponse or RedirectResponse
Returns
false|SymfonyResponse
Exceptions
UnexpectedValueException

Definition at line 251 of file ResponseFactory.php.

◆ respondFromContent()

Elgg\Http\ResponseFactory::respondFromContent ( ResponseBuilder  $response)

Send OK response.

Parameters
ResponseBuilder$responseResponseBuilder instance An instance of an ErrorResponse, OkResponse or RedirectResponse
Returns
SymfonyResponse|false

Definition at line 377 of file ResponseFactory.php.

◆ respondWithError()

Elgg\Http\ResponseFactory::respondWithError ( ResponseBuilder  $response)

Send error HTTP response.

Parameters
ResponseBuilder$responseResponseBuilder instance An instance of an ErrorResponse, OkResponse or RedirectResponse
Returns
false|SymfonyResponse

Definition at line 324 of file ResponseFactory.php.

◆ send()

Elgg\Http\ResponseFactory::send ( SymfonyResponse  $response)

Send a response.

Parameters
SymfonyResponse$responseResponse object
Returns
SymfonyResponse|false

Definition at line 183 of file ResponseFactory.php.

◆ setCookie()

Elgg\Http\ResponseFactory::setCookie ( \ElggCookie  $cookie)

Set a cookie, but allow plugins to customize it first.

To customize all cookies, register for the 'init:cookie', 'all' event.

Parameters
\ElggCookie$cookieThe cookie that is being set
Returns
bool

Definition at line 69 of file ResponseFactory.php.

◆ setHeader()

Elgg\Http\ResponseFactory::setHeader ( string  $name,
string  $value,
bool  $replace = true 
)

Sets headers to apply to all responses being sent.

Parameters
string$nameHeader name
string$valueHeader value
bool$replaceReplace existing headers
Returns
void

Definition at line 56 of file ResponseFactory.php.

◆ setTransport()

Elgg\Http\ResponseFactory::setTransport ( ResponseTransport  $transport)

Replaces response transport.

Parameters
ResponseTransport$transportTransport interface
Returns
void

Definition at line 536 of file ResponseFactory.php.

◆ stringify()

Elgg\Http\ResponseFactory::stringify (   $content = '')

Stringify/serialize response data.

Casts objects implementing __toString method to strings Serializes non-scalar values to JSON

Parameters
mixed$contentContent to serialize
Returns
string

Definition at line 511 of file ResponseFactory.php.

◆ wrapAjaxResponse()

Elgg\Http\ResponseFactory::wrapAjaxResponse (   $content = '',
?string  $forward_url = null 
)

Wraps response content in an Ajax2 compatible format.

Parameters
string$contentResponse content
null | string$forward_urlForward URL
Returns
string

Definition at line 395 of file ResponseFactory.php.

Member Data Documentation

◆ $headers

ResponseHeaderBag Elgg\Http\ResponseFactory::$headers
protected

Definition at line 27 of file ResponseFactory.php.

◆ $response_sent

SymfonyResponse Elgg\Http\ResponseFactory::$response_sent = null
protected

Definition at line 29 of file ResponseFactory.php.

◆ $transport

ResponseTransport Elgg\Http\ResponseFactory::$transport
protected

Definition at line 25 of file ResponseFactory.php.


The documentation for this class was generated from the following file: