Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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