public function cacheRoutes() { TBGCache::fileAdd(TBGCache::KEY_ROUTES_CACHE, $this->getRoutes()); TBGCache::add(TBGCache::KEY_ROUTES_CACHE, $this->getRoutes()); }
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); } }
/** * Cache all permissions */ public static function cacheAllPermissions() { TBGLogging::log('caches permissions'); self::$_permissions = array(); if ($permissions = TBGCache::get('permissions')) { self::$_permissions = $permissions; TBGLogging::log('Using cached permissions'); } else { if (!($permissions = TBGCache::fileGet(TBGCache::KEY_PERMISSIONS_CACHE))) { TBGLogging::log('starting to cache access permissions'); if ($res = B2DB::getTable('TBGPermissionsTable')->getAll()) { while ($row = $res->getNextRow()) { if (!array_key_exists($row->get(TBGPermissionsTable::MODULE), self::$_permissions)) { self::$_permissions[$row->get(TBGPermissionsTable::MODULE)] = array(); } if (!array_key_exists($row->get(TBGPermissionsTable::PERMISSION_TYPE), self::$_permissions[$row->get(TBGPermissionsTable::MODULE)])) { self::$_permissions[$row->get(TBGPermissionsTable::MODULE)][$row->get(TBGPermissionsTable::PERMISSION_TYPE)] = array(); } if (!array_key_exists($row->get(TBGPermissionsTable::TARGET_ID), self::$_permissions[$row->get(TBGPermissionsTable::MODULE)][$row->get(TBGPermissionsTable::PERMISSION_TYPE)])) { self::$_permissions[$row->get(TBGPermissionsTable::MODULE)][$row->get(TBGPermissionsTable::PERMISSION_TYPE)][$row->get(TBGPermissionsTable::TARGET_ID)] = array(); } self::$_permissions[$row->get(TBGPermissionsTable::MODULE)][$row->get(TBGPermissionsTable::PERMISSION_TYPE)][$row->get(TBGPermissionsTable::TARGET_ID)][] = array('uid' => $row->get(TBGPermissionsTable::UID), 'gid' => $row->get(TBGPermissionsTable::GID), 'tid' => $row->get(TBGPermissionsTable::TID), 'allowed' => (bool) $row->get(TBGPermissionsTable::ALLOWED)); } } TBGLogging::log('done (starting to cache access permissions)'); TBGCache::fileAdd(TBGCache::KEY_PERMISSIONS_CACHE, self::$_permissions); } else { self::$_permissions = $permissions; } TBGCache::add('permissions', self::$_permissions); } TBGLogging::log('...cached'); }