sfData.class.php 3.2 KB
Newer Older
Игорь's avatar
init    
Игорь committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * This class defines the interface for interacting with data, as well
 * as default implementations.
 *
 * @package    symfony
 * @subpackage addon
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfData.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
abstract class sfData
{
  protected
    $deleteCurrentData = true,
    $object_references = array();

  /**
   * Sets a flag to indicate if the current data in the database
   * should be deleted before new data is loaded.
   *
   * @param boolean $boolean The flag value
   */
  public function setDeleteCurrentData($boolean)
  {
    $this->deleteCurrentData = $boolean;
  }

  /**
   * Gets the current value of the flag that indicates whether
   * current data is to be deleted or not.
   *
   * @return boolean
   */
  public function getDeleteCurrentData()
  {
    return $this->deleteCurrentData;
  }

  /**
   * Loads data for the database from a YAML file
   *
   * @param string $file The path to the YAML file.
   */
  protected function doLoadDataFromFile($file)
  {
    // import new datas
    $data = sfYaml::load($file);

    $this->loadDataFromArray($data);
  }

  /**
   * Manages the insertion of data into the data source
   *
   * @param array $data The data to be inserted into the data source
   */
  abstract public function loadDataFromArray($data);

  /**
   * Manages reading all of the fixture data files and
   * loading them into the data source
   *
   * @param array $files The path names of the YAML data files
   */
  protected function doLoadData(array $files)
  {
    $this->object_references = array();
    $this->maps = array();

    foreach ($files as $file)
    {
      $this->doLoadDataFromFile($file);
    }
  }

  /**
   * Gets a list of one or more *.yml files and returns the list in an array.
   *
   * The returned array of files is sorted by alphabetical order.
   *
   * @param string|array $element A directory or file name or an array of directories and/or file names
   *                              If null, then defaults to 'sf_data_dir'/fixtures
   *
   * @return array A list of *.yml files
   *
   * @throws sfInitializationException If the directory or file does not exist.
   */
  public function getFiles($element = null)
  {
    if (null === $element)
    {
      $element = sfConfig::get('sf_data_dir').'/fixtures';
    }

    $files = array();
    if (is_array($element))
    {
      foreach ($element as $e)
      {
        $files = array_merge($files, $this->getFiles($e));
      }
    }
    else if (is_file($element))
    {
      $files[] = $element;
    }
    else if (is_dir($element))
    {
      $files = sfFinder::type('file')->name('*.yml')->sort_by_name()->in($element);
    }
    else
    {
      throw new sfInitializationException(sprintf('You must give an array, a directory or a file to sfData::getFiles() (%s given).', $element));
    }

    $files = array_unique($files);
    sort($files);

    return $files;
  }
}