added module cache

This commit is contained in:
Roland Gruber 2013-05-09 19:10:35 +00:00
parent a8711815fb
commit b893be577b
6 changed files with 63 additions and 31 deletions

View File

@ -951,4 +951,33 @@ function sendPasswordMail($pwd, $user, $recipient = null) {
} }
} }
/**
* Caches module objects.
* This improves performance if the same module does not need to be created multiple times (calling get_metaData() each time).
*
* @author Roland Gruber
*/
class moduleCache {
/** module cache ("name:scope" => module) */
private static $cache = array();
/**
* Returns a new/cached module with the given name and scope.
*
* @param String $name module name
* @param String $scope module scope (e.g. user)
*/
public static function getModule($name, $scope) {
if (isset(moduleCache::$cache[$name . ':' . $scope])) {
return moduleCache::$cache[$name . ':' . $scope];
}
else {
moduleCache::$cache[$name . ':' . $scope] = new $name($scope);
return moduleCache::$cache[$name . ':' . $scope];
}
}
}
?> ?>

View File

@ -63,7 +63,7 @@ if ((substr($entry, strlen($entry) - 4, 4) == '.inc') && is_file($modulesINC_dir
* @return string alias name * @return string alias name
*/ */
function getModuleAlias($name, $scope) { function getModuleAlias($name, $scope) {
$module = new $name($scope); $module = moduleCache::getModule($name, $scope);
return $module->get_alias(); return $module->get_alias();
} }
@ -75,7 +75,7 @@ function getModuleAlias($name, $scope) {
* @return boolean true if base module * @return boolean true if base module
*/ */
function is_base_module($name, $scope) { function is_base_module($name, $scope) {
$module = new $name($scope); $module = moduleCache::getModule($name, $scope);
return $module->is_base_module(); return $module->is_base_module();
} }
@ -90,7 +90,7 @@ function get_ldap_filter($scope) {
$filters = array(); $filters = array();
$orFilter = ''; $orFilter = '';
for ($i = 0; $i < sizeof($mods); $i++) { for ($i = 0; $i < sizeof($mods); $i++) {
$module = new $mods[$i]($scope); $module = moduleCache::getModule($mods[$i], $scope);
$modinfo = $module->get_ldap_filter(); $modinfo = $module->get_ldap_filter();
if (isset($modinfo['or'])) $filters['or'][] = $modinfo['or']; if (isset($modinfo['or'])) $filters['or'][] = $modinfo['or'];
if (isset($modinfo['and'])) $filters['and'][] = $modinfo['and']; if (isset($modinfo['and'])) $filters['and'][] = $modinfo['and'];
@ -142,7 +142,7 @@ function getRDNAttributes($scope, $selectedModules=null) {
$attrs_high = array(); $attrs_high = array();
for ($i = 0; $i < sizeof($mods); $i++) { for ($i = 0; $i < sizeof($mods); $i++) {
// get list of attributes // get list of attributes
$module = new $mods[$i]($scope); $module = moduleCache::getModule($mods[$i], $scope);
$attrs = $module->get_RDNAttributes(); $attrs = $module->get_RDNAttributes();
$keys = array_keys($attrs); $keys = array_keys($attrs);
// sort attributes // sort attributes
@ -187,7 +187,7 @@ function getRDNAttributes($scope, $selectedModules=null) {
function getModulesDependencies($scope) { function getModulesDependencies($scope) {
$mods = getAvailableModules($scope); $mods = getAvailableModules($scope);
for ($i = 0; $i < sizeof($mods); $i++) { for ($i = 0; $i < sizeof($mods); $i++) {
$module = new $mods[$i]($scope); $module = moduleCache::getModule($mods[$i], $scope);
$return[$mods[$i]] = $module->get_dependencies(); $return[$mods[$i]] = $module->get_dependencies();
} }
return $return; return $return;
@ -271,7 +271,7 @@ function getAvailableModules($scope, $mustSupportAdminInterface = false) {
while ($entry = $dir->read()) while ($entry = $dir->read())
if ((substr($entry, strlen($entry) - 4, 4) == '.inc') && is_file($dirname . '/'.$entry)) { if ((substr($entry, strlen($entry) - 4, 4) == '.inc') && is_file($dirname . '/'.$entry)) {
$entry = substr($entry, 0, strpos($entry, '.')); $entry = substr($entry, 0, strpos($entry, '.'));
$temp = new $entry($scope); $temp = moduleCache::getModule($entry, $scope);
if ($mustSupportAdminInterface && !$temp->supportsAdminInterface()) { if ($mustSupportAdminInterface && !$temp->supportsAdminInterface()) {
continue; continue;
} }
@ -292,7 +292,7 @@ function getProfileOptions($scope) {
$mods = $_SESSION['config']->get_AccountModules($scope); $mods = $_SESSION['config']->get_AccountModules($scope);
$return = array(); $return = array();
for ($i = 0; $i < sizeof($mods); $i++) { for ($i = 0; $i < sizeof($mods); $i++) {
$module = new $mods[$i]($scope); $module = moduleCache::getModule($mods[$i], $scope);
$return[$mods[$i]] = $module->get_profileOptions(); $return[$mods[$i]] = $module->get_profileOptions();
} }
return $return; return $return;
@ -309,7 +309,7 @@ function checkProfileOptions($scope, $options) {
$mods = $_SESSION['config']->get_AccountModules($scope); $mods = $_SESSION['config']->get_AccountModules($scope);
$return = array(); $return = array();
for ($i = 0; $i < sizeof($mods); $i++) { for ($i = 0; $i < sizeof($mods); $i++) {
$module = new $mods[$i]($scope); $module = moduleCache::getModule($mods[$i], $scope);
$temp = $module->check_profileOptions($options); $temp = $module->check_profileOptions($options);
$return = array_merge($return, $temp); $return = array_merge($return, $temp);
} }
@ -326,7 +326,7 @@ function getConfigOptions($scopes) {
$return = array(); $return = array();
$modules = array_keys($scopes); $modules = array_keys($scopes);
for ($i = 0; $i < sizeof($modules); $i++) { for ($i = 0; $i < sizeof($modules); $i++) {
$m = new $modules[$i]('none'); $m = moduleCache::getModule($modules[$i], 'none');
$return[$modules[$i]] = $m->get_configOptions($scopes[$modules[$i]], $scopes); $return[$modules[$i]] = $m->get_configOptions($scopes[$modules[$i]], $scopes);
} }
return $return; return $return;
@ -343,7 +343,7 @@ function checkConfigOptions($scopes, &$options) {
$return = array(); $return = array();
$modules = array_keys($scopes); $modules = array_keys($scopes);
for ($i = 0; $i < sizeof($modules); $i++) { for ($i = 0; $i < sizeof($modules); $i++) {
$m = new $modules[$i]('none'); $m = moduleCache::getModule($modules[$i], 'none');
$errors = $m->check_configOptions($scopes[$modules[$i]], $options); $errors = $m->check_configOptions($scopes[$modules[$i]], $options);
if (isset($errors) && is_array($errors)) { if (isset($errors) && is_array($errors)) {
$return = array_merge($return, $errors); $return = array_merge($return, $errors);
@ -369,7 +369,10 @@ function getHelp($module,$helpID,$scope='') {
} }
return $helpArray[$helpID]; return $helpArray[$helpID];
} }
$moduleObject = new $module((($scope != '') ? $scope : 'none')); if (empty($scope)) {
$scope = 'none';
}
$moduleObject = moduleCache::getModule($module, $scope);
return $moduleObject->get_help($helpID); return $moduleObject->get_help($helpID);
} }
@ -383,7 +386,7 @@ function getAvailablePDFFields($scope) {
$mods = $_SESSION['config']->get_AccountModules($scope); $mods = $_SESSION['config']->get_AccountModules($scope);
$return = array(); $return = array();
for ($i = 0; $i < sizeof($mods); $i++) { for ($i = 0; $i < sizeof($mods); $i++) {
$module = new $mods[$i]($scope); $module = moduleCache::getModule($mods[$i], $scope);
$fields = $module->get_pdfFields(); $fields = $module->get_pdfFields();
$return[$mods[$i]] = array(); $return[$mods[$i]] = array();
if (is_array($fields)) { if (is_array($fields)) {
@ -421,7 +424,7 @@ function getAvailablePDFFields($scope) {
function getUploadColumns($scope, $selectedModules) { function getUploadColumns($scope, $selectedModules) {
$return = array(); $return = array();
for ($i = 0; $i < sizeof($selectedModules); $i++) { for ($i = 0; $i < sizeof($selectedModules); $i++) {
$module = new $selectedModules[$i]($scope); $module = moduleCache::getModule($selectedModules[$i], $scope);
$return[$selectedModules[$i]] = $module->get_uploadColumns($selectedModules); $return[$selectedModules[$i]] = $module->get_uploadColumns($selectedModules);
} }
return $return; return $return;
@ -445,7 +448,7 @@ function buildUploadAccounts($scope, $data, $ids, $selectedModules) {
$predepends = array(); $predepends = array();
// get dependencies // get dependencies
for ($i = 0; $i < sizeof($unOrdered); $i++) { for ($i = 0; $i < sizeof($unOrdered); $i++) {
$mod = new $unOrdered[$i]($scope); $mod = moduleCache::getModule($unOrdered[$i], $scope);
$predepends[$unOrdered[$i]] = $mod->get_uploadPreDepends(); $predepends[$unOrdered[$i]] = $mod->get_uploadPreDepends();
} }
// first all modules without predepends can be ordered // first all modules without predepends can be ordered
@ -488,7 +491,7 @@ function buildUploadAccounts($scope, $data, $ids, $selectedModules) {
$partialAccounts = array(); $partialAccounts = array();
for ($i = 0; $i < sizeof($data); $i++) $partialAccounts[$i]['objectClass'] = array(); for ($i = 0; $i < sizeof($data); $i++) $partialAccounts[$i]['objectClass'] = array();
for ($i = 0; $i < sizeof($ordered); $i++) { for ($i = 0; $i < sizeof($ordered); $i++) {
$module = new $ordered[$i]($scope); $module = moduleCache::getModule($ordered[$i], $scope);
$errors = $module->build_uploadAccounts($data, $ids, $partialAccounts, $selectedModules); $errors = $module->build_uploadAccounts($data, $ids, $partialAccounts, $selectedModules);
if (sizeof($errors) > 0) { if (sizeof($errors) > 0) {
array_unshift($errors, array("INFO", _("Displayed account numbers start at \"0\". Add 2 to get the row in your spreadsheet."), "")); array_unshift($errors, array("INFO", _("Displayed account numbers start at \"0\". Add 2 to get the row in your spreadsheet."), ""));
@ -515,7 +518,7 @@ function doUploadPreActions($scope, $selectedModules, $attributes) {
$messages = array(); $messages = array();
for ($i = 0; $i < sizeof($selectedModules); $i++) { for ($i = 0; $i < sizeof($selectedModules); $i++) {
$activeModule = $selectedModules[$i]; $activeModule = $selectedModules[$i];
$module = new $activeModule($scope); $module = moduleCache::getModule($activeModule, $scope);
$messages = array_merge($messages, $module->doUploadPreActions($attributes)); $messages = array_merge($messages, $module->doUploadPreActions($attributes));
} }
return $messages; return $messages;
@ -551,7 +554,7 @@ function doUploadPostActions($scope, &$data, $ids, $failed, $selectedModules, &$
$_SESSION['mass_postActions'][$activeModule] = array(); $_SESSION['mass_postActions'][$activeModule] = array();
} }
// let first module do one post action // let first module do one post action
$module = new $activeModule($scope); $module = moduleCache::getModule($activeModule, $scope);
$return = $module->doUploadPostActions($data, $ids, $failed, $_SESSION['mass_postActions'][$activeModule], $accounts); $return = $module->doUploadPostActions($data, $ids, $failed, $_SESSION['mass_postActions'][$activeModule], $accounts);
// remove active module from list if already finished // remove active module from list if already finished
if ($return['status'] == 'finished') { if ($return['status'] == 'finished') {
@ -580,7 +583,7 @@ function getRequiredExtensions() {
for ($i = 0; $i < sizeof($scopes); $i++) { for ($i = 0; $i < sizeof($scopes); $i++) {
$mods = $_SESSION['config']->get_AccountModules($scopes[$i]); $mods = $_SESSION['config']->get_AccountModules($scopes[$i]);
for ($m = 0; $m < sizeof($mods); $m++) { for ($m = 0; $m < sizeof($mods); $m++) {
$module = new $mods[$m]($scopes[$i]); $module = moduleCache::getModule($mods[$m], $scopes[$i]);
$ext = $module->getRequiredExtensions(); $ext = $module->getRequiredExtensions();
for ($e = 0; $e < sizeof($ext); $e++) { for ($e = 0; $e < sizeof($ext); $e++) {
if (!in_array($ext[$e], $extList)) $extList[] = $ext[$e]; if (!in_array($ext[$e], $extList)) $extList[] = $ext[$e];
@ -1518,7 +1521,7 @@ class accountContainer {
foreach ($modules as $module) { foreach ($modules as $module) {
if (!isset($this->module[$module])) { if (!isset($this->module[$module])) {
$this->module[$module] = new $module($this->type); $this->module[$module] = moduleCache::getModule($module, $this->type);
$this->module[$module]->init($this->base); $this->module[$module]->init($this->base);
} }
$this->module[$module]->load_attributes($attr); $this->module[$module]->load_attributes($attr);
@ -1548,7 +1551,7 @@ class accountContainer {
$aliases = array(); $aliases = array();
$ldapAttributesTemp = array(); $ldapAttributesTemp = array();
foreach ($modules as $module) { foreach ($modules as $module) {
$moduleObj = new $module($this->type); $moduleObj = moduleCache::getModule($module, $this->type);
$objectClasses = array_merge($objectClasses, $moduleObj->getManagedObjectClasses()); $objectClasses = array_merge($objectClasses, $moduleObj->getManagedObjectClasses());
$aliases = array_merge($aliases, $moduleObj->getLDAPAliases()); $aliases = array_merge($aliases, $moduleObj->getLDAPAliases());
$ldapAttributesTemp = array_merge($ldapAttributesTemp, $moduleObj->getManagedAttributes()); $ldapAttributesTemp = array_merge($ldapAttributesTemp, $moduleObj->getManagedAttributes());
@ -1619,7 +1622,7 @@ class accountContainer {
$this->lastLoadedProfile = 'default'; $this->lastLoadedProfile = 'default';
$modules = $_SESSION['config']->get_AccountModules($this->type); $modules = $_SESSION['config']->get_AccountModules($this->type);
foreach ($modules as $module) { foreach ($modules as $module) {
$this->module[$module] = new $module($this->type); $this->module[$module] = moduleCache::getModule($module, $this->type);
$this->module[$module]->init($this->base); $this->module[$module]->init($this->base);
} }
// sort module buttons // sort module buttons

View File

@ -56,7 +56,7 @@ function getSelfServiceSearchAttributes($scope) {
$return = array(); $return = array();
$modules = getAvailableModules($scope); $modules = getAvailableModules($scope);
for ($i = 0; $i < sizeof($modules); $i++) { for ($i = 0; $i < sizeof($modules); $i++) {
$m = new $modules[$i]($scope); $m = moduleCache::getModule($modules[$i], $scope);
$attributes = $m->getSelfServiceSearchAttributes(); $attributes = $m->getSelfServiceSearchAttributes();
$return = array_merge($return, $attributes); $return = array_merge($return, $attributes);
} }
@ -76,7 +76,7 @@ function getSelfServiceFieldSettings($scope) {
$return = array(); $return = array();
$modules = getAvailableModules($scope); $modules = getAvailableModules($scope);
for ($i = 0; $i < sizeof($modules); $i++) { for ($i = 0; $i < sizeof($modules); $i++) {
$m = new $modules[$i]($scope); $m = moduleCache::getModule($modules[$i], $scope);
$settings = $m->getSelfServiceFields(); $settings = $m->getSelfServiceFields();
if (sizeof($settings) > 0) $return[$modules[$i]] = $settings; if (sizeof($settings) > 0) $return[$modules[$i]] = $settings;
} }
@ -99,7 +99,7 @@ function getSelfServiceOptions($scope, $fields, $attributes, $passwordChangeOnly
$modules = getAvailableModules($scope); $modules = getAvailableModules($scope);
for ($i = 0; $i < sizeof($modules); $i++) { for ($i = 0; $i < sizeof($modules); $i++) {
if (!isset($fields[$modules[$i]])) continue; if (!isset($fields[$modules[$i]])) continue;
$m = new $modules[$i]($scope); $m = moduleCache::getModule($modules[$i], $scope);
$modReadOnlyFields = array(); $modReadOnlyFields = array();
for ($r = 0; $r < sizeof($readOnlyFields); $r++) { for ($r = 0; $r < sizeof($readOnlyFields); $r++) {
$parts = explode('_', $readOnlyFields[$r]); $parts = explode('_', $readOnlyFields[$r]);
@ -129,7 +129,7 @@ function checkSelfServiceOptions($scope, $fields, $attributes, $passwordChangeOn
$modules = getAvailableModules($scope); $modules = getAvailableModules($scope);
for ($i = 0; $i < sizeof($modules); $i++) { for ($i = 0; $i < sizeof($modules); $i++) {
if (!isset($fields[$modules[$i]])) continue; if (!isset($fields[$modules[$i]])) continue;
$m = new $modules[$i]($scope); $m = moduleCache::getModule($modules[$i], $scope);
$modReadOnlyFields = array(); $modReadOnlyFields = array();
for ($r = 0; $r < sizeof($readOnlyFields); $r++) { for ($r = 0; $r < sizeof($readOnlyFields); $r++) {
$parts = explode('_', $readOnlyFields[$r]); $parts = explode('_', $readOnlyFields[$r]);
@ -260,7 +260,7 @@ function getSelfServiceSettings($scope, $profile) {
$return = array(); $return = array();
$modules = getAvailableModules($scope); $modules = getAvailableModules($scope);
for ($i = 0; $i < sizeof($modules); $i++) { for ($i = 0; $i < sizeof($modules); $i++) {
$m = new $modules[$i]($scope); $m = moduleCache::getModule($modules[$i], $scope);
$return[$modules[$i]] = $m->getSelfServiceSettings($profile); $return[$modules[$i]] = $m->getSelfServiceSettings($profile);
} }
return $return; return $return;
@ -278,7 +278,7 @@ function checkSelfServiceSettings($scope, &$options, &$profile) {
$return = array(); $return = array();
$modules = getAvailableModules($scope); $modules = getAvailableModules($scope);
for ($i = 0; $i < sizeof($modules); $i++) { for ($i = 0; $i < sizeof($modules); $i++) {
$m = new $modules[$i]($scope); $m = moduleCache::getModule($modules[$i], $scope);
$errors = $m->checkSelfServiceSettings($options, $profile); $errors = $m->checkSelfServiceSettings($options, $profile);
$return = array_merge($return, $errors); $return = array_merge($return, $errors);
} }

View File

@ -214,7 +214,7 @@ for ($i = 0; $i < sizeof($modules); $i++) {
if (sizeof($options[$modules[$i]]) < 1) continue; if (sizeof($options[$modules[$i]]) < 1) continue;
echo "<fieldset class=\"ui-corner-all user-border user-bright\">\n"; echo "<fieldset class=\"ui-corner-all user-border user-bright\">\n";
$icon = ''; $icon = '';
$module = new $modules[$i]('none'); $module = moduleCache::getModule($modules[$i], 'none');
$iconImage = $module->getIcon(); $iconImage = $module->getIcon();
if ($iconImage != null) { if ($iconImage != null) {
$icon = '<img align="middle" src="../../graphics/' . $iconImage . '" alt="' . $iconImage . '"> '; $icon = '<img align="middle" src="../../graphics/' . $iconImage . '" alt="' . $iconImage . '"> ';

View File

@ -112,7 +112,7 @@ if (isset($_GET['type']) && isset($_SESSION['delete_dn'])) {
$values = array(); $values = array();
$tabindex = 100; $tabindex = 100;
foreach ($modules as $module) { foreach ($modules as $module) {
$module = new $module($_GET['type']); $module = moduleCache::getModule($module, $_GET['type']);
parseHtml(get_class($module), $module->display_html_delete(), $values, true, $tabindex, $_GET['type']); parseHtml(get_class($module), $module->display_html_delete(), $values, true, $tabindex, $_GET['type']);
} }
echo "</table>\n"; echo "</table>\n";

View File

@ -170,7 +170,7 @@ for ($i = 0; $i < sizeof($types); $i++) {
if (($m != 0) && ($m%3 == 0)) { if (($m != 0) && ($m%3 == 0)) {
echo $innerTable->addNewLine(); echo $innerTable->addNewLine();
} }
$module = new $modules[$m]($types[$i]); $module = moduleCache::getModule($modules[$m], $types[$i]);
$iconImage = '../graphics/' . $module->getIcon(); $iconImage = '../graphics/' . $module->getIcon();
$innerTable->addElement(new htmlImage($iconImage)); $innerTable->addElement(new htmlImage($iconImage));
$enabled = true; $enabled = true;
@ -349,7 +349,7 @@ function showMainPage($scope, $selectedModules) {
} }
$columnContainer->addElement(new htmlSpacer(null, '10px'), true); $columnContainer->addElement(new htmlSpacer(null, '10px'), true);
$icon = ''; $icon = '';
$module = new $modules[$m]($scope); $module = moduleCache::getModule($modules[$m], $scope);
$iconImage = $module->getIcon(); $iconImage = $module->getIcon();
if ($iconImage != null) { if ($iconImage != null) {
$icon = '../graphics/' . $iconImage; $icon = '../graphics/' . $iconImage;