Skip to content

Commit

Permalink
[feature/redirect-to-external]:+ redirect hydrator improvement for ex…
Browse files Browse the repository at this point in the history
…ternal urls (#67)

* Redirect hydrator improvement for external urls

Co-authored-by: Willem Poortman <68-willempoortman@users.noreply.gitlab.hyva.io>
  • Loading branch information
wpoortman and Willem Poortman authored Nov 9, 2022
1 parent f9461d3 commit 97cfe8d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/Model/Concern/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ trait Redirect
* Redirect away from the current page.
*
* @param string $path
* @param array $params
* @param array|null $params
* @param bool $secure
* @return RedirectElement
*/
public function redirect(string $path, array $params = []): RedirectElement
public function redirect(string $path, array $params = null, bool $secure = true): RedirectElement
{
return $this->redirect = new RedirectElement($path, $params);
return $this->redirect = new RedirectElement($path, $params, $secure);
}

/**
Expand Down
26 changes: 19 additions & 7 deletions src/Model/Element/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@

class Redirect
{
protected string $url;
protected array $params = [];
private string $url;
private ?array $params;
private bool $secure;

/**
* Redirect constructor.
* @param string $path
* @param array $params
* @param array|null $params
* @param bool $secure
*/
public function __construct(
string $path,
array $params = []
?array $params = null,
bool $secure = true
) {
$this->url = $path;
$this->params = $params;
$this->secure = $secure;
}

/**
Expand All @@ -35,9 +39,9 @@ public function getUrl(): string
}

/**
* @return array
* @return ?array
*/
public function getParams(): array
public function getParams(): ?array
{
return $this->params;
}
Expand All @@ -47,6 +51,14 @@ public function getParams(): array
*/
public function hasParams(): bool
{
return !empty($this->params);
return ! empty($this->params);
}

/**
* @return bool
*/
public function isSecure(): bool
{
return $this->secure;
}
}
53 changes: 46 additions & 7 deletions src/Model/Hydrator/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

namespace Magewirephp\Magewire\Model\Hydrator;

use Laminas\Uri\UriFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Url\HostChecker;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\ScopeInterface;
use Magewirephp\Magewire\Component;
use Magewirephp\Magewire\Model\HydratorInterface;
use Magewirephp\Magewire\Model\RequestInterface;
Expand All @@ -17,15 +21,22 @@
class Redirect implements HydratorInterface
{
protected UrlInterface $builder;
protected HostChecker $hostChecker;
protected ScopeConfigInterface $scopeConfig;

/**
* Redirect constructor.
* @param UrlInterface $builder
* @param HostChecker $hostChecker
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
UrlInterface $builder
UrlInterface $builder,
HostChecker $hostChecker,
ScopeConfigInterface $scopeConfig
) {
$this->builder = $builder;
$this->hostChecker = $hostChecker;
$this->scopeConfig = $scopeConfig;
}

/**
Expand All @@ -43,14 +54,42 @@ public function hydrate(Component $component, RequestInterface $request): void
*/
public function dehydrate(Component $component, ResponseInterface $response): void
{
if ($redirect = $component->getRedirect()) {
$url = $redirect->getUrl();
$redirect = $component->getRedirect();

if ($redirect->hasParams()) {
$url = $this->builder->getUrl($url, $redirect->getParams());
if ($redirect === null) {
return;
}

$url = $redirect->getUrl();

if (strncmp('www.', $url, 4) === 0) {
$url = ($redirect->isSecure() ? 'https://' : 'http://') . str_replace('www.', '', $url);
}

$parse = UriFactory::factory($url);

if ($this->hostChecker->isOwnOrigin($parse->toString())) {
if ($url === '/' && $redirect->hasParams()) {
$url = $this->getDefaultWebUrl();
}

$response->effects['redirect'] = $url;
$url = $url === '/' ? $url : ltrim($url, '\/');

$parse = UriFactory::factory(
$this->builder->getUrl($url, $redirect->hasParams() ? $redirect->getParams() : null)
);
} elseif ($redirect->hasParams() && count($parse->getQueryAsArray()) === 0) {
$parse->setQuery($redirect->getParams());
}

$response->effects['redirect'] = $parse->toString();
}

/**
* @return string
*/
public function getDefaultWebUrl(): string
{
return $this->scopeConfig->getValue('web/default/front', ScopeInterface::SCOPE_STORE);
}
}

0 comments on commit 97cfe8d

Please sign in to comment.