PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Data.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 279 lines | 89 code | 26 blank | 164 comment | 4 complexity | dbc0715f45419efecafae56cd20f32fc MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Data.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.doctrine-project.org>.
  20. */
  21. /**
  22. * Doctrine_Data
  23. *
  24. * Base Doctrine_Data class for dumping and loading data to and from fixtures files.
  25. * Support formats are based on what formats are available in Doctrine_Parser such as yaml, xml, json, etc.
  26. *
  27. * @package Doctrine
  28. * @subpackage Data
  29. * @author Jonathan H. Wage <jwage@mac.com>
  30. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  31. * @link www.doctrine-project.org
  32. * @since 1.0
  33. * @version $Revision: 2552 $
  34. */
  35. class Doctrine_Data
  36. {
  37. /**
  38. * formats
  39. *
  40. * array of formats data can be in
  41. *
  42. * @var string
  43. */
  44. protected $_formats = array('csv', 'yml', 'xml');
  45. /**
  46. * format
  47. *
  48. * the default and current format we are working with
  49. *
  50. * @var string
  51. */
  52. protected $_format = 'yml';
  53. /**
  54. * directory
  55. *
  56. * array of directory/yml paths or single directory/yml file
  57. *
  58. * @var string
  59. */
  60. protected $_directory = null;
  61. /**
  62. * models
  63. *
  64. * specified array of models to use
  65. *
  66. * @var string
  67. */
  68. protected $_models = array();
  69. /**
  70. * _exportIndividualFiles
  71. *
  72. * whether or not to export data to individual files instead of 1
  73. *
  74. * @var string
  75. */
  76. protected $_exportIndividualFiles = false;
  77. /**
  78. * setFormat
  79. *
  80. * Set the current format we are working with
  81. *
  82. * @param string $format
  83. * @return void
  84. */
  85. public function setFormat($format)
  86. {
  87. $this->_format = $format;
  88. }
  89. /**
  90. * getFormat
  91. *
  92. * Get the current format we are working with
  93. *
  94. * @return void
  95. */
  96. public function getFormat()
  97. {
  98. return $this->_format;
  99. }
  100. /**
  101. * getFormats
  102. *
  103. * Get array of available formats
  104. *
  105. * @return void
  106. */
  107. public function getFormats()
  108. {
  109. return $this->_formats;
  110. }
  111. /**
  112. * setDirectory
  113. *
  114. * Set the array/string of directories or yml file paths
  115. *
  116. * @return void
  117. */
  118. public function setDirectory($directory)
  119. {
  120. $this->_directory = $directory;
  121. }
  122. /**
  123. * getDirectory
  124. *
  125. * Get directory for dumping/loading data from and to
  126. *
  127. * @return void
  128. */
  129. public function getDirectory()
  130. {
  131. return $this->_directory;
  132. }
  133. /**
  134. * setModels
  135. *
  136. * Set the array of specified models to work with
  137. *
  138. * @param string $models
  139. * @return void
  140. */
  141. public function setModels($models)
  142. {
  143. $this->_models = $models;
  144. }
  145. /**
  146. * getModels
  147. *
  148. * Get the array of specified models to work with
  149. *
  150. * @return void
  151. */
  152. public function getModels()
  153. {
  154. return $this->_models;
  155. }
  156. /**
  157. * _exportIndividualFiles
  158. *
  159. * Set/Get whether or not to export individual files
  160. *
  161. * @return bool $_exportIndividualFiles
  162. */
  163. public function exportIndividualFiles($bool = null)
  164. {
  165. if ($bool !== null) {
  166. $this->_exportIndividualFiles = $bool;
  167. }
  168. return $this->_exportIndividualFiles;
  169. }
  170. /**
  171. * exportData
  172. *
  173. * Interface for exporting data to fixtures files from Doctrine models
  174. *
  175. * @param string $directory
  176. * @param string $format
  177. * @param string $models
  178. * @param string $_exportIndividualFiles
  179. * @return void
  180. */
  181. public function exportData($directory, $format = 'yml', $models = array(), $_exportIndividualFiles = false)
  182. {
  183. $export = new Doctrine_Data_Export($directory);
  184. $export->setFormat($format);
  185. $export->setModels($models);
  186. $export->exportIndividualFiles($_exportIndividualFiles);
  187. return $export->doExport();
  188. }
  189. /**
  190. * importData
  191. *
  192. * Interface for importing data from fixture files to Doctrine models
  193. *
  194. * @param string $directory
  195. * @param string $format
  196. * @param string $models
  197. * @return void
  198. */
  199. public function importData($directory, $format = 'yml', $models = array(), $append = false)
  200. {
  201. $import = new Doctrine_Data_Import($directory);
  202. $import->setFormat($format);
  203. $import->setModels($models);
  204. return $import->doImport($append);
  205. }
  206. /**
  207. * isRelation
  208. *
  209. * Check if a fieldName on a Doctrine_Record is a relation, if it is we return that relationData
  210. *
  211. * @param string $Doctrine_Record
  212. * @param string $fieldName
  213. * @return void
  214. */
  215. public function isRelation(Doctrine_Record $record, $fieldName)
  216. {
  217. $relations = $record->getTable()->getRelations();
  218. foreach ($relations as $relation) {
  219. $relationData = $relation->toArray();
  220. if ($relationData['local'] === $fieldName) {
  221. return $relationData;
  222. }
  223. }
  224. return false;
  225. }
  226. /**
  227. * purge
  228. *
  229. * Purge all data for loaded models or for the passed array of Doctrine_Records
  230. *
  231. * @param string $models
  232. * @return void
  233. */
  234. public function purge($models = null)
  235. {
  236. if ($models) {
  237. $models = Doctrine_Core::filterInvalidModels($models);
  238. } else {
  239. $models = Doctrine_Core::getLoadedModels();
  240. }
  241. $connections = array();
  242. foreach ($models as $model) {
  243. $connections[Doctrine_Core::getTable($model)->getConnection()->getName()][] = $model;
  244. }
  245. foreach ($connections as $connection => $models) {
  246. $models = Doctrine_Manager::getInstance()->getConnection($connection)->unitOfWork->buildFlushTree($models);
  247. $models = array_reverse($models);
  248. foreach ($models as $model) {
  249. Doctrine_Core::getTable($model)->createQuery()->delete()->execute();
  250. }
  251. }
  252. }
  253. }