Commit 55a7a513 by pupi1985

Add ability to load plugins after and before connecting to the DB

parent 076a40af
......@@ -26,14 +26,103 @@ class Q2A_Plugin_PluginManager
const PLUGIN_DELIMITER = ';';
const OPT_ENABLED_PLUGINS = 'enabled_plugins';
private $loadBeforeDbInit = array();
private $loadAfterDbInit = array();
public function readAllPluginMetadatas()
{
$pluginDirectories = $this->getFilesystemPlugins(true);
foreach ($pluginDirectories as $pluginDirectory) {
$pluginFile = $pluginDirectory . 'qa-plugin.php';
if (!file_exists($pluginFile)) {
continue;
}
$metadataUtil = new Q2A_Util_Metadata();
$metadata = $metadataUtil->fetchFromAddonPath($pluginDirectory);
if (empty($metadata)) {
// limit plugin parsing to first 8kB
$contents = file_get_contents($pluginFile, false, null, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin', true);
}
// skip plugin which requires a later version of Q2A
if (isset($metadata['min_q2a']) && qa_qa_version_below($metadata['min_q2a'])) {
continue;
}
// skip plugin which requires a later version of PHP
if (isset($metadata['min_php']) && qa_php_version_below($metadata['min_php'])) {
continue;
}
$pluginInfoKey = basename($pluginDirectory);
$pluginInfo = array(
'pluginfile' => $pluginFile,
'directory' => $pluginDirectory,
'urltoroot' => substr($pluginDirectory, strlen(QA_BASE_DIR)),
);
if (isset($metadata['load_order'])) {
switch ($metadata['load_order']) {
case 'after_db_init':
$this->loadAfterDbInit[$pluginInfoKey] = $pluginInfo;
break;
case 'before_db_init':
$this->loadBeforeDbInit[$pluginInfoKey] = $pluginInfo;
break;
default:
}
} else {
$this->loadBeforeDbInit[$pluginInfoKey] = $pluginInfo;
}
}
}
private function loadPlugins($pluginInfos)
{
global $qa_plugin_directory, $qa_plugin_urltoroot;
foreach ($pluginInfos as $pluginInfo) {
$qa_plugin_directory = $pluginInfo['directory'];
$qa_plugin_urltoroot = $pluginInfo['urltoroot'];
require_once $pluginInfo['pluginfile'];
}
$qa_plugin_directory = null;
$qa_plugin_urltoroot = null;
}
public function loadPluginsBeforeDbInit()
{
$this->loadPlugins($this->loadBeforeDbInit);
}
public function loadPluginsAfterDbInit()
{
$enabledPlugins = $this->getEnabledPlugins(false);
$enabledForAfterDbInit = array();
foreach ($enabledPlugins as $enabledPluginDirectory) {
if (isset($this->loadAfterDbInit[$enabledPluginDirectory])) {
$enabledForAfterDbInit[$enabledPluginDirectory] = $this->loadAfterDbInit[$enabledPluginDirectory];
}
}
$this->loadPlugins($enabledForAfterDbInit);
}
public function getEnabledPlugins($fullPath = false)
{
$pluginDirectories = $this->getEnabledPluginsOption();
if ($fullPath) {
foreach ($pluginDirectories as $key => &$pluginDirectory)
foreach ($pluginDirectories as $key => &$pluginDirectory) {
$pluginDirectory = QA_PLUGIN_DIR . $pluginDirectory . '/';
}
}
return $pluginDirectories;
}
......@@ -50,7 +139,7 @@ class Q2A_Plugin_PluginManager
$fileSystemPluginFiles = glob(QA_PLUGIN_DIR . '*/qa-plugin.php');
foreach ($fileSystemPluginFiles as $pluginFile) {
$directory = dirname($pluginFile);
$directory = dirname($pluginFile) . '/';
if (!$fullPath) {
$directory = basename($directory);
......
......@@ -121,14 +121,16 @@ if (!empty($fileSystemPlugins)) {
$sortedPluginFiles = array();
foreach ($fileSystemPlugins as $pluginDirectory) {
$metadata = $metadataUtil->fetchFromAddonPath($pluginDirectory);
$pluginDirectoryPath = QA_PLUGIN_DIR . $pluginDirectory;
$metadata = $metadataUtil->fetchFromAddonPath($pluginDirectoryPath);
if (empty($metadata)) {
$pluginFile = QA_PLUGIN_DIR . $pluginDirectory . '/qa-plugin.php';
$pluginFile = $pluginDirectoryPath . '/qa-plugin.php';
// limit plugin parsing to first 8kB
$contents = file_get_contents($pluginFile, false, null, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin');
}
$metadata['name'] = isset($metadata['name']) && !empty($metadata['name'])
? qa_html($metadata['name'])
: qa_lang_html('admin/unnamed_plugin');
......@@ -189,7 +191,12 @@ if (!empty($fileSystemPlugins)) {
qa_lang_html('admin/options') . '</a>';
}
$beforeDbInit = isset($metadata['load_order']) && $metadata['load_order'] === 'before_db_init';
if ($beforeDbInit) {
$enabled = true;
} else {
$enabled = in_array($pluginDirectory, $enabledPlugins);
}
$pluginhtml = $namehtml . ' ' . $authorhtml . ' ' . $updatehtml . '<br>';
$pluginhtml .= $deschtml . (strlen($deschtml) > 0 ? '<br>' : '');
$pluginhtml .= '<small style="color:#666">' . qa_html($pluginDirectoryPath) . '/</small>';
......@@ -210,7 +217,7 @@ if (!empty($fileSystemPlugins)) {
'type' => 'checkbox',
'label' => qa_lang_html('admin/enabled'),
'value' => $enabled,
'tags' => sprintf('id="plugin_enabled_%s"', $hash),
'tags' => sprintf('id="plugin_enabled_%s"%s', $hash, $beforeDbInit ? 'disabled' : ''),
),
array(
'type' => 'custom',
......
......@@ -58,16 +58,27 @@
require_once QA_JOOMLA_LOAD_FILE;
}
qa_initialize_constants_2();
qa_initialize_modularity();
qa_register_core_modules();
qa_load_plugin_files();
qa_load_override_files();
require_once QA_INCLUDE_DIR.'qa-db.php';
qa_initialize_plugins();
function qa_initialize_plugins() {
$pluginManager = new Q2A_Plugin_PluginManager();
$pluginManager->readAllPluginMetadatas();
$pluginManager->loadPluginsBeforeDbInit();
qa_load_override_files();
qa_db_allow_connect();
$pluginManager->loadPluginsAfterDbInit();
qa_load_override_files();
}
// Version comparison functions
......@@ -309,11 +320,12 @@
Gets everything ready to start using modules, layers and overrides
*/
{
global $qa_modules, $qa_layers, $qa_override_files, $qa_overrides, $qa_direct;
global $qa_modules, $qa_layers, $qa_override_files, $qa_override_files_temp, $qa_overrides, $qa_direct;
$qa_modules=array();
$qa_layers=array();
$qa_override_files=array();
$qa_override_files_temp=array();
$qa_overrides=array();
$qa_direct=array();
}
......@@ -394,62 +406,17 @@
}
function qa_load_plugin_files()
/*
Load all the qa-plugin.php files from plugins that are compatible with this version of Q2A
*/
{
global $qa_plugin_directory, $qa_plugin_urltoroot;
// temporarily disabled
// $pluginManager = new Q2A_Plugin_PluginManager();
// $pluginDirectories = $pluginManager->getEnabledPlugins(true);
$pluginDirectories = glob(QA_PLUGIN_DIR.'*/');
$metadataUtil = new Q2A_Util_Metadata();
foreach ($pluginDirectories as $pluginDirectory) {
$pluginFile = $pluginDirectory . 'qa-plugin.php';
if (!file_exists($pluginFile))
continue;
$metadata = $metadataUtil->fetchFromAddonPath($qa_plugin_directory);
if (empty($metadata)) {
// limit plugin parsing to first 8kB
$contents = file_get_contents($pluginFile, false, null, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin', true);
}
// skip plugin which requires a later version of Q2A
if (isset($metadata['min_q2a']) && qa_qa_version_below($metadata['min_q2a']))
continue;
// skip plugin which requires a later version of PHP
if (isset($metadata['min_php']) && qa_php_version_below($metadata['min_php']))
continue;
// these variables are utilized in the qa_register_plugin_* functions
$qa_plugin_directory = $pluginDirectory;
$qa_plugin_urltoroot = substr($qa_plugin_directory, strlen(QA_BASE_DIR));
require_once $pluginFile;
}
$qa_plugin_directory = null;
$qa_plugin_urltoroot = null;
}
function qa_load_override_files()
/*
Apply all the function overrides in override files that have been registered by plugins
*/
{
global $qa_override_files, $qa_overrides;
global $qa_override_files, $qa_override_files_temp, $qa_overrides;
$functionindex=array();
foreach ($qa_override_files as $index => $override) {
foreach ($qa_override_files_temp as $override) {
$qa_override_files[] = $override;
$filename=$override['directory'].$override['include'];
$functionsphp=file_get_contents($filename);
......@@ -483,6 +450,8 @@
qa_eval_from_file($functionsphp, $filename);
}
$qa_override_files_temp = array();
}
......@@ -539,9 +508,9 @@
pass in the local plugin $directory and the $urltoroot relative url for that directory
*/
{
global $qa_override_files;
global $qa_override_files_temp;
$qa_override_files[]=array(
$qa_override_files_temp[] = array(
'directory' => $directory,
'urltoroot' => $urltoroot,
'include' => $include
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment