<?php

/**
 * Implements hook_schema().
 */
function autoload_schema() {
  $schema['autoload_registry'] = array(
    'description' => "Each record is a function, class, or interface name and the file it is in.",
    'fields' => array(
      'name'   => array(
        'description' => 'The name of the function, class, or interface.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'type'   => array(
        'description' => 'Either function or class or interface.',
        'type' => 'varchar',
        'length' => 9,
        'not null' => TRUE,
        'default' => '',
      ),
      'filename'   => array(
        'description' => 'Name of the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'module' => array(
        'description' => 'Name of the module the file belongs to.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'weight' => array(
        'description' => "The order in which this module's hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name.",
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('name', 'type'),
    'indexes' => array(
      'hook' => array('type', 'weight', 'module'),
    ),
  );

  $schema['autoload_registry_file'] = array(
    'description' => "Files parsed to build the registry.",
    'fields' => array(
      'filename'   => array(
        'description' => 'Path to the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'hash'  => array(
        'description' => "sha-256 hash of the file's contents when last parsed.",
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('filename'),
  );

  return $schema;
}

/**
 * Implements hook_install().
 */
function autoload_install() {
  drupal_install_schema('autoload');
}

/**
 * Implements hook_enable().
 */
function autoload_enable() {
  db_query("UPDATE {system} SET weight = -100 WHERE name = 'autoload' AND type = 'module'");

  spl_autoload_register('autoload_class');
  spl_autoload_register('autoload_interface');
}

/**
 * Implements hook_uninstall().
 */
function autoload_uninstall() {
  drupal_uninstall_schema('autoload');

  variable_del('autoload_registry_watchdog_misses');
}

/**
 * Set a low module weight to ensure this module is enabled before those that depend on it.
 */
function autoload_update_6001() {
  db_query("UPDATE {system} SET weight = -100 WHERE name = 'autoload' AND type = 'module'");

  return array();
}

/**
 * Convert to using the backport D7 registry.
 */
function autoload_update_6200() {
    $schema['autoload_registry'] = array(
    'description' => "Each record is a function, class, or interface name and the file it is in.",
    'fields' => array(
      'name'   => array(
        'description' => 'The name of the function, class, or interface.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'type'   => array(
        'description' => 'Either function or class or interface.',
        'type' => 'varchar',
        'length' => 9,
        'not null' => TRUE,
        'default' => '',
      ),
      'filename'   => array(
        'description' => 'Name of the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'module' => array(
        'description' => 'Name of the module the file belongs to.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'weight' => array(
        'description' => "The order in which this module's hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name.",
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('name', 'type'),
    'indexes' => array(
      'hook' => array('type', 'weight', 'module'),
    ),
  );
  $schema['autoload_registry_file'] = array(
    'description' => "Files parsed to build the registry.",
    'fields' => array(
      'filename'   => array(
        'description' => 'Path to the file.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'hash'  => array(
        'description' => "sha-256 hash of the file's contents when last parsed.",
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('filename'),
  );

  $ret = array();
  foreach ($schema as $table => $table_schema) {
    db_create_table($ret, $table, $table_schema);
  }

  return $ret;
}
