Exemplo n.º 1
0
 /**
  * Register the bar and register the event which will block the screen log
  */
 public static function register()
 {
     $class = new self();
     Events::addListener(array($class, 'screenLogEventListener'), 'screenLogEvent', EventPriority::NORMAL);
     $bar = Debugger::getBar();
     $bar->addPanel($class);
 }
Exemplo n.º 2
0
 /**
  * Get a model.
  * 
  * Supply the name and the model will be loaded from the supplied directory,
  * or from one of the modelPaths (which you can add).
  * 
  * @param string        $modelName  Name of the model
  * @param string|null   $directory  Directory to load the model from, will ignore $modelPaths
  * @return ModelAbstract            The Model object
  */
 public function get($modelName, $directory = null)
 {
     if (empty($modelName)) {
         throw new ModelException("Could not load model. No name provided", 1);
     }
     // First get the directories where the model can be located
     $directories = is_null($directory) ? $this->modelPaths : array($directory);
     // Fire a model load event
     $event = Events::fireEvent('modelLoadEvent', $modelName, $directories);
     $directories = $event->directories;
     $modelName = $event->modelName;
     // If the event is cancelled, stop loading
     if ($event->isCancelled()) {
         return false;
     }
     // And attempt to load the model
     return $this->loadModel($modelName, $directories);
 }
Exemplo n.º 3
0
 /**
  * Load a helper.
  * 
  * Supply the name and the helper will be loaded from the supplied directory,
  * or from one of the helperPaths (which you can add).
  * 
  * @param string        $helperName Name of the helper
  * @param string|null   $directory  Directory to load the helper from, will ignore $helperPaths
  * @return bool                     Whether the helper was succesfully loaded (true if yes)
  */
 public function load($helperName, $directory = null)
 {
     // First determine the name of the helper
     $helperName = strtolower(str_replace(array('_helper', '.php'), '', $helperName) . '_helper');
     // Determine what directories should be checked
     $directories = is_null($directory) ? $this->helperPaths : array($directory);
     // Check it is already loaded
     if (isset($this->helpers[$helperName])) {
         Logger::log("Helper '" . $helperName . "' is already loaded. Skipping");
         return false;
     }
     // First check if there is an 'extension' class
     $extendedHelper = Config::get('main')->application_prefix . $helperName;
     $extendedHelperLoaded = false;
     foreach ($directories as $helperPath) {
         $file = $helperPath . DS . $extendedHelper . '.php';
         if (file_exists($file)) {
             $extendedHelperLoaded = true;
             $extendedHelperFile = $file;
         }
     }
     // If an extension is loaded there needs to be a base helper
     if ($extendedHelperLoaded) {
         $baseHelper = Core::$coreDir . DS . 'Helpers' . DS . $helperName . '.php';
         if (!file_exists($baseHelper)) {
             throw new HelperException("Could not load helper. Base Helper not found while Extension loaded", 1);
         }
         // Fire the associated event
         $event = Events::fireEvent('helperLoadEvent', $helperName, $baseHelper, $extendedHelper, $extendedHelperFile);
         if ($event->isCancelled()) {
             Logger::log("Not loading helper. Aborted by event");
             return false;
         }
         include_once $event->extendedHelperFile;
         include_once $event->helperFile;
         $this->helpers[$event->helperName] = true;
         Logger::log("Loading base helper '" . $event->helperName . "' and extended helper '" . $event->extendedHelperName . "'");
         return true;
     }
     // If no extension exists, try loading a regular helper
     foreach ($directories as $helperPath) {
         $file = $helperPath . DS . $helperName . '.php';
         if (file_exists($file)) {
             // Fire the associated event
             $event = Events::fireEvent('helperLoadEvent', $helperName, $file);
             if ($event->isCancelled()) {
                 Logger::log("Not loading helper. Aborted by event");
                 return false;
             }
             include_once $event->helperFile;
             $this->helpers[$event->helperName] = true;
             Logger::log("Loading helper '" . $event->helperName . "'");
             return true;
         }
     }
     throw new HelperException("Could not load helper. Helper not found.", 1);
 }
Exemplo n.º 4
0
 /**
  * Output the entire log to the screen. Used for debugging problems with your code.
  * @codeCoverageIgnore
  */
 public static function logToScreen()
 {
     // Send a screenLogEvent, allows for new screen log designs
     $event = Events::fireEvent('screenLogEvent');
     if ($event->isCancelled()) {
         return false;
     }
     $logs = self::$Logs;
     require dirname(__DIR__) . '/views/view.' . self::$logger_template . '.php';
 }
Exemplo n.º 5
0
 /**
  * The default callable.
  *
  * This callable will do the 'old skool' routing. It will load the controllers from the controller-directory
  * in the application-directory.
  */
 public function defaultCallable($arguments = array())
 {
     $this->logger->log('Default callable called!');
     $controller = $arguments['controller'];
     $function = $arguments['function'];
     $parameters = empty($arguments['parameters']) ? null : $arguments['parameters'];
     // Construct file paths and classes
     $class = '\\Application\\Controller\\' . ucfirst($controller);
     $directory = Core::$appDir . DS . 'Controller';
     $file = $directory . DS . 'controller.' . $controller . '.php';
     $event = Events::fireEvent('routerLoadControllerEvent', $file, $directory, $class, $controller, $function, $parameters);
     // Cancel if requested to do so
     if ($event->isCancelled()) {
         return;
     }
     // Check if the file exists
     if (file_exists($event->file)) {
         if (!class_exists($event->className)) {
             $this->logger->log('Loading controller ' . $event->className . ' from file: ' . $event->file);
             include $event->file;
         }
         // Get the path the controller should know about
         $path = implode('/', $this->uri->rsegments);
         // And create the controller
         $this->callable = new $event->className($path);
         // If the controller does not want a function to be loaded, provide a halt parameter.
         if (isset($this->callable->halt)) {
             return;
         }
         // Check if method exists or if there is a caller function
         if (method_exists($this->callable, $event->function) || method_exists($this->callable, '__call')) {
             // Execute the function on the controller
             echo $this->callable->{$event->function}($event->parameters);
         } else {
             // Function could not be found
             $this->logger->log('Could not find function ' . $event->function . ' on controller ' . $event->className);
             $this->logger->http_error(404);
         }
     } else {
         // Controller could not be found
         $this->logger->log('Could not find controller ' . $event->className);
         $this->logger->http_error(404);
     }
 }
Exemplo n.º 6
0
 /**
  * Load the template engines by sending a layoutLoadEngineEvent.
  */
 public static function loadTemplateEngines()
 {
     if (!self::$engines_loaded) {
         Events::fireEvent('layoutLoadEngineEvent');
         // Load the engines provided in this file
         self::registerEngine(new PHPEngine(), 'PHP', array('php'));
         self::registerEngine(new JsonEngine(), 'JSON', array('json'));
         self::registerEngine(new SmartyEngine(), 'Smarty', array('tpl'));
         self::registerEngine(new LatteEngine(), 'Latte', array('latte'));
         self::$engines_loaded = true;
     }
 }
Exemplo n.º 7
0
 /**
  * Create a register with all the module headers from all the existing modules.
  *
  * Used to correctly load all modules
  * @param   bool    $cache          true if loading from cache
  * @param   string  $cachingMethod  the driver used in the caching library
  * @param   int     $cachingTime    The time the registers are cached
  */
 public static function buildRegister($cache = false, $cachingMethod = 'file', $cachingTime = 300)
 {
     // First check if the caching engine can be used
     if ($cache) {
         // Retrieve the cache if possible
         $cache = Factory::getInstance()->libraries->getDriver('cache');
         $cacheData = $cache->{$cachingMethod}->get('moduleRegisters');
         if (!is_bool($cacheData)) {
             // Separate the data
             $moduleRegister = $cacheData['moduleRegister'];
             $eventRegister = $cacheData['eventRegister'];
             $routeRegister = $cacheData['routeRegister'];
             $advertiseRegister = $cacheData['advertiseRegister'];
             // And register it all
             Logger::newLevel("Loadig Module Headers from Cache");
             self::$register = $moduleRegister;
             Events::$register = $eventRegister;
             self::$advertiseRegister = $advertiseRegister;
             self::$module_routes = $routeRegister;
             foreach ($routeRegister as $route => $name) {
                 Factory::getInstance()->router->addRoute($route, array('callable' => array('\\FuzeWorks\\Modules', 'moduleCallable')), true);
             }
             Logger::stopLevel();
             return true;
         }
     }
     Logger::newLevel('Loading Module Headers', 'Core');
     // Get all the module directories
     $dir = Core::$appDir . DS . 'Modules/';
     $mod_dirs = array();
     $mod_dirs = array_values(array_diff(scandir($dir), array('..', '.')));
     // Build the module and event register
     $register = array();
     $event_register = array();
     // Cycle through all module directories
     for ($i = 0; $i < count($mod_dirs); ++$i) {
         $mod_dir = $dir . $mod_dirs[$i] . '/';
         // If a moduleInfo.php exists, load it
         if (file_exists($mod_dir . '/moduleInfo.php')) {
             // Load the configuration file
             $cfg = (object) (include $mod_dir . '/moduleInfo.php');
             // Set enabled for now
             $enabled = true;
             // Define the module name
             $name = '';
             $name .= !empty($cfg->author) ? strtolower($cfg->author) . '/' : '';
             $name .= strtolower($cfg->module_name);
             // Get the module directory
             $cfg->directory = $mod_dir;
             // Check whether the module is disabled
             if (isset($cfg->enabled)) {
                 if (!$cfg->enabled) {
                     // If disabled, set the variable
                     $enabled = false;
                     // If disabled, a holder will be placed so it might be enabled in the future
                     $mock = new StdClass();
                     $mock->module_name = $cfg->module_name;
                     $mock->directory = $cfg->directory;
                     $mock->meta = $cfg;
                     $mock->aliases = $cfg->aliases;
                     // Important, change the configuration to the mock, so we can duplicate it afterwards
                     $cfg = $mock;
                 }
             }
             // Copy all the data into the register and enable
             $register[$name] = (array) $cfg;
             // Log the name for enabled and disabled
             if (!$enabled) {
                 Logger::newLevel("[OFF] '" . $name . "'");
             } else {
                 Logger::newLevel("[ON]  '" . $name . "'");
             }
             // And possibly some aliases
             if (isset($cfg->aliases)) {
                 foreach ($cfg->aliases as $alias) {
                     $register[$alias] = (array) $cfg;
                     unset($register[$alias]['events']);
                     Logger::log("&nbsp;&nbsp;&nbsp;'" . $alias . "' (alias of '" . $name . "')");
                 }
             }
             // If not enabled, log it, wrap it and off to the next one
             if (!$enabled) {
                 Logger::stopLevel();
                 continue;
             }
             // Otherwise continue and add routing paths
             if (isset($cfg->routes)) {
                 // Get routes and add them
                 foreach ($cfg->routes as $route) {
                     // Create the route and callable and parse them
                     $callable = array('\\FuzeWorks\\Modules', 'moduleCallable');
                     Factory::getInstance()->router->addRoute($route, array('callable' => $callable), true);
                     self::$module_routes[$route] = $name;
                 }
             }
             // And for the events
             if (isset($cfg->events)) {
                 // Get the events and add them
                 foreach ($cfg->events as $event) {
                     // First check if the event already exists, if so, append it
                     if (isset($event_register[$event])) {
                         $event_register[$event][] = $name;
                     } else {
                         $event_register[$event] = array($name);
                     }
                     // Log the event
                     Logger::Log('Event added: \'' . $event . '\'');
                 }
             }
             // And check for an advertisement tag
             if (isset($cfg->advertise)) {
                 // Cycle through advertisements
                 foreach ($cfg->advertise as $advertiseName => $advertiseData) {
                     // Log advertisement
                     Logger::log('Advertisement added: \'' . $advertiseName . '\'');
                     // Add to advertiseRegister
                     self::$advertiseRegister[$advertiseName][$name] = $advertiseData;
                 }
             }
             Logger::stopLevel();
         } else {
             // If no details are specified, create a basic mock module
             $name = $mod_dirs[$i];
             // Build a default mock module config
             $mock = new stdClass();
             $mock->module_class = ucfirst($name);
             $mock->module_file = 'class.' . strtolower($name) . '.php';
             $mock->module_name = $name;
             $mock->dependencies = array();
             $mock->versions = array();
             $mock->directory = $mod_dir;
             // Apply it
             $register[$name] = (array) $mock;
             Logger::newLevel("[ON]  '" . $name . "'");
             Logger::stopLevel();
         }
     }
     if ($cache) {
         Logger::log("Saving registry to cache");
         $cacheData = array('moduleRegister' => $register, 'eventRegister' => $event_register, 'routeRegister' => self::$module_routes, 'advertiseRegister' => self::$advertiseRegister);
         $cache->{$cachingMethod}->save('moduleRegisters', $cacheData, $cachingTime);
     }
     // And apply the registers to their dedicate location
     self::$register = $register;
     Events::$register = $event_register;
     Logger::stopLevel();
 }
Exemplo n.º 8
0
 /**
  * Stop FuzeWorks and run all shutdown functions.
  *
  * Afterwards run the Logger shutdown function in order to possibly display the log
  */
 public static function shutdown()
 {
     // Fix Apache bug where CWD is changed upon shutdown
     chdir(self::$cwd);
     // Fire the Shutdown event
     $event = Events::fireEvent('coreShutdownEvent');
     if ($event->isCancelled() === false) {
         // If the output should be displayed, send the final render and parse the logger
         Logger::shutdownError();
         Factory::getInstance()->output->_display();
         Logger::shutdown();
     }
 }