-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8735a83
commit 9124e9d
Showing
12 changed files
with
223 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,12 @@ | |
use craft\web\RedirectRule; | ||
|
||
/** | ||
* RedirectRuleEvent class. | ||
* RedirectEvent class. | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.6.0 | ||
*/ | ||
class RedirectRuleEvent extends Event | ||
class RedirectEvent extends Event | ||
{ | ||
public RedirectRule $redirectRule; | ||
public RedirectRule $rule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,44 +7,48 @@ | |
|
||
namespace craft\web; | ||
|
||
use Closure; | ||
use Craft; | ||
use craft\helpers\StringHelper; | ||
use Illuminate\Support\Collection; | ||
use League\Uri\Http; | ||
use yii\base\Component; | ||
|
||
class RedirectRule extends \yii\base\BaseObject | ||
/** | ||
* Class RedirectRule | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.6.0 | ||
*/ | ||
class RedirectRule extends Component | ||
{ | ||
/** | ||
* @event \yii\base\Event The event that is triggered before redirecting the request. | ||
*/ | ||
public const EVENT_BEFORE_REDIRECT = 'beforeRedirect'; | ||
|
||
public string $to; | ||
public string $from; | ||
public int $statusCode = 302; | ||
public bool $caseSensitive = false; | ||
private \Closure $_match; | ||
private Closure $_match; | ||
private array $regexTokens = []; | ||
|
||
public function __invoke(?callable $callback = null): void | ||
public function __invoke(): void | ||
{ | ||
$to = $this->getMatch(); | ||
$url = $this->getMatch(); | ||
|
||
if ($to === null) { | ||
if ($url === null) { | ||
return; | ||
} | ||
|
||
if ($callback) { | ||
$callback($this); | ||
if ($this->hasEventHandlers(self::EVENT_BEFORE_REDIRECT)) { | ||
$this->trigger(self::EVENT_BEFORE_REDIRECT); | ||
} | ||
|
||
Craft::$app->getResponse()->redirect( | ||
$to, | ||
$this->statusCode, | ||
); | ||
Craft::$app->getResponse()->redirect($url, $this->statusCode); | ||
Craft::$app->end(); | ||
} | ||
|
||
public function setMatch(\Closure $match): void | ||
{ | ||
$this->_match = $match; | ||
} | ||
|
||
public function getMatch(): ?string | ||
{ | ||
if (isset($this->_match)) { | ||
|
@@ -72,6 +76,11 @@ public function getMatch(): ?string | |
return strcasecmp($this->from, $subject) === 0 ? $this->to : null; | ||
} | ||
|
||
public function setMatch(callable $match): void | ||
{ | ||
$this->_match = $match; | ||
} | ||
|
||
private function replaceParams(string $value, array $params): string | ||
{ | ||
$params = Collection::make($params) | ||
|
@@ -85,7 +94,7 @@ private function toRegexPattern(string $from): string | |
// Tokenize the patterns first, so we only escape regex chars outside of patterns | ||
$tokenizedPattern = preg_replace_callback('/<([\w._-]+):?([^>]+)?>/', function($match) { | ||
$name = $match[1]; | ||
$pattern = strtr($match[2] ?? '[^\/]+', StringHelper::regexTokens()); | ||
$pattern = strtr($match[2] ?? '[^\/]+', UrlRule::regexTokens()); | ||
$token = "<$name>"; | ||
$this->regexTokens[$token] = "(?P<$name>$pattern)"; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.