Esempio n. 1
0
 /**
  *	Main method :)
  *	Creates and initialize set of objects for proper storage functioning. Accepts hash of following parameters:
  *
  *		Name			Type					Default			Description
  *		----			----					-------			-----------
  *		table			string									Table name
  *		props			hash<prop:db>							Object properies and DB mappings. See RM_Store_Object_Metadata for details
  *		keys			list|hash				'id'			Keys. See RM_Store_Object_Metadata for details. Sample: qw2('id name>parent_id+name')
  *		auto-id-prop 	string					NULL			Name of field to fill with AUTO_INCREMENT value. If NULL - AUTO_INCREMENT is disabled
  *		guid			string					NULL			Name of GUID type to auto alloc ID. If NULL - GUID is disabled.
  *																Also passed as "guidType" parameter to object constructor.
  *		guid-prop		string					'id'			Name of GUID property. Used only if guid is not NULL
  *		guid-type-prop	string					'guid_type'		Name of GUID-TYPE property. Used only if guid is not NULL
  *																Passed as "guidTypeProp" parameter to object constructor.
  *		class			string					RM_Store_Object	Class which will be instantiated for every object in storage.
  *																If class is RM_Store_Object and GUID is enabled -
  *																RM_Store_Object_Guid is used
  *		cache-prefix	string					''				Cache prefix for caching loadById() requests.
  *																If empty(default), caching is disabled
  *		validator		RM_Validator_iValidator	NULL			Validator for every created object
  *		factory			RM_Store_iFactory|string NULL			Factory for creating objects. Default is RM_Store_Factory_Hash. See it doc for details.
  *																You can pass either object or class name. Class constructor must be compatible with
  *																RM_Store_Factory_Hash constructor.
  *		no-lazy-load	bool					FALSE			Always fetch all object fields in queries for objects sets
  *		ctorArgs		hash					NULL			Extra arguments to pass to object constructor.
  *																For every item, property with same name AND PREFIXED
  *																WITH UNDERSCORE! (_prop) will created!
  *																WARNING! Properties with such names must exist in class.
  *
  *	All parameters without default values ARE REQUIRED!
  *
  *	@param		args	hash<name:value>	Configuration
  *	@return		RM_Store_Object_Storage
  */
 public function storage($args)
 {
     $mediator = new RM_Store_Object_Mediator();
     # Meta data
     $mediator->setMeta(new RM_Store_Object_Metadata($args['props'], lor(@$args['keys'], qw2('id'))));
     # IdentityMap
     $mediator->setIdentityMap($this->_noCache ? new RM_Store_IdentityMap_Fake() : new RM_Store_IdentityMap_Simple($mediator));
     # Validator
     if (@$args['validator']) {
         $mediator->setValidator($args['validator']);
     }
     # Factory
     if (@$args['guid']) {
         $args['ctorArgs']['guidType'] = $args['guid'];
         $args['ctorArgs']['guidTypeProp'] = @$args['guid-type-prop'];
         if (@$args['class'] == '') {
             $args['class'] = 'RM_Store_Object_Guid';
         }
     }
     if (!@$args['factory']) {
         $args['factory'] = 'RM_Store_Factory_Hash';
     }
     if (!is_object($args['factory'])) {
         $args['factory'] = new $args['factory']($mediator, lor(@$args['class'], 'RM_Store_Object'), @$args['ctorArgs']);
     }
     if (@$args['guid']) {
         $args['factory'] = $this->factoryGuid($args['factory'], @$args['guid'], lor(@$args['guid-prop'], 'id'));
     }
     $mediator->setFactory($args['factory']);
     # Storage
     $mediator->setStorage(new RM_Store_Object_Storage($mediator, array('table' => $args['table'], 'table-history' => @$args['table-history'] ? $args['table-history'] : null, 'auto-id-prop' => @$args['auto-id-prop'], 'cache-prefix' => $this->_noIdCache ? '' : @$args['cache-prefix'], 'no-lazy-load' => lor(@$args['no-lazy-load'], $this->_noLazyLoad))));
     return $mediator->storage;
 }