| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- Lib::loadClass('Config');
- Lib::loadClass('Core_StorageBase');
- // TODO: Lib::loadClass('Core_StorageObject');
- class Core_StorageFactory {
- public static function getStorage($storageId = null) {
- static $_instance;
- if (!is_array($_instance)) {
- $_instance = array();
- }
- $dbConfName = 'default_db';
- if (is_numeric($storageId) && $storageId > 0) {
- $dbConfName = "zasob_{$storageId}";
- } else if ($storageId == 'import_db') {
- $dbConfName = "import_db";
- } else if ($storageId == 'test_db') {
- $dbConfName = "test_db";
- } else if ($storageId == 'billing_db') {
- $dbConfName = "billing_db";
- }
- if (!array_key_exists($dbConfName, $_instance)) {
- $_instance[$dbConfName] = null;
- Lib::loadClass('Config');
- $conf = Config::getConfFile($dbConfName);
- if (!$conf) {
- throw new Exception("Config for {$dbConfName} not found");
- }
- $params = array();
- $params['type'] = V::get('type', 'mysql', $conf);
- $params['host'] = V::get('host', '', $conf);
- $params['port'] = V::get('port', '', $conf);
- $params['user'] = V::get('user', '', $conf);
- $params['pass'] = V::get('pass', '', $conf);
- $params['zasob_id'] = V::get('zasob_id', '', $conf);
- $params['database'] = V::get('database', '', $conf);
- $params['tdsver'] = V::get('tdsver', '', $conf);
- $params['names'] = 'utf8';
- if (empty($params['type'])) {
- throw new Exception("Storage config error: type not defined");
- }
- $storageClassName = 'Core_Storage_' . ucfirst($params['type']);
- Lib::loadClass($storageClassName);
- if (class_exists($storageClassName)) {
- $_instance[$dbConfName] = new $storageClassName($params);
- }
- }
- return $_instance[$dbConfName];
- }
- public static function getObjectSchema($objectUri) {
- if (false === strpos($objectUri, ':')) {
- throw new Exception("Wrong uri '{$objectUri}'");
- }
- $uriParts = explode(':', $objectUri);
- if (count($uriParts) != 2) throw new Exception("Wrong object uri");
- $objectName = array_pop($uriParts);
- require_once APP_PATH_SCHEMA . DS . 'objectsFromXsd.php';
- $objectClassName = "SchemaObject_{$objectName}";
- if (!class_exists($objectClassName)) {
- throw new Exception("Object definition not exists '{$objectName}'.");
- }
- $storageObject = new $objectClassName("p5:{$objectName}");
- return $storageObject;
- }
- public static function getObject($objectName) {
- require_once APP_PATH_SCHEMA . DS . 'objectsFromXsd.php';
- $objectClassName = "SchemaObject_{$objectName}";
- if (!class_exists($objectClassName)) {
- throw new Exception("Object definition not exists '{$objectName}'.");
- }
- $storageObject = null;// TODO: new Core_StorageObject("p5:{$objectName}");
- return $storageObject;
- }
- /**
- * @param string $objectUri
- * examples:
- * 'default_db:TEST_PERMS' - xsd from main storage
- * 'p5_913:TEST_PERMS' - xsd from storage id 913
- * 'Projekt' - main xsd file
- */
- public static function getObjectFromStructure($objectUri) {
- if (false !== strpos($objectUri, ':')) {
- $uriParts = explode(':', $objectUri, 2);
- if (count($uriParts) != 2) throw new Exception("Wrong object uri");
- $storageName = array_shift($uriParts);
- $tableName = array_shift($uriParts);
- $storage = Core_StorageFactory::getStorage($storageName);
- $storageZasobId = $storage->getZasobId();
- $storageObject = new Core_StorageObject("{$storageZasobId}/{$tableName}");
- $tableStruct = $storage->getTableStruct($tableName);
- $pkFieldName = null;
- Lib::loadClass('Core_StorageField');
- foreach ($tableStruct as $fldName => $vFldType) {
- $vField = new Core_StorageField("{$storageZasobId}/{$tableName}/{$fldName}");
- $vField->setName($fldName);
- $storageObject->setField($vField);
- if ($vFldType->isPrimaryKey()) {
- $pkFieldName = $fldName;
- }
- }
- if ($pkFieldName) {
- $storageObject->setPrimaryKeyFieldName($pkFieldName);
- }
- } else {
- throw new Exception('TODO: getObject from main schema');
- }
- return $storageObject;
- }
- public static function getType($type) {
- $storageClassName = 'Core_StorageType_' . ucfirst($type);
- Lib::loadClass($storageClassName);
- return new $storageClassName();
- }
- public function getTypeByUri($uri) {
- $uriParts = explode('/', $uri);
- $storageId = $uriParts[0];
- $tableName = $uriParts[1];
- $fieldName = $uriParts[2];
- $storage = Core_StorageFactory::getStorage($storageId);
- $tableStruct = $storage->getTableStruct($tableName);
- $storageType = (array_key_exists($fieldName, $tableStruct)) ? $tableStruct[$fieldName] : null;
- return $storageType;
- }
- /**
- * Cache - write
- */
- public static function serializeTableStructure($tableStructure) {
- $tableStructureRaw = array();
- $tableStructureRaw['_classMap'] = array();
- $tableStructureRaw['_rawData'] = array();
- foreach ($tableStructure as $fldName => $fldType) {
- $tableStructureRaw['_rawData'][$fldName] = $fldType->getRawData();
- $tableStructureRaw['_classMap'][$fldName] = get_class($fldType);
- }
- return $tableStructureRaw;
- }
- /**
- * Cache - read
- */
- public static function unserializeTableStructure($tableStructureRaw) {
- $tableStructure = array();
- if (empty($tableStructureRaw['_classMap'])) {
- //throw new Exception('Wrong structure in cache');
- return;
- }
- foreach ($tableStructureRaw['_classMap'] as $fldName => $fldClassName) {
- Lib::loadClass($fldClassName);
- $fldType = new $fldClassName();
- $fldType->rebuildFromRawData($tableStructureRaw['_rawData'][$fldName]);
- $tableStructure[$fldName] = $fldType;
- }
- return $tableStructure;
- }
- }
|