Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

Components (core subsystems + plugins) related code.

Copyright: 2013 Petr Skoda {@link http://skodak.org}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 1285 lines (47 kb)
Included or required: 1 time
Referenced: 0 times
Includes or requires: 1 file
 version.php

Defines 1 class


Class: core_component  - X-Ref

Collection of components related methods.

classloader($classname)   X-Ref
Class loader for Frankenstyle named classes in standard locations.
Frankenstyle namespaces are supported.

The expected location for core classes is:
1/ core_xx_yy_zz ---> lib/classes/xx_yy_zz.php
2/ \core\xx_yy_zz ---> lib/classes/xx_yy_zz.php
3/ \core\xx\yy_zz ---> lib/classes/xx/yy_zz.php

The expected location for plugin classes is:
1/ mod_name_xx_yy_zz ---> mod/name/classes/xx_yy_zz.php
2/ \mod_name\xx_yy_zz ---> mod/name/classes/xx_yy_zz.php
3/ \mod_name\xx\yy_zz ---> mod/name/classes/xx/yy_zz.php

param: string $classname

psr_classloader($class)   X-Ref
Return the path to a class from our defined PSR-0 or PSR-4 standard namespaces on
demand. Only returns paths to files that exist.

Adapated from http://www.php-fig.org/psr/psr-4/examples/ and made PSR-0
compatible.

param: string $class the name of the class.
return: string|bool The full path to the file defining the class. Or false if it could not be resolved or does not exist.

get_class_file($class, $prefix, $path, $separators)   X-Ref
Return the path to the class based on the given namespace prefix and path it corresponds to.

Will return the path even if the file does not exist. Check the file esists before requiring.

param: string $class the name of the class.
param: string $prefix The namespace prefix used to identify the base directory of the source files.
param: string $path The relative path to the base directory of the source files.
param: string[] $separators The characters that should be used for separating.
return: string|bool The full path to the file defining the class. Or false if it could not be resolved.

init()   X-Ref
Initialise caches, always call before accessing self:: caches.


is_developer()   X-Ref
Are we in developer debug mode?

Note: You need to set "$CFG->debug = (E_ALL | E_STRICT);" in config.php,
the reason is we need to use this before we setup DB connection or caches for CFG.

return: bool

get_cache_content()   X-Ref
Create cache file content.

return: string

fill_all_caches()   X-Ref
Fill all caches.


fetch_core_version()   X-Ref
Get the core version.

In order for this to work properly, opcache should be reset beforehand.

return: float core version.

fetch_subsystems()   X-Ref
Returns list of core subsystems.

return: array

fetch_plugintypes()   X-Ref
Returns list of known plugin types.

return: array

fetch_component_source(string $key)   X-Ref
Returns the component source content as loaded from /lib/components.json.

return: array

fetch_subtypes($ownerdir)   X-Ref
Returns list of subtypes.

param: string $ownerdir
return: array

fetch_plugins($plugintype, $fulldir)   X-Ref
Returns list of plugins of given type in given directory.

param: string $plugintype
param: string $fulldir
return: array

fill_classmap_cache()   X-Ref
Find all classes that can be autoloaded including frankenstyle namespaces.


fill_filemap_cache()   X-Ref
Fills up the cache defining what plugins have certain files.

return: void

load_classes($component, $fulldir, $namespace = '')   X-Ref
Find classes in directory and recurse to subdirs.

param: string $component
param: string $fulldir
param: string $namespace

get_core_subsystems()   X-Ref
List all core subsystems and their location

This is a list of components that are part of the core and their
language strings are defined in /lang/en/<<subsystem>>.php. If a given
plugin is not listed here and it does not have proper plugintype prefix,
then it is considered as course activity module.

The location is absolute file path to dir. NULL means there is no special
directory for this subsystem. If the location is set, the subsystem's
renderer.php is expected to be there.

return: array of (string)name => (string|null)full dir location

get_plugin_types()   X-Ref
Get list of available plugin types together with their location.

return: array as (string)plugintype => (string)fulldir

get_plugin_list($plugintype)   X-Ref
Get list of plugins of given type.

param: string $plugintype
return: array as (string)pluginname => (string)fulldir

get_plugin_list_with_class($plugintype, $class, $file = null)   X-Ref
Get a list of all the plugins of a given type that define a certain class
in a certain file. The plugin component names and class names are returned.

param: string $plugintype the type of plugin, e.g. 'mod' or 'report'.
param: string $class the part of the name of the class after the
param: string $file the name of file within the plugin that defines the class.
return: array with frankenstyle plugin names as keys (e.g. 'report_courselist', 'mod_forum')

get_plugin_list_with_file($plugintype, $file, $include = false)   X-Ref
Get a list of all the plugins of a given type that contain a particular file.

param: string $plugintype the type of plugin, e.g. 'mod' or 'report'.
param: string $file the name of file that must be present in the plugin.
param: bool $include if true (default false), the file will be include_once-ed if found.
return: array with plugin name as keys (e.g. 'forum', 'courselist') and the path

get_component_classes_in_namespace($component = null, $namespace = '')   X-Ref
Returns all classes in a component matching the provided namespace.

It checks that the class exists.

e.g. get_component_classes_in_namespace('mod_forum', 'event')

param: string|null $component A valid moodle component (frankenstyle) or null if searching all components
param: string $namespace Namespace from the component name or empty string if all $component classes.
return: array The full class name as key and the class path as value, empty array if $component is `null`

get_plugin_directory($plugintype, $pluginname)   X-Ref
Returns the exact absolute path to plugin directory.

param: string $plugintype type of plugin
param: string $pluginname name of the plugin
return: string full path to plugin directory; null if not found

get_subsystem_directory($subsystem)   X-Ref
Returns the exact absolute path to plugin directory.

param: string $subsystem type of core subsystem
return: string full path to subsystem directory; null if not found

is_valid_plugin_name($plugintype, $pluginname)   X-Ref
This method validates a plug name. It is much faster than calling clean_param.

param: string $plugintype type of plugin
param: string $pluginname a string that might be a plugin name.
return: bool if this string is a valid plugin name.

normalize_componentname($componentname)   X-Ref
Normalize the component name.

Note: this does not verify the validity of the plugin or component.

param: string $component
return: string

normalize_component($component)   X-Ref
Normalize the component name using the "frankenstyle" rules.

Note: this does not verify the validity of plugin or type names.

param: string $component
return: array two-items list of [(string)type, (string|null)name]

get_component_directory($component)   X-Ref
Return exact absolute path to a plugin directory.

param: string $component name such as 'moodle', 'mod_forum'
return: string full path to component directory; NULL if not found

get_plugin_types_with_subplugins()   X-Ref
Returns list of plugin types that allow subplugins.

return: array as (string)plugintype => (string)fulldir

get_subtype_parent($type)   X-Ref
Returns parent of this subplugin type.

param: string $type
return: string parent component or null

get_subplugins($component)   X-Ref
Return all subplugins of this component.

param: string $component.
return: array $subtype=>array($component, ..), null if no subtypes defined

get_all_versions_hash()   X-Ref
Returns hash of all versions including core and all plugins.

This is relatively slow and not fully cached, use with care!

return: string sha1 hash

get_all_versions()   X-Ref
Returns hash of all versions including core and all plugins.

This is relatively slow and not fully cached, use with care!

return: array as (string)plugintype_pluginname => (int)version

invalidate_opcode_php_cache($file)   X-Ref
Invalidate opcode cache for given file, this is intended for
php files that are stored in dataroot.

Note: we need it here because this class must be self-contained.

param: string $file

is_core_subsystem($subsystemname)   X-Ref
Return true if subsystemname is core subsystem.

param: string $subsystemname name of the subsystem.
return: bool true if core subsystem.

fill_classmap_renames_cache()   X-Ref
Records all class renames that have been made to facilitate autoloading.


load_renamed_classes(?string $fulldir)   X-Ref
Loads the db/renamedclasses.php file from the given directory.

The renamedclasses.php should contain a key => value array ($renamedclasses) where the key is old class name,
and the value is the new class name.
It is only included when we are populating the component cache. After that is not needed.

param: string|null $fulldir The directory to the renamed classes.

get_component_list()   X-Ref
Returns a list of frankenstyle component names and their paths, for all components (plugins and subsystems).

E.g.
[
'mod' => [
'mod_forum' => FORUM_PLUGIN_PATH,
...
],
...
'core' => [
'core_comment' => COMMENT_SUBSYSTEM_PATH,
...
]
]

return: array an associative array of components and their corresponding paths.

get_component_names()   X-Ref
Returns a list of frankenstyle component names.

E.g.
[
'core_course',
'core_message',
'mod_assign',
...
]
return: array the list of frankenstyle component names.