Changeset View
Changeset View
Standalone View
Standalone View
src/symbols/PhutilSymbolLoader.php
Show First 20 Lines • Show All 168 Lines • ▼ Show 20 Lines | public function selectAndLoadSymbols() { | ||||
if ($this->library) { | if ($this->library) { | ||||
$libraries = array($this->library); | $libraries = array($this->library); | ||||
} else { | } else { | ||||
$libraries = $bootloader->getAllLibraries(); | $libraries = $bootloader->getAllLibraries(); | ||||
} | } | ||||
if ($this->type) { | if ($this->type) { | ||||
$types = array($this->type); | $types = (array)$this->type; | ||||
} else { | } else { | ||||
$types = array( | $types = array( | ||||
'class', | 'class', | ||||
'function', | 'function', | ||||
'interface', | |||||
); | ); | ||||
} | } | ||||
$names = null; | $names = null; | ||||
if ($this->base) { | if ($this->base) { | ||||
$names = $this->selectDescendantsOf( | $names = $this->selectDescendantsOf( | ||||
$bootloader->getClassTree(), | $bootloader->getClassTree(), | ||||
$this->base); | $this->base); | ||||
} | } | ||||
$symbols = array(); | $symbols = array(); | ||||
foreach ($libraries as $library) { | foreach ($libraries as $library) { | ||||
$map = $bootloader->getLibraryMap($library); | $map = $bootloader->getLibraryMap($library); | ||||
foreach ($types as $type) { | foreach ($types as $type) { | ||||
if ($type == 'interface') { | |||||
$lookup_map = $map['class']; | |||||
} else { | |||||
$lookup_map = $map[$type]; | |||||
} | |||||
// As an optimization, we filter the list of candidate symbols in | // As an optimization, we filter the list of candidate symbols in | ||||
// several passes, applying a name-based filter first if possible since | // several passes, applying a name-based filter first if possible since | ||||
// it is highly selective and guaranteed to match at most one symbol. | // it is highly selective and guaranteed to match at most one symbol. | ||||
// This is the common case and we land here through `__autoload()` so | // This is the common case and we land here through `__autoload()` so | ||||
// it's worthwhile to micro-optimize a bit because this code path is | // it's worthwhile to micro-optimize a bit because this code path is | ||||
// very hot and we save 5-10ms per page for a very moderate increase in | // very hot and we save 5-10ms per page for a very moderate increase in | ||||
// complexity. | // complexity. | ||||
if ($this->name) { | if ($this->name) { | ||||
// If we have a name filter, just pick the matching name out if it | // If we have a name filter, just pick the matching name out if it | ||||
// exists. | // exists. | ||||
if (isset($lookup_map[$this->name])) { | if (isset($map[$type][$this->name])) { | ||||
$filtered_map = array( | $filtered_map = array( | ||||
$this->name => $lookup_map[$this->name], | $this->name => $map[$type][$this->name], | ||||
); | ); | ||||
} else { | } else { | ||||
$filtered_map = array(); | $filtered_map = array(); | ||||
} | } | ||||
} else if ($names !== null) { | } else if ($names !== null) { | ||||
$filtered_map = array(); | $filtered_map = array(); | ||||
foreach ($names as $name => $ignored) { | foreach ($names as $name => $ignored) { | ||||
if (isset($lookup_map[$name])) { | if (isset($map[$type][$name])) { | ||||
$filtered_map[$name] = $lookup_map[$name]; | $filtered_map[$name] = $map[$type][$name]; | ||||
} | } | ||||
} | } | ||||
} else { | } else { | ||||
// Otherwise, start with everything. | // Otherwise, start with everything. | ||||
$filtered_map = $lookup_map; | $filtered_map = $map[$type]; | ||||
} | } | ||||
if ($this->pathPrefix) { | if ($this->pathPrefix) { | ||||
$len = strlen($this->pathPrefix); | $len = strlen($this->pathPrefix); | ||||
foreach ($filtered_map as $name => $where) { | foreach ($filtered_map as $name => $where) { | ||||
if (strncmp($where, $this->pathPrefix, $len) !== 0) { | if (strncmp($where, $this->pathPrefix, $len) !== 0) { | ||||
unset($filtered_map[$name]); | unset($filtered_map[$name]); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | /* -( Load )--------------------------------------------------------------- */ | ||||
* classes. | * classes. | ||||
* | * | ||||
* @param list<wild> List of constructor arguments. | * @param list<wild> List of constructor arguments. | ||||
* @return map<string, object> Map of class names to constructed objects. | * @return map<string, object> Map of class names to constructed objects. | ||||
*/ | */ | ||||
public function loadObjects(array $argv = array()) { | public function loadObjects(array $argv = array()) { | ||||
$symbols = $this | $symbols = $this | ||||
->setConcreteOnly(true) | ->setConcreteOnly(true) | ||||
->setType('class') | ->setType(array('class', 'interface')) | ||||
->selectAndLoadSymbols(); | ->selectAndLoadSymbols(); | ||||
$objects = array(); | $objects = array(); | ||||
foreach ($symbols as $symbol) { | foreach ($symbols as $symbol) { | ||||
$objects[$symbol['name']] = newv($symbol['name'], $argv); | $objects[$symbol['name']] = newv($symbol['name'], $argv); | ||||
} | } | ||||
return $objects; | return $objects; | ||||
▲ Show 20 Lines • Show All 83 Lines • Show Last 20 Lines |