StorageFactory.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. Lib::loadClass('Config');
  3. Lib::loadClass('Core_StorageBase');
  4. Lib::loadClass('Core_StorageField');
  5. Lib::loadClass('Core_StorageObject');
  6. class Core_StorageFactory {
  7. public static function getStorage($storageId = null) {
  8. static $_instance;
  9. if (!is_array($_instance)) {
  10. $_instance = array();
  11. }
  12. $dbConfName = 'default_db';
  13. if (is_numeric($storageId) && $storageId > 0) {
  14. $dbConfName = "zasob_{$storageId}";
  15. } else if ($storageId == 'import_db') {
  16. $dbConfName = "import_db";
  17. } else if ($storageId == 'test_db') {
  18. $dbConfName = "test_db";
  19. } else if ($storageId == 'billing_db') {
  20. $dbConfName = "billing_db";
  21. }
  22. if (!array_key_exists($dbConfName, $_instance)) {
  23. $_instance[$dbConfName] = null;
  24. Lib::loadClass('Config');
  25. $conf = Config::getConfFile($dbConfName);
  26. if (!$conf) {
  27. throw new Exception("Config for {$dbConfName} not found");
  28. }
  29. $params = array();
  30. $params['type'] = V::get('type', 'mysql', $conf);
  31. $params['host'] = V::get('host', '', $conf);
  32. $params['port'] = V::get('port', '', $conf);
  33. $params['user'] = V::get('user', '', $conf);
  34. $params['pass'] = V::get('pass', '', $conf);
  35. $params['zasob_id'] = V::get('zasob_id', '', $conf);
  36. $params['database'] = V::get('database', '', $conf);
  37. $params['tdsver'] = V::get('tdsver', '', $conf);
  38. $params['names'] = 'utf8';
  39. if (empty($params['type'])) {
  40. throw new Exception("Storage config error: type not defined");
  41. }
  42. $storageClassName = 'Core_Storage_' . ucfirst($params['type']);
  43. Lib::loadClass($storageClassName);
  44. if (class_exists($storageClassName)) {
  45. $_instance[$dbConfName] = new $storageClassName($params);
  46. }
  47. }
  48. return $_instance[$dbConfName];
  49. }
  50. public static function getObjectSchema($objectUri) {
  51. if (false === strpos($objectUri, ':')) {
  52. throw new Exception("Wrong uri '{$objectUri}'");
  53. }
  54. $uriParts = explode(':', $objectUri);
  55. if (count($uriParts) != 2) throw new Exception("Wrong object uri");
  56. $objectName = array_pop($uriParts);
  57. require_once APP_PATH_SCHEMA . DS . 'objectsFromXsd.php';
  58. $objectClassName = "SchemaObject_{$objectName}";
  59. if (!class_exists($objectClassName)) {
  60. throw new Exception("Object definition not exists '{$objectName}'.");
  61. }
  62. $storageObject = new $objectClassName("p5:{$objectName}");
  63. return $storageObject;
  64. }
  65. public static function getObject($objectName) {
  66. require_once APP_PATH_SCHEMA . DS . 'objectsFromXsd.php';
  67. $objectClassName = "SchemaObject_{$objectName}";
  68. if (!class_exists($objectClassName)) {
  69. throw new Exception("Object definition not exists '{$objectName}'.");
  70. }
  71. $storageObject = new Core_StorageObject("p5:{$objectName}");
  72. return $storageObject;
  73. }
  74. /**
  75. * @param string $objectUri
  76. * examples:
  77. * 'default_db:TEST_PERMS' - xsd from main storage
  78. * 'p5_913:TEST_PERMS' - xsd from storage id 913
  79. * 'Projekt' - main xsd file
  80. */
  81. public static function getObjectFromStructure($objectUri) {
  82. if (false !== strpos($objectUri, ':')) {
  83. $uriParts = explode(':', $objectUri, 2);
  84. if (count($uriParts) != 2) throw new Exception("Wrong object uri");
  85. $storageName = array_shift($uriParts);
  86. $tableName = array_shift($uriParts);
  87. $storage = Core_StorageFactory::getStorage($storageName);
  88. $storageZasobId = $storage->getZasobId();
  89. $storageObject = new Core_StorageObject("{$storageZasobId}/{$tableName}");
  90. $tableStruct = $storage->getTableStruct($tableName);
  91. $pkFieldName = null;
  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. }