1313namespace PatternLab \Faker ;
1414
1515use \PatternLab \Config ;
16+ use \PatternLab \Console ;
17+ use \PatternLab \Data ;
1618use \PatternLab \PatternEngine \Twig \TwigUtil ;
1719
1820class PatternLabListener extends \PatternLab \Listener {
1921
22+ protected $ faker ;
23+ protected $ locale ;
24+
2025 /**
2126 * Add the listeners for this plug-in
2227 */
@@ -28,13 +33,15 @@ public function __construct() {
2833 // set-up locale
2934 $ locale = Config::getOption ("faker.locale " );
3035 $ locale = ($ locale ) ? $ locale : "en_US " ;
36+ $ this ->locale = $ locale ;
3137
3238 // set-up Faker
3339 $ 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 ));
3845
3946 }
4047
@@ -58,25 +65,33 @@ private function clean($option) {
5865 * @return {String} replaced version of link.pattern
5966 */
6067 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 );
6270 $ formatter = $ matches [1 ];
63- $ options = isset ($ matches [5 ]) ? $ matches [5 ] : "" ;
71+ $ options = isset ($ matches [4 ]) ? $ matches [4 ] : "" ;
6472 if ($ options != "" ) {
6573 return $ this ->formatOptionsAndFake ($ formatter , $ options );
6674 } 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+ }
6881 }
6982 }
7083 return $ value ;
7184 }
7285
7386 /**
74- * Fake some content
87+ * Fake some content. Replace the entire store.
7588 */
7689 public function fakeContent () {
7790
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+ }
8095
8196 }
8297
@@ -110,19 +125,24 @@ public function formatOptionsAndFake($formatter, $options) {
110125 $ option6 = isset ($ options [6 ]) ? $ this ->clean ($ options [6 ]) : "" ;
111126
112127 // 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 ()."... " );
125144 }
145+
126146 }
127147
128148 }
@@ -143,7 +163,4 @@ private function recursiveWalk($array) {
143163 }
144164 return $ array ;
145165 }
146-
147-
148-
149166}
0 commit comments