diff --git a/src/DataSource/DataListDataSource.php b/src/DataSource/DataListDataSource.php index c8e5e50..8e23f37 100644 --- a/src/DataSource/DataListDataSource.php +++ b/src/DataSource/DataListDataSource.php @@ -3,6 +3,7 @@ namespace Heyday\Redirects\DataSource; use Heyday\Redirects\CacheableDataSourceInterface; +use Heyday\Redirects\Redirect; use Heyday\Redirects\TransformerInterface; class DataListDataSource implements CacheableDataSourceInterface @@ -24,11 +25,23 @@ public function __construct(\DataList $list, TransformerInterface $transformer) } /** + * In order to improve performance for a larger number of redirects, instead of a standard array being generated + * an associative array (keyed on the 'from' URL) is generated which allows a direct check rather than needing + * to iterate through a large list (especially considering this would be done on each page request). + * * @return \Heyday\Redirects\Redirect[] */ public function get() { - return array_map([$this->transformer, 'transform'], $this->list->toArray()); + $data = array(); + + /* @var \Heyday\Redirects\Redirect $redirect */ + foreach (array_map([$this->transformer, 'transform'], $this->list->toArray()) as $redirect) { + // Format the URL so it will match the SS_HTTPRequest request URL + $data[Redirect::formatUrl($redirect->getFrom())] = $redirect; + } + + return $data; } /** diff --git a/src/Redirector.php b/src/Redirector.php index d84feef..6128dd5 100644 --- a/src/Redirector.php +++ b/src/Redirector.php @@ -21,14 +21,17 @@ public function __construct(DataSourceInterface $dataSource) */ public function getRedirectForRequest(\SS_HTTPRequest $request) { - $url = $request->getURL(); + // Format the URL as the key will have been formatted + $url = Redirect::formatUrl($request->getURL()); + $dataSource = $this->dataSource->get(); - foreach ($this->dataSource->get() as $redirect) { + // Check if there's a key for the URL + if (isset($dataSource[$url])) { + $redirect = $dataSource[$url]; if ($redirect->match($url)) { return $redirect; } } - return false; }