Problem

There are 3 things that bother me with our current API.

1. libraries_detect_library being called by reference on the libraries array.
This forces you to do:

$library = libraries_info('foo');
libraries_detect_library($library);

instead of simply:

$library = libraries_detect_library(libraries_info('foo'));

Note that you can currently do:

$library = libraries_info('foo');
if (libraries_detect_library($library) && $library['installed']) {

which would then become:

if ($library = libraries_detect_library(libraries_info('foo')) && $library['installed']) {

2. libraries_detect_library() has the _library suffix, while libraries_info() and libraries_load do not.

3. libraries_detect_library() gets the entire the library array passed, while libraries_info() and libraries_load() simply take the name.

Background

The weirdness of libraries_detect_library was introduced because it was basically a helper of libraries_detect().

Proposal

1. Rip out the current libraries_detect()
This function is practically completely useless at least for implementing modules. No module would ever want to detect all libraries. It might make sense to detect all libraries for instance when visiting the libraries overview UI page (once there is such a thing), but that is theoretical at this point, and it's probably (though arguably) even more readable to simply loop over all libraries in a foreach.
2. Rename libraries_detect_library() to libraries_detect() and make it take the library's name as an argument.
It would then call libraries_info() on its own.
Modules could then do:

if ($library = libraries_detect('foo') && $library['installed']) {

Then we would have the following beautiful consistency:

// Just get some info about the library in general.
$library = libraries_info('foo');
// See whether the library is available, etc.
$library = libraries_detect('foo');
// Load it!
$library = libraries_load('foo');

The only inconsistency would be the following:

// This is valid code.
$libraries = libraries_info();
// This results in PHP erros.
$libraries = libraries_detect(); $libraries = libraries_load();

Though it would be about 5 LOC to either of the latter functions to support that behaviour.

Thoughts?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tstoeckler’s picture

Status: Active » Needs review
FileSize
2.46 KB

Here's a patch.
I went with allowing libraries_detect() (with no library name) as it seems a reasonable use-case to want to detect all libraries, but not libraries_load() (with no library name) as loading all libraries seems kind of weird. That is, like anything, perfectly debatable.

sun’s picture

Status: Needs review » Needs work
+++ b/libraries.module
@@ -224,26 +224,38 @@ function libraries_info($name = NULL) {
+ * @param $name
+ *   (optional) The machine name of a library to return registered information
+ *   for, or FALSE if no library with the given name exists. If omitted,
+ *   information about all libraries is returned.
...
+function libraries_detect($name = NULL) {
+  if (empty($name)) {

That @param sounds like a @return description (with regard to FALSE), and with regard to FALSE, empty() should be !isset() when keeping NULL, but in the end, what prevents us from making the argument obligatory? That "detect all" calling code should be rare -- very rare.

Powered by Dreditor.

tstoeckler’s picture

Status: Needs work » Needs review
FileSize
8.38 KB

Yes, I was kind of on the edge about the "detect all" thing. I'm fine with killing that.
Here's a patch. It actually works :)

sun’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! Looks like we catched some return value bugs in the last minute? :)

Did you manually run the tests? I mean, it's nice that testbot runs our patches against Drupal core, but not really helpful... ;)

(And I almost thought you went crazy with writing tests for Libraries API :-D)

tstoeckler’s picture

From http://qa.drupal.org/pifr/test/136354:

Libraries detection and loading (LibrariesTestCase) [Libraries API] 77 0 0

But yes, after the 'desaster' in the other issue yesterday, I did try out this patch locally :)

tstoeckler’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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