-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathBasicScaffoldPluginFactory.php
More file actions
57 lines (50 loc) · 1.45 KB
/
BasicScaffoldPluginFactory.php
File metadata and controls
57 lines (50 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* MWPD Basic Plugin Scaffold.
*
* @package MWPD\BasicScaffold
* @author Alain Schlesser <alain.schlesser@gmail.com>
* @license MIT
* @link https://www.mwpd.io/
* @copyright 2019 Alain Schlesser
*/
declare( strict_types=1 );
namespace MWPD\BasicScaffold;
use MWPD\BasicScaffold\Infrastructure\Plugin;
/**
* The plugin factory is responsible for instantiating the plugin and returning
* that instance.
*
* It can decide whether to return a shared or a fresh instance as needed.
*
* To read more about why this is preferable to a Singleton,
*
* @see https://www.alainschlesser.com/singletons-shared-instances/
*/
final class BasicScaffoldPluginFactory {
/**
* Create and return an instance of the plugin.
*
* This always returns a shared instance. This way, outside code can always
* get access to the object instance of the plugin.
*
* @return Plugin Plugin instance.
*/
public static function create(array $providers): Plugin {
/**
* We use a static variable to ensure that the plugin is only instantiated
* once. This is important for performance reasons and to ensure that the
* plugin is properly initialized.
*
* This serves the same purpose as a Singleton, but it is implemented as
* a factory to stick to SOLID principles.
*
* @var Plugin|null $plugin
*/
static $plugin = null;
if ( null === $plugin ) {
$plugin = new BasicScaffoldPlugin( $providers );
}
return $plugin;
}
}