Guest User

Untitled

a guest
Jun 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. Index: core/model/SQLMap.php
  2. ===================================================================
  3. --- core/model/SQLMap.php (revision 77921)
  4. +++ core/model/SQLMap.php (working copy)
  5. @@ -40,8 +40,7 @@
  6. $this->query->where[] = "$baseTable.ID = $id";
  7. $record = $this->query->execute()->first();
  8. if($record) {
  9. - $className = $record['ClassName'];
  10. - $obj = new $className($record);
  11. + $obj = DataObject::data_factory()->instantiateDataObject($record);
  12. return $obj->Title;
  13. }
  14. }
  15. Index: core/model/DataObject.php
  16. ===================================================================
  17. --- core/model/DataObject.php (revision 77921)
  18. +++ core/model/DataObject.php (working copy)
  19. @@ -459,6 +459,39 @@
  20. return _t($this->class.'.PLURALNAME', $name);
  21. }
  22.  
  23. +
  24. + /**
  25. + * Gets the {@link DataFactory} used to create DataObject records.
  26. + * By default returns a {@link SimpleDataFactory}, but this can be replaced by calling
  27. + * {@link set_data_factory()}
  28. + *
  29. + * EXPERIMENTAL - This may change wildly before the release of 2.4; call from outside
  30. + * DataObject at your own risk!
  31. + */
  32. + static function data_factory() {
  33. + if(!self::$data_factory) self::$data_factory = new SimpleDataFactory();
  34. + return self::$data_factory;
  35. + }
  36. +
  37. + /**
  38. + * Sets the {@link DataFactory} used to create DataObject records.
  39. + * Call this to replace the {@link SimpleDataFactory} with something more sophisticated,
  40. + * for example, an identity map.
  41. + *
  42. + * EXPERIMENTAL - This may change wildly before the release of 2.4; call from outside
  43. + * DataObject at your own risk!
  44. + */
  45. + static function set_data_factory(DataFactory $factory) {
  46. + self::$data_factory = $factory;
  47. + }
  48. +
  49. + /**
  50. + * The data factory.
  51. + * @see data_factory()
  52. + * @see set_data_factory()
  53. + */
  54. + private static $data_factory = null;
  55. +
  56. /**
  57. * Standard implementation of a title/label for a specific
  58. * record. Tries to find properties 'Title' or 'Name',
  59. @@ -2477,15 +2510,9 @@
  60. * @return mixed The new objects in an object of type $containerClass
  61. */
  62. function buildDataObjectSet($records, $containerClass = "DataObjectSet", $query = null, $baseClass = null) {
  63. + $factory = self::data_factory();
  64. foreach($records as $record) {
  65. - if(empty($record['RecordClassName'])) {
  66. - $record['RecordClassName'] = $record['ClassName'];
  67. - }
  68. - if(class_exists($record['RecordClassName'])) {
  69. - $results[] = new $record['RecordClassName']($record);
  70. - } else {
  71. - $results[] = new $baseClass($record);
  72. - }
  73. + $results[] = $factory->instantiateDataObject($record, $baseClass);
  74. }
  75.  
  76. if(isset($results)) {
  77. @@ -2584,17 +2611,7 @@
  78. $record = $records->current();
  79.  
  80. if($record) {
  81. - // Mid-upgrade, the database can have invalid RecordClassName values that need to be guarded against.
  82. - if(class_exists($record['RecordClassName'])) {
  83. - $record = new $record['RecordClassName']($record);
  84. - } else {
  85. - $record = new $this->class($record);
  86. - }
  87. -
  88. - // Rather than restrict classes at the SQL-query level, we now check once the object has been instantiated
  89. - // This lets us check up on weird errors where the class has been incorrectly set, and give warnings to our
  90. - // developers
  91. - return $record;
  92. + return self::data_factory()->instantiateDataObject($record, $this->class);
  93. }
  94. }
  95.  
  96. Index: core/model/DataFactory.php
  97. ===================================================================
  98. --- core/model/DataFactory.php (revision 0)
  99. +++ core/model/DataFactory.php (revision 0)
  100. @@ -0,0 +1,24 @@
  101. +<?php
  102. +
  103. +/**
  104. + * DataFactory defines an interface that the ORM can use to instantiate {@Link DataObject}
  105. + * objects. The interface is intended to provide a place where an identity map can be
  106. + * incorporated into Sapphire.
  107. + *
  108. + * EXPERIMENTAL - This may change wildly before the release of 2.4; call from outside
  109. + * DataObject at your own risk!
  110. + */
  111. +interface DataFactory {
  112. + /**
  113. + * Instantiate a DataObject from the given database row.
  114. + *
  115. + * @param $databaseRow A map of the database values. It is expected that either ClassName
  116. + * or RecordClassName will be included in the map
  117. + * @param $defaultClass If ClassName or RecordClassName can't be found or doesn't contain an
  118. + * appropriate value, then use this class. It is optional, but instantiateDataObject() will
  119. + * throw an error if it's needed and hasn't been provided.
  120. + *
  121. + * @return DataObject The relevant DataObject object.
  122. + */
  123. + function instantiateDataObject($databaseRow, $defaultClass = null);
  124. +}
  125. Index: core/model/SimpleDataFactory.php
  126. ===================================================================
  127. --- core/model/SimpleDataFactory.php (revision 0)
  128. +++ core/model/SimpleDataFactory.php (revision 0)
  129. @@ -0,0 +1,27 @@
  130. +<?php
  131. +
  132. +/**
  133. + * Simple DataFactory that instantiates a new object for every single call. No identity mpa
  134. + * exists.
  135. + *
  136. + * EXPERIMENTAL - This may change wildly before the release of 2.4; call from outside
  137. + * DataObject at your own risk!
  138. + */
  139. +class SimpleDataFactory implements DataFactory {
  140. + /**
  141. + * Instantiate a DataObject from the given database row.
  142. + */
  143. + function instantiateDataObject($databaseRow, $defaultClass = null) {
  144. + if(empty($databaseRow['RecordClassName'])) {
  145. + $databaseRow['RecordClassName'] = $databaseRow['ClassName'];
  146. + }
  147. + if(class_exists($databaseRow['RecordClassName'])) {
  148. + return new $databaseRow['RecordClassName']($databaseRow);
  149. + } else if($defaultClass) {
  150. + return new $defaultClass($databaseRow);
  151. + } else {
  152. + user_error("SimpleDataFactory::instantiateDataObject(): Couldn't get class from " .
  153. + "database row and no default class given", E_USER_ERROR);
  154. + }
  155. + }
  156. +}
  157. \ No newline at end of file
  158. Index: search/SearchForm.php
  159. ===================================================================
  160. --- search/SearchForm.php (revision 77921)
  161. +++ search/SearchForm.php (working copy)
  162. @@ -231,8 +231,9 @@
  163. // Get records
  164. $records = DB::query($fullQuery);
  165.  
  166. - foreach($records as $record)
  167. - $objects[] = new $record['ClassName']($record);
  168. + foreach($records as $record) {
  169. + $objects[] = DataObject::data_factory()->instantiateDataObject($record);
  170. + }
  171.  
  172. if(isset($objects)) $doSet = new DataObjectSet($objects);
  173. else $doSet = new DataObjectSet();
  174. Index: search/SearchContext.php
  175. ===================================================================
  176. --- search/SearchContext.php (revision 77921)
  177. +++ search/SearchContext.php (working copy)
  178. @@ -162,8 +162,7 @@
  179. // use if a raw SQL query is needed
  180. $results = new DataObjectSet();
  181. foreach($query->execute() as $row) {
  182. - $className = $row['RecordClassName'];
  183. - $results->push(new $className($row));
  184. + $results->push(DataObject::data_factory()->instantiateDataObject($row));
  185. }
  186. return $results;
  187. //
Add Comment
Please, Sign In to add comment