first commit
This commit is contained in:
76
vendor/php-flasher/flasher/Http/RequestExtension.php
vendored
Normal file
76
vendor/php-flasher/flasher/Http/RequestExtension.php
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Prime\Http;
|
||||
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
|
||||
final class RequestExtension
|
||||
{
|
||||
/**
|
||||
* @var FlasherInterface
|
||||
*/
|
||||
private $flasher;
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private $mapping;
|
||||
|
||||
/**
|
||||
* @param array<string, string[]> $mapping
|
||||
*/
|
||||
public function __construct(FlasherInterface $flasher, array $mapping = array())
|
||||
{
|
||||
$this->flasher = $flasher;
|
||||
$this->mapping = $this->flatMapping($mapping);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function flash(RequestInterface $request, ResponseInterface $response)
|
||||
{
|
||||
if (!$request->hasSession()) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
foreach ($this->mapping as $alias => $type) {
|
||||
if (false === $request->hasType($alias)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$messages = (array) $request->getType($alias);
|
||||
|
||||
foreach ($messages as $message) {
|
||||
$this->flasher->addFlash($type, $message);
|
||||
}
|
||||
|
||||
$request->forgetType($alias);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string[]> $mapping
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function flatMapping(array $mapping)
|
||||
{
|
||||
$flatMapping = array();
|
||||
|
||||
foreach ($mapping as $type => $aliases) {
|
||||
foreach ($aliases as $alias) {
|
||||
$flatMapping[$alias] = $type;
|
||||
}
|
||||
}
|
||||
|
||||
return $flatMapping;
|
||||
}
|
||||
}
|
47
vendor/php-flasher/flasher/Http/RequestInterface.php
vendored
Normal file
47
vendor/php-flasher/flasher/Http/RequestInterface.php
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Prime\Http;
|
||||
|
||||
interface RequestInterface
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isXmlHttpRequest();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isHtmlRequestFormat();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSession();
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasType($type);
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return string|string[]
|
||||
*/
|
||||
public function getType($type);
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forgetType($type);
|
||||
}
|
84
vendor/php-flasher/flasher/Http/ResponseExtension.php
vendored
Normal file
84
vendor/php-flasher/flasher/Http/ResponseExtension.php
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Prime\Http;
|
||||
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
use Flasher\Prime\Response\Presenter\HtmlPresenter;
|
||||
|
||||
final class ResponseExtension
|
||||
{
|
||||
/**
|
||||
* @var FlasherInterface
|
||||
*/
|
||||
private $flasher;
|
||||
|
||||
public function __construct(FlasherInterface $flasher)
|
||||
{
|
||||
$this->flasher = $flasher;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function render(RequestInterface $request, ResponseInterface $response)
|
||||
{
|
||||
if (!$this->isRenderable($request, $response)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$content = $response->getContent() ?: '';
|
||||
if (!\is_string($content)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$placeHolders = array(
|
||||
HtmlPresenter::FLASHER_FLASH_BAG_PLACE_HOLDER,
|
||||
HtmlPresenter::HEAD_END_PLACE_HOLDER,
|
||||
HtmlPresenter::BODY_END_PLACE_HOLDER,
|
||||
);
|
||||
|
||||
foreach ($placeHolders as $insertPlaceHolder) {
|
||||
$insertPosition = strripos($content, $insertPlaceHolder);
|
||||
if (false !== $insertPosition) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (false === $insertPosition) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$alreadyRendered = HtmlPresenter::FLASHER_FLASH_BAG_PLACE_HOLDER === $insertPlaceHolder;
|
||||
$htmlResponse = $this->flasher->render(array(), 'html', array('envelopes_only' => $alreadyRendered));
|
||||
|
||||
if (empty($htmlResponse)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$htmlResponse = "\n".str_replace("\n", '', $htmlResponse)."\n";
|
||||
$offset = $alreadyRendered ? strlen(HtmlPresenter::FLASHER_FLASH_BAG_PLACE_HOLDER) : 0;
|
||||
|
||||
$content = substr($content, 0, $insertPosition).$htmlResponse.substr($content, $insertPosition + $offset);
|
||||
$response->setContent($content);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function isRenderable(RequestInterface $request, ResponseInterface $response)
|
||||
{
|
||||
return !$request->isXmlHttpRequest()
|
||||
&& $request->isHtmlRequestFormat()
|
||||
&& !$response->isRedirection()
|
||||
&& $response->isHtml()
|
||||
&& !$response->isAttachment()
|
||||
&& !$response->isJson();
|
||||
}
|
||||
}
|
43
vendor/php-flasher/flasher/Http/ResponseInterface.php
vendored
Normal file
43
vendor/php-flasher/flasher/Http/ResponseInterface.php
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Prime\Http;
|
||||
|
||||
interface ResponseInterface
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRedirection();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isJson();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isHtml();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isAttachment();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContent();
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setContent($content);
|
||||
}
|
Reference in New Issue
Block a user