1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\DB; |
4
|
|
|
|
5
|
|
|
use Faker\Generator; |
6
|
|
|
use Yarak\Config\Config; |
7
|
|
|
use Yarak\Helpers\Filesystem; |
8
|
|
|
use Phalcon\Mvc\User\Component; |
9
|
|
|
|
10
|
|
|
class ModelFactory extends Component |
11
|
|
|
{ |
12
|
|
|
use Filesystem; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Faker instance. |
16
|
|
|
* |
17
|
|
|
* @var Generator |
18
|
|
|
*/ |
19
|
|
|
protected $faker; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Array of user defined factory definitions. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $definitions; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Construct. |
30
|
|
|
* |
31
|
|
|
* @param Generator $faker |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Generator $faker) |
34
|
|
|
{ |
35
|
|
|
$this->faker = $faker; |
36
|
|
|
|
37
|
|
|
$this->load(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Make instances of class, overriding default values with attributes. |
42
|
|
|
* |
43
|
|
|
* @param string $class |
44
|
|
|
* @param array $attributes |
45
|
|
|
* @param int $times |
46
|
|
|
* |
47
|
|
|
* @return Phalcon\Mvc\Model|array |
48
|
|
|
*/ |
49
|
|
|
public function make($class, array $attributes = [], $times = null) |
50
|
|
|
{ |
51
|
|
|
return $this->forClass($class)->times($times)->make($attributes); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Make instances of class using name. |
56
|
|
|
* |
57
|
|
|
* @param string $class |
58
|
|
|
* @param string $name |
59
|
|
|
* @param array $attributes |
60
|
|
|
* @param int $times |
61
|
|
|
* |
62
|
|
|
* @return Phalcon\Mvc\Model|array |
63
|
|
|
*/ |
64
|
|
|
public function makeAs($class, $name, array $attributes = [], $times = null) |
65
|
|
|
{ |
66
|
|
|
return $this->forClass($class, $name)->times($times)->make($attributes); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create instances of class, overriding default values with attributes. |
71
|
|
|
* |
72
|
|
|
* @param string $class |
73
|
|
|
* @param array $attributes |
74
|
|
|
* @param int $times |
75
|
|
|
* |
76
|
|
|
* @return Phalcon\Mvc\Model|array |
77
|
|
|
*/ |
78
|
|
|
public function create($class, array $attributes = [], $times = null) |
79
|
|
|
{ |
80
|
|
|
return $this->forClass($class)->times($times)->create($attributes); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create instances of class using name. |
85
|
|
|
* |
86
|
|
|
* @param string $class |
87
|
|
|
* @param string $name |
88
|
|
|
* @param array $attributes |
89
|
|
|
* @param int $times |
90
|
|
|
* |
91
|
|
|
* @return Phalcon\Mvc\Model|array |
92
|
|
|
*/ |
93
|
|
|
public function createAs($class, $name, array $attributes = [], $times = null) |
94
|
|
|
{ |
95
|
|
|
return $this->forClass($class, $name)->times($times)->create($attributes); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get instance of factory builder with set attributes. |
100
|
|
|
* |
101
|
|
|
* @param string $class |
102
|
|
|
* @param string $name |
103
|
|
|
* |
104
|
|
|
* @return ModelFactoryBuilder |
105
|
|
|
*/ |
106
|
|
|
public function forClass($class, $name = 'default') |
107
|
|
|
{ |
108
|
|
|
return new ModelFactoryBuilder( |
109
|
|
|
$class, |
110
|
|
|
$name, |
111
|
|
|
$this->definitions, |
112
|
|
|
$this->faker |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Load factory definitions onto object. |
118
|
|
|
* |
119
|
|
|
* @return this |
120
|
|
|
*/ |
121
|
|
|
protected function load() |
122
|
|
|
{ |
123
|
|
|
$factory = $this; |
124
|
|
|
|
125
|
|
|
$config = Config::getInstance(); |
126
|
|
|
|
127
|
|
|
$path = $config->getFactoryDirectory(); |
128
|
|
|
|
129
|
|
|
$this->makeDirectoryStructure([ |
130
|
|
|
$config->getDatabaseDirectory(), |
131
|
|
|
$path, |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
$this->requireFactories($path, $factory); |
135
|
|
|
|
136
|
|
|
return $factory; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Require all factory files in path. |
141
|
|
|
* |
142
|
|
|
* @param string $path |
143
|
|
|
* @param ModelFactory $factory |
144
|
|
|
* |
145
|
|
|
* @throws Exception |
146
|
|
|
*/ |
147
|
|
|
protected function requireFactories($path, $factory) |
148
|
|
|
{ |
149
|
|
|
$dir = new \DirectoryIterator($path); |
150
|
|
|
|
151
|
|
|
foreach ($dir as $fileinfo) { |
152
|
|
|
if (!$fileinfo->isDot()) { |
153
|
|
|
require $fileinfo->getRealPath(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (empty($this->definitions)) { |
158
|
|
|
throw new \Exception('No factory definitions found.'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Define a class with a given set of attributes. |
164
|
|
|
* |
165
|
|
|
* @param string $class |
166
|
|
|
* @param callable $attributes |
167
|
|
|
* @param string $name |
168
|
|
|
* |
169
|
|
|
* @return this |
170
|
|
|
*/ |
171
|
|
|
public function define($class, callable $attributes, $name = 'default') |
172
|
|
|
{ |
173
|
|
|
$this->definitions[$class][$name] = $attributes; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|