File: hook.php - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

<?php

defined ('VARIABLE') or die;

/*
** Create a list of required hooks according to current one's dependencies
** $id    : id of current hook in global $gl_hooks array
** $chain : list of already required hooks to avoid circular references
** $result  : output array containing hooks to be included
*/

function  hookRequire ($id, $chain, &$result)
{
  global  $gl_hooks;

  // Browse hooks required by current one
  $hook = &$gl_hooks[$id];

  foreach ($hook['ref'] as $ref => $needed)
  {
    // Check for circular references
    if (isset ($chain[$ref]))
      showError (tra ('error.internal_hook_circular', array ('source' => $id, 'target' => $ref)));

    // Check for required hook availability and dependencies
    if (isset ($gl_hooks[$ref]))
      hookRequire ($ref, $chain + array ($ref => 0), $result);
    else if ($needed)
      showError (tra ('error.internal_hook_needed', array ('source' => $id, 'target' => $ref)));
  }

  // Check if hook has already been registered
  foreach ($result as $i => $item)
  {
    if ($item['id'] == $id)
      return;
  }

  // Insert hook regarding its weights and already included dependencies
  if (is_file ($hook['path']))
  {
    $min = -1;

    foreach ($hook['ref'] as $ref => $null)
      foreach ($result as $i => $item)
        if ($item['id'] == $ref)
          $min = max ($min, $i);

    for ($index = count ($result); $index > $min + 1 && $result[$index - 1]['weight'] > $hook['weight']; )
      --$index;

    array_splice ($result, $index, 0, array (array
    (
      'access'  => $hook['access'],
      'id'    => $id,
      'module'  => $hook['module'],
      'path'    => $hook['path'],
      'weight'  => $hook['weight']
    )));
  }
  else
    showError (tra ('error.internal_hook_file', array ('file' => $hook['file'], 'module' => tra ($hook['module'], array (), false))));
}

?>