1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CorpSoft\Fixture; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use CorpSoft\Fixture\Exceptions\InvalidConfigException; |
7
|
|
|
use CorpSoft\Fixture\Traits\ArrayAccessTrait; |
8
|
|
|
use Countable; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* BaseActiveFixture is the base class for fixture classes that support accessing fixture data as ActiveRecord objects. |
13
|
|
|
*/ |
14
|
|
|
abstract class BaseActiveFixture extends Fixture implements IteratorAggregate, ArrayAccess, Countable |
15
|
|
|
{ |
16
|
|
|
use ArrayAccessTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string the AR model class associated with this fixture |
20
|
|
|
*/ |
21
|
|
|
public $modelClass; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array the data rows. Each array element represents one row of data (column name => column value). |
25
|
|
|
*/ |
26
|
|
|
public $data = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string the file path that contains the fixture data to be returned by [[getData()]] |
30
|
|
|
*/ |
31
|
|
|
public $dataFile; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function load(): void |
37
|
|
|
{ |
38
|
|
|
$this->data = $this->getData(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function unload(): void |
45
|
|
|
{ |
46
|
|
|
$this->data = []; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the fixture data. |
51
|
|
|
* |
52
|
|
|
* The default implementation will try to return the fixture data by including the external file specified by [[dataFile]]. |
53
|
|
|
* The file should return the data array that will be stored in [[data]] after inserting into the database. |
54
|
|
|
* |
55
|
|
|
* @throws InvalidConfigException if the specified data file does not exist |
56
|
|
|
* |
57
|
|
|
* @return array the data to be put into the database |
58
|
|
|
*/ |
59
|
|
|
protected function getData(): array |
60
|
|
|
{ |
61
|
|
|
if ($this->dataFile === false || $this->dataFile === null) { |
62
|
|
|
return []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$dataFile = $this->resolveDataFilePath(); |
66
|
|
|
|
67
|
|
|
if (is_file($dataFile)) { |
68
|
|
|
return require($dataFile); |
69
|
|
|
} else { |
70
|
|
|
throw new InvalidConfigException("Fixture data file does not exist: {$this->dataFile}"); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the fixture data file path. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function resolveDataFilePath(): string |
80
|
|
|
{ |
81
|
|
|
return storage_path($this->dataFile); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|