Changeset View
Changeset View
Standalone View
Standalone View
src/applications/pholio/query/PholioImageQuery.php
<?php | <?php | ||||
final class PholioImageQuery | final class PholioImageQuery | ||||
extends PhabricatorCursorPagedPolicyAwareQuery { | extends PhabricatorCursorPagedPolicyAwareQuery { | ||||
private $ids; | private $ids; | ||||
private $phids; | private $phids; | ||||
private $mockPHIDs; | private $mockPHIDs; | ||||
private $mocks; | |||||
private $needInlineComments; | private $needInlineComments; | ||||
private $mockCache = array(); | |||||
public function withIDs(array $ids) { | public function withIDs(array $ids) { | ||||
$this->ids = $ids; | $this->ids = $ids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withPHIDs(array $phids) { | public function withPHIDs(array $phids) { | ||||
$this->phids = $phids; | $this->phids = $phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withMocks(array $mocks) { | |||||
epriestley: `withMocks(...)` now has the effect of working like a cache //and// implying `withMockPHIDs(... | |||||
assert_instances_of($mocks, 'PholioMock'); | |||||
$mocks = mpull($mocks, null, 'getPHID'); | |||||
$this->mocks = $mocks; | |||||
$this->mockPHIDs = array_keys($mocks); | |||||
return $this; | |||||
} | |||||
public function withMockPHIDs(array $mock_phids) { | public function withMockPHIDs(array $mock_phids) { | ||||
$this->mockPHIDs = $mock_phids; | $this->mockPHIDs = $mock_phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function needInlineComments($need_inline_comments) { | public function needInlineComments($need_inline_comments) { | ||||
$this->needInlineComments = $need_inline_comments; | $this->needInlineComments = $need_inline_comments; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function setMockCache($mock_cache) { | |||||
$this->mockCache = $mock_cache; | |||||
return $this; | |||||
} | |||||
public function getMockCache() { | |||||
return $this->mockCache; | |||||
} | |||||
public function newResultObject() { | public function newResultObject() { | ||||
return new PholioImage(); | return new PholioImage(); | ||||
} | } | ||||
protected function loadPage() { | protected function loadPage() { | ||||
return $this->loadStandardPage($this->newResultObject()); | return $this->loadStandardPage($this->newResultObject()); | ||||
} | } | ||||
Show All 19 Lines | if ($this->mockPHIDs !== null) { | ||||
$conn, | $conn, | ||||
'mockPHID IN (%Ls)', | 'mockPHID IN (%Ls)', | ||||
$this->mockPHIDs); | $this->mockPHIDs); | ||||
} | } | ||||
return $where; | return $where; | ||||
} | } | ||||
protected function willFilterPage(array $images) { | protected function willFilterPage(array $images) { | ||||
Done Inline ActionsThere's a behavioral change here: we'll now load Images if they have a null value for mockPHID (i.e., an image not yet attached to a mock). Previously, we would always reject them. epriestley: There's a behavioral change here: we'll now load Images if they have a `null` value for… | |||||
assert_instances_of($images, 'PholioImage'); | assert_instances_of($images, 'PholioImage'); | ||||
if ($this->getMockCache()) { | $mock_phids = array(); | ||||
$mocks = $this->getMockCache(); | foreach ($images as $image) { | ||||
} else { | if (!$image->hasMock()) { | ||||
$mock_phids = mpull($images, 'getMockPHID'); | continue; | ||||
} | |||||
$mock_phids[] = $image->getMockPHID(); | |||||
} | |||||
// DO NOT set needImages to true; recursion results! | if ($mock_phids) { | ||||
Done Inline Actions(Not actually that scary: it does some extra fetches, but hits the cache the second time through.) epriestley: (Not actually that scary: it does some extra fetches, but hits the cache the second time… | |||||
if ($this->mocks) { | |||||
$mocks = $this->mocks; | |||||
} else { | |||||
$mocks = id(new PholioMockQuery()) | $mocks = id(new PholioMockQuery()) | ||||
->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
->withPHIDs($mock_phids) | ->withPHIDs($mock_phids) | ||||
->execute(); | ->execute(); | ||||
} | |||||
$mocks = mpull($mocks, null, 'getPHID'); | $mocks = mpull($mocks, null, 'getPHID'); | ||||
foreach ($images as $key => $image) { | |||||
if (!$image->hasMock()) { | |||||
continue; | |||||
} | } | ||||
foreach ($images as $index => $image) { | |||||
$mock = idx($mocks, $image->getMockPHID()); | $mock = idx($mocks, $image->getMockPHID()); | ||||
if ($mock) { | if (!$mock) { | ||||
unset($images[$key]); | |||||
$this->didRejectResult($image); | |||||
continue; | |||||
} | |||||
$image->attachMock($mock); | $image->attachMock($mock); | ||||
} else { | |||||
// mock is missing or we can't see it | |||||
unset($images[$index]); | |||||
} | } | ||||
} | } | ||||
return $images; | return $images; | ||||
} | } | ||||
protected function didFilterPage(array $images) { | protected function didFilterPage(array $images) { | ||||
assert_instances_of($images, 'PholioImage'); | assert_instances_of($images, 'PholioImage'); | ||||
Show All 40 Lines |
withMocks(...) now has the effect of working like a cache and implying withMockPHIDs(...), similar to withRepositories(...) elsewhere.