Provision now includes a class autoloader, so, as long as you put you classes in the correct location and inform provision of the root folder of your classes, they will be loaded on demand as required.
Instead of putting classes in files that are included manually, first you need to register your path with the autoloader in a *.drush.inc, this example is for example.drush.inc:
/**
* Implements hook_drush_init().
*/
function example_drush_init() {
example_provision_register_autoload();
}
/**
* Register our directory as a place to find provision classes.
*/
function example_provision_register_autoload() {
static $loaded = FALSE;
if (!$loaded) {
$loaded = TRUE;
provision_autoload_register_prefix('Provision_', __DIR__);
}
}
If you provide provision services then you will additionally need to register your directory with the autoloader. So in 6.x-1.x you would have done this:
/**
* Expose the service type this extension defines to provision.
*/
function example_provision_services() {
return array('example' => NULL);
}
But in 6.x-2.x you should do this:
/**
* Expose the service type this extension defines to provision.
*/
function example_provision_services() {
example_provision_register_autoload();
return array('example' => NULL);
}
The above code will allow you to put classes in a 'Provision' subdirectory of your 'example' drush extension. E.g. your file structure would look like:
- example.drush.inc
- Provision
- classes go here
Classes are autoloaded according to the following system:
- Any underscores in the class name are converted to '/'.
- .php is added to the end
- Registered directories are searched one by one for a matching file.
This basically means that if our example provides the 'Provision_Service_example_basic' class, then our file structure should look like:
- example.drush.inc
- Provision
-
Service
-
example
- basic.php
-
example
-
Service
Clearly this means that you should only have one class per file.
All class names related to provision should begin with 'Provision'. If you have classes that start with a different prefix, you may need to register an autoloader prefix for those:
/**
* Register our directory as a place to find OtherPrefix classes.
*/
function example_provision_register_autoload() {
static $loaded = FALSE;
if (!$loaded) {
$loaded = TRUE;
provision_autoload_register_prefix('OtherPrefix_', __DIR__);
}
}