1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ablett\TwigFaker; |
4
|
|
|
|
5
|
|
|
use Faker\Factory; |
6
|
|
|
|
7
|
|
|
class TwigFakerExtension extends \Twig_Extension { |
8
|
|
|
|
9
|
|
|
public static $factoriesPath = 'factories/'; |
10
|
|
|
private $cache; |
11
|
|
|
private $fake_data; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Returns the name of the extension. |
15
|
|
|
* |
16
|
|
|
* @return string The extension name |
17
|
|
|
*/ |
18
|
1 |
|
public function getName() |
19
|
1 |
|
{ |
20
|
1 |
|
return 'faker'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Callback for twig. |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
1 |
|
public function getFunctions() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
1 |
|
'fake' => new \Twig_Function_Method($this, 'fakerData') |
32
|
1 |
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Generate fake data. |
37
|
|
|
* |
38
|
|
|
* @param $type |
39
|
|
|
* @param int $count |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
7 |
|
public function fakerData($type, $count = 1, $cache_key = null) |
43
|
|
|
{ |
44
|
7 |
|
if (isset($cache_key)) $cache_key = "{$type}-$cache_key"; |
45
|
|
|
|
46
|
7 |
|
if (isset($this->cache[$cache_key])) |
47
|
7 |
|
{ |
48
|
3 |
|
return $this->cache[$cache_key]; |
49
|
|
|
} |
50
|
|
|
|
51
|
7 |
|
$this->createNewFakeData($type, $count); |
52
|
6 |
|
$this->cacheNewFakeData($cache_key); |
53
|
|
|
|
54
|
6 |
|
return $this->fake_data; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create the fake data. |
59
|
|
|
* |
60
|
|
|
* @param $type |
61
|
|
|
* @param integer $count |
62
|
|
|
* @throws InvalidFactoryException |
63
|
|
|
*/ |
64
|
7 |
|
private function createNewFakeData($type, $count) |
65
|
|
|
{ |
66
|
7 |
|
$faker = Factory::create(); |
67
|
7 |
|
$this->fake_data = []; |
68
|
|
|
|
69
|
7 |
|
for ($i = 0; $i < $count; $i++) |
70
|
|
|
{ |
71
|
7 |
|
if ( ! file_exists($this->fullFactoryPath($type))) |
72
|
7 |
|
{ |
73
|
1 |
|
throw new InvalidFactoryException('does not exist'); |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
$fake_item = include $this->fullFactoryPath($type); |
77
|
|
|
|
78
|
6 |
|
array_push($this->fake_data, $fake_item); |
79
|
6 |
|
} |
80
|
6 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Cache the new fake data if a key was sent. |
84
|
|
|
* |
85
|
|
|
* @param $cache_key |
86
|
|
|
*/ |
87
|
6 |
|
private function cacheNewFakeData($cache_key) |
88
|
|
|
{ |
89
|
|
|
if ($cache_key) |
90
|
6 |
|
{ |
91
|
4 |
|
$this->cache[$cache_key] = $this->fake_data; |
92
|
4 |
|
} |
93
|
6 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the full factory path. |
97
|
|
|
* |
98
|
|
|
* @param $type |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
7 |
|
private function fullFactoryPath($type) |
102
|
|
|
{ |
103
|
7 |
|
return static::$factoriesPath . $type . '.php'; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
class InvalidFactoryException extends \Exception {} |
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.