Index: libraries.api.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/libraries/libraries.api.php,v
retrieving revision 1.1
diff -u -p -r1.1 libraries.api.php
--- libraries.api.php	3 Apr 2010 18:55:10 -0000	1.1
+++ libraries.api.php	12 Apr 2010 22:01:12 -0000
@@ -62,6 +62,8 @@
  *     the same notion as the top-level 'files' property. Each specified file
  *     should contain the full path to the file.
  *   Additional top-level properties can be registered as needed.
+ *
+ * @see hook_library()
  */
 function hook_libraries_info() {
   // The following is a full explanation of all properties. See below for more
@@ -203,9 +205,9 @@ function hook_libraries_info() {
     'download url' => 'http://tinymce.moxiecode.com/download.php',
     'path' => 'jscripts/tiny_mce',
     'version arguments' => array(
-      // It can be easier to parse the first chars of a minified file instead of
-      // doing a multi-line pattern matching in a source file. See 'lines' and
-      // 'cols' below.
+      // It can be easier to parse the first characters of a minified file
+      // instead of doing a multi-line pattern matching in a source file. See
+      // 'lines' and 'cols' below.
       'file' => 'jscripts/tiny_mce/tiny_mce.js',
       // Best practice: Document the actual version strings for later reference.
       // 2.x: this.majorVersion="2";this.minorVersion="1.3"
Index: libraries.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/libraries/libraries.module,v
retrieving revision 1.4
diff -u -p -r1.4 libraries.module
--- libraries.module	3 Apr 2010 18:55:10 -0000	1.4
+++ libraries.module	12 Apr 2010 22:01:12 -0000
@@ -188,7 +188,7 @@ function libraries_info($library = NULL)
  */
 function libraries_detect($libraries) {
   foreach ($libraries as $name => $library) {
-    libraries_detect_library(&$libraries[$name]);
+    libraries_detect_library($libraries[$name]);
   }
   return $libraries;
 }
@@ -250,3 +250,82 @@ function libraries_detect_library(&$libr
   }
 }
 
+/**
+ * Loads a library.
+ *
+ * @param $library
+ *   The name of the library to load.
+ */
+function libraries_load($library) {
+  $library = libraries_info($library);
+  libraries_detect_library($library);
+
+  // Load both the JavaScript and the CSS files.
+  // The parameters for drupal_add_js() and drupal_add_css() require special
+  // handling.
+  foreach (array('js', 'css') as $type) {
+    foreach ($library['files'][$type] as $data => $options) {
+      // If the value is not an array, it's a filename and passed as first
+      // (and only) argument.
+      if (!is_array($options)) {
+        $data = $options;
+        $options = NULL;
+      }
+      // In some cases, the first parameter ($data) is an array. Arrays can't be
+      // passed as keys in PHP, so we have to get $data from the value array.
+      if (is_numeric($data)) {
+        $data = $options['data'];
+        unset($options['data']);
+      }
+      // Apply the default weight if the weight isn't explicitly given.
+      if (!isset($options['weight'])) {
+        $options['weight'] = $weight;
+      }
+      call_user_func('drupal_add_' . $type, $data, $options);
+    }
+  }
+
+
+  // Load PHP files.
+  if ($files = $library['files']['php']) {
+    foreach ($files as $file) {
+      require_once($library['library path'] . $file);
+    }
+  }
+}
+
+/**
+ * Gets the version information from an arbitrary library.
+ *
+ * @param $file
+ *   The filename to parse for the version, relative to the library path. For
+ *   example: 'docs/changelog.txt'.
+ * @param $pattern
+ *   A string containing a regular expression (PCRE) to match the library
+ *   version. For example: '/@version (\d+)\.(\d+)/'.
+ * @param $lines
+ *   The maximum number of lines to search the pattern in. For example: 20.
+ * @param $cols
+ *   (optional) The maximum number of characters per line to take into account.
+ *   For example: 40. Defaults to unlimited. To be used if the file containing
+ *   the library version is minified/compressed, i.e. reading a single line
+ *   would read the entire library into memory.
+ *
+ * @return
+ *   A string containing the version of the library.
+ */
+function libraries_get_version($file, $pattern, $lines, $cols = NULL) {
+  if (!file_exists($file)) {
+    return;
+  }
+  $file = fopen($file, 'r');
+  while ($lines && $line = fgets($file, $cols)) {
+    if (preg_match($pattern, $line, $version)) {
+      fclose($library);
+      return $version[1];
+    }
+    $lines--;
+  }
+  fclose($library);
+}
+
Index: tests/example1.css
===================================================================
RCS file: tests/example1.css
diff -N tests/example1.css
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1.css	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1 @@
+/* $Id$ */
Index: tests/example1.js
===================================================================
RCS file: tests/example1.js
diff -N tests/example1.js
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1.js	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,2 @@
+<?php
+// $Id$
Index: tests/example1.php
===================================================================
RCS file: tests/example1.php
diff -N tests/example1.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1.php	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,6 @@
+<?php
+// $Id$
+
+// Define a dummy function to see if this file was loaded.
+function example1() {
+}
Index: tests/example1_1.css
===================================================================
RCS file: tests/example1_1.css
diff -N tests/example1_1.css
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1_1.css	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1 @@
+/* $Id$ */
Index: tests/example1_1.js
===================================================================
RCS file: tests/example1_1.js
diff -N tests/example1_1.js
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1_1.js	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,2 @@
+<?php
+// $Id$
Index: tests/example1_1.php
===================================================================
RCS file: tests/example1_1.php
diff -N tests/example1_1.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1_1.php	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,6 @@
+<?php
+// $Id$
+
+// Define a dummy function to see if this file was loaded.
+function example1_1() {
+}
Index: tests/example1_2.css
===================================================================
RCS file: tests/example1_2.css
diff -N tests/example1_2.css
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1_2.css	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1 @@
+/* $Id$ */
Index: tests/example1_2.js
===================================================================
RCS file: tests/example1_2.js
diff -N tests/example1_2.js
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1_2.js	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,2 @@
+<?php
+// $Id$
Index: tests/example1_2.php
===================================================================
RCS file: tests/example1_2.php
diff -N tests/example1_2.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example1_2.php	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,6 @@
+<?php
+// $Id$
+
+// Define a dummy function to see if this file was loaded.
+function example1_2() {
+}
Index: tests/example2.txt
===================================================================
RCS file: tests/example2.txt
diff -N tests/example2.txt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/example2.txt	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,6 @@
+; $Id$
+
+Example library 1
+
+Version 1.0
+
Index: tests/libraries.test
===================================================================
RCS file: tests/libraries.test
diff -N tests/libraries.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/libraries.test	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,37 @@
+<?php
+// $Id: libraries.test,v 1.1 2010/04/03 18:55:10 sun Exp $
+
+/**
+ * @file
+ * Unit tests for Libraries API.
+ */
+
+class LibrariesTestCase extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => t('Libraries API tests'),
+      'description' => t('Test the detection and loading of libraries'),
+      'group' => t('Libraries API'),
+    );
+  }
+
+  public function setUp() {
+    parent::setUp('libraries', 'libraries_test');
+  }
+
+  public function testLibrariesDetection() {
+    $this->drupalGet('libraries_test');
+    $this->assertRaw('example1_2.js', t('Make sure that the correct JavaScript file is loaded.'));
+    $this->assertNoRaw('example1.js', t('Make sure that the wrong JavaScript file is not loaded.'));
+    $this->assertNoRaw('example1_1.js', t('Make sure that the wrong JavaScript file is not loaded.'));
+    $this->assertRaw('example1_2.css', t('Make sure that the correct CSS file is loaded.'));
+    $this->assertNoRaw('example1.css', t('Make sure that the wrong CSS file is not loaded.'));
+    $this->assertNoRaw('example1_1.css', t('Make sure that the wrong CSS file is not loaded.'));
+    $this->assertText('example1_2.php', t('Make sure that the correct PHP file is loaded.'));
+    $this->assertText('example1.php', t('Make sure that the wrong PHP file is not loaded.'));
+    $this->assertText('example1_1.php', t('Make sure that the wrong PHP file is not loaded.'));
+  }
+
+}
+
Index: tests/libraries_test.info
===================================================================
RCS file: tests/libraries_test.info
diff -N tests/libraries_test.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/libraries_test.info	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,7 @@
+; $Id$
+name = Libraries test module
+description = Tests library detection and loading
+core = 7.x
+dependencies[] = libraries
+files[] = libraries_test.module
+hidden = TRUE
Index: tests/libraries_test.module
===================================================================
RCS file: tests/libraries_test.module
diff -N tests/libraries_test.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/libraries_test.module	12 Apr 2010 22:01:12 -0000
@@ -0,0 +1,132 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Tests the library detection and loading.
+ */
+
+/**
+ * Implements hook_libraries_info().
+ */
+libraries_test_libraries_info() {
+  // Test all possible options with an existing library.
+  $libraries['example1'] = array(
+    'library path' => drupal_get_path('module', 'libraries') . '/tests',
+    'version callback' => 'libraries_test_get_version',
+    'files' => array(
+      'js' => array(
+        'example1.js',
+      ),
+      'css' => array(
+        'example1.css',
+      ),
+      'php' => array(
+        'example1.php'
+      ),
+    ),
+    'versions' => array(
+      '1' => array(
+        'files' => array(
+          'js' => array(
+            'example1_1.js',
+          ),
+          'css' => array(
+            'example1_1.css',
+          ),
+          'php' => array(
+            'example1_1.php',
+          ),
+        ),
+      ),
+      '2' => array(
+        'files' => array(
+          'js' => array(
+            'example1_2.js',
+          ),
+          'css' => array(
+            'example1_2.css',
+          ),
+          'php' => array(
+            'example1_2.php',
+          ),
+        ),
+      ),
+    ),
+    'integration files' => array(
+      'libraries_test' => array(
+        'js' => array(
+          'libraries_test_1.js',
+        ),
+        'css' => array(
+          'libraries_test_1.css',
+        ),
+        'php' => array(
+          'libraries_test_1.php',
+      ),
+    ),
+  );
+  // Test the default version callback libraries_get_version() as well as the
+  // error handling when the library files do not exist.
+  $libraries['example2'] = array(
+    'library path' => drupal_get_path('module', 'libraries') . '/tests',
+    'version arguments' => array(
+      'file' => 'example2.txt',
+      'pattern' => '/Version (\d+)\.(\d+)/',
+      'lines' => 7,
+    ),
+    'files' => array(
+      'js' => array(
+        'example2.js',
+      ),
+      'css' => array(
+        'example2.css',
+      ),
+      'php' => array(
+        'example2.php',
+      ),
+    ),
+  );
+  return $libraries;
+}
+
+/**
+ * Gets the version of the example library.
+ *
+ * @return
+ *   The string '2'.
+ */
+function libraries_test_get_version() {
+  return '2';
+}
+
+/**
+ * Implements hook_menu().
+ */
+libraries_test_menu() {
+  $items['libraries_test'] = array(
+    'title' => 'Libraries test',
+    'page callback' => 'libraries_test_page',
+    'access callback' => TRUE,
+  );
+  return $items;
+}
+
+/**
+ * Loads the test libraries.
+ */
+function libraries_test_page() {
+  libraries_load('example1');
+  libraries_load('example2');
+  $return = '';
+  if (function_exists('example1')) {
+    $return .= 'example1.php';
+  }
+  if (function_exists('example1_1')) {
+    $return .= 'example1_1.php';
+  }
+  if (function_exists('example1_2')) {
+    $return .= 'example1_2.php';
+  }
+  return $return;
+}
