5 * Created on Dec 27, 2012
7 * Copyright © 2012 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
29 * This class holds a list of modules and handles instantiation
34 class ApiModuleManager
extends ContextSource
{
37 private $mInstances = array();
38 private $mGroups = array();
39 private $mModules = array();
42 * Construct new module manager
43 * @param ApiBase $parentModule Parent module instance will be used during instantiation
45 public function __construct( ApiBase
$parentModule ) {
46 $this->mParent
= $parentModule;
50 * Add a list of modules to the manager
51 * @param array $modules A map of ModuleName => ModuleClass
52 * @param string $group Which group modules belong to (action,format,...)
54 public function addModules( array $modules, $group ) {
55 foreach ( $modules as $name => $class ) {
56 $this->addModule( $name, $group, $class );
61 * Add or overwrite a module in this ApiMain instance. Intended for use by extending
62 * classes who wish to add their own modules to their lexicon or override the
63 * behavior of inherent ones.
65 * @param string $group Name of the module group
66 * @param string $name The identifier for this module.
67 * @param string $class The class where this module is implemented.
69 public function addModule( $name, $group, $class ) {
70 $this->mGroups
[$group] = null;
71 $this->mModules
[$name] = array( $group, $class );
75 * Get module instance by name, or instantiate it if it does not exist
76 * @param string $moduleName Module name
77 * @param string $group Optionally validate that the module is in a specific group
78 * @param bool $ignoreCache If true, force-creates a new instance and does not cache it
79 * @return mixed The new module instance, or null if failed
81 public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
82 if ( !isset( $this->mModules
[$moduleName] ) ) {
85 $grpCls = $this->mModules
[$moduleName];
86 if ( $group !== null && $grpCls[0] !== $group ) {
89 if ( !$ignoreCache && isset( $this->mInstances
[$moduleName] ) ) {
91 return $this->mInstances
[$moduleName];
95 $instance = new $class ( $this->mParent
, $moduleName );
96 if ( !$ignoreCache ) {
97 // cache this instance in case it is needed later
98 $this->mInstances
[$moduleName] = $instance;
106 * Get an array of modules in a specific group or all if no group is set.
107 * @param string $group Optional group filter
108 * @return array List of module names
110 public function getNames( $group = null ) {
111 if ( $group === null ) {
112 return array_keys( $this->mModules
);
115 foreach ( $this->mModules
as $name => $grpCls ) {
116 if ( $grpCls[0] === $group ) {
125 * Create an array of (moduleName => moduleClass) for a specific group or for all.
126 * @param string $group Name of the group to get or null for all
127 * @return array Name=>class map
129 public function getNamesWithClasses( $group = null ) {
131 foreach ( $this->mModules
as $name => $grpCls ) {
132 if ( $group === null ||
$grpCls[0] === $group ) {
133 $result[$name] = $grpCls[1];
141 * Returns true if the specific module is defined at all or in a specific group.
142 * @param string $moduleName Module name
143 * @param string $group Group name to check against, or null to check all groups,
144 * @return bool True if defined
146 public function isDefined( $moduleName, $group = null ) {
147 if ( isset( $this->mModules
[$moduleName] ) ) {
148 return $group === null ||
$this->mModules
[$moduleName][0] === $group;
155 * Returns the group name for the given module
156 * @param string $moduleName
157 * @return string Group name or null if missing
159 public function getModuleGroup( $moduleName ) {
160 if ( isset( $this->mModules
[$moduleName] ) ) {
161 return $this->mModules
[$moduleName][0];
168 * Get a list of groups this manager contains.
171 public function getGroups() {
172 return array_keys( $this->mGroups
);