1 | <?php |
||
27 | class DependencyManager implements DependencyManagerInterface { |
||
28 | |||
29 | use ConfigTrait; |
||
30 | |||
31 | /* |
||
32 | * Default dependency handler implementations. |
||
33 | */ |
||
34 | const DEFAULT_SCRIPT_HANDLER = '\BrightNucleus\Dependency\ScriptHandler'; |
||
35 | const DEFAULT_STYLE_HANDLER = '\BrightNucleus\Dependency\StyleHandler'; |
||
36 | |||
37 | /* |
||
38 | * Names of the configuration keys. |
||
39 | */ |
||
40 | const KEY_HANDLERS = 'handlers'; |
||
41 | const KEY_SCRIPTS = 'scripts'; |
||
42 | const KEY_STYLES = 'styles'; |
||
43 | |||
44 | /** |
||
45 | * Hold the dependencies, grouped by type. |
||
46 | * |
||
47 | * @since 0.1.0 |
||
48 | * |
||
49 | * @var array; |
||
50 | */ |
||
51 | protected $dependencies = [ ]; |
||
52 | |||
53 | /** |
||
54 | * Hold the handlers. |
||
55 | * |
||
56 | * @since 0.1.0 |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $handlers = [ ]; |
||
61 | |||
62 | /** |
||
63 | * Instantiate DependencyManager object. |
||
64 | * |
||
65 | * @since 0.1.0 |
||
66 | * |
||
67 | * @param ConfigInterface $config ConfigInterface object that |
||
68 | * contains dependency settings. |
||
69 | * @throws RuntimeException If the config could not be processed. |
||
70 | * @throws InvalidArgumentException If no dependency handlers were |
||
71 | * specified. |
||
72 | */ |
||
73 | public function __construct( ConfigInterface $config ) { |
||
78 | |||
79 | /** |
||
80 | * Initialize the dependency handlers. |
||
81 | * |
||
82 | * @since 0.1.0 |
||
83 | */ |
||
84 | protected function init_handlers() { |
||
92 | |||
93 | /** |
||
94 | * Add a single dependency handler. |
||
95 | * |
||
96 | * @since 0.1.0 |
||
97 | * |
||
98 | * @param string $dependency The dependency type for which to add a handler. |
||
99 | */ |
||
100 | protected function add_handler( $dependency ) { |
||
110 | |||
111 | /** |
||
112 | * Get the default handler class for a given type of dependency. |
||
113 | * |
||
114 | * @since 0.1.0 |
||
115 | * |
||
116 | * @param string $dependency The dependency that needs a handler. |
||
117 | * @return string|null Class name of the handler. Null if none. |
||
118 | */ |
||
119 | protected function get_default_handler( $dependency ) { |
||
129 | |||
130 | /** |
||
131 | * Initialize the actual dependencies. |
||
132 | * |
||
133 | * @since 0.1.0 |
||
134 | */ |
||
135 | protected function init_dependencies() { |
||
143 | |||
144 | /** |
||
145 | * Register all dependencies. |
||
146 | * |
||
147 | * @since 0.1.0 |
||
148 | * |
||
149 | * @param mixed $context Optional. The context to pass to the dependencies. |
||
150 | */ |
||
151 | public function register( $context = null ) { |
||
156 | |||
157 | /** |
||
158 | * Validate the context to make sure it is an array. |
||
159 | * |
||
160 | * @since 0.2.1 |
||
161 | * |
||
162 | * @param mixed $context The context as passed in by WordPress. |
||
163 | * @return array Validated context. |
||
164 | */ |
||
165 | protected function validate_context( $context ) { |
||
171 | |||
172 | /** |
||
173 | * Enqueue all dependencies. |
||
174 | * |
||
175 | * @since 0.1.0 |
||
176 | * |
||
177 | * @param mixed $context Optional. The context to pass to the dependencies. |
||
178 | */ |
||
179 | public function enqueue( $context = null ) { |
||
184 | |||
185 | /** |
||
186 | * Enqueue all dependencies of a specific type. |
||
187 | * |
||
188 | * @since 0.1.0 |
||
189 | * |
||
190 | * @param array $dependencies The dependencies to enqueue. |
||
191 | * @param string $dependency_type The type of the dependencies. |
||
192 | * @param mixed $context Optional. The context to pass to the |
||
193 | * dependencies. |
||
194 | */ |
||
195 | protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) { |
||
199 | |||
200 | /** |
||
201 | * Register all dependencies of a specific type. |
||
202 | * |
||
203 | * @since 0.1.0 |
||
204 | * |
||
205 | * @param array $dependencies The dependencies to register. |
||
206 | * @param string $dependency_type The type of the dependencies. |
||
207 | * @param mixed $context Optional. The context to pass to the |
||
208 | * dependencies. |
||
209 | */ |
||
210 | protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) { |
||
214 | |||
215 | /** |
||
216 | * Register a single dependency. |
||
217 | * |
||
218 | * @since 0.1.0 |
||
219 | * |
||
220 | * @param array $dependency Configuration data of the dependency. |
||
221 | * @param string $dependency_key Config key of the dependency. |
||
222 | * @param mixed $context Optional. Context to pass to the |
||
223 | * dependencies. Contains the type of the |
||
224 | * dependency at key |
||
225 | * 'dependency_type'. |
||
226 | */ |
||
227 | protected function register_dependency( $dependency, $dependency_key, $context = null ) { |
||
239 | |||
240 | /** |
||
241 | * Localize the script of a given dependency. |
||
242 | * |
||
243 | * @since 0.1.0 |
||
244 | * |
||
245 | * @param array $dependency The dependency to localize the script of. |
||
246 | * @param mixed $context Contextual data to pass to the callback. |
||
247 | * Contains the type of the dependency at key |
||
248 | * 'dependency_type'. |
||
249 | */ |
||
250 | protected function localize( $dependency, $context ) { |
||
258 | |||
259 | /** |
||
260 | * Enqueue a single dependency. |
||
261 | * |
||
262 | * @since 0.1.0 |
||
263 | * |
||
264 | * @param array $dependency Configuration data of the dependency. |
||
265 | * @param string $dependency_key Config key of the dependency. |
||
266 | * @param mixed $context Optional. Context to pass to the |
||
267 | * dependencies. Contains the type of the |
||
268 | * dependency at key |
||
269 | * 'dependency_type'. |
||
270 | */ |
||
271 | protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) { |
||
279 | |||
280 | /** |
||
281 | * Check whether a specific dependency is needed. |
||
282 | * |
||
283 | * @since 0.1.0 |
||
284 | * |
||
285 | * @param array $dependency Configuration of the dependency to check. |
||
286 | * @param mixed $context Context to pass to the dependencies. |
||
287 | * Contains the type of the dependency at key |
||
288 | * 'dependency_type'. |
||
289 | * @return bool Whether it is needed or not. |
||
290 | */ |
||
291 | protected function is_needed( $dependency, $context ) { |
||
302 | } |
||
303 |