| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Foundation; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Activator; |
| 6 |
use FluentForm\App\Modules\Deactivator; |
| 7 |
use FluentForm\Framework\Foundation\Application; |
| 8 |
|
| 9 |
class Bootstrap |
| 10 |
{ |
| 11 |
/** |
| 12 |
* The main plugin file path |
| 13 |
* @var strring |
| 14 |
*/ |
| 15 |
protected static $file = null; |
| 16 |
|
| 17 |
/** |
| 18 |
* The base dir path of the plugin |
| 19 |
* @var strring |
| 20 |
*/ |
| 21 |
protected static $basePath = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* The app config (/config/app.php) |
| 25 |
* @var strring |
| 26 |
*/ |
| 27 |
protected static $config = array(); |
| 28 |
|
| 29 |
/** |
| 30 |
* Conveniently start the framework |
| 31 |
* @param string $file |
| 32 |
* @return $ */ |
| 33 |
public static function run($file) |
| 34 |
{ |
| 35 |
static::init($file); |
| 36 |
static::registerHooks(); |
| 37 |
static::registerAutoLoader(); |
| 38 |
static::registerApplication(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialize the framework |
| 43 |
* @param string $file [the main plugin file path] |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public static function init($file) |
| 47 |
{ |
| 48 |
static::$file = $file; |
| 49 |
|
| 50 |
static::$basePath = plugin_dir_path($file); |
| 51 |
|
| 52 |
if (file_exists($activator = static::$basePath.'app/Modules/Activator.php')) { |
| 53 |
include_once $activator; |
| 54 |
} |
| 55 |
|
| 56 |
if (file_exists($deactivator = static::$basePath.'app/Modules/Deactivator.php')) { |
| 57 |
include_once $deactivator; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Register activation/deactivation hooks |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public static function registerHooks() |
| 66 |
{ |
| 67 |
static::registerActivationHook(); |
| 68 |
static::registerDeactivationHook(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Register activation hook |
| 73 |
* @return bool |
| 74 |
*/ |
| 75 |
public static function registerActivationHook() |
| 76 |
{ |
| 77 |
return register_activation_hook( |
| 78 |
static::$file, array(__CLASS__, 'activate') |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Register deactivation hook |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
public static function registerDeactivationHook() |
| 87 |
{ |
| 88 |
return register_deactivation_hook( |
| 89 |
static::$file, array(__CLASS__, 'deactivate') |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Validate and activate the plugin |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public static function activate() |
| 98 |
{ |
| 99 |
static::validatePlugin(); |
| 100 |
if (class_exists('FluentForm\App\Modules\Activator')) { |
| 101 |
(new Activator)->handleActivation(static::$file); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Deactivate the plugin |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public static function deactivate() |
| 110 |
{ |
| 111 |
// Framework specific implementation if necessary... |
| 112 |
if (class_exists('FluentForm\App\Modules\Deactivator')) { |
| 113 |
(new Deactivator)->handleDeactivation(static::$file); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Validate the plugin by checking all rquired files/settings |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public static function validatePlugin() |
| 122 |
{ |
| 123 |
if(!file_exists($glueJson = static::$basePath.'glue.json')) { |
| 124 |
die('The [glue.json] file is missing from "'.$basePath.'" directory.'); |
| 125 |
} |
| 126 |
|
| 127 |
static::$config = json_decode(file_get_contents($glueJson), true); |
| 128 |
|
| 129 |
$configPath = static::$basePath.'config'; |
| 130 |
|
| 131 |
if(!file_exists($file = $configPath.'/app.php')) { |
| 132 |
die('The [config.php] file is missing from "'.$configPath.'" directory.'); |
| 133 |
} |
| 134 |
|
| 135 |
static::$config = array_merge(include $file, static::$config); |
| 136 |
|
| 137 |
if (!($autoload = @static::$config['autoload'])) { |
| 138 |
die('The [autoload] key is not specified or invalid in "'.$glueJson.'" file.'); |
| 139 |
} |
| 140 |
|
| 141 |
if (!($namespace = @$autoload['namespace'])) { |
| 142 |
die('The [namespace] key is not specified or invalid in "'.$glueJson.'" file.'); |
| 143 |
} |
| 144 |
|
| 145 |
$namespaceMapping = @$autoload['mapping']; |
| 146 |
|
| 147 |
if (!($namespaceMapping || empty((array) $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 |
return new Application(static::$file, static::$config); |
| 202 |
}); |
| 203 |
} |
| 204 |
} |
| 205 |
|