You are here

xautoload.api.php in X Autoload 7.4

Same filename and directory in other branches
  1. 7.5 xautoload.api.php
  2. 7.2 xautoload.api.php
  3. 7.3 xautoload.api.php

Hooks provided by X Autoload.

File

xautoload.api.php
View source
<?php

/**
 * @file
 * Hooks provided by X Autoload.
 */

/**
 * Example method showing how to register namespaces from anywhere.
 */
function EXAMPLE_foo() {

  // Register stuff directly to the class finder.
  xautoload()->finder
    ->addPsr4('Aaa\\Bbb\\', 'sites/all/libraries/aaa-bbb/src');

  // Or use an adapter with more powerful methods.
  xautoload()->adapter
    ->composerDir('sites/all/vendor/composer');
}

/**
 * Implements hook_xautoload()
 *
 * Register additional classes, namespaces, autoload patterns, that are not
 * already registered by default.
 *
 * @param \xautoload_InjectedAPI_hookXautoload $api
 *   An adapter object that can register stuff into the class loader.
 */
function hook_xautoload($api) {

  // Register a namespace with PSR-0.
  $api
    ->add('Acme\\GardenKit\\', 'shrubbery/lib');

  // Register a namespace with PSR-4.
  $api
    ->absolute()
    ->addPsr4('Acme\\ShrubGardens\\', '/home/karnouffle/php/shrub-gardens/src');

  // Scan sites/all/vendor/composer for Composer-generated autoload files, e.g.
  // 'sites/all/vendor/composer/autoload_namespaces.php', etc.
  $api
    ->absolute()
    ->composerDir('sites/all/vendor/composer');
}

/**
 * Implements hook_libraries_info()
 *
 * Allows to register PSR-0 (or other) class folders for your libraries.
 * (those things living in sites/all/libraries)
 *
 * The original documentation for this hook is at libraries module,
 * libraries.api.php
 *
 * X Autoload extends the capabilities of this hook, by adding an "xautoload"
 * key. This key takes a callback or closure function, which has the same
 * signature as hook_xautoload($api).
 * This means, you can use the same methods on the $api object.
 *
 * @return array[]
 *   Same as explained in libraries module, but with added key 'xautoload'.
 */
function mymodule_libraries_info() {
  return array(
    'ruebenkraut' => array(
      'name' => 'Rübenkraut library',
      'vendor url' => 'http://www.example.com',
      'download url' => 'http://github.com/example/ruebenkraut',
      'version' => '1.0',
      'xautoload' => function ($api) {

        /**
         * @var \xautoload_InjectedAPI_hookXautoload $api
         *   An adapter object that can register stuff into the class loader.
         */

        // Register a namespace with PSR-0 root in
        // 'sites/all/libraries/ruebenkraut/src'.
        $api
          ->add('Rueben\\Kraut\\', 'src');
      },
    ),
    'gurkentraum' => array(
      'name' => 'Gurkentraum library',
      'xautoload' => function ($api) {

        /** @var \xautoload_InjectedAPI_hookXautoload $api */

        // Scan sites/all/libraries/ruebenkraut/composer.json to look for
        // autoload information.
        $api
          ->composerJson('composer.json');
      },
    ),
  );
}

Functions

Namesort descending Description
EXAMPLE_foo Example method showing how to register namespaces from anywhere.
hook_xautoload Implements hook_xautoload()
mymodule_libraries_info Implements hook_libraries_info()