I know this isn't really of great importance currently, but it has been sitting in the back of my head for while and so I wanted to get rid of it.
I thought about not using drupal_static, but wasn't really sure. From http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/drupal...
In a few cases, a function can have certainty that there is no legitimate use-case for resetting that function's static variable. This is rare, because when writing a function, it's hard to forecast all the situations in which it will be used. A guideline is that if a function's static variable does not depend on any information outside of the function that might change during a single page request, then it's ok to use the "static" keyword instead of the drupal_static() function.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | libraries-HEAD.load_.11.patch | 2.8 KB | sun |
| #9 | libraries-HEAD.load_.9.patch | 2.49 KB | sun |
| #7 | dont-load-twice-4.patch | 2.09 KB | tstoeckler |
| #6 | dont-load-twice-3.patch | 2.07 KB | tstoeckler |
| #2 | dont-load-twice-2.patch | 1.88 KB | tstoeckler |
Comments
Comment #1
sunLet's squeeze a newline between the static and early return, and also return the statically cached value.
Let's assign the return value to $loaded_libraries[$name] instead of $count, and also simply return $loaded_libraries[$name] (optionally casted to bool)
Powered by Dreditor.
Comment #2
tstoecklerThis adjusts the return value according to #1. I switched the logic around, leading to a bit more indentation, but having the same return statement twice in one function seemed awkward to me.
Comment #3
tstoecklerWhile reviewing the patch, I just noticed a little weirdness with the current return value, which this patch brings to light.
In short, there is no way to distinguish between an already loaded library and a broken/unloadable library.
Imagine we had #962214: Load library dependencies in and library B depends on library A.
Module X needs library A, so it does
which works well, because
libraries_load()returns an integer, which is casted toTRUE, as long as it is not0.Now module Y needs library B, so it does
which, because of the dependency calls in turn:
Since library A was already loaded,
libraries_load('library_a')will return0which will be cast toFALSE, even though it would be fine to load library B.On the other hand if
libraries_load('library_b')doesn't in turn doif (libraries_load('library_a')) { ... }library B will be loaded even is library A is not loaded or even can't be loaded.Setting to 'needs work' for consideration.
Comment #4
sunThis is exactly what I meant at the end of #1, we need to store and return the first/last return value (optionally casted to Boolean, or not).
Needs to default to an array, otherwise $loaded_libraries might now always be an array.
Let's not early return here - instead, assign the already figured out value (FALSE) to the static cache and return that instead at the end of the function.
This should be wrapped in a check for FALSE then.
Powered by Dreditor.
Comment #5
tstoecklerAhh, now I get it...
That's pretty smart, using the static cache to store the return value, I hadn't thought of that. :)
Comment #6
tstoecklerMaybe something like this?
Comment #7
tstoecklerAnd without whitespace.
Comment #8
sunThe second argument to drupal_static() specifies the default value; just move the array() in there.
1) Let's initialize $loaded_libraries[$name] with FALSE early, and afterwards, negate the first if condition.
2) Let's turn the second if into an elseif, and drop the last if condition.
3) All of the !empty() in the second condition should bet isset().
4) I think the second condition should only check whether a variant name was passed, and regardless of whether it exists or not, we process it. In turn, the third condition should be sufficient when checking for !empty(variant][installed).
Powered by Dreditor.
Comment #9
sunHelping you out a bit here ;)
Comment #10
tstoecklerThat's pretty sweet. I didn't know "+=" works multi-dimensionally.
Only some minor nitpicks below.
Also, if you can think of some way to test this, do tell me, because I couldn't think of one.
I think it would be good to state that the mentioned keys are additional to the normal ones, something like:
"An array of library information as returned from libraries_info(), containing additionally: ..."
Since we are adding this libraries_detect_library() maybe it makes sense to document it there.
I sense there is some point to splitting that up into 2 lines, but I don't get it.
Powered by Dreditor.
Comment #11
sunIt doesn't ;) The operator adds each key of the right-hand array to the left-hand array, if the array key does not exist yet. In case it doesn't exist yet, the entire corresponding value of the right-hand array is taken over. Note that a left-hand array key with value NULL is also considered to exist; i.e., the operator basically uses array_key_exists(), not isset().
In this case, we merely need to ensure that there is a $variant key. That is, because if there is already a $variant key, then 'installed' is guaranteed to exist on it, too. Only if the $variant key does not exist, we need to ensure that it exists, so the subsequent array_merge() doesn't fail.
Clarified the rest. :)
Comment #12
tstoecklerRight. I didn't have in mind, that
'installed'is always initialized if the variant is.Thanks, the code is really slick, now!
Comment #13
sunThanks for reporting, reviewing, and testing! Committed to HEAD.
A new development snapshot will be available within the next 12 hours. This improvement will be available in the next official release.