PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 1.5.0
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v1.5.0
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / framework / Foundation / Bootstrap.php

Bootstrap.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 1.5.0, at framework/Foundation/Bootstrap.php

205 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\Framework\Foundation;
4
5 use FluentForm\App\Modules\Activator;
6 use FluentForm\App\Modules\Deactivator;
7
8 class Bootstrap
9 {
10 /**
11 * The main plugin file path
12 * @var strring
13 */
14 protected static $file = null;
15
16 /**
17 * The base dir path of the plugin
18 * @var strring
19 */
20 protected static $basePath = null;
21
22 /**
23 * The app config (/config/app.php)
24 * @var strring
25 */
26 protected static $config = array();
27
28 /**
29 * Conveniently start the framework
30 * @param string $file
31 * @return $ */
32 public static function run($file)
33 {
34 static::init($file);
35 static::registerHooks();
36 static::registerAutoLoader();
37 static::registerApplication();
38 }
39
40 /**
41 * Initialize the framework
42 * @param string $file [the main plugin file path]
43 * @return void
44 */
45 public static function init($file)
46 {
47 static::$file = $file;
48
49 static::$basePath = plugin_dir_path($file);
50
51 if (file_exists($activator = static::$basePath.'app/Modules/Activator.php')) {
52 include_once $activator;
53 }
54
55 if (file_exists($deactivator = static::$basePath.'app/Modules/Deactivator.php')) {
56 include_once $deactivator;
57 }
58 }
59
60 /**
61 * Register activation/deactivation hooks
62 * @return void
63 */
64 public static function registerHooks()
65 {
66 static::registerActivationHook();
67 static::registerDeactivationHook();
68 }
69
70 /**
71 * Register activation hook
72 * @return bool
73 */
74 public static function registerActivationHook()
75 {
76 return register_activation_hook(
77 static::$file, array(__CLASS__, 'activate')
78 );
79 }
80
81 /**
82 * Register deactivation hook
83 * @return bool
84 */
85 public static function registerDeactivationHook()
86 {
87 return register_deactivation_hook(
88 static::$file, array(__CLASS__, 'deactivate')
89 );
90 }
91
92 /**
93 * Validate and activate the plugin
94 * @return void
95 */
96 public static function activate()
97 {
98 static::validatePlugin();
99 if (class_exists('FluentForm\App\Modules\Activator')) {
100 (new Activator)->handleActivation(static::$file);
101 }
102 }
103
104 /**
105 * Deactivate the plugin
106 * @return void
107 */
108 public static function deactivate()
109 {
110 // Framework specific implementation if necessary...
111 if (class_exists('FluentForm\App\Modules\Deactivator')) {
112 (new Deactivator)->handleDeactivation(static::$file);
113 }
114 }
115
116 /**
117 * Validate the plugin by checking all rquired files/settings
118 * @return void
119 */
120 public static function validatePlugin()
121 {
122 if(!file_exists($glueJson = static::$basePath.'glue.json')) {
123 die('The [glue.json] file is missing from "'.static::$basePath.'" directory.');
124 }
125
126 static::$config = json_decode(file_get_contents($glueJson), true);
127
128 $configPath = static::$basePath.'config';
129
130 if(!file_exists($file = $configPath.'/app.php')) {
131 die('The [config.php] file is missing from "'.$configPath.'" directory.');
132 }
133
134 static::$config = array_merge(include $file, static::$config);
135
136 if (!($autoload = @static::$config['autoload'])) {
137 die('The [autoload] key is not specified or invalid in "'.$glueJson.'" file.');
138 }
139
140 if (!($namespace = @$autoload['namespace'])) {
141 die('The [namespace] key is not specified or invalid in "'.$glueJson.'" file.');
142 }
143
144 $namespaceMapping = (array) @$autoload['mapping'];
145
146
147 if (!$namespaceMapping) {
148 die('The [mapping] key is not specified or invalid in "'.$glueJson.'" file.');
149 }
150 }
151
152 /**
153 * Register the autoloader
154 * @return void
155 */
156 public static function registerAutoLoader()
157 {
158 if (!static::$config) {
159 static::$config = json_decode(file_get_contents(static::$basePath.'glue.json'), true);
160 static::$config = array_merge(include static::$basePath.'config/app.php', static::$config);
161 }
162
163 spl_autoload_register([__CLASS__, 'loader']);
164 }
165
166 /**
167 * Framework's custom autoloader
168 * @param string $class
169 * @return mixed
170 */
171 public static function loader($class)
172 {
173 $namespace = static::$config['autoload']['namespace'];
174
175 if(substr($class, 0, strlen($namespace)) !== $namespace) {
176 return false;
177 }
178
179 foreach (static::$config['autoload']['mapping'] as $key => $value) {
180 $className = str_replace(
181 array('\\', $key, $namespace),
182 array('/', $value, ''),
183 $class
184 );
185
186 $file = static::$basePath.trim($className, '/').'.php';
187
188 if (is_readable($file)) {
189 return include $file;
190 }
191 }
192 }
193
194 /**
195 * Register "plugins_loaded" hook to run the plugin
196 * @return void
197 */
198 public static function registerApplication()
199 {
200 add_action('plugins_loaded', function () {
201 Application::run(static::$file, static::$config);
202 });
203 }
204 }
205