/** * Trigger event with given $name from current active application. * * @param string $name Event name. * @param mixed $params Parameters passed to event handlers. * @param string[] $array_keys Array of expected template items for UI events. * @return array app_id or plugin_id => data returned from handler (unless null is returned) */ public function event($name, &$params = null, $array_keys = null) { $result = array(); if (is_array($name)) { $event_app_id = $name[0]; $event_system = self::getInstance($event_app_id); $name = $name[1]; } else { $event_app_id = $this->getConfig()->getApplication(); $event_system = $this; } $event_prefix = wa($event_app_id)->getConfig()->getPrefix(); if (!isset(self::$handlers['apps'])) { self::$handlers['apps'] = array(); $cache_file = $this->config->getPath('cache', 'config/handlers'); if (!waSystemConfig::isDebug() && file_exists($cache_file)) { self::$handlers['apps'] = (include $cache_file); } if (!self::$handlers['apps'] || !is_array(self::$handlers['apps'])) { $apps = $this->getApps(); $path = $this->getConfig()->getPath('apps'); foreach ($apps as $app_id => $app_info) { $files = waFiles::listdir($path . '/' . $app_id . '/lib/handlers/'); foreach ($files as $file) { if (substr($file, -12) == '.handler.php') { $file = explode('.', substr($file, 0, -12), 2); self::$handlers['apps'][$file[0]][$file[1]][] = $app_id; } } } if (!waSystemConfig::isDebug()) { waUtils::varExportToFile(self::$handlers['apps'], $cache_file); } } } if (!isset(self::$handlers['plugins'][$event_app_id])) { self::$handlers['plugins'][$event_app_id] = array(); $plugins = $event_system->getConfig()->getPlugins(); foreach ($plugins as $plugin_id => $plugin) { if (!empty($plugin['handlers'])) { foreach ($plugin['handlers'] as $handler_event => $handler_method) { self::$handlers['plugins'][$event_app_id][$handler_event][$plugin_id] = $handler_method; } } } } if (isset(self::$handlers['apps'][$event_app_id][$name])) { $path = $this->getConfig()->getPath('apps'); foreach (self::$handlers['apps'][$event_app_id][$name] as $app_id) { $file_path = $path . '/' . $app_id . '/lib/handlers/' . $event_prefix . "." . $name . ".handler.php"; if (!file_exists($file_path)) { continue; } wa($app_id); include_once $file_path; $class_name = $name; if (strpos($name, '.') !== false) { $class_name = strtok($class_name, '.') . ucfirst(strtok('')); } $class_name = $app_id . ucfirst($event_prefix) . ucfirst($class_name) . "Handler"; /** * @var $handler waEventHandler */ $handler = new $class_name(); try { $r = $handler->execute($params); if ($r !== null) { $result[$app_id] = $r; } } catch (Exception $e) { waLog::log('Event handling error in ' . $file_path . ': ' . $e->getMessage()); } } } if (isset(self::$handlers['plugins'][$event_app_id][$name])) { $plugins = $event_system->getConfig()->getPlugins(); foreach (self::$handlers['plugins'][$event_app_id][$name] as $plugin_id => $method) { if (!isset($plugins[$plugin_id])) { continue; } $plugin = $plugins[$plugin_id]; self::pushActivePlugin($plugin_id, $event_prefix); $class_name = $event_app_id . ucfirst($plugin_id) . 'Plugin'; try { $class = new $class_name($plugin); // Load plugin locale if it exists $locale_path = $this->getAppPath('plugins/' . $plugin_id . '/locale', $event_app_id); if (is_dir($locale_path)) { waLocale::load($this->getLocale(), $locale_path, self::getActiveLocaleDomain(), false); } if (method_exists($class, $method) && null !== ($r = $class->{$method}($params))) { if ($array_keys && is_array($r)) { foreach ($array_keys as $k) { if (!isset($r[$k])) { $r[$k] = ''; } } } $result[$plugin_id . '-plugin'] = $r; } } catch (Exception $e) { waLog::log('Event handling error in ' . $class_name . '->' . $name . '(): ' . $e->getMessage()); } self::popActivePlugin(); } } return $result; }
public function getApps($system = false) { if (self::$apps === null) { $locale = $this->getUser()->getLocale(); $file = $this->config->getPath('cache', 'config/apps' . $locale); if (!file_exists($this->getConfig()->getPath('config', 'apps'))) { self::$apps = array(); throw new waException('File wa-config/apps.php not found.', 600); } if (!file_exists($file) || filemtime($file) < filemtime($this->getConfig()->getPath('config', 'apps')) || waSystemConfig::isDebug()) { waFiles::create($this->getConfig()->getPath('cache') . '/config'); $all_apps = (include $this->getConfig()->getPath('config', 'apps')); $all_apps['webasyst'] = true; self::$apps = array(); foreach ($all_apps as $app => $enabled) { if ($enabled) { waLocale::loadByDomain($app, $locale); $app_config = $this->getAppPath('lib/config/app.php', $app); if (!file_exists($app_config)) { if (false && SystemConfig::isDebug()) { throw new waException("Config not found. Create config by path " . $app_config); } continue; } $app_info = (include $app_config); $build_file = $app_config = $this->getAppPath('lib/config/build.php', $app); if (file_exists($build_file)) { $app_info['build'] = (include $build_file); } else { if (SystemConfig::isDebug()) { $app_info['build'] = time(); } else { $app_info['build'] = 0; } } $app_info['id'] = $app; $app_info['name'] = _wd($app, $app_info['name']); if (isset($app_info['icon'])) { if (is_array($app_info['icon'])) { foreach ($app_info['icon'] as $size => $url) { $app_info['icon'][$size] = 'wa-apps/' . $app . '/' . $url; } } else { $app_info['icon'] = array(48 => 'wa-apps/' . $app . '/' . $app_info['icon']); } } else { $app_info['icon'] = array(); } if (isset($app_info['img'])) { $app_info['img'] = 'wa-apps/' . $app . '/' . $app_info['img']; } else { $app_info['img'] = isset($app_info['icon'][48]) ? $app_info['icon'][48] : 'wa-apps/' . $app . '/img/' . $app . ".png"; } if (!isset($app_info['icon'][48])) { $app_info['icon'][48] = $app_info['img']; } if (!isset($app_info['icon'][24])) { $app_info['icon'][24] = $app_info['icon'][48]; } if (!isset($app_info['icon'][16])) { $app_info['icon'][16] = $app_info['icon'][24]; } self::$apps[$app] = $app_info; } } if (!file_exists($file) || filemtime($file) < filemtime($this->getConfig()->getPath('config', 'apps'))) { waUtils::varExportToFile(self::$apps, $file); } } else { self::$apps = (include $file); waLocale::loadByDomain('webasyst'); } } if ($system) { return self::$apps; } else { $apps = self::$apps; unset($apps['webasyst']); return $apps; } }