public function manufacture($classname, $id, $row = null)
 {
     // Check that the id is valid
     if ((int) $id == 0) {
         throw new Exception('Invalid id');
     }
     // Set up the name for the factory array
     $factory_array_name = "_{$classname}s";
     $item = null;
     // Set up the manufactured array if it doesn't exist
     if (!isset($this->{$factory_array_name})) {
         TBGLogging::log("Setting up manufactured array for {$classname}");
         $this->{$factory_array_name} = array();
     }
     // If the current id doesn't exist in the manufactured array, manufacture it
     if (!array_key_exists($id, $this->{$factory_array_name})) {
         // Initialize a position for the item in the manufactured array
         $this->{$factory_array_name}[$id] = null;
         try {
             // Check if the class is cacheable as well
             $cacheable = false;
             // in_array($classname, array('TBGProject', 'TBGStatus', 'TBGPriority', 'TBGCategory', 'TBGUserstate'));
             $item = null;
             // If the class is cacheable, check if it exists in the cache
             if ($cacheable) {
                 if ($item = TBGCache::get(TBGCache::KEY_TBG_FACTORY . "{$factory_array_name}_{$id}")) {
                     TBGLogging::log("Using cached {$classname} with id {$id}");
                 }
             }
             // If we didn't get an item from the cache, manufacture it
             if (!$cacheable || !is_object($item)) {
                 $item = new $classname($id, $row);
                 TBGLogging::log("Manufacturing {$classname} with id {$id}");
                 // Add the item to the cache if it's cacheable
                 if ($cacheable) {
                     TBGCache::add(TBGCache::KEY_TBG_FACTORY . "{$factory_array_name}_{$id}", $item);
                 }
             }
             // Add the manufactured item to the manufactured array
             $this->{$factory_array_name}[$id] = $item;
         } catch (Exception $e) {
             throw $e;
         }
     } else {
         TBGLogging::log("Using previously manufactured {$classname} with id {$id}");
     }
     // Return the item at that id in the manufactured array
     return $this->{$factory_array_name}[$id];
 }
 public static function getAll()
 {
     if (self::$_userstates === null) {
         if (!($states = TBGCache::get(TBGCache::KEY_USERSTATES_CACHE))) {
             $res = TBGUserStateTable::getTable()->doSelectAll();
             $states = array();
             while ($row = $res->getNextRow()) {
                 $states[$row->get(TBGUserStateTable::ID)] = TBGContext::factory()->TBGUserstate($row->get(TBGUserStateTable::ID), $row);
             }
             TBGCache::add(TBGCache::KEY_USERSTATES_CACHE, $states);
         }
         self::$_userstates = $states;
     }
     return self::$_userstates;
 }
 public static function getIssueRegex()
 {
     if (!($regex = TBGCache::get(TBGCache::KEY_TEXTPARSER_ISSUE_REGEX))) {
         $issue_strings = array('bug', 'issue', 'ticket', 'story');
         foreach (TBGIssuetype::getAll() as $issuetype) {
             $issue_strings[] = $issuetype->getName();
         }
         $issue_string = join('|', $issue_strings);
         $issue_string = html_entity_decode($issue_string, ENT_QUOTES);
         $issue_string = str_replace(array(' ', "'"), array('\\s{1,1}', "\\'"), $issue_string);
         $regex = '#( |^)(?<!\\!)((' . $issue_string . ')\\s\\#?(([A-Z0-9]+\\-)?\\d+))#i';
         TBGCache::add(TBGCache::KEY_TEXTPARSER_ISSUE_REGEX, $regex);
     }
     return $regex;
 }
Exemple #4
0
 protected static function cacheEntityClass($classname, $reflection_classname = null)
 {
     $rc_name = $reflection_classname !== null ? $reflection_classname : $classname;
     try {
         $reflection = new \ReflectionClass($rc_name);
         $annotationset = new AnnotationSet($reflection->getDocComment());
         if (!$annotationset->hasAnnotations()) {
             throw new Exception('Empty annotationset!');
         }
     } catch (\Exception $e) {
         throw new Exception("An error occured when trying to load the database details for class {$classname}: " . $e->getMessage());
     }
     if ($reflection_classname === null) {
         self::$_cached_entity_classes[$classname] = array('columns' => array(), 'relations' => array(), 'foreign_columns' => array());
         if (!($annotation = $annotationset->getAnnotation('Table'))) {
             throw new Exception("The class '{$classname}' is missing a valid @Table annotation");
         } else {
             $tablename = $annotation->getProperty('name');
         }
         if (!\class_exists($tablename)) {
             throw new Exception("The class table class '{$tablename}' for class '{$classname}' does not exist");
         }
         self::$_cached_entity_classes[$classname]['table'] = $tablename;
         self::_populateCachedTableClassFiles($tablename);
         if (($re = $reflection->getExtension()) && ($classnames = $re->getClassNames())) {
             foreach ($classnames as $extends_classname) {
                 self::cacheEntityClass($classname, $extends_classname);
             }
         }
     }
     if (!\array_key_exists('name', self::$_cached_table_classes[self::$_cached_entity_classes[$classname]['table']])) {
         throw new Exception("The class @Table annotation in '" . self::$_cached_entity_classes[$classname]['table'] . "' is missing required 'name' property");
     }
     $column_prefix = self::$_cached_table_classes[self::$_cached_entity_classes[$classname]['table']]['name'] . '.';
     foreach ($reflection->getProperties() as $property) {
         if ($property instanceof \ReflectionProperty) {
         }
         $annotationset = new AnnotationSet($property->getDocComment());
         if ($annotationset->hasAnnotations()) {
             $property_name = $property->getName();
             if ($column_annotation = $annotationset->getAnnotation('Column')) {
                 $column_name = $column_prefix . ($column_annotation->hasProperty('name') ? $column_annotation->getProperty('name') : substr($property_name, 1));
                 $column = array('property' => $property_name, 'default_value' => $column_annotation->hasProperty('default') ? $column_annotation->getProperty('default') : null, 'not_null' => $column_annotation->hasProperty('not_null') ? $column_annotation->getProperty('not_null') : false, 'name' => $column_name, 'type' => $column_annotation->getProperty('type'));
                 switch ($column['type']) {
                     case 'varchar':
                     case 'string':
                         $column['type'] = 'varchar';
                         $column['length'] = $column_annotation->hasProperty('length') ? $column_annotation->getProperty('length') : null;
                         break;
                     case 'float':
                         $column['precision'] = $column_annotation->hasProperty('precision') ? $column_annotation->getProperty('precision') : 2;
                     case 'integer':
                         $column['auto_inc'] = $column_annotation->hasProperty('auto_increment') ? $column_annotation->getProperty('auto_increment') : false;
                         $column['unsigned'] = $column_annotation->hasProperty('unsigned') ? $column_annotation->getProperty('unsigned') : false;
                         $column['length'] = $column_annotation->hasProperty('length') ? $column_annotation->getProperty('length') : 10;
                         if ($column['type'] != 'float') {
                             $column['default_value'] = $column_annotation->hasProperty('default') ? $column_annotation->getProperty('default') : 0;
                         }
                         break;
                 }
                 self::$_cached_entity_classes[$classname]['columns'][$column_name] = $column;
             }
             if ($annotation = $annotationset->getAnnotation('Id')) {
                 self::$_cached_entity_classes[$classname]['id_column'] = $column_name;
             }
             if ($annotation = $annotationset->getAnnotation('Relates')) {
                 $value = $annotation->getProperty('class');
                 $collection = (bool) $annotation->getProperty('collection');
                 $manytomany = (bool) $annotation->getProperty('manytomany');
                 $joinclass = $annotation->getProperty('joinclass');
                 $foreign_column = $annotation->getProperty('foreign_column');
                 $orderby = $annotation->getProperty('orderby');
                 $f_column = $annotation->getProperty('column');
                 self::$_cached_entity_classes[$classname]['relations'][$property_name] = array('collection' => $collection, 'property' => $property_name, 'foreign_column' => $foreign_column, 'manytomany' => $manytomany, 'joinclass' => $joinclass, 'class' => $annotation->getProperty('class'), 'column' => $f_column, 'orderby' => $orderby);
                 if (!$collection) {
                     if (!$column_annotation) {
                         throw new Exception("The property '{$property_name}' in class '{$classname}' is missing an @Column annotation, or is improperly marked as not being a collection");
                     }
                     $column_name = $column_prefix . ($annotation->hasProperty('name') ? $annotation->getProperty('name') : substr($property_name, 1));
                     $column['class'] = self::getCachedB2DBTableClass($value);
                     $column['key'] = $annotation->hasProperty('key') ? $annotation->getProperty('key') : null;
                     self::$_cached_entity_classes[$classname]['foreign_columns'][$column_name] = $column;
                 }
             }
         }
     }
     if (self::$_cache_enabled && !self::$_debug_mode) {
         $key = 'b2db_cache_' . $classname;
         \TBGCache::add($key, self::$_cached_entity_classes[$classname], false);
         \TBGCache::fileAdd($key, self::$_cached_entity_classes[$classname], false);
     }
 }
 public static function loadSettings($uid = 0)
 {
     TBGLogging::log("Loading settings");
     if (self::$_settings === null || $uid > 0 && !array_key_exists($uid, self::$_loadedsettings)) {
         TBGLogging::log('Loading settings');
         if (self::$_settings === null) {
             self::$_settings = array();
         }
         if (!TBGContext::isInstallmode() && $uid == 0 && (self::$_settings = TBGCache::get(TBGCache::KEY_SETTINGS))) {
             TBGLogging::log('Using cached settings');
         } else {
             TBGLogging::log('Settings not cached or install mode enabled. Retrieving from database');
             if ($res = B2DB::getTable('TBGSettingsTable')->getSettingsForScope(TBGContext::getScope()->getID(), $uid)) {
                 $cc = 0;
                 while ($row = $res->getNextRow()) {
                     $cc++;
                     self::$_settings[$row->get(TBGSettingsTable::MODULE)][$row->get(TBGSettingsTable::NAME)][$row->get(TBGSettingsTable::UID)] = $row->get(TBGSettingsTable::VALUE);
                 }
                 if ($cc == 0 && !TBGContext::isInstallmode() && $uid == 0) {
                     TBGLogging::log('There were no settings stored in the database!', 'main', TBGLogging::LEVEL_FATAL);
                     throw new TBGSettingsException('Could not retrieve settings from database (no settings stored)');
                 }
             } elseif (!TBGContext::isInstallmode() && $uid == 0) {
                 TBGLogging::log('Settings could not be retrieved from the database!', 'main', TBGLogging::LEVEL_FATAL);
                 throw new TBGSettingsException('Could not retrieve settings from database');
             }
             self::$_loadedsettings[$uid] = true;
             TBGLogging::log('Retrieved');
             TBGCache::add(TBGCache::KEY_SETTINGS, self::$_settings);
         }
     }
     TBGLogging::log("...done");
 }
 public function cacheRoutes()
 {
     TBGCache::fileAdd(TBGCache::KEY_ROUTES_CACHE, $this->getRoutes());
     TBGCache::add(TBGCache::KEY_ROUTES_CACHE, $this->getRoutes());
 }
 protected function loadStrings($module = null)
 {
     $strings_key = $module !== null ? 'i18n_strings' : "i18n_strings_{$module}";
     if (!($strings = TBGCache::get($strings_key))) {
         $filename = '';
         if ($module !== null) {
             if (file_exists(THEBUGGENIE_PATH . 'i18n' . DS . $this->_language . DS . "{$module}.inc.php")) {
                 $filename = THEBUGGENIE_PATH . 'i18n' . DS . $this->_language . DS . "{$module}.inc.php";
             } else {
                 $filename = THEBUGGENIE_MODULES_PATH . $module . DS . 'i18n' . DS . $this->_language . DS . "{$module}.inc.php";
             }
         } else {
             $filename = $this->getStringsFilename();
         }
         if (file_exists($filename)) {
             TBGLogging::log("Loading strings from file '{$filename}", 'i18n');
             $strings = array();
             require $filename;
             TBGCache::add($strings_key, $strings);
         } else {
             $message = 'Could not find language file ' . $filename;
             TBGLogging::log($message, 'i18n', TBGLogging::LEVEL_NOTICE);
         }
     } else {
         TBGLogging::log('Using cached strings', 'i18n');
     }
     $this->addStrings($strings);
 }
 /**
  * Returns all the links on the frontpage
  * 
  * @return array
  */
 public static function getMainLinks()
 {
     if (!($links = TBGCache::get('core_main_links'))) {
         $links = B2DB::getTable('TBGLinksTable')->getMainLinks();
         TBGCache::add('core_main_links', $links);
     }
     return $links;
 }
 /**
  * Returns all the links on the frontpage
  * 
  * @return array
  */
 public static function getMainLinks()
 {
     if (!($links = TBGCache::get(TBGCache::KEY_MAIN_MENU_LINKS))) {
         $links = TBGLinksTable::getTable()->getMainLinks();
         if (!self::isInstallmode()) {
             TBGCache::add(TBGCache::KEY_MAIN_MENU_LINKS, $links);
         }
     }
     return $links;
 }
 /** 
  * Returns an array of regular expressions that should be used for matching
  * the issue numbers and workflow transitions in a VCS commit.
  *
  * Each element of an array is a single regular expression that will be
  * applied against the incoming commit message. Each regular expression
  * should have two named patterns - one denoting the issue number (should
  * include prefix if used in project), and one denoting workflow
  * transitions.
  *
  * Simple example would be:
  *
  * '#fixes issue #(?P<issues>([A-Z0-9]+\-)?\d+) (?P<transitions> \(.*?\))?#i'
  * 
  * @return array
  */
 public static function getIssueRegex()
 {
     // Try getting the regexes from cache first.
     if (!($regex = TBGCache::get(TBGCache::KEY_TEXTPARSER_ISSUE_REGEX))) {
         // List of keywords that are expected to prefix the issue number in a
         // commit message (these are _not_ project prefixes).
         $issue_strings = array('bug', 'issue', 'ticket', 'fix', 'fixes', 'fixed', 'fixing', 'applies to', 'closes', 'references', 'ref', 'addresses', 're', 'see', 'according to', 'also see', 'story');
         // Add the issue types as prefixes as well.
         foreach (TBGIssuetype::getAll() as $issuetype) {
             $issue_strings[] = $issuetype->getName();
         }
         // Construct the OR'ed (|) regex out of issue prefixes.
         $issue_string = join('|', $issue_strings);
         $issue_string = html_entity_decode($issue_string, ENT_QUOTES);
         $issue_string = str_replace(array(' ', "'"), array('\\s{1,1}', "\\'"), $issue_string);
         // Store all regular expressions for mathces in an array.
         $regex = array();
         // This regex will match messages that contain template like "KEYWORD
         // (#)ISSUE_NUMBER (TRANSITIONS)" (parenthesis means optional). For
         // example:
         // "Resolves issue #2 (Resolve issue)"
         $regex[] = '#( |^)(?<!\\!)((' . $issue_string . ')\\s\\#?(?P<issues>([A-Z0-9]+\\-)?\\d+))( \\((?P<transitions>.*?)\\))?#i';
         // This regex will match messages that contain template at the beginning
         // of message in format "ISSUE_NUMBER: (TRANSITIONS)".
         $regex[] = '#^(?<!\\!)((?P<issues>([A-Z0-9]+\\-)?\\d+)):( \\((?P<transitions>.*?)\\))?#i';
         // Add the constructed regexes to cache.
         TBGCache::add(TBGCache::KEY_TEXTPARSER_ISSUE_REGEX, $regex);
     }
     // Return the regular expressions.
     return $regex;
 }