Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModuleRegistry
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 6
56
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 unregister
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 has
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getActive
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 collection
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\ModuleLoader;
4
5use WP_Forge\Collection\Collection;
6
7class ModuleRegistry {
8
9    /**
10     * Register a module.
11     *
12     * @param Module $module Module object
13     */
14    public static function register( Module $module ) {
15
16        // Set isActive from the value in the database, otherwise, fall back to the default isActive state
17        $module->isActive = options()->get( $module->name, $module->isActive );
18
19        // Add module to the collection
20        self::collection()->put( $module->name, $module );
21    }
22
23    /**
24     * Unregister a module.
25     *
26     * @param string $name Module name
27     */
28    public static function unregister( string $name ) {
29
30        // Set module as inactive in the database.
31        options()->set( $name, false );
32
33        // Remove the module from the collection
34        self::collection()->forget( $name );
35    }
36
37    /**
38     * Get a module by name.
39     *
40     * @param string $name Module name
41     *
42     * @return Module|null
43     */
44    public static function get( string $name ) {
45        return self::collection()->get( $name );
46    }
47
48    /**
49     * Check if a module has been registered.
50     *
51     * @param string $name Module name
52     *
53     * @return bool
54     */
55    public static function has( string $name ) {
56        return self::collection()->has( $name );
57    }
58
59    /**
60     * Get a collection containing active modules.
61     *
62     * @return Collection
63     */
64    public static function getActive() {
65        return self::collection()->where( 'isActive', '===', true );
66    }
67
68    /**
69     * Get the collection of registered modules.
70     *
71     * @return Collection
72     */
73    public static function collection() {
74        static $collection;
75        if ( ! isset( $collection ) ) {
76            $collection = Collection::make();
77        }
78
79        return $collection;
80    }
81
82}