PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Data/Import.php

http://opac-sbweb.googlecode.com/
PHP | 352 lines | 194 code | 41 blank | 117 comment | 40 complexity | 43e0f1a4f65cef274b7f78d729432a3a MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * $Id: Import.php 2552 2007-09-19 19:33:00Z Jonathan.Wage $
  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.phpdoctrine.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.phpdoctrine.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('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. foreach ($it as $file) {
  80. $e = explode('.', $file->getFileName());
  81. if (in_array(end($e), $this->getFormats())) {
  82. $array = $mergeFunction($array, Doctrine_Parser::load($file->getPathName(), $this->getFormat()));
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return $array;
  89. }
  90. /**
  91. * Do the importing of the data parsed from the fixtures
  92. *
  93. * @return void
  94. */
  95. public function doImport($append = false)
  96. {
  97. $array = $this->doParsing();
  98. if ( ! $append) {
  99. $this->purge(array_reverse(array_keys($array)));
  100. }
  101. $this->_loadData($array);
  102. }
  103. /**
  104. * Recursively loop over all data fixtures and build the array of className rows
  105. *
  106. * @return void
  107. */
  108. protected function _buildRows($className, $data)
  109. {
  110. foreach ($data as $rowKey => $row) {
  111. // do the same for the row information
  112. $this->_rows[$className][$rowKey] = $row;
  113. foreach ((array) $row as $key => $value) {
  114. if (Doctrine::getTable($className)->hasRelation($key) && is_array($value)) {
  115. $keys = array_keys($value);
  116. // Skip associative arrays defining keys to relationships
  117. if ( ! isset($keys[0])) {
  118. $this->_buildRows(Doctrine::getTable($className)->getRelation($key)->getTable()->getOption('name'), $value);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * Build the rows for nested set models
  126. *
  127. * @return void
  128. */
  129. protected function _buildNestedSetRows($className, $data)
  130. {
  131. foreach ($data as $rowKey => $row) {
  132. $children = isset($row['children']) ? $row['children']:array();
  133. unset($row['children']);
  134. $this->_rows[$className][$rowKey] = $row;
  135. $this->_buildNestedSetRows($className, $children);
  136. }
  137. }
  138. /**
  139. * Get the unsaved object for a specified row key and validate that it is the valid object class
  140. * for the passed record and relation name
  141. *
  142. * @param string $rowKey
  143. * @param Doctrine_Record $record
  144. * @param string $relationName
  145. * @param string $referringRowKey
  146. * @return Doctrine_Record
  147. * @throws Doctrine_Data_Exception
  148. */
  149. protected function _getImportedObject($rowKey, Doctrine_Record $record, $relationName, $referringRowKey)
  150. {
  151. if ( ! isset($this->_importedObjects[$rowKey])) {
  152. throw new Doctrine_Data_Exception('Invalid row key specified: ' . $rowKey);
  153. }
  154. $relatedRowKeyObject = $this->_importedObjects[$rowKey];
  155. $relation = $record->getTable()->getRelation($relationName);
  156. if ($relation->getClass() !== get_class($relatedRowKeyObject)) {
  157. if ( ! is_subclass_of($relatedRowKeyObject, $relation->getClass())) {
  158. throw new Doctrine_Data_Exception(sprintf(
  159. 'Class referred to in "%s" is expected to be "%s" and "%s" was given',
  160. $referringRowKey, $relation->getClass(), get_class($relatedRowKeyObject)));
  161. }
  162. }
  163. return $relatedRowKeyObject;
  164. }
  165. /**
  166. * Process a row and make all the appropriate relations between the imported data
  167. *
  168. * @param string $rowKey
  169. * @param string $row
  170. * @return void
  171. */
  172. protected function _processRow($rowKey, $row)
  173. {
  174. $obj = $this->_importedObjects[$rowKey];
  175. foreach ((array) $row as $key => $value) {
  176. if (method_exists($obj, 'set' . Doctrine_Inflector::classify($key))) {
  177. $func = 'set' . Doctrine_Inflector::classify($key);
  178. $obj->$func($value);
  179. } else if ($obj->getTable()->hasField($key)) {
  180. $obj->set($key, $value);
  181. } else if ($obj->getTable()->hasRelation($key)) {
  182. if (is_array($value)) {
  183. if (isset($value[0]) && ! is_array($value[0])) {
  184. foreach ($value as $link) {
  185. if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::ONE) {
  186. $obj->set($key, $this->_getImportedObject($link, $obj, $key, $rowKey));
  187. } else if ($obj->getTable()->getRelation($key)->getType() === Doctrine_Relation::MANY) {
  188. $relation = $obj->$key;
  189. $relation[] = $this->_getImportedObject($link, $obj, $key, $rowKey);
  190. }
  191. }
  192. } else {
  193. $obj->$key->fromArray($value);
  194. }
  195. } else {
  196. $obj->set($key, $this->_getImportedObject($value, $obj, $key, $rowKey));
  197. }
  198. // used for Doctrine plugin methods (Doctrine_Template)
  199. } else if (is_callable(array($obj, 'set' . Doctrine_Inflector::classify($key)))) {
  200. $func = 'set' . Doctrine_Inflector::classify($key);
  201. $obj->$func($value);
  202. } else {
  203. throw new Doctrine_Data_Exception('Invalid fixture element "'. $key . '" under "' . $rowKey . '"');
  204. }
  205. }
  206. }
  207. /**
  208. * NestedSet fixtures may come in a 'natural' format with nested children listed under a 'children'
  209. * key or in a raw, non-nested format with lft/rgt values.
  210. *
  211. * This method returns true if the given $data is a nested set in 'natural' form.
  212. *
  213. * @param $className
  214. * @param $data
  215. * @return boolean
  216. */
  217. protected function _hasNaturalNestedSetFormat($className, array $data) {
  218. $first = current($data);
  219. return isset($first['children']) && Doctrine::getTable($className)->isTree();
  220. }
  221. /**
  222. * Perform the loading of the data from the passed array
  223. *
  224. * @param string $array
  225. * @return void
  226. */
  227. protected function _loadData(array $array)
  228. {
  229. $nestedSets = array();
  230. $specifiedModels = $this->getModels();
  231. $rows = array();
  232. foreach ($array as $className => $data) {
  233. if ( ! empty($specifiedModels) && !in_array($className, $specifiedModels)) {
  234. continue;
  235. }
  236. // if loaded data is a nested set in natural format, process through _buildNestedSetRows.
  237. // 'raw' nested sets and all other models are processed through _buildRows.
  238. if ($this->_hasNaturalNestedSetFormat($className, $data)) {
  239. $nestedSets[$className][] = $data;
  240. $this->_buildNestedSetRows($className, $data);
  241. } else {
  242. $this->_buildRows($className, $data);
  243. }
  244. }
  245. $buildRows = array();
  246. foreach ($this->_rows as $className => $classRows) {
  247. foreach ($classRows as $rowKey => $row) {
  248. $buildRows[$rowKey] = $row;
  249. $this->_importedObjects[$rowKey] = new $className();
  250. $this->_importedObjects[$rowKey]->state('TDIRTY');
  251. }
  252. }
  253. foreach($buildRows as $rowKey => $row) {
  254. $this->_processRow($rowKey, $row);
  255. }
  256. // save natural nested set fixture data and unset from _importedObjects
  257. foreach ($nestedSets as $className => $sets) {
  258. foreach ($sets as $data) {
  259. $this->_loadNestedSetData($className, $data);
  260. }
  261. }
  262. $objects = array();
  263. foreach ($this->_importedObjects as $object) {
  264. $className = get_class($object);
  265. $objects[$className] = $className;
  266. }
  267. $manager = Doctrine_Manager::getInstance();
  268. foreach ($manager as $connection) {
  269. $tree = $connection->unitOfWork->buildFlushTree($objects);
  270. foreach ($tree as $model) {
  271. foreach ($this->_importedObjects as $obj) {
  272. if ($obj instanceof $model) {
  273. $obj->save();
  274. }
  275. }
  276. }
  277. }
  278. }
  279. /**
  280. * Load nested set data for models with nested set enabled
  281. *
  282. * @param string $model
  283. * @param string $nestedSetData
  284. * @param string $parent
  285. * @return void
  286. */
  287. protected function _loadNestedSetData($model, $nestedSetData, $parent = null)
  288. {
  289. foreach($nestedSetData AS $rowKey => $nestedSet) {
  290. $children = array();
  291. $data = array();
  292. if (array_key_exists('children', $nestedSet)) {
  293. $children = $nestedSet['children'];
  294. $children = array_reverse($children, true);
  295. unset($nestedSet['children']);
  296. }
  297. $record = $this->_importedObjects[$rowKey];
  298. // remove this nested set from _importedObjects so it's not processed in the save routine for normal objects
  299. unset($this->_importedObjects[$rowKey]);
  300. if( ! $parent) {
  301. $record->save(); // save, so that createRoot can do: root id = id
  302. Doctrine::getTable($model)->getTree()->createRoot($record);
  303. } else {
  304. $parent->getNode()->addChild($record);
  305. }
  306. if (is_array($children) AND !empty($children)) {
  307. $this->_loadNestedSetData($model, $children, $record);
  308. }
  309. }
  310. }
  311. }