If you have a "load" callback with some PHP Library, the PHP for the associated library isn't loaded until after the "load" callback is invoked. Having the files load before the callback is invoked allows you to make use of the Libraries PHP files within the callback itself.

Files: 
CommentFileSizeAuthor
#12 1321372_0-commit.patch12.51 KBtstoeckler
#10 1321372.patch11.59 KBRobLoach
PASSED: [[SimpleTest]]: [MySQL] 128 pass(es).
[ View ]
#8 1321372.patch11.58 KBRobLoach
PASSED: [[SimpleTest]]: [MySQL] 128 pass(es).
[ View ]
#3 libraryspreloadpostload.patch2.22 KBRobLoach
FAILED: [[SimpleTest]]: [MySQL] 122 pass(es), 4 fail(s), and 0 exception(es).
[ View ]
loadfilesbeforecallback.patch536 bytesRobLoach
PASSED: [[SimpleTest]]: [MySQL] 126 pass(es).
[ View ]

Comments

Example from Symfony...

<?php
/**
* Implements hook_libraries_info().
*/
function symfony_libraries_info() {
 
$libraries['symfony'] = array(
   
'name' => 'Symfony',
   
'vendor url' => 'http://symfony.com',
   
'download url' => 'http://symfony.com/download',
   
'path' => 'Symfony',
   
'version arguments' => array(
     
'file' => 'Symfony/deps',
     
'pattern' => '@version=v([0-9\.-].[0-9\.-].[0-9\.-])@',
     
'lines' => 4,
     
'cols' => 50,
    ),
   
'files' => array(
     
'php' => array(
       
// When loading the library, load the Class Loader.
       
'vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php',
      ),
    ),
   
'callbacks' = array(
     
'load' => array(
       
'symfony_libraries_callback_load',
      ),
    ),
  );
  return
$libraries;
}
function
symfony_libraries_callback_load($symfony) {
  if (
$symfony['loaded']) {
   
$loader = new \\Symfony\\Component\\ClassLoader\\UniversalClassLoader();
   
$loader->registerNamespace('Symfony', $symfony['library path'] . '/Symfony/vendor/symfony/src');
   
$loader->register();
  }
}
?>

With the patch applied, since the PHP files are loaded before the invoke is made, we can use the ClassLoader.

Category:bug» feature

Hmm... that makes sense. We actually want both, though. We don't do that yet, but a use-case for a 'pre-load' callback is turning filepaths like "jquery-[version]-min.js" into the proper names so they can be loaded. So this is actually a feature request, however a very valid one.

Status:Active» Needs review
StatusFileSize
new2.22 KB
FAILED: [[SimpleTest]]: [MySQL] 122 pass(es), 4 fail(s), and 0 exception(es).
[ View ]

"pre-load" and "post-load" maybe?

Status:Needs review» Needs work

The last submitted patch, libraryspreloadpostload.patch, failed testing.

Status:Needs work» Needs review

Maybe "load" and "loaded"?

Status:Needs review» Needs work

+++ b/libraries.api.php
@@ -275,11 +275,27 @@ function hook_libraries_info() {
+      'info' => array(
+        'mymodule_example_libraries_info_callback',
+      ),

That should probably come first.

+++ b/libraries.module
@@ -596,10 +597,14 @@ function libraries_load($name, $variant = NULL) {
+      // The "loaded" flag is determined by whether all files were loaded.

That's not really correct. 'loaded' currently counts the number of loaded files, so if ($library['loaded']) {}<\code> works, but it's not really a flag. Maybe we can just leave out the comment. :)

This looks really good already. We need to update the hook_libraries_info() documentation, though, and we need some tests for this. The callbacks are already tested in general so we just need to adapt the existing tests.

"pre-load" and "post-load" is seld-documenting and is inline with "pre-detect" and "post-detect", so let's go with that.

Status:Needs work» Needs review
StatusFileSize
new11.58 KB
PASSED: [[SimpleTest]]: [MySQL] 128 pass(es).
[ View ]

Sounds good... I fixed up the tests.

Status:Needs review» Needs work

Thanks very much for the patch. That was exactly what I was looking for. Below are some nitpicks, but they are really minor. And the tests show that this works as expected. I'll commit this in the next few days, and unless you want to roll another patch (which would be even more awesome) I'll fix the stuff below pre-commit.

+++ b/libraries.api.php
@@ -159,10 +159,14 @@
+ *       after this library is loaded. The library now also contains a "loaded"
+ *       variable, which holds how many files were processed during the load
+ *       sequence.

To be more inline with the other groups, that should be something like: "At this point the library contains a 'loaded' key, which contains the number of files that were loaded."

+++ b/libraries.api.php
@@ -275,11 +279,27 @@ function hook_libraries_info() {
+      // Used to alter the info associated with the Library.

And below: Library -> library.

+++ b/tests/libraries.test
@@ -322,11 +327,12 @@ class LibrariesTestCase extends DrupalWebTestCase {
+    $this->assertEqual($library, $expected, 'Pre-Load callback was applied correctly.');

And below: That should be "Pre-load and post-load callbacks ..."

Status:Needs work» Needs review
StatusFileSize
new11.59 KB
PASSED: [[SimpleTest]]: [MySQL] 128 pass(es).
[ View ]

Status:Needs review» Reviewed & tested by the community

Thanks for the reroll. Below are some very minor nitpicks. I'm still marking RTBC, though, because (unless you'd like to re-roll again, which would be awesome, of course), I'd happily fix that prior to commit. If I weren't so terribly tired right now, I'd go ahead and commit it right now. I hope to get to that beginning of next week, though.

+++ b/libraries.api.php
@@ -159,10 +159,13 @@
+ *     - post-load: Callbacks registered in this group are applied directly
+ *       after this library is loaded. At this point, the library contains a
+ *       "loaded" key, which contains the number of files that were loaded.

"loaded" -> 'loaded', also I think the first line wraps early.

+++ b/tests/libraries.test
@@ -370,14 +376,16 @@ class LibrariesTestCase extends DrupalWebTestCase {
+    // When the library information is cached only the pre-load callback group
+    // should be invoked.

"... only the pre-load AND POST-LOAD callback groupS ..."

Status:Reviewed & tested by the community» Fixed
StatusFileSize
new12.51 KB

I did some minor documentation fixes pre-commit, and then committed this. Patch is attached.
Thanks again!
http://drupal.org/commitlog/commit/10030/3db1b3ed803d00a4633096fa43faafb...

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.