Changeset View
Changeset View
Standalone View
Standalone View
src/symbols/PhutilClassMapQuery.php
Show First 20 Lines • Show All 220 Lines • ▼ Show 20 Lines | if (!class_exists($ancestor) && !interface_exists($ancestor)) { | ||||
$ancestor)); | $ancestor)); | ||||
} | } | ||||
$expand = $this->expandMethod; | $expand = $this->expandMethod; | ||||
$filter = $this->filterMethod; | $filter = $this->filterMethod; | ||||
$unique = $this->uniqueMethod; | $unique = $this->uniqueMethod; | ||||
$sort = $this->sortMethod; | $sort = $this->sortMethod; | ||||
if (strlen($expand)) { | if ($expand !== null) { | ||||
if (!strlen($unique)) { | if ($unique === null) { | ||||
throw new Exception( | throw new Exception( | ||||
pht( | pht( | ||||
'Trying to execute a class map query for descendants of class '. | 'Trying to execute a class map query for descendants of class '. | ||||
'"%s", but the query specifies an "expand method" ("%s") without '. | '"%s", but the query specifies an "expand method" ("%s") without '. | ||||
'specifying a "unique method". Class maps which support expansion '. | 'specifying a "unique method". Class maps which support expansion '. | ||||
'must have unique keys.', | 'must have unique keys.', | ||||
$ancestor, | $ancestor, | ||||
$expand)); | $expand)); | ||||
} | } | ||||
} | } | ||||
$objects = id(new PhutilSymbolLoader()) | $objects = id(new PhutilSymbolLoader()) | ||||
->setAncestorClass($ancestor) | ->setAncestorClass($ancestor) | ||||
->setContinueOnFailure($this->continueOnFailure) | ->setContinueOnFailure($this->continueOnFailure) | ||||
->loadObjects(); | ->loadObjects(); | ||||
// Apply the "expand" mechanism, if it is configured. | // Apply the "expand" mechanism, if it is configured. | ||||
if (strlen($expand)) { | if ($expand !== null) { | ||||
$list = array(); | $list = array(); | ||||
foreach ($objects as $object) { | foreach ($objects as $object) { | ||||
foreach (call_user_func(array($object, $expand)) as $instance) { | foreach (call_user_func(array($object, $expand)) as $instance) { | ||||
$list[] = $instance; | $list[] = $instance; | ||||
} | } | ||||
} | } | ||||
} else { | } else { | ||||
$list = $objects; | $list = $objects; | ||||
} | } | ||||
// Apply the "unique" mechanism, if it is configured. | // Apply the "unique" mechanism, if it is configured. | ||||
if (strlen($unique)) { | if ($unique !== null) { | ||||
$map = array(); | $map = array(); | ||||
foreach ($list as $object) { | foreach ($list as $object) { | ||||
$key = call_user_func(array($object, $unique)); | $key = call_user_func(array($object, $unique)); | ||||
if ($key === null && $this->filterNull) { | if ($key === null && $this->filterNull) { | ||||
continue; | continue; | ||||
} | } | ||||
Show All 13 Lines | if ($unique !== null) { | ||||
$unique.'()', | $unique.'()', | ||||
$key)); | $key)); | ||||
} | } | ||||
} else { | } else { | ||||
$map = $list; | $map = $list; | ||||
} | } | ||||
// Apply the "filter" mechanism, if it is configured. | // Apply the "filter" mechanism, if it is configured. | ||||
if (strlen($filter)) { | if ($filter !== null) { | ||||
$map = mfilter($map, $filter); | $map = mfilter($map, $filter); | ||||
} | } | ||||
// Apply the "sort" mechanism, if it is configured. | // Apply the "sort" mechanism, if it is configured. | ||||
if (strlen($sort)) { | if ($sort !== null) { | ||||
if ($map) { | if ($map) { | ||||
// The "sort" method may return scalars (which we want to sort with | // The "sort" method may return scalars (which we want to sort with | ||||
// "msort()"), or may return PhutilSortVector objects (which we want | // "msort()"), or may return PhutilSortVector objects (which we want | ||||
// to sort with "msortv()"). | // to sort with "msortv()"). | ||||
$item = call_user_func(array(head($map), $sort)); | $item = call_user_func(array(head($map), $sort)); | ||||
// Since we may be early in the stack, use a string to avoid triggering | // Since we may be early in the stack, use a string to avoid triggering | ||||
// autoload in old versions of PHP. | // autoload in old versions of PHP. | ||||
Show All 35 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0