1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Faker |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License 3.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\faker\models; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use gplcart\core\Container; |
14
|
|
|
use LogicException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base class for faker generators |
18
|
|
|
*/ |
19
|
|
|
abstract class Generator |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Config class instance |
24
|
|
|
* @var \gplcart\core\Config $config |
25
|
|
|
*/ |
26
|
|
|
protected $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Library class instance |
30
|
|
|
* @var \gplcart\core\Library |
31
|
|
|
*/ |
32
|
|
|
protected $library; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Faker class instance |
36
|
|
|
* @var \Faker\Generator $faker |
37
|
|
|
*/ |
38
|
|
|
protected $faker; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* File model class instance |
42
|
|
|
* @var \gplcart\core\models\File $file |
43
|
|
|
*/ |
44
|
|
|
protected $file; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Store model class instance |
48
|
|
|
* @var \gplcart\core\models\Store $store |
49
|
|
|
*/ |
50
|
|
|
protected $store; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* User model class instance |
54
|
|
|
* @var \gplcart\core\models\User $user |
55
|
|
|
*/ |
56
|
|
|
protected $user; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Category model class instance |
60
|
|
|
* @var \gplcart\core\models\Category $category |
61
|
|
|
*/ |
62
|
|
|
protected $category; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Alias model class instance |
66
|
|
|
* @var \gplcart\core\models\Alias $alias |
67
|
|
|
*/ |
68
|
|
|
protected $alias; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Translation UI model instance |
72
|
|
|
* @var \gplcart\core\models\Translation $translation |
73
|
|
|
*/ |
74
|
|
|
protected $translation; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Constructor |
78
|
|
|
*/ |
79
|
|
|
public function __construct() |
80
|
|
|
{ |
81
|
|
|
$this->config = Container::get('gplcart\\core\\Config'); |
82
|
|
|
$this->library = Container::get('gplcart\\core\\Library'); |
83
|
|
|
$this->user = Container::get('gplcart\\core\\models\\User'); |
84
|
|
|
$this->file = Container::get('gplcart\\core\\models\\File'); |
85
|
|
|
$this->store = Container::get('gplcart\\core\\models\\Store'); |
86
|
|
|
$this->alias = Container::get('gplcart\\core\\models\\Alias'); |
87
|
|
|
$this->category = Container::get('gplcart\\core\\models\\Category'); |
88
|
|
|
$this->translation = Container::get('gplcart\\core\\models\\Translation'); |
89
|
|
|
|
90
|
|
|
$this->setFakerInstance(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create a single entity |
95
|
|
|
* Require generators to have this method |
96
|
|
|
*/ |
97
|
|
|
abstract public function create(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns a generator name |
101
|
|
|
* Require generators to have this method |
102
|
|
|
*/ |
103
|
|
|
abstract public function getName(); |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Sets a property |
107
|
|
|
* @param string $name |
108
|
|
|
* @param mixed $value |
109
|
|
|
*/ |
110
|
|
|
public function setProperty($name, $value) |
111
|
|
|
{ |
112
|
|
|
$this->{$name} = $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set Faker class instance |
117
|
|
|
* @return \Faker\Generator |
118
|
|
|
* @throws LogicException |
119
|
|
|
*/ |
120
|
|
|
protected function setFakerInstance() |
121
|
|
|
{ |
122
|
|
|
$this->library->load('faker'); |
123
|
|
|
|
124
|
|
|
if (!class_exists('Faker\\Factory')) { |
125
|
|
|
throw new LogicException('Class "Faker\Factory" not found'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->faker = \Faker\Factory::create(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Generate a given number of entities |
133
|
|
|
* @param integer $limit |
134
|
|
|
* @return integer |
135
|
|
|
*/ |
136
|
|
|
public function generate($limit = 20) |
137
|
|
|
{ |
138
|
|
|
ini_set('memory_limit', -1); |
139
|
|
|
|
140
|
|
|
$created = 0; |
141
|
|
|
for ($i = 0; $i < $limit; $i++) { |
142
|
|
|
$created += (int) $this->create(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $created; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns a random array of images from the database |
150
|
|
|
* @param bool $return_paths |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
protected function getImages($return_paths = true) |
154
|
|
|
{ |
155
|
|
|
static $files = null; |
156
|
|
|
|
157
|
|
|
if (!isset($files)) { |
158
|
|
|
$files = $this->file->getList(array('file_type' => 'image', 'limit' => array(0, 300))); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (empty($files)) { |
162
|
|
|
return array(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$limit = $this->config->get('module_faker_entity_file_limit', 5); |
166
|
|
|
$images = array_slice($this->faker->shuffle($files), 0, $limit, true); |
167
|
|
|
|
168
|
|
|
if (!$return_paths) { |
169
|
|
|
return array_keys($images); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$paths = array(); |
173
|
|
|
foreach ($images as $image) { |
174
|
|
|
$paths[] = array('path' => $image['path']); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $paths; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns a random store ID |
182
|
|
|
* @return integer |
183
|
|
|
*/ |
184
|
|
|
protected function getStoreId() |
185
|
|
|
{ |
186
|
|
|
static $stores = null; |
187
|
|
|
|
188
|
|
|
if (!isset($stores)) { |
189
|
|
|
$stores = $this->store->getList(array('limit' => array(0, 100))); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return (int) array_rand($stores); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Returns a random user ID |
197
|
|
|
* @return integer |
198
|
|
|
*/ |
199
|
|
|
protected function getUserId() |
200
|
|
|
{ |
201
|
|
|
static $users = null; |
202
|
|
|
|
203
|
|
|
if (!isset($users)) { |
204
|
|
|
$users = $this->user->getList(array('limit' => array(0, 100))); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return (int) array_rand($users); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Returns a random category ID |
212
|
|
|
* @param string $type |
213
|
|
|
* @return integer |
214
|
|
|
*/ |
215
|
|
|
protected function getCategoryId($type) |
216
|
|
|
{ |
217
|
|
|
static $categories = array(); |
218
|
|
|
|
219
|
|
|
if (!isset($categories[$type])) { |
220
|
|
|
|
221
|
|
|
$options = array( |
222
|
|
|
'type' => $type, |
223
|
|
|
'limit' => array(0, 100) |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
$categories[$type] = $this->category->getList($options); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return (int) array_rand($categories[$type]); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns a unique URL alias |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
protected function getAlias() |
237
|
|
|
{ |
238
|
|
|
return $this->alias->getUnique($this->faker->slug()); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns an array of available generator models |
243
|
|
|
* This method is static because we cannot instantiate abstract classes |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
|
|
public static function getList() |
247
|
|
|
{ |
248
|
|
|
$generators = array(); |
249
|
|
|
foreach (glob(__DIR__ . "/generators/*.php") as $file) { |
250
|
|
|
|
251
|
|
|
$id = strtolower(pathinfo($file, PATHINFO_FILENAME)); |
252
|
|
|
|
253
|
|
|
try { |
254
|
|
|
$instance = Container::get("gplcart\\modules\\faker\\models\\generators\\$id"); |
255
|
|
|
} catch (Exception $ex) { |
256
|
|
|
continue; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if ($instance instanceof Generator) { |
260
|
|
|
$generators[$id] = $instance; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
ksort($generators); |
265
|
|
|
return $generators; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Returns a generator model instance |
270
|
|
|
* @param string $name |
271
|
|
|
* @return object|null |
272
|
|
|
*/ |
273
|
|
|
public static function get($name) |
274
|
|
|
{ |
275
|
|
|
$generators = static::getList(); |
276
|
|
|
return empty($generators[$name]) ? null : $generators[$name]; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
|