sfData.class.php 3.2 KB

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