Carbon github page, I've installed and have this directory structure: `/sites/all/libraries/Carbon/src/Carbon/Carbon.php`

I've tried Xautoload before with Migrate and got Class missing error, so I thought it was a problem with Migrate, but now I tried minimal setup and still can't autoload a php library. Here is the test-case:

File testautoload.info:

name = Test Autoload
core = 7.x

dependencies[] = xautoload
dependencies[] = libraries

version = 7.x-1.x

File testautoload.module:


/**
 * Test Xautoload functionality.
 */

/**
 * Implements hook_libraries_info().
 */
function testautoload_libraries_info() {
  $libraries['Carbon'] = array(
    'name' => 'Carbon',
    'download url' => 'https://github.com/briannesbitt/Carbon',
    'version' => '1.4.0',
    'xautoload' => '_testautoload_xautoload_callback',
  );

  return $libraries;
}

/**
 * Implements hook_menu().
 */
function testautoload_menu() {
  $items['testautoload'] = array(
    'title' => t('Test Xautoload'),
    'page callback' => '_testautoload_page_view',
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Callback function from hook_libraries_info().
 */
function _testautoload_xautoload_callback($api) {
  $api->namespaceRoot('Carbon', 'src/Carbon');
}

/**
 * Callback function from hook_menu().
 */
function _testautoload_page_view() {
  $time = \Carbon\Carbon::createFromTimestampUTC(REQUEST_TIME);
}

Clear cache, navigate to `/testautoload` gives me this:

Fatal error: Class 'Carbon\Carbon' not found in /var/www/club411.fahri/htdocs/sites/all/modules/custom/testautoload/testautoload.module on line 46

I have libraries-7.x-2.1, PHP-5.3.27, Apache-2.2.25, is there other requirements I'm missing? How to debug this?

PS: why I needed Carbon is because of this http://stackoverflow.com/questions/13128854/php-datetime-class-change-fi...

Comments

dozymoe’s picture

Probably related, I think I have apc enabled on apache, here's the content of ext-active/apc.ini:

extension=apc.so
dozymoe’s picture

Status: Active » Closed (works as designed)

Fixed with:

/**
* Callback function from hook_libraries_info().
*/
function _testautoload_xautoload_callback($api) {
  $api->namespaceRoot('Carbon', 'src');
}

I guess this is closed then, I traced the execution till `xautoload_ClassFinder_Helper_Map::findFile_map()`.

dozymoe’s picture

Shouldn't `\Carbon` equal to `src/Carbon`? and `\Carbon\Carbon` equal to Carbon class inside `src/Carbon/Carbon.php`?, it's abit counter intuitive.

donquixote’s picture

This is how PSR-0 works. You always specify the root directory, not any child directory.
There's an $api->namespaceDeep() where you specify the child directory instead.

For Drupal 8 we are discussing to introduce PSR-4 instead of PSR-0 for modules.
X Autoload is going to support this as well in the future.

donquixote’s picture

Issue summary: View changes

Update:
The namespaceRoot() and namespaceDeep() are deprecated in recent versions of xautoload.
Instead, you should use add() (equivalent to addPsr0()), or addPsr4(), or one of the other methods with more descriptive names.
The goal was to be as familiar as possible with the Composer class loader.