/** * Fires an Event. * * The Event gets created, passed around and then returned to the issuer. * * @param mixed $input Object for direct event, string for system event or notifierEvent * @todo Implement Application Events * @todo Implement Directory input for Events from other locations (like Modules) * * @return \FuzeWorks\Event The Event */ public static function fireEvent($input) { if (is_string($input)) { // If the input is a string $eventClass = $input; $eventName = $input; if (!class_exists($eventClass)) { // Check if the file even exists $file = Core::$coreDir . DS . 'Events' . DS . 'event.' . $eventName . '.php'; if (file_exists($file)) { // Load the file $eventClass = "\\FuzeWorks\\Event\\" . $eventClass; include_once $file; } else { // No event arguments? Looks like a notify-event if (func_num_args() == 1) { // Load notify-event-class $eventClass = '\\FuzeWorks\\Event\\NotifierEvent'; } else { // No notify-event: we tried all we could throw new EventException('Event ' . $eventName . ' could not be found!'); } } } $event = new $eventClass($this); } elseif (is_object($input)) { $eventName = get_class($input); $eventName = explode('\\', $eventName); $eventName = end($eventName); $event = $input; } else { // INVALID EVENT return false; } if (self::$enabled) { Logger::newLevel("Firing Event: '" . $eventName . "'"); Logger::log('Initializing Event'); } if (func_num_args() > 1) { call_user_func_array(array($event, 'init'), array_slice(func_get_args(), 1)); } // Do not run if the event system is disabled if (!self::$enabled) { return $event; } Logger::log('Checking for Listeners'); // Read the event register for listeners $register = self::$register; if (isset($register[$eventName])) { for ($i = 0; $i < count($register[$eventName]); ++$i) { Modules::get($register[$eventName][$i]); } } //There are listeners for this event if (isset(self::$listeners[$eventName])) { //Loop from the highest priority to the lowest for ($priority = EventPriority::getHighestPriority(); $priority <= EventPriority::getLowestPriority(); ++$priority) { //Check for listeners in this priority if (isset(self::$listeners[$eventName][$priority])) { $listeners = self::$listeners[$eventName][$priority]; Logger::newLevel('Found listeners with priority ' . EventPriority::getPriority($priority)); //Fire the event to each listener foreach ($listeners as $callback) { if (is_callable($callback)) { Logger::newLevel('Firing function'); } elseif (!is_string($callback[0])) { Logger::newLevel('Firing ' . get_class($callback[0]) . '->' . $callback[1]); } else { Logger::newLevel('Firing ' . implode('->', $callback)); } try { call_user_func($callback, $event); } catch (ModuleException $e) { Logger::exceptionHandler($e); } Logger::stopLevel(); } Logger::stopLevel(); } } } Logger::stopLevel(); return $event; }
/** * The Module Callable. * * When a module listens for a specific routing path, this callable get's called. * After this the module can handle the request with the route() function in the module's root directory * * @param array Regex matches */ public static function moduleCallable($matches = array()) { // First detect what module is attached to this route Logger::newLevel('Module callable called!'); // Get the route $route = !empty($matches['route']) ? $matches['route'] : null; // See if the route exists if (isset(self::$module_routes[$route])) { Logger::log("Module '" . self::$module_routes[$route] . "' matched given route"); // Load the module $mod = self::get(self::$module_routes[$route]); unset($matches['route']); $mod->route($matches); } else { Logger::logError('Route did not match known module. Fatal error'); return Logger::http_error(500); } Logger::stopLevel(); }
/** * Retrieve a template file using a string and a directory. * * What template file gets loaded depends on the template engine that is being used. * PHP for example uses .php files. Providing this function with 'home/dashboard' will load the home/view.dashboard.php file. * You can also provide no particular engine, and the manager will decide what template to load. * Remember that doing so will result in a LayoutException when multiple compatible files are found. * * @param string $file File to load * @param string $directory Directory to load it from * * @return string The output of the template * * @throws LayoutException On error */ public static function get($file, $directory = null) { $directory = is_null($directory) ? self::$directory : $directory; Logger::newLevel("Loading template file '" . $file . "' in '" . $directory . "'"); // First load the template engines self::loadTemplateEngines(); // First retrieve the filepath if (is_null(self::$current_engine)) { self::setFileFromString($file, $directory, array_keys(self::$file_extensions)); } else { self::setFileFromString($file, $directory, self::$current_engine->getFileExtensions()); } // Then assign some basic variables for the template self::$assigned_variables['wwwDir'] = Config::get('main')->base_url; self::$assigned_variables['siteURL'] = Config::get('main')->base_url; self::$assigned_variables['serverName'] = Config::get('main')->server_name; self::$assigned_variables['adminMail'] = Config::get('main')->administrator_mail; self::$assigned_variables['contact'] = Config::get('contact')->toArray(); // Select an engine if one is not already selected if (is_null(self::$current_engine)) { self::$current_engine = self::getEngineFromExtension(self::getExtensionFromFile(self::$file)); } self::$current_engine->setDirectory(self::$directory); // And run an Event to see what other parts have to say about it $event = Events::fireEvent('layoutLoadViewEvent', self::$file, self::$directory, self::$current_engine, self::$assigned_variables); // The event has been cancelled if ($event->isCancelled()) { return false; } // And refetch the data from the event self::$current_engine = $event->engine; self::$assigned_variables = $event->assigned_variables; Logger::stopLevel(); // And finally run it if (file_exists($event->file)) { return self::$current_engine->get($event->file, self::$assigned_variables); } throw new LayoutException('The requested file was not found', 1); }
/** * Class constructor * * @param array $params Configuration parameters * @return void */ public function __construct(array $params = array()) { Logger::newLevel('Initializing Encryption Library'); $this->_drivers = array('mcrypt' => defined('MCRYPT_DEV_URANDOM'), 'openssl' => Core::isPHP('5.3.3') && extension_loaded('openssl')); if (!$this->_drivers['mcrypt'] && !$this->_drivers['openssl']) { throw new LibraryException('Encryption: Unable to find an available encryption driver.', 1); } isset(self::$func_override) or self::$func_override = extension_loaded('mbstring') && ini_get('mbstring.func_override'); $this->initialize($params); if (!isset($this->_key) && self::strlen($key = Config::get('encryption')->encryption_key) > 0) { $this->_key = $key; } Logger::log('Encryption Class Initialized'); Logger::stopLevel(); }