Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2656 from fvovan/cm-961_site_model_performance
Browse files Browse the repository at this point in the history
CM-CR-94 FB-961 Improve performance of CM_Site_Model
  • Loading branch information
fvovan authored Jun 30, 2017
2 parents 3d842c5 + ce32bd9 commit 1e7c0ec
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
54 changes: 36 additions & 18 deletions library/CM/Site/SiteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@

class CM_Site_SiteFactory {

/** @var CM_Site_Abstract[] */
/** @var CM_Site_Abstract[]|null */
private $_siteList;

/** @var CM_Site_Abstract[]|null */
private $_siteListSorted = null;

/**
* @param CM_Site_Abstract[]|null $siteList
*/
public function __construct(array $siteList = null) {
if (null === $siteList) {
$siteList = (new CM_Paging_Site_All())->getItems();
}

usort($siteList, function (CM_Site_Abstract $site1, CM_Site_Abstract $site2) {
$length1 = mb_strlen($site1->getUrlString());
$length2 = mb_strlen($site2->getUrlString());
if ($length1 == $length2) {
return 0;
}
return $length1 > $length2 ? -1 : 1;
});

$this->_siteList = $siteList;
}

Expand All @@ -31,7 +21,7 @@ public function __construct(array $siteList = null) {
*/
public function findSite(CM_Http_Request_Abstract $request) {
$request = clone $request;
foreach ($this->_siteList as $site) {
foreach ($this->_getSiteListSorted() as $site) {
if ($site->match($request)) {
return $site;
}
Expand All @@ -58,7 +48,7 @@ public function getSite(CM_Http_Request_Abstract $request) {
*/
public function findSiteById($id) {
$id = (string) $id;
return \Functional\first($this->_siteList, function (CM_Site_Abstract $site) use ($id) {
return \Functional\first($this->_getSiteList(), function (CM_Site_Abstract $site) use ($id) {
return $site->getId() === $id;
});
}
Expand Down Expand Up @@ -87,7 +77,7 @@ public function getSiteById($id) {
*/
public function findSiteByType($type) {
$type = (int) $type;
return \Functional\first($this->_siteList, function (CM_Site_Abstract $site) use ($type) {
return \Functional\first($this->_getSiteList(), function (CM_Site_Abstract $site) use ($type) {
return $site->getType() === $type;
});
}
Expand All @@ -112,7 +102,7 @@ public function getSiteByType($type) {
* @throws CM_Exception_Invalid
*/
public function getDefaultSite() {
$defaultSite = \Functional\first($this->_siteList, function (CM_Site_Abstract $site) {
$defaultSite = \Functional\first($this->_getSiteList(), function (CM_Site_Abstract $site) {
return true === $site->getDefault();
});
if (null === $defaultSite) {
Expand All @@ -121,4 +111,32 @@ public function getDefaultSite() {
return $defaultSite;
}

/**
* @return CM_Site_Abstract[]
*/
protected function _getSiteList() {
if (null === $this->_siteList) {
$this->_siteList = (new CM_Paging_Site_All())->getItems();
}
return $this->_siteList;
}

/**
* @return CM_Site_Abstract[]
*/
protected function _getSiteListSorted() {
if (null === $this->_siteListSorted) {
$siteList = $this->_getSiteList();
usort($siteList, function (CM_Site_Abstract $site1, CM_Site_Abstract $site2) {
$length1 = mb_strlen($site1->getUrlString());
$length2 = mb_strlen($site2->getUrlString());
if ($length1 == $length2) {
return 0;
}
return $length1 > $length2 ? -1 : 1;
});
$this->_siteListSorted = $siteList;
}
return $this->_siteListSorted;
}
}
7 changes: 2 additions & 5 deletions tests/library/CM/Site/SiteFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ public function testSiteListSorted() {
ksort($unsorted);

$siteFactory = new CM_Site_SiteFactory($unsorted);

$reflection = new ReflectionClass($siteFactory);
$property = $reflection->getProperty('_siteList');
$property->setAccessible(true);
$this->assertEquals($sorted, $property->getValue($siteFactory));
$siteListSorted = $this->callProtectedMethod($siteFactory, '_getSiteListSorted');
$this->assertEquals($sorted, $siteListSorted);
}

public function testFindSite() {
Expand Down

0 comments on commit 1e7c0ec

Please sign in to comment.