-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKernel.php
203 lines (146 loc) · 3.97 KB
/
Kernel.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* Created by Asier Marqués <[email protected]>
* Date: 13/8/16
* Time: 19:11
*/
namespace Simettric\Sense;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class Kernel
{
const HOOK_REGISTERED_SERVICES = 'sense.registered_services';
/**
* @var array
*/
private static $configParams=array();
/**
* @var Kernel
*/
private static $instance=null;
/**
* This controls if Sense Kernel has been initialized
* It´s useful when you are using a theme witch uses Sense but
* there is not any plugin that initializes Sense
* @var bool
*/
private $initialized=false;
/**
* @var ContainerBuilder
*/
private $container;
private function __construct($config_params=array())
{
$this->container = new ContainerBuilder();
foreach ($config_params as $key=>$value) {
$this->container->setParameter("sense.".$key, $value);
}
$loader = new YamlFileLoader($this->container, new FileLocator([__DIR__ . "/Config"]));
$loader->load('services.yml');
$this->initCoreSubscribers();
}
/**
* @return Kernel
*/
public static function getInstance()
{
if(!self::$instance) {
self::$instance = new Kernel(array_merge(self::$configParams, array(
"plugins_order" => array()
)));
}
return self::$instance;
}
/**
* @param array $config_params
*
*/
public static function configure($config_params=array())
{
self::$configParams = $config_params;
}
public function initCoreSubscribers()
{
\add_action( 'muplugins_loaded', array($this, 'onMuPluginsLoaded'));
\add_action( 'plugins_loaded', array($this, 'onPluginsLoaded'));
\add_action( 'after_setup_theme', array($this, 'onAfterSetupTheme'));
\add_action( 'parse_query', array($this->container->get("router"), "match"));
\add_action( 'init' , array($this, 'onInit'));
add_action('admin_menu', array($this->container->get("admin.router"), "registerRouteRules"));
}
public function onMuPluginsLoaded()
{
$this->initialized = true;
}
public function onPluginsLoaded ()
{
if(!$this->initialized)
$this->initialized = true;
$this->loadPluggableFunctions();
}
public function onAfterSetupTheme()
{
if(!$this->initialized){
$notice = "You need to activate almost one plugin using Sense in order to use it in a Theme";
if(!is_admin() && $GLOBALS['pagenow'] != 'wp-login.php') {
wp_die($notice, "Sense Error");
}else{
add_action( 'admin_notices', function () use ($notice) {
?>
<div class="notice notice-success is-dismissible">
<p><strong>Sense Framework</strong>: <?php echo $notice; ?></p>
</div>
<?php
});
}
}
$this->registerServices();
$this->registerRoutes();
}
public function onInit()
{
$this->container->get("router")->registerRouteRules();
if($this->container->getParameter("debug_mode")){
$this->container->get("router")->regenerateWPRouteCache();
}
}
public function loadPluggableFunctions()
{
/**
* @var $plugin AbstractPlugin
*/
foreach($this->container->get("plugin_manager")->getPlugins() as $plugin){
if(!$plugin->isTheme()) $plugin->registerPluggableFunctions();
}
}
public function registerServices()
{
$this->container->setParameter('debug_mode', WP_DEBUG);
/**
* @var $plugin AbstractPlugin
*/
foreach($this->container->get("plugin_manager")->getPlugins() as $plugin){
$plugin->registerServices($this->container);
}
$this->container->compile();
do_action(static::HOOK_REGISTERED_SERVICES);
}
public function registerRoutes()
{
/**
* @var $plugin AbstractPlugin
*/
foreach($this->container->get("plugin_manager")->getPlugins() as $plugin){
$plugin->registerRoutes($this->container->get("router.route_container"));
$plugin->registerAdminRoutes($this->container->get("admin.route_container"));
}
}
/**
* @return ContainerBuilder
*/
public function getContainer()
{
return $this->container;
}
}