-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPatternLabListener.php
175 lines (147 loc) · 5.49 KB
/
PatternLabListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/*!
* Faker Listener Class
*
* Copyright (c) 2016 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Adds Faker support to Pattern Lab
*
*/
namespace PatternLab\Faker;
use \PatternLab\Config;
use \PatternLab\Console;
use \PatternLab\Data;
use \PatternLab\PatternEngine\Twig\TwigUtil;
class PatternLabListener extends \PatternLab\Listener {
protected $faker;
protected $locale;
/**
* Add the listeners for this plug-in
*/
public function __construct() {
// add listener
$this->addListener("patternData.dataLoaded","fakeContent");
// set-up locale
$locale = Config::getOption("plugins.faker.locale");
$locale = ($locale) ? $locale : "en_US";
$this->locale = $locale;
// Setup Faker seed directive, so we can controll the faker generated results..
$setUniqueResults = Config::getOption("plugins.faker.setUniqueResults");
// set-up Faker
$this->faker = \Faker\Factory::create($locale);
// Force seed generator to produce the same results.
if (!empty($setUniqueResults)) {
$this->faker->seed($setUniqueResults);
}
$this->faker->addProvider(new \Faker\Provider\Color($this->faker));
$this->faker->addProvider(new \Faker\Provider\Payment($this->faker));
$this->faker->addProvider(new \Faker\Provider\DateTime($this->faker));
$this->faker->addProvider(new \Faker\Provider\Image($this->faker));
$this->faker->addProvider(new \Faker\Provider\Miscellaneous($this->faker));
}
/**
* Clean the passed option
* @param {String} the option to be cleaned
*
* @return {String} the cleaned option
*/
private function clean($option) {
$option = trim($option);
$option = (($option[0] == '"') || ($option[0] == "'")) ? substr($option, 1) : $option;
$option = (($option[strlen($option)-1] == '"') || ($option[strlen($option)-1] == "'")) ? substr($option, 0, -1) : $option;
return $option;
}
/**
* Go through data and replace any values that match items from the link.array
* @param {String} a string entry from the data to check for link.pattern
*
* @return {String} replaced version of link.pattern
*/
private function compareReplaceFaker($value) {
if (is_string($value) && (strpos($value,"Faker.") === 0)) {
preg_match("/^Faker\.([A-z]+)(\(('|\")?(.*)?('|\")?\))?$/",$value,$matches);
$formatter = $matches[1];
$options = isset($matches[4]) ? $matches[4] : "";
if ($options != "") {
return $this->formatOptionsAndFake($formatter, $options);
} else {
try {
return $this->faker->$formatter;
} catch (\InvalidArgumentException $e) {
Console::writeWarning("Faker plugin error: ".$e->getMessage()."...");
return $value;
}
}
}
return $value;
}
/**
* Fake some content. Replace the entire store.
*/
public function fakeContent() {
if ((bool)Config::getOption("plugins.faker.enabled")) {
$fakedContent = $this->recursiveWalk(Data::get());
Data::replaceStore($fakedContent);
}
}
/**
* Format the options and fake out the data
* @param {String} the name of the formatter
* @param {String} a string of options. separated by commas if appropriate
*
* @return {String} the formatted text
*/
public function formatOptionsAndFake($formatter, $options) {
if (($formatter == "date") || ($formatter == "time")) {
// don't try to parse date or time options. cross our fingers
return $this->faker->$formatter($options);
} else {
// get explodey
$options = explode(",", $options);
$count = count($options);
// clean up the options
$option0 = $this->clean($options[0]);
$option1 = isset($options[1]) ? $this->clean($options[1]) : "";
$option2 = isset($options[2]) ? $this->clean($options[2]) : "";
$option3 = isset($options[3]) ? $this->clean($options[3]) : "";
$option4 = isset($options[4]) ? $this->clean($options[4]) : "";
$option5 = isset($options[5]) ? $this->clean($options[5]) : "";
$option6 = isset($options[6]) ? $this->clean($options[6]) : "";
// probably should have used a switch. i'm lazy
try {
if ($count === 6) {
return $this->faker->$formatter($option0,$option1,$option2,$option3,$option4,$option5);
} else if ($count === 5) {
return $this->faker->$formatter($option0,$option1,$option2,$option3,$option4);
} else if ($count === 4) {
return $this->faker->$formatter($option0,$option1,$option2,$option3);
} else if ($count === 3) {
return $this->faker->$formatter($option0,$option1,$option2);
} else if ($count === 2) {
return $this->faker->$formatter($option0,$option1);
} else {
return $this->faker->$formatter($option0);
}
} catch (\InvalidArgumentException $e) {
Console::writeWarning("Faker plugin error: ".$e->getMessage()."...");
}
}
}
/**
* Work through a given array and decide if the walk should continue or if we should replace the var
* @param {Array} the array to be checked
*
* @return {Array} the "fixed" array
*/
private function recursiveWalk($array) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$array[$k] = self::recursiveWalk($v);
} else {
$array[$k] = self::compareReplaceFaker($v);
}
}
return $array;
}
}