-
Improvement
-
Resolution: Fixed
-
Minor
-
3.0, 3.2
-
MOODLE_30_STABLE, MOODLE_32_STABLE
-
MOODLE_32_STABLE
-
MDL-53016-master -
In core_component we have a facility to autoload PSR compliant libraries (it says psr-0 in phpdocs, but think it could be restricted to psr-4) which landed in MDL-47195. It's currently used by horde, and I am using it for GeoIp2 in MDL-48766).
I'm not a fan of the current approach - because:
a) we are bloating (169 horde classes atm) the core_component cache with this third party stuff. I'm not clear exactly why we decided to define the classmap in its entirety in the cache for our own classes - but I feel like we certainly shouldn't need it for third party stuff, and we can keep this cleaner by avoiding it. We can load on demand if namespace matches (see below code snippet) .
b) This is a lesser point than a) but I'm not totally convinced that we should keep these third party libs always ready for inclusion. For example with horde, we only really use it one file, in MDL-48766 it is only needed in one place, and there was talk of using it for google in MDL-51223 - also in only one place.
It just feels cleaner to me to have the libs restricted in their inclusion.
We could fix a) regardless of b), but I think the two combined would mean we could consider doing something like:
core_component::autoload_namespace('Horde', $CFG->libdir.'/horde/framework/');
|
(as an equivalent to of require_once) in admin/tool/messageinbound/classes/manager.php and it wouldn't have much impact for the use of the library and would keep core clean of it. In fact I made a POC implementation of this:
/**
|
* Register a namespace in PSR-4 standard with autoloader.
|
*
|
* Adapated from http://www.php-fig.org/psr/psr-4/examples/
|
*
|
* @param string $prefix The namespace to autoload classes from.
|
* @param string $path The fully qualified path to autoload the classes from.
|
*/
|
function autoload_psr4_namespace($prefix, $path) {
|
spl_autoload_register(
|
function ($class) use ($prefix, $path) {
|
// Does the class use the namespace prefix?
|
$len = strlen($prefix);
|
if (strncmp($prefix, $class, $len) !== 0) {
|
// No, move to the next registered autoloader.
|
return;
|
}
|
|
// Get the relative class name.
|
$relativeclass = substr($class, $len);
|
|
// Replace the namespace prefix with the base directory, replace namespace
|
// separators with directory separators in the relative class name, append
|
// with .php.
|
$file = $path. str_replace('\\', '/', $relativeclass) . '.php';
|
|
// If the file exists, require it.
|
if (file_exists($file)) {
|
require($file);
|
}
|
}
|
);
|
}
|
- blocks
-
MDL-53832 Upgrade enrol_lti to support LTI v2.0
- Closed
- has been marked as being related by
-
MDL-50573 Google docs and other repos can be optimised by lazy loading google/other library
- Closed
-
MDL-59684 Allow plugins to specify PSR-4 namespaces
- Closed
- is blocked by
-
MDL-48766 Support IPv6 in IP lookup tool
- Closed
- will help resolve
-
MDL-51223 Use PSR standard to autoload Google classes
- Closed