StorageFactory.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. Lib::loadClass('Config');
  3. Lib::loadClass('Core_StorageBase');
  4. // TODO: Lib::loadClass('Core_StorageObject');
  5. class Core_StorageFactory {
  6. public static function getStorage($storageId = null) {
  7. static $_instance;
  8. if (!is_array($_instance)) {
  9. $_instance = array();
  10. }
  11. $dbConfName = 'default_db';
  12. if (is_numeric($storageId) && $storageId > 0) {
  13. $dbConfName = "zasob_{$storageId}";
  14. } else if ($storageId == 'import_db') {
  15. $dbConfName = "import_db";
  16. } else if ($storageId == 'test_db') {
  17. $dbConfName = "test_db";
  18. } else if ($storageId == 'billing_db') {
  19. $dbConfName = "billing_db";
  20. }
  21. if (!array_key_exists($dbConfName, $_instance)) {
  22. $_instance[$dbConfName] = null;
  23. Lib::loadClass('Config');
  24. $conf = Config::getConfFile($dbConfName);
  25. if (!$conf) {
  26. throw new Exception("Config for {$dbConfName} not found");
  27. }
  28. $params = array();
  29. $params['type'] = V::get('type', 'mysql', $conf);
  30. $params['host'] = V::get('host', '', $conf);
  31. $params['port'] = V::get('port', '', $conf);
  32. $params['user'] = V::get('user', '', $conf);
  33. $params['pass'] = V::get('pass', '', $conf);
  34. $params['zasob_id'] = V::get('zasob_id', '', $conf);
  35. $params['database'] = V::get('database', '', $conf);
  36. $params['tdsver'] = V::get('tdsver', '', $conf);
  37. $params['names'] = 'utf8';
  38. if (empty($params['type'])) {
  39. throw new Exception("Storage config error: type not defined");
  40. }
  41. $storageClassName = 'Core_Storage_' . ucfirst($params['type']);
  42. Lib::loadClass($storageClassName);
  43. if (class_exists($storageClassName)) {
  44. $_instance[$dbConfName] = new $storageClassName($params);
  45. }
  46. }
  47. return $_instance[$dbConfName];
  48. }
  49. public static function getObjectSchema($objectUri) {
  50. if (false === strpos($objectUri, ':')) {
  51. throw new Exception("Wrong uri '{$objectUri}'");
  52. }
  53. $uriParts = explode(':', $objectUri);
  54. if (count($uriParts) != 2) throw new Exception("Wrong object uri");
  55. $objectName = array_pop($uriParts);
  56. require_once APP_PATH_SCHEMA . DS . 'objectsFromXsd.php';
  57. $objectClassName = "SchemaObject_{$objectName}";
  58. if (!class_exists($objectClassName)) {
  59. throw new Exception("Object definition not exists '{$objectName}'.");
  60. }
  61. $storageObject = new $objectClassName("p5:{$objectName}");
  62. return $storageObject;
  63. }
  64. public static function getObject($objectName) {
  65. require_once APP_PATH_SCHEMA . DS . 'objectsFromXsd.php';
  66. $objectClassName = "SchemaObject_{$objectName}";
  67. if (!class_exists($objectClassName)) {
  68. throw new Exception("Object definition not exists '{$objectName}'.");
  69. }
  70. $storageObject = null;// TODO: new Core_StorageObject("p5:{$objectName}");
  71. return $storageObject;
  72. }
  73. /**
  74. * @param string $objectUri
  75. * examples:
  76. * 'default_db:TEST_PERMS' - xsd from main storage
  77. * 'p5_913:TEST_PERMS' - xsd from storage id 913
  78. * 'Projekt' - main xsd file
  79. */
  80. public static function getObjectFromStructure($objectUri) {
  81. if (false !== strpos($objectUri, ':')) {
  82. $uriParts = explode(':', $objectUri, 2);
  83. if (count($uriParts) != 2) throw new Exception("Wrong object uri");
  84. $storageName = array_shift($uriParts);
  85. $tableName = array_shift($uriParts);
  86. $storage = Core_StorageFactory::getStorage($storageName);
  87. $storageZasobId = $storage->getZasobId();
  88. $storageObject = new Core_StorageObject("{$storageZasobId}/{$tableName}");
  89. $tableStruct = $storage->getTableStruct($tableName);
  90. $pkFieldName = null;
  91. Lib::loadClass('Core_StorageField');
  92. foreach ($tableStruct as $fldName => $vFldType) {
  93. $vField = new Core_StorageField("{$storageZasobId}/{$tableName}/{$fldName}");
  94. $vField->setName($fldName);
  95. $storageObject->setField($vField);
  96. if ($vFldType->isPrimaryKey()) {
  97. $pkFieldName = $fldName;
  98. }
  99. }
  100. if ($pkFieldName) {
  101. $storageObject->setPrimaryKeyFieldName($pkFieldName);
  102. }
  103. } else {
  104. throw new Exception('TODO: getObject from main schema');
  105. }
  106. return $storageObject;
  107. }
  108. public static function getType($type) {
  109. $storageClassName = 'Core_StorageType_' . ucfirst($type);
  110. Lib::loadClass($storageClassName);
  111. return new $storageClassName();
  112. }
  113. public function getTypeByUri($uri) {
  114. $uriParts = explode('/', $uri);
  115. $storageId = $uriParts[0];
  116. $tableName = $uriParts[1];
  117. $fieldName = $uriParts[2];
  118. $storage = Core_StorageFactory::getStorage($storageId);
  119. $tableStruct = $storage->getTableStruct($tableName);
  120. $storageType = (array_key_exists($fieldName, $tableStruct)) ? $tableStruct[$fieldName] : null;
  121. return $storageType;
  122. }
  123. /**
  124. * Cache - write
  125. */
  126. public static function serializeTableStructure($tableStructure) {
  127. $tableStructureRaw = array();
  128. $tableStructureRaw['_classMap'] = array();
  129. $tableStructureRaw['_rawData'] = array();
  130. foreach ($tableStructure as $fldName => $fldType) {
  131. $tableStructureRaw['_rawData'][$fldName] = $fldType->getRawData();
  132. $tableStructureRaw['_classMap'][$fldName] = get_class($fldType);
  133. }
  134. return $tableStructureRaw;
  135. }
  136. /**
  137. * Cache - read
  138. */
  139. public static function unserializeTableStructure($tableStructureRaw) {
  140. $tableStructure = array();
  141. if (empty($tableStructureRaw['_classMap'])) {
  142. //throw new Exception('Wrong structure in cache');
  143. return;
  144. }
  145. foreach ($tableStructureRaw['_classMap'] as $fldName => $fldClassName) {
  146. Lib::loadClass($fldClassName);
  147. $fldType = new $fldClassName();
  148. $fldType->rebuildFromRawData($tableStructureRaw['_rawData'][$fldName]);
  149. $tableStructure[$fldName] = $fldType;
  150. }
  151. return $tableStructure;
  152. }
  153. }