View source
<?php
class crumbs_PluginSystem_PluginEngine {
protected $pluginBag;
protected $router;
protected $weightMap;
function __construct($pluginBag, $router, $weightMap) {
$this->pluginBag = $pluginBag;
$this->router = $router;
$this->weightMap = $weightMap;
}
function decorateBreadcrumb($breadcrumb) {
$iterator = $this->pluginBag
->getDecorateBreadcrumbPlugins();
foreach ($iterator as $plugin_key => $plugin) {
if (!method_exists($plugin, 'decorateBreadcrumb')) {
continue;
}
$plugin
->decorateBreadcrumb($breadcrumb);
}
}
function findParent($path, $item) {
$iterator = $this->pluginBag
->getRoutePluginMethodIterator('findParent', $item['route']);
$result = $this
->find($iterator, array(
$path,
$item,
), TRUE);
return $result;
}
function findAllParents($path, $item) {
$plugin_methods = $this->pluginBag
->getRoutePluginMethodIterator('findParent', $item['route']);
return $this
->findAll($plugin_methods, array(
$path,
$item,
), TRUE);
}
protected function processFindParent($parent_raw) {
if ($this->router
->urlIsExternal($parent_raw)) {
return NULL;
}
return $this->router
->getNormalPath($parent_raw);
}
function findTitle($path, $item, $breadcrumb) {
$plugin_methods = $this->pluginBag
->getRoutePluginMethodIterator('findTitle', $item['route']);
$result = $this
->find($plugin_methods, array(
$path,
$item,
$breadcrumb,
), FALSE);
return $result;
}
function findAllTitles($path, $item, $breadcrumb) {
$plugin_methods = $this->pluginBag
->getRoutePluginMethodIterator('findTitle', $item['route']);
return $this
->findAll($plugin_methods, array(
$path,
$item,
$breadcrumb,
), FALSE);
}
protected function find($iterator, $args, $processFindParent = FALSE) {
$best_candidate = NULL;
$best_candidate_weight = 999999;
$best_candidate_key = NULL;
foreach ($iterator as $plugin_key => $position) {
if (!$position instanceof crumbs_PluginSystem_PluginMethodIteratorPosition) {
continue;
}
if ($position
->isMultiPlugin()) {
$localWeightMap = $this->weightMap
->localWeightMap($plugin_key);
if ($best_candidate_weight <= $localWeightMap
->smallestValue()) {
return $best_candidate;
}
$candidates = $position
->invokeFinderMethod($args);
if (empty($candidates)) {
continue;
}
foreach ($candidates as $candidate_key => $candidate_raw) {
if (!isset($candidate_raw)) {
continue;
}
$candidate_weight = $localWeightMap
->valueAtKey($candidate_key);
if (FALSE === $candidate_weight) {
continue;
}
if ($best_candidate_weight <= $candidate_weight) {
continue;
}
if ($processFindParent) {
$candidate = $this
->processFindParent($candidate_raw);
if (!isset($candidate)) {
continue;
}
}
else {
$candidate = $candidate_raw;
}
$best_candidate = $candidate;
$best_candidate_weight = $candidate_weight;
}
}
elseif ($position
->isMonoPlugin()) {
$candidate_weight = $this->weightMap
->valueAtKey($plugin_key);
if ($best_candidate_weight <= $candidate_weight) {
return $best_candidate;
}
$candidate_raw = $position
->invokeFinderMethod($args);
if (!isset($candidate_raw)) {
continue;
}
$candidate = $processFindParent ? $this
->processFindParent($candidate_raw) : $candidate_raw;
if (isset($candidate)) {
$best_candidate = $candidate;
$best_candidate_weight = $candidate_weight;
}
}
}
return $best_candidate;
}
protected function findAll($iterator, $args, $processFindParent = FALSE) {
$all_candidates = array();
foreach ($iterator as $plugin_key => $position) {
if ($position
->isMultiPlugin()) {
$candidates = $position
->invokeFinderMethod($args);
if (empty($candidates)) {
continue;
}
foreach ($candidates as $candidate_key => $candidate) {
if (!isset($candidate)) {
continue;
}
if ($processFindParent) {
$candidate = $this
->processFindParent($candidate);
if (!isset($candidate)) {
continue;
}
}
$all_candidates["{$plugin_key}.{$candidate_key}"] = $candidate;
}
}
else {
$candidate = $position
->invokeFinderMethod($args);
if (!isset($candidate)) {
continue;
}
if ($processFindParent) {
$candidate = $this
->processFindParent($candidate);
if (!isset($candidate)) {
continue;
}
}
$all_candidates[$plugin_key] = $candidate;
}
}
return $all_candidates;
}
}