Class auto-loading
Provision has been refactored in 6.x-2.x to use class auto-loading. This simplifies use of classes, as it obviates the need to explicitly include the files in which the class and it's parent classes are defined. In addition, it standardizes the placement of class definitions within a hierarchical file-system structure and will make name-spacing simpler if/when we begin to adopt Symfony components.
While not strictly required in contributed modules, it is highly encouraged. If you maintain a contributed Provision extension, some re-factoring will be required to support class auto-loading.
First off, you'll need to add a registration function that will enable Provision to autoload your extensions classes. Here's an example from provision_civicrm:
/**
* Register our directory as a place to find Provision classes.
*
* This allows Provision to autoload our classes, so that we don't need to
* specifically include the files before we use the class.
*/
function civicrm_provision_register_autoload() {
static $loaded = FALSE;
if (!$loaded) {
$loaded = TRUE;
$list = drush_commandfile_list();
$provision_dir = dirname($list['provision']);
include_once($provision_dir . '/provision.inc');
include_once($provision_dir . '/provision.service.inc');
provision_autoload_register_prefix('Provision_', dirname(__FILE__));
}
}
Next, your extension will need to call your new function in a hook_drush_init(), so that it is run as early as possible during a Drush bootstrap.
/**
* Implements hook_drush_init().
*/
function provision_civicrm_drush_init() {
// Register our service classes for autoloading.
civicrm_provision_register_autoload();
...
Finally, you can move your class definitions into the proper file-system structure. In provision_civicrm, we defined a new (stub) service to allow saving data from the front-end into a site context (entity/alias). So, we created a file at Provision/Service/civicrm.php that included the class definition:
/
* The civicrm service base class.
*/
class Provision_Service_civicrm extends Provision_Service {
public $service = 'civicrm';
/
* Add the civicrm_version property to the site context.
*/
static function subscribe_site($context) {
$context->setProperty('civicrm_version');
}
}
- Print entire section
- Login or register to post comments