1. sfData.class.php
  2. /** * This class defines the interface for interacting with data, as well * as default implementations. * * @package symfony * @subpackage addon * @author Fabien Potencier * @version SVN: $Id: sfData.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. abstract class sfData
  4. {
  5. protected
  6. $deleteCurrentData = true,
  7. $object_references = array();
  8. /**
  9. * Sets a flag to indicate if the current data in the database
  10. * should be deleted before new data is loaded.
  11. *
  12. * @param boolean $boolean The flag value
  13. */
  14. public function setDeleteCurrentData($boolean)
  15. {
  16. $this->deleteCurrentData = $boolean;
  17. }
  18. /**
  19. * Gets the current value of the flag that indicates whether
  20. * current data is to be deleted or not.
  21. *
  22. * @return boolean
  23. */
  24. public function getDeleteCurrentData()
  25. {
  26. return $this->deleteCurrentData;
  27. }
  28. /**
  29. * Loads data for the database from a YAML file
  30. *
  31. * @param string $file The path to the YAML file.
  32. */
  33. protected function doLoadDataFromFile($file)
  34. {
  35. // import new datas
  36. $data = sfYaml::load($file);
  37. $this->loadDataFromArray($data);
  38. }
  39. /**
  40. * Manages the insertion of data into the data source
  41. *
  42. * @param array $data The data to be inserted into the data source
  43. */
  44. abstract public function loadDataFromArray($data);
  45. /**
  46. * Manages reading all of the fixture data files and
  47. * loading them into the data source
  48. *
  49. * @param array $files The path names of the YAML data files
  50. */
  51. protected function doLoadData(array $files)
  52. {
  53. $this->object_references = array();
  54. $this->maps = array();
  55. foreach ($files as $file)
  56. {
  57. $this->doLoadDataFromFile($file);
  58. }
  59. }
  60. /**
  61. * Gets a list of one or more *.yml files and returns the list in an array.
  62. *
  63. * The returned array of files is sorted by alphabetical order.
  64. *
  65. * @param string|array $element A directory or file name or an array of directories and/or file names
  66. * If null, then defaults to 'sf_data_dir'/fixtures
  67. *
  68. * @return array A list of *.yml files
  69. *
  70. * @throws sfInitializationException If the directory or file does not exist.
  71. */
  72. public function getFiles($element = null)
  73. {
  74. if (null === $element)
  75. {
  76. $element = sfConfig::get('sf_data_dir').'/fixtures';
  77. }
  78. $files = array();
  79. if (is_array($element))
  80. {
  81. foreach ($element as $e)
  82. {
  83. $files = array_merge($files, $this->getFiles($e));
  84. }
  85. }
  86. else if (is_file($element))
  87. {
  88. $files[] = $element;
  89. }
  90. else if (is_dir($element))
  91. {
  92. $files = sfFinder::type('file')->name('*.yml')->sort_by_name()->in($element);
  93. }
  94. else
  95. {
  96. throw new sfInitializationException(sprintf('You must give an array, a directory or a file to sfData::getFiles() (%s given).', $element));
  97. }
  98. $files = array_unique($files);
  99. sort($files);
  100. return $files;
  101. }
  102. }

Debug toolbar