Esempio n. 1
0
 /**
  * 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;
 }
Esempio n. 2
0
 /**
  * Initializes the core.
  *
  * @throws \Exception
  */
 public static function init()
 {
     // Set the CWD for usage in the shutdown function
     self::$cwd = getcwd();
     // Set the core dir for when the loading of classes is required
     self::$coreDir = dirname(__DIR__);
     // If the environment is not yet defined, use production settings
     if (!defined('ENVIRONMENT')) {
         define('ENVIRONMENT', 'PRODUCTION');
     }
     // Defines the time the framework starts. Used for timing functions in the framework
     if (!defined('STARTTIME')) {
         define('STARTTIME', microtime(true));
         define('DS', DIRECTORY_SEPARATOR);
     }
     // Load basics
     ignore_user_abort(true);
     register_shutdown_function(array('\\FuzeWorks\\Core', 'shutdown'));
     // Load core functionality
     new Factory();
     // Load the config file of the FuzeWorks core
     $config = Config::get('core');
     // Disable events if requested to do so
     if (!$config->enable_events) {
         Events::disable();
     }
     // Build all the registers for correct operation, if modules are enabled
     if ($config->enable_modules) {
         Modules::buildRegister($config->registry_caching, $config->registry_caching_method, $config->registry_caching_time);
     }
     // And fire the coreStartEvent
     $event = Events::fireEvent('coreStartEvent');
     if ($event->isCancelled()) {
         return true;
     }
     // And initialize multiple classes
     Layout::init();
     Language::init();
 }