PluginProbe
FakerPress / 0.8.0
FakerPress v0.8.0
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 / src / FakerPress / Module / Factory.php

Factory.php in FakerPress 0.8.0, at src/FakerPress/Module/Factory.php

92 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FakerPress\Module;
4
5 use FakerPress\Contracts\Service_Provider;
6
7 /**
8 * Class Factory
9 *
10 * @since 0.6.0
11 *
12 * @package FakerPress\Module
13 */
14 class Factory extends Service_Provider {
15 /**
16 * Store the modules that were initialized.
17 *
18 * @since 0.6.0
19 *
20 * @var array
21 */
22 protected $modules = [];
23
24 /**
25 * Fetches all modules used by FakerPress
26 *
27 * @since 0.6.0
28 *
29 * @return Abstract_Module[]
30 */
31 public function get_all(): array {
32 if ( empty( $this->views ) ) {
33 $modules_classes = [
34 Attachment::class,
35 Comment::class,
36 Meta::class,
37 Post::class,
38 Term::class,
39 User::class,
40 ];
41
42 foreach ( $modules_classes as $module_class ) {
43 $this->container->singleton( $module_class, $module_class, [ 'hook' ] );
44 $this->modules[] = $this->container->make( $module_class );
45 }
46 }
47
48 /**
49 * Allows the filtering of the FakerPress available modules.
50 *
51 * @since 0.6.0
52 *
53 * @param Abstract_Module[] $modules Which modules are available.
54 */
55 return apply_filters( 'fakerpress.modules', $this->modules );
56 }
57
58 /**
59 * Gets a specific module based on its slug.
60 *
61 * @since 0.6.0
62 *
63 * @param string $slug Which module we are looking for.
64 *
65 * @return Abstract_Module|null
66 */
67 public function get( string $slug ) {
68 $views = array_filter( $this->get_all(), static function( $module ) use ( $slug ) {
69 return $module::get_slug() === $slug;
70 } );
71
72 if ( empty( $views ) ) {
73 return null;
74 }
75
76 return reset( $views );
77 }
78
79 /**
80 * Register all the Modules as Singletons and initializes them.
81 *
82 * @since 0.6.0
83 */
84 public function register() {
85 // Register the provider as a singleton.
86 $this->container->singleton( static::class, $this );
87
88 // Initializes all modules.
89 $this->get_all();
90 }
91 }
92