PluginProbe
FakerPress / 0.1.1
FakerPress v0.1.1
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / modules / base.php

base.php in FakerPress 0.1.1, at modules/base.php

111 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FakerPress\Module;
3
4 // Inluding needed providers
5 include_once \Fakerpress\Plugin::path( 'providers/html.php' );
6 include_once \Fakerpress\Plugin::path( 'providers/wp-term.php' );
7 include_once \Fakerpress\Plugin::path( 'providers/wp-post.php' );
8 include_once \Fakerpress\Plugin::path( 'providers/wp-comment.php' );
9 include_once \Fakerpress\Plugin::path( 'providers/wp-user.php' );
10
11 /**
12 * Abstract of a Module Generator.
13 * When creating a new module generator you should extend this one using `extends \FakerPress\Module\Base` in order to
14 * be make sure we have the needed methods.
15 */
16 abstract class Base {
17
18 public $faker = null;
19
20 public static $instance = null;
21
22 public $args = array();
23
24 public $params = array();
25
26 public $settings = array();
27
28 public $faked = array();
29
30 public $dependencies = array();
31
32 public $provider = null;
33
34 final public function __construct( $settings = array() ) {
35 $reflection = new \ReflectionClass( get_called_class() );
36
37 $this->slug = strtolower( $reflection->getShortName() );
38 $this->faker = \Faker\Factory::create();
39
40 $this->settings = (array) apply_filters( "fakerpress.module.{$this->slug}.settings", wp_parse_args( $this->settings, $settings ) );
41
42 $this->provider = (string) apply_filters( "fakerpress.module.{$this->slug}.provider", $this->provider );
43
44 $this->dependencies = (array) apply_filters( "fakerpress.module.{$this->slug}.dependencies", $this->dependencies );
45
46 // We need to merge the Provider to the Dependecies, so everything is loaded
47 $providers = array_merge( $this->dependencies, (array) $this->provider );
48 foreach ( $providers as $provider_class ) {
49 $provider = new $provider_class( $this->faker );
50 $this->faker->addProvider( $provider );
51 }
52
53 // Create a Reflection of the Provider class to discover all the methods that will fake an Argument
54 $provider_reflection = new \ReflectionClass( $this->provider );
55 $provider_methods = $provider_reflection->getMethods();
56
57 // Loop and verify which methods are will be faked on `generate`
58 foreach ( $provider_methods as $method ) {
59 if ( $provider_reflection->getName() !== $method->class ){
60 continue;
61 }
62 $this->faked[] = $method->name;
63 }
64
65 // Execute a method that can be overwritten by the called class
66 $this->init();
67 }
68
69 final public function __get( $name ){
70 return $this->faker->$name;
71 }
72
73 final public function __call( $name, $arguments ){
74 return call_user_func_array( array( $this->faker, $name ), $arguments );
75 }
76
77 final public static function __callStatic( $name, $arguments ){
78 $_class = get_called_class();
79 null === self::$instance and self::$instance = new $_class();
80 return call_user_func_array( array( self::$instance->faker, $name ), $arguments );
81 }
82
83 /**
84 * Method that will be called from the construct which is a final function
85 * @return null
86 */
87 public function init() {
88 return;
89 }
90
91 /**
92 * Use this method to save the fake data to the database
93 * @return int|bool|WP_Error Should return an error, or the $wpdb->insert_id or bool for the state
94 */
95 abstract public function save();
96
97 /**
98 * Use this method to generate all the needed data
99 * @return array An array of the data generated
100 */
101 public function generate( $args = array() ) {
102 $this->args = apply_filters( "fakerpress.module.{$this->slug}.args", wp_parse_args( $this->args, $args ) );
103
104 foreach ( $this->faked as $name ) {
105 $this->params[ $name ] = call_user_func_array( array( $this->faker, $name ), ( isset( $this->args[ $name ] ) ? (array) $this->args[ $name ] : array() ) );
106 }
107
108 return $this->params;
109 }
110
111 }