13
13
namespace PatternLab \Faker ;
14
14
15
15
use \PatternLab \Config ;
16
+ use \PatternLab \Console ;
17
+ use \PatternLab \Data ;
16
18
use \PatternLab \PatternEngine \Twig \TwigUtil ;
17
19
18
20
class PatternLabListener extends \PatternLab \Listener {
19
21
22
+ protected $ faker ;
23
+ protected $ locale ;
24
+
20
25
/**
21
26
* Add the listeners for this plug-in
22
27
*/
@@ -28,13 +33,15 @@ public function __construct() {
28
33
// set-up locale
29
34
$ locale = Config::getOption ("faker.locale " );
30
35
$ locale = ($ locale ) ? $ locale : "en_US " ;
36
+ $ this ->locale = $ locale ;
31
37
32
38
// set-up Faker
33
39
$ this ->faker = \Faker \Factory::create ($ locale );
34
- $ this ->faker ->addProvider (new \Faker \Provider \Color ($ faker ));
35
- $ this ->faker ->addProvider (new \Faker \Provider \Payment ($ faker ));
36
- $ this ->faker ->addProvider (new \Faker \Provider \DateTime ($ faker ));
37
- $ this ->faker ->addProvider (new \Faker \Provider \Image ($ faker ));
40
+ $ this ->faker ->addProvider (new \Faker \Provider \Color ($ this ->faker ));
41
+ $ this ->faker ->addProvider (new \Faker \Provider \Payment ($ this ->faker ));
42
+ $ this ->faker ->addProvider (new \Faker \Provider \DateTime ($ this ->faker ));
43
+ $ this ->faker ->addProvider (new \Faker \Provider \Image ($ this ->faker ));
44
+ $ this ->faker ->addProvider (new \Faker \Provider \Miscellaneous ($ this ->faker ));
38
45
39
46
}
40
47
@@ -58,25 +65,33 @@ private function clean($option) {
58
65
* @return {String} replaced version of link.pattern
59
66
*/
60
67
private function compareReplaceFaker ($ value ) {
61
- if (is_string ($ value ) && preg_match ("/^Faker\.([A-z]+)(\((\'| \")?([A-z]+)?(\'| \")?\))?$/ " ,$ value ,$ matches )) {
68
+ if (is_string ($ value ) && (strpos ($ value ,"Faker. " ) === 0 )) {
69
+ preg_match ("/^Faker\.([A-z]+)(\(('| \")?(.*)?('| \")?\))?$/ " ,$ value ,$ matches );
62
70
$ formatter = $ matches [1 ];
63
- $ options = isset ($ matches [5 ]) ? $ matches [5 ] : "" ;
71
+ $ options = isset ($ matches [4 ]) ? $ matches [4 ] : "" ;
64
72
if ($ options != "" ) {
65
73
return $ this ->formatOptionsAndFake ($ formatter , $ options );
66
74
} else {
67
- return $ this ->faker ->$ formatter ;
75
+ try {
76
+ return $ this ->faker ->$ formatter ;
77
+ } catch (\InvalidArgumentException $ e ) {
78
+ Console::writeWarning ("Faker plugin error: " .$ e ->getMessage ()."... " );
79
+ return $ value ;
80
+ }
68
81
}
69
82
}
70
83
return $ value ;
71
84
}
72
85
73
86
/**
74
- * Fake some content
87
+ * Fake some content. Replace the entire store.
75
88
*/
76
89
public function fakeContent () {
77
90
78
- $ foo = $ this ->recursiveWalk (Data::get ());
79
- print_r ($ foo );
91
+ if ((bool )Config::getOption ("faker.on " )) {
92
+ $ fakedContent = $ this ->recursiveWalk (Data::get ());
93
+ Data::replaceStore ($ fakedContent );
94
+ }
80
95
81
96
}
82
97
@@ -110,19 +125,24 @@ public function formatOptionsAndFake($formatter, $options) {
110
125
$ option6 = isset ($ options [6 ]) ? $ this ->clean ($ options [6 ]) : "" ;
111
126
112
127
// probably should have used a switch. i'm lazy
113
- if ($ count === 6 ) {
114
- return $ this ->faker ->$ formatter ($ option0 ,$ option1 ,$ option2 ,$ option3 ,$ option4 ,$ option5 );
115
- } else if ($ count === 5 ) {
116
- return $ this ->faker ->$ formatter ($ option0 ,$ option1 ,$ option2 ,$ option3 ,$ option4 );
117
- } else if ($ count === 4 ) {
118
- return $ this ->faker ->$ formatter ($ option0 ,$ option1 ,$ option2 ,$ option3 );
119
- } else if ($ count === 3 ) {
120
- return $ this ->faker ->$ formatter ($ option0 ,$ option1 ,$ option2 );
121
- } else if ($ count === 2 ) {
122
- return $ this ->faker ->$ formatter ($ option0 ,$ option1 );
123
- } else {
124
- return $ this ->faker ->$ formatter ($ option0 );
128
+ try {
129
+ if ($ count === 6 ) {
130
+ return $ this ->faker ->$ formatter ($ option0 ,$ option1 ,$ option2 ,$ option3 ,$ option4 ,$ option5 );
131
+ } else if ($ count === 5 ) {
132
+ return $ this ->faker ->$ formatter ($ option0 ,$ option1 ,$ option2 ,$ option3 ,$ option4 );
133
+ } else if ($ count === 4 ) {
134
+ return $ this ->faker ->$ formatter ($ option0 ,$ option1 ,$ option2 ,$ option3 );
135
+ } else if ($ count === 3 ) {
136
+ return $ this ->faker ->$ formatter ($ option0 ,$ option1 ,$ option2 );
137
+ } else if ($ count === 2 ) {
138
+ return $ this ->faker ->$ formatter ($ option0 ,$ option1 );
139
+ } else {
140
+ return $ this ->faker ->$ formatter ($ option0 );
141
+ }
142
+ } catch (\InvalidArgumentException $ e ) {
143
+ Console::writeWarning ("Faker plugin error: " .$ e ->getMessage ()."... " );
125
144
}
145
+
126
146
}
127
147
128
148
}
@@ -143,7 +163,4 @@ private function recursiveWalk($array) {
143
163
}
144
164
return $ array ;
145
165
}
146
-
147
-
148
-
149
166
}
0 commit comments