sfData.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 $fixture_file The path to the YAML file.
  47. */
  48. protected function doLoadDataFromFile($fixture_file)
  49. {
  50. // import new datas
  51. $data = sfYaml::load($fixture_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 $fixture_files The path names of the YAML data files
  65. */
  66. protected function doLoadData($fixture_files)
  67. {
  68. $this->object_references = array();
  69. $this->maps = array();
  70. sort($fixture_files);
  71. foreach ($fixture_files as $fixture_file)
  72. {
  73. $this->doLoadDataFromFile($fixture_file);
  74. }
  75. }
  76. /**
  77. * Gets a list of one or more *.yml files and returns the list in an array
  78. *
  79. * @param string $directory_or_file A directory or file name; if null, then defaults to 'sf_data_dir'/fixtures
  80. *
  81. * @returns array A list of *.yml files.
  82. *
  83. * @throws sfInitializationException If the directory or file does not exist.
  84. */
  85. protected function getFiles($directory_or_file = null)
  86. {
  87. // directory or file?
  88. $fixture_files = array();
  89. if (!$directory_or_file)
  90. {
  91. $directory_or_file = sfConfig::get('sf_data_dir').'/fixtures';
  92. }
  93. if (is_file($directory_or_file))
  94. {
  95. $fixture_files[] = $directory_or_file;
  96. }
  97. else if (is_dir($directory_or_file))
  98. {
  99. $fixture_files = sfFinder::type('file')->name('*.yml')->sort_by_name()->in($directory_or_file);
  100. }
  101. else
  102. {
  103. throw new sfInitializationException('You must give a directory or a file.');
  104. }
  105. return $fixture_files;
  106. }
  107. }