You are here

abstract class crumbs_InvokeAction_findForPath in Crumbs, the Breadcrumbs suite 6.2

Hierarchy

Expanded class hierarchy of crumbs_InvokeAction_findForPath

File

./crumbs.plugin_engine.inc, line 308

View source
abstract class crumbs_InvokeAction_findForPath implements crumbs_InvokeActionInterface_find {

  // injected constructor parameters
  protected $_path;
  protected $_item;
  protected $_method;

  // child class should override this.
  protected $_methods = array();

  // weight, value and key of the candidate with highest priority
  protected $_candidate_key;
  protected $_candidate_value;
  protected $_candidate_weight;
  protected $_log;
  function __construct($path, $item) {
    $this->_path = $path;
    $this->_item = $item;

    // Replace all characters with something that is allowed in method names.
    // while avoiding false positives.
    // Example: 'findParent__node_x()' should only match 'node/%',
    // but not 'node-_' or 'node-x', or other exotic router paths.
    // Special character router paths can not be matched by any method name,
    // so you will need to use switch() or if/else on $item['path'].
    $method_suffix = crumbs_build_method_suffix($item['path']);
    if ($method_suffix !== FALSE) {
      $this->_methods[] = $this->_method . '__' . $method_suffix;
    }
    $this->_methods[] = $this->_method;
  }

  /**
   * This should run once for each plugin object.
   * It should be called by the PluginEngine, during invokeUntilFound().
   */
  function invoke($plugin, $plugin_key, $weight_keeper) {
    $smallest_weight = $weight_keeper
      ->getSmallestWeight();
    if (isset($this->_candidate_weight) && $this->_candidate_weight <= $smallest_weight) {

      // any further candidate would have a higher weight, thus lower priority,
      // than what we already have found. Thus, we can stop searching.
      return TRUE;
    }
    foreach ($this->_methods as $method) {
      if (method_exists($plugin, $method)) {
        $result = $this
          ->_invoke($plugin, $method);
        break;
      }
    }
    if (method_exists($plugin, 'defineMany') || method_exists($plugin, 'define')) {

      // we expect an array result.
      if (is_array($result) && !empty($result)) {
        foreach ($result as $key => $value) {
          $weight = $weight_keeper
            ->findWeight($key);
          $this
            ->_setValue($plugin_key . '.' . $key, $value, $weight);
        }
      }
      else {
        $this->_log[$plugin_key . '.*'] = array(
          NULL,
          NULL,
        );
      }
    }
    else {

      // we expect a simple value as a result
      if (isset($result)) {
        $weight = $weight_keeper
          ->findWeight();
        $this
          ->_setValue($plugin_key, $result, $weight);
      }
      else {
        $this->_log[$plugin_key] = array(
          NULL,
          NULL,
        );
      }
    }
  }

  /**
   * This runs at the end of the InvokeAction's life cycle,
   * and returns the value that was determined.
   */
  function getValue() {
    return $this->_candidate_value;
  }
  function getCandidateKey() {
    return $this->_candidate_key;
  }
  function getLoggedCandidates() {
    return $this->_log;
  }
  protected function _setValue($key, $value, $weight) {
    if ($weight !== FALSE) {
      if (!isset($this->_candidate_weight) || $weight < $this->_candidate_weight) {
        $this->_candidate_weight = $weight;
        $this->_candidate_value = $value;
        $this->_candidate_key = $key;
      }
    }
    $this->_log[$key] = array(
      $value,
      $weight,
    );
  }
  protected function _getMethods($method) {
    return array(
      $method,
      $method . '__' . $this->_method_suffix,
    );
  }
  protected abstract function _invoke($plugin, $method);

}

Members