1
|
|
|
<?php namespace Intraxia\Jaxion\Core; |
2
|
|
|
|
3
|
|
|
use Intraxia\Jaxion\Contract\Core\Application as ApplicationContract; |
4
|
|
|
use Intraxia\Jaxion\Contract\Core\HasActions; |
5
|
|
|
use Intraxia\Jaxion\Contract\Core\HasFilters; |
6
|
|
|
use Intraxia\Jaxion\Contract\Core\HasShortcode; |
7
|
|
|
use Intraxia\Jaxion\Contract\Core\Loader as LoaderContract; |
8
|
|
|
use UnexpectedValueException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Application |
12
|
|
|
* |
13
|
|
|
* @package Intraxia\Jaxion |
14
|
|
|
*/ |
15
|
|
|
class Application extends Container implements ApplicationContract { |
16
|
|
|
/** |
17
|
|
|
* Define plugin version on Application. |
18
|
|
|
*/ |
19
|
|
|
const VERSION = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Singleton instance of the Application object |
23
|
|
|
* |
24
|
|
|
* @var Application[] |
25
|
|
|
*/ |
26
|
|
|
protected static $instances = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Instantiates a new Application container. |
30
|
|
|
* |
31
|
|
|
* The Application constructor enforces the presence of of a single instance |
32
|
|
|
* of the Application. If an instance already exists, an Exception will be thrown. |
33
|
|
|
* |
34
|
|
|
* @param Config $config |
35
|
|
|
* @param array $providers |
36
|
|
|
* |
37
|
|
|
* @throws ApplicationAlreadyBootedException |
38
|
|
|
*/ |
39
|
27 |
|
public function __construct( $config, array $providers = array() ) { |
40
|
27 |
|
if ( isset( static::$instances[ get_called_class() ] ) ) { |
41
|
3 |
|
throw new ApplicationAlreadyBootedException; |
42
|
|
|
} |
43
|
|
|
|
44
|
27 |
|
static::$instances[ get_called_class() ] = $this; |
45
|
|
|
|
46
|
27 |
|
if ( ! ( $config instanceof Config ) ) { |
47
|
|
|
$config = new Config( ConfigType::PLUGIN, $config ); |
48
|
|
|
} |
49
|
|
|
|
50
|
27 |
|
$this->register_constants( $config ); |
51
|
27 |
|
$this->register_core_services( $config ); |
52
|
|
|
|
53
|
27 |
|
register_activation_hook( $config->file, array( $this, 'activate' ) ); |
54
|
27 |
|
register_deactivation_hook( $config->file, array( $this, 'deactivate' ) ); |
55
|
|
|
|
56
|
27 |
|
parent::__construct( $providers ); |
57
|
27 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
* |
62
|
|
|
* @throws UnexpectedValueException |
63
|
|
|
*/ |
64
|
12 |
|
public function boot() { |
65
|
12 |
|
$loader = $this->fetch( 'loader' ); |
66
|
|
|
|
67
|
12 |
|
if ( ! ( $loader instanceof LoaderContract ) ) { |
68
|
3 |
|
throw new UnexpectedValueException; |
69
|
|
|
} |
70
|
|
|
|
71
|
9 |
|
foreach ( $this as $alias => $value ) { |
72
|
9 |
|
if ( $value instanceof HasActions ) { |
73
|
9 |
|
$loader->register_actions( $value ); |
74
|
6 |
|
} |
75
|
|
|
|
76
|
9 |
|
if ( $value instanceof HasFilters ) { |
77
|
3 |
|
$loader->register_filters( $value ); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
9 |
|
if ( $value instanceof HasShortcode ) { |
81
|
5 |
|
$loader->register_shortcode( $value ); |
82
|
2 |
|
} |
83
|
6 |
|
} |
84
|
|
|
|
85
|
9 |
|
add_action( 'plugins_loaded', array( $loader, 'run' ) ); |
86
|
9 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
* |
91
|
|
|
* @codeCoverageIgnore |
92
|
|
|
*/ |
93
|
|
|
public function activate() { |
94
|
|
|
// no-op |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
* |
100
|
|
|
* @codeCoverageIgnore |
101
|
|
|
*/ |
102
|
|
|
public function deactivate() { |
103
|
|
|
// no-op |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
* |
109
|
|
|
* @return Application |
110
|
|
|
* @throws ApplicationNotBootedException |
111
|
|
|
*/ |
112
|
9 |
|
public static function instance() { |
113
|
9 |
|
if ( ! isset( static::$instances[ get_called_class() ] ) ) { |
114
|
6 |
|
throw new ApplicationNotBootedException; |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
return static::$instances[ get_called_class() ]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritDoc} |
122
|
|
|
*/ |
123
|
30 |
|
public static function shutdown() { |
124
|
30 |
|
if ( isset( static::$instances[ get_called_class() ] ) ) { |
125
|
27 |
|
unset( static::$instances[ get_called_class() ] ); |
126
|
18 |
|
} |
127
|
30 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets the plugin's url, path, and basename. |
131
|
|
|
* |
132
|
|
|
* @param Config $config |
133
|
|
|
*/ |
134
|
18 |
|
private function register_constants( Config $config ) { |
135
|
9 |
|
$this->share( 'file', function() use ( $config ) { |
136
|
9 |
|
return $config->file; |
137
|
27 |
|
} ); |
138
|
9 |
|
$this->share( 'url', function() use ( $config ) { |
139
|
9 |
|
return $config->url; |
140
|
27 |
|
} ); |
141
|
9 |
|
$this->share( 'path', function() use ( $config ) { |
142
|
9 |
|
return $config->path; |
143
|
27 |
|
} ); |
144
|
9 |
|
$this->share( 'basename', function() use ( $config ) { |
145
|
9 |
|
return $config->basename; |
146
|
27 |
|
} ); |
147
|
9 |
|
$this->share( 'slug', function() use ( $config ) { |
148
|
9 |
|
return $config->slug; |
149
|
27 |
|
} ); |
150
|
27 |
|
$this->share( 'version', static::VERSION ); |
151
|
27 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Registers the built-in services with the Application container. |
155
|
|
|
* |
156
|
|
|
* @param Config $config |
157
|
|
|
*/ |
158
|
27 |
|
private function register_core_services( Config $config ) { |
159
|
27 |
|
$this->share( array( 'config' => 'Intraxia\Jaxion\Core\Config' ), $config ); |
160
|
9 |
|
$this->share( array( 'loader' => 'Intraxia\Jaxion\Contract\Core\Loader' ), function () { |
161
|
3 |
|
return new Loader; |
162
|
27 |
|
} ); |
163
|
27 |
|
$this->share( array( 'i18n' => 'Intaxia\Jaxion\Contract\Core\I18n' ), function ( Container $app ) { |
164
|
9 |
|
return new I18n( $app->fetch( 'basename' ), $app->fetch( 'path' ) ); |
165
|
27 |
|
} ); |
166
|
18 |
|
} |
167
|
|
|
} |
168
|
|
|
|