从头搭建rpc框架_如何从头开始在PHP中创建插件引导程序(无框架)

从头搭建rpc框架

This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing systems such as WordPress. Joomla, Drupal, etc., as they already have their own plugin bootstraps.

这是为您设计PHP应用程序创建自己的自定义插件系统的常规方法(或希望扩展第三方程序以具有尚不具备的插件功能)。 这不是为WordPress等现有系统制作插件的方法。 Joomla,Drupal等,因为它们已经有自己的插件引导程序。

This article assumes you have basic PHP knowledge.

本文假定您具有基本PHP知识。

Each plugin will be in it's own folder by the same name as the plugin. For example:

每个插件将以与插件相同的名称位于其自己的文件夹中。 例如:

   If your plugin is named  "example", then you would create a folder in your "plugins" directory, named "example", and a file in your "plugins / example" directory name "example.php".

如果您的插件名为“ example”,那么您将在“ plugins”目录中创建一个名为“ example”的文件夹,并在“ plugins / example”目录中创建一个名为“ example.php”的文件。

   Additionally, any functions (or classes) inside "example.php", should be preceded with the name of the plugin:   example_init() { }

此外,“ example.php”内的任何函数(或类)都应以插件的名称开头:example_init(){}

-------------------------------------------------------------------------------------------------------

-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------

STEP 1.

第1步。

This is where we write the code for /index.php . This is the core file, and will contain all the logic as to what we want to load inside the plugins, including any functions we wish to support (or classes as you can expand on this code later).

这是我们为/index.php编写代码的地方。 这是核心文件,将包含有关我们要在插件中加载的内容的所有逻辑,包括我们希望支持的任何功能(或类,您可以稍后在此代码上进行扩展)。

/index.php

/index.php

<?php
// Set the path of the plugin folder
define('PATH_PLUGINS','plugins');

$plugin_path = "." . DIRECTORY_SEPARATOR . PATH_PLUGINS . DIRECTORY_SEPARATOR;
if ($handle = opendir($plugin_path)) {
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
		switch($file) {
			case ".":
			case "..":
				break;
			default:
				if(is_dir($plugin_path . $file)) {
					// INITIALIZE PLUGIN HERE
					$plugin_name = $file;
					$plugin_load = $plugin_path . $file . DIRECTORY_SEPARATOR . $file . ".php";
					if(file_exists($plugin_load)) {
						try {
							// CHECK IF PLUGIN INITIALIZED OR CONFLICTING INIT FUNCTION
							if(!function_exists($file . '_init')) {
								include($plugin_load);
								
								// CHECK IF PLUGIN_INIT() FUNCTION EXISTS AND EXECUTE IT
								if(function_exists($file . '_init')) {
									eval($file . '_init();');  // Execute the INIT function in the plugin;
								}
							}
						} catch (Exception $e) {
							echo "PLUGIN [" . $file . "] FAILED TO LOAD: " . $e->getDescription();
						}
					} else {
						echo "PLUGIN [" . $file . "] MISSING BOOTSTRAP.";
					}
				}
				break;
		}
    }
    closedir($handle);
}
?>

STEP 2

第2步

This is example "plugin" code. Name all of the functions inside the plugin using the following naming convention:

这是示例“插件”代码。 使用以下命名约定为插件内的所有功能命名:

PLUGINNAME_FUNCTIONNAME

PLUGINNAME_FUNCTIONNAME

Not only does this keep the plugin names unique, but they are easier to call when you need them. If you have multiple plugins (the whole point of writing a plugin system), then each file that is loaded will add functions (or classes) to the currently running scripts pool.

这不仅使插件名称唯一,而且在需要时更易于调用。 如果您有多个插件(编写插件系统的全部要点),则每个加载的文件都会向当前正在运行的脚本池添加函数(或类)。

/plugins/example/example.php

/plugins/example/example.p 生命值

function example_init() {
	try{
		// Do some stuff here
		echo "PLUGIN INITIALIZED";
		
	} catch (Exception $e) {
		// Save debug to log file or whatever -- returns false as init failed.
		return false;
	}
	return true;
}

function example_displaystuff() {
	echo "This should be displayed if the function exists";
}

You can continue to add more functions using the same naming convention. Additionally, when the plugin is loaded, you can choose to check for each function you wish to access and add them to an array/database as to whether they exist or not.

您可以继续使用相同的命名约定添加更多功能。 另外,在加载插件时,您可以选择检查要访问的每个函数,并将它们是否存在添加到数组/数据库中。

To call a function inside your plugin,

要在插件中调用函数,

eval($plugin_name . '_yourfunctionname();');

If there is a function that requires parameters in your plugin that you wish to call:

如果您的插件中有需要您调用的参数的函数:

eval($plugin_name . '_yourfunctionname(' . $myparam1 . ',' . $myparam2 .');');

There are several variations on writing this code as PHP is a dynamic language, however I feel this is the simplest method to create a functional plugin system for scripts that otherwise do not have one.

由于PHP是一种动态语言,因此编写此代码有多种变体,但是我认为这是为没有脚本的脚本创建功能性插件系统的最简单方法。

plugin-bootstrap.zip plugin-bootstrap.zip

翻译自: https://www.experts-exchange.com/articles/4823/How-to-create-a-PlugIn-bootstrap-in-PHP-from-scratch-no-frameworks.html

从头搭建rpc框架

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值