Feature   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 153
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 5 2
A getOption() 0 5 3
A getDir() 0 5 2
A register() 0 30 3
A isRegistered() 0 4 1
A getFeature() 0 7 2
A getFeatures() 0 4 1
A setInitialFile() 0 4 1
1
<?php
2
3
namespace Flynt\Utils;
4
5
class Feature
6
{
7
8
    private static $initialFile = 'functions.php';
9
    private static $features = [];
10
11
    /**
12
     * Gets all options for a feature.
13
     *
14
     * Returns all parameters passed to `add_theme_support` in the lib/Init.php file.
15
     *
16
     * @since 0.1.0
17
     *
18
     * @param string $feature Name of the feature.
19
     *
20
     * @return array|null Returns an array of options or null if the feature wasn't found.
21
     */
22
    public static function getOptions($feature)
23
    {
24
        $feature = self::getFeature($feature);
25
        return $feature ? $feature['options'] : null;
26
    }
27
28
    /**
29
     * Gets single option for a feature.
30
     *
31
     * Returns a single parameter passed to `add_theme_support` in the lib/Init.php file using the key (starting with 0) of that option.
32
     *
33
     * @since 0.1.0
34
     *
35
     * @param string $feature Name of the feature.
36
     * @param string $key The option key.
37
     *
38
     * @return mixed|null Returns the option or null if the option / the feature doesn't exist.
39
     */
40
    public static function getOption($feature, $key)
41
    {
42
        $options = self::getOptions($feature);
43
        return is_array($options) && array_key_exists($key, $options) ? $options[$key] : null;
44
    }
45
46
    /**
47
     * Gets the absolute path of a feature.
48
     *
49
     * @since 0.1.0
50
     *
51
     * @param string $feature Name of the feature.
52
     *
53
     * @return string|null Returns the path or null if the feature doesn't exist.
54
     */
55
    public static function getDir($feature)
56
    {
57
        $feature = self::getFeature($feature);
58
        return $feature ? $feature['dir'] : null;
59
    }
60
61
    /**
62
     * Registers a feature.
63
     *
64
     * @since 0.1.0
65
     *
66
     * @param string $feature Name of the feature.
67
     * @param string $basePath The feature base path.
68
     * @param array $options An array of options. Optional.
69
     *
70
     * @return boolean
71
     */
72
    public static function register($feature, $basePath, $options = [])
73
    {
74
        if (!isset(self::$features[$feature])) {
75
            $prettyName = StringHelpers::removePrefix('flynt', StringHelpers::kebapCaseToCamelCase($feature));
76
            $dir = implode('/', [$basePath, $prettyName]);
77
            $file = implode('/', [$dir, self::$initialFile]);
78
79
            if (is_file($file)) {
80
                $options = (array) $options;
81
82
                self::$features[$feature] = [
83
                'options' => $options,
84
                'dir' => $dir,
85
                'name' => $prettyName
86
                ];
87
88
                require_once $file;
89
90
                // execute post register actions
91
                do_action('Flynt/registerFeature', $prettyName, $options, $dir);
92
                do_action("Flynt/registerFeature?name={$prettyName}", $prettyName, $options, $dir);
93
94
                return true;
95
            }
96
97
            trigger_error("{$feature}: Could not register feature! File not found: {$file}", E_USER_WARNING);
98
99
            return false;
100
        }
101
    }
102
103
    /**
104
     * Checks if a feature is already registered.
105
     *
106
     * @since 0.1.0
107
     *
108
     * @param string $name The feature name.
109
     *
110
     * @return boolean
111
     */
112
    public static function isRegistered($name)
113
    {
114
        return array_key_exists($name, self::$features);
115
    }
116
117
    /**
118
     * Gets a registered feature.
119
     *
120
     * @since 0.1.0
121
     *
122
     * @param string $name Name of the feature.
123
     *
124
     * @return array|boolean Returns an array with feature options and its directory or false if the feature is not registered.
125
     */
126
    public static function getFeature($name)
127
    {
128
        if (isset(self::$features[$name])) {
129
            return self::$features[$name];
130
        }
131
        return false;
132
    }
133
134
    /**
135
     * Gets all registered features.
136
     *
137
     * @since 0.1.0
138
     *
139
     * @return array Array of features with their options and directory.
140
     */
141
    public static function getFeatures()
142
    {
143
        return self::$features;
144
    }
145
146
    /**
147
     * Sets the initial file to be required for a feature.
148
     *
149
     * @since 0.1.0
150
     *
151
     * @param string $fileName File name or path relative to a feature's directory.
152
     */
153
    public static function setInitialFile($fileName)
154
    {
155
        self::$initialFile = $fileName;
156
    }
157
}
158