PageRenderTime 71ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/rokcommon/Doctrine/Data/Import.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 402 lines | 233 code | 45 blank | 124 comment | 52 complexity | 12583698ca45ae668589b1fd3dc52b9f MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Import.php 48519 2012-02-03 23:18:52Z btowles $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. /**
  22. * Doctrine_Data_Import
  23. *
  24. * @package Doctrine
  25. * @package Data
  26. * @author Jonathan H. Wage <jwage@mac.com>
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.doctrine-project.org
  29. * @since 1.0
  30. * @version $Revision: 2552 $
  31. */
  32. class Doctrine_Data_Import extends Doctrine_Data
  33. {
  34. /**
  35. * Array of imported objects for processing and saving
  36. *
  37. * @var array
  38. */
  39. protected $_importedObjects = array();
  40. /**
  41. * Array of the raw data parsed from yaml
  42. *
  43. * @var array
  44. */
  45. protected $_rows = array();
  46. /**
  47. * Optionally pass the directory/path to the yaml for importing
  48. *
  49. * @param string $directory
  50. * @return void
  51. */
  52. public function __construct($directory = null)
  53. {
  54. if ($directory !== null) {
  55. $this->setDirectory($directory);
  56. }
  57. }
  58. /**
  59. * Do the parsing of the yaml files and return the final parsed array
  60. *
  61. * @return array $array
  62. */
  63. public function doParsing()
  64. {
  65. $recursiveMerge = Doctrine_Manager::getInstance()->getAttribute(Doctrine_Core::ATTR_RECURSIVE_MERGE_FIXTURES);
  66. $mergeFunction = $recursiveMerge === true ? 'array_merge_recursive':'array_merge';
  67. $directory = $this->getDirectory();
  68. $array = array();
  69. if ($directory !== null) {
  70. foreach ((array) $directory as $dir) {
  71. $e = explode('.', $dir);
  72. // If they specified a specific yml file
  73. if (end($e) == 'yml') {
  74. $array = $mergeFunction($array, Doctrine_Parser::load($dir, $this->getFormat()));
  75. // If they specified a directory
  76. } else if (is_dir($dir)) {
  77. $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
  78. RecursiveIteratorIterator::LEAVES_ONLY);
  79. $filesOrdered = array();
  80. foreach ($it as $file) {
  81. $filesOrdered[] = $file;
  82. }
  83. // force correct order
  84. natcasesort($filesOrdered);
  85. foreach ($filesOrdered as $file) {
  86. $e = explode('.', $file->getFileName());
  87. if (in_array(end($e), $this->getFormats())) {
  88. $array = $mergeFunction($array, Doctrine_Parser::load($file->getPathName(), $this->getFormat()));
  89. }
  90. }
  91. }
  92. }
  93. }
  94. return $array;
  95. }
  96. /**
  97. * Do the importing of the data parsed from the fixtures
  98. *
  99. * @return void
  100. */
  101. public function doImport($append = false)
  102. {
  103. $array = $this->doParsing();
  104. if ( ! $append) {
  105. $this->purge(array_reverse(array_keys($array)));
  106. }
  107. $this->_loadData($array);
  108. }
  109. /**
  110. * Recursively loop over all data fixtures and build the array of className rows
  111. *
  112. * @return void
  113. */
  114. protected function _buildRows($className, $data)
  115. {
  116. $table = Doctrine_Core::getTable($className);
  117. foreach ($data as $rowKey => $row) {
  118. // do the same for the row information
  119. $this->_rows[$className][$rowKey] = $row;
  120. foreach ((array) $row as $key => $value) {
  121. if ($table->hasRelation($key) && is_array($value) && ! $table->hasTemplate('Doctrine_Template_I18n')) {
  122. // Skip associative arrays defining keys to relationships
  123. if ( ! isset($value[0]) || (isset($value[0]) && is_array($value[0]))) {
  124. $rel = $table->getRelation($key);
  125. $relClassName = $rel->getTable()->getOption('name');
  126. $relRowKey = $rowKey . '_' . $relClassName;
  127. if ($rel->getType() == Doctrine_Relation::ONE) {
  128. $val = array($relRowKey => $value);
  129. $this->_rows[$className][$rowKey][$key] = $relRowKey;
  130. } else {
  131. $val = $value;
  132. $this->_rows[$className][$rowKey][$key] = array_keys($val);
  133. }
  134. $this->_buildRows($relClassName, $val);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * Build the rows for nested set models
  142. *
  143. * @return void
  144. */
  145. protected function _buildNestedSetRows($className, $data)
  146. {
  147. foreach ($data as $rowKey => $row) {
  148. $children = isset($row['children']) ? $row['children']:array();
  149. unset($row['children']);
  150. $this->_rows[$className][$rowKey] = $row;
  151. $this->_buildNestedSetRows($className, $children);
  152. }
  153. }
  154. /**
  155. * Get the unsaved object for a specified row key and validate that it is the valid object class
  156. * for the passed record and relation name
  157. *
  158. * @param string $rowKey
  159. * @param Doctrine_Record $record
  160. * @param string $relationName
  161. * @param string $referringRowKey
  162. * @return Doctrine_Record
  163. * @throws Doctrine_Data_Exception
  164. */
  165. protected function _getImportedObject($rowKey, Doctrine_Record $record, $relationName, $referringRowKey)
  166. {
  167. $relation = $record->getTable()->getRelation($relationName);
  168. $rowKey = $this->_getRowKeyPrefix($relation->getTable()) . $rowKey;
  169. if ( ! isset($this->_importedObjects[$rowKey])) {
  170. throw new Doctrine_Data_Exception(
  171. sprintf('Invalid row key specified: %s, referred to in %s', $rowKey, $referringRowKey)
  172. );
  173. }
  174. $relatedRowKeyObject = $this->_importedObjects[$rowKey];
  175. $relationClass = $relation->getClass();
  176. if ( ! $relatedRowKeyObject instanceof $relationClass) {
  177. throw new Doctrine_Data_Exception(sprintf(
  178. 'Class referred to in "%s" is expected to be "%s" and "%s" was given',
  179. $referringRowKey, $relation->getClass(), get_class($relatedRowKeyObject)
  180. ));
  181. }
  182. return $relatedRowKeyObject;
  183. }
  184. /**
  185. * Process a row and make all the appropriate relations between the imported data
  186. *
  187. * @param string $rowKey
  188. * @param string $row
  189. * @return void
  190. */
  191. protected function _processRow($rowKey, $row)
  192. {
  193. $obj = $this->_importedObjects[$rowKey];
  194. foreach ((array) $row as $key => $value) {
  195. if (method_exists($obj, 'set' . Doctrine_Inflector::classify($key))) {
  196. $func = 'set' . Doctrine_Inflector::classify($key);
  197. $obj->$func($value);
  198. } else if ($obj->getTable()->hasField($key)) {
  199. if ($obj->getTable()->getTypeOf($key) == 'object') {
  200. $value = unserialize($value);
  201. }
  202. $obj->set($key, $value);
  203. } else if ($obj->getTable()->hasRelation($key)) {
  204. if (is_array($value)) {
  205. if (isset($value[0]) && ! is_array($value[0])) {
  206. foreach ($value as $link) {
  207. if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::ONE) {
  208. $obj->set($key, $this->_getImportedObject($link, $obj, $key, $rowKey));
  209. } else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) {
  210. $relation = $obj->$key;
  211. $relation[] = $this->_getImportedObject($link, $obj, $key, $rowKey);
  212. }
  213. }
  214. } else {
  215. $obj->$key->fromArray($value);
  216. }
  217. } else {
  218. $obj->set($key, $this->_getImportedObject($value, $obj, $key, $rowKey));
  219. }
  220. } else {
  221. try {
  222. $obj->$key = $value;
  223. } catch (Exception $e) {
  224. // used for Doctrine plugin methods (Doctrine_Template)
  225. if (is_callable(array($obj, 'set' . Doctrine_Inflector::classify($key)))) {
  226. $func = 'set' . Doctrine_Inflector::classify($key);
  227. $obj->$func($value);
  228. } else {
  229. throw new Doctrine_Data_Exception('Invalid fixture element "'. $key . '" under "' . $rowKey . '"');
  230. }
  231. }
  232. }
  233. }
  234. }
  235. /**
  236. * NestedSet fixtures may come in a 'natural' format with nested children listed under a 'children'
  237. * key or in a raw, non-nested format with lft/rgt values.
  238. *
  239. * This method returns true if the given $data is a nested set in 'natural' form.
  240. *
  241. * @param $className
  242. * @param $data
  243. * @return boolean
  244. */
  245. protected function _hasNaturalNestedSetFormat($className, array &$data)
  246. {
  247. if (Doctrine_Core::getTable($className)->isTree()) {
  248. if (isset($data['NestedSet']) && $data['NestedSet'] == true) {
  249. unset($data['NestedSet']);
  250. return true;
  251. } else {
  252. $first = current($data);
  253. return array_key_exists('children', $first);
  254. }
  255. } else {
  256. return false;
  257. }
  258. }
  259. /**
  260. * Perform the loading of the data from the passed array
  261. *
  262. * @param string $array
  263. * @return void
  264. */
  265. protected function _loadData(array $array)
  266. {
  267. $nestedSets = array();
  268. $specifiedModels = $this->getModels();
  269. $rows = array();
  270. foreach ($array as $className => $data) {
  271. if ( ! empty($specifiedModels) && !in_array($className, $specifiedModels)) {
  272. continue;
  273. }
  274. // if loaded data is a nested set in natural format, process through _buildNestedSetRows.
  275. // 'raw' nested sets and all other models are processed through _buildRows.
  276. if ($this->_hasNaturalNestedSetFormat($className, $data)) {
  277. $nestedSets[$className][] = $data;
  278. $this->_buildNestedSetRows($className, $data);
  279. } else {
  280. $this->_buildRows($className, $data);
  281. }
  282. }
  283. $buildRows = array();
  284. foreach ($this->_rows as $className => $classRows) {
  285. $rowKeyPrefix = $this->_getRowKeyPrefix(Doctrine_Core::getTable($className));
  286. foreach ($classRows as $rowKey => $row) {
  287. $rowKey = $rowKeyPrefix . $rowKey;
  288. $buildRows[$rowKey] = $row;
  289. $this->_importedObjects[$rowKey] = new $className();
  290. $this->_importedObjects[$rowKey]->state('TDIRTY');
  291. }
  292. }
  293. foreach($buildRows as $rowKey => $row) {
  294. $this->_processRow($rowKey, $row);
  295. }
  296. // save natural nested set fixture data and unset from _importedObjects
  297. foreach ($nestedSets as $className => $sets) {
  298. foreach ($sets as $data) {
  299. $this->_loadNestedSetData($className, $data);
  300. }
  301. }
  302. $manager = Doctrine_Manager::getInstance();
  303. foreach ($manager as $connection) {
  304. $tree = $connection->unitOfWork->buildFlushTree(array_keys($array));
  305. foreach ($tree as $model) {
  306. foreach ($this->_importedObjects as $obj) {
  307. if ($obj instanceof $model) {
  308. $obj->save();
  309. }
  310. }
  311. }
  312. }
  313. }
  314. /**
  315. * Load nested set data for models with nested set enabled
  316. *
  317. * @param string $model
  318. * @param string $nestedSetData
  319. * @param string $parent
  320. * @return void
  321. */
  322. protected function _loadNestedSetData($model, $nestedSetData, $parent = null)
  323. {
  324. foreach($nestedSetData AS $rowKey => $nestedSet) {
  325. $children = array();
  326. $data = array();
  327. if (array_key_exists('children', $nestedSet)) {
  328. $children = (array) $nestedSet['children'];
  329. $children = array_reverse($children, true);
  330. unset($nestedSet['children']);
  331. }
  332. $rowKey = $this->_getRowKeyPrefix(Doctrine_Core::getTable($model)) . $rowKey;
  333. $record = $this->_importedObjects[$rowKey];
  334. // remove this nested set from _importedObjects so it's not processed in the save routine for normal objects
  335. unset($this->_importedObjects[$rowKey]);
  336. if ( ! $parent) {
  337. $record->save(); // save, so that createRoot can do: root id = id
  338. Doctrine_Core::getTable($model)->getTree()->createRoot($record);
  339. } else {
  340. $parent->getNode()->addChild($record);
  341. }
  342. if (is_array($children) AND !empty($children)) {
  343. $this->_loadNestedSetData($model, $children, $record);
  344. }
  345. }
  346. }
  347. /**
  348. * Returns the prefix to use when indexing an object from the supplied table.
  349. *
  350. * @param Doctrine_Table $table
  351. * @return string
  352. */
  353. protected function _getRowKeyPrefix(Doctrine_Table $table)
  354. {
  355. return sprintf('(%s) ', $table->getTableName());
  356. }
  357. }