3bg-supply-co/php-functions

此包包含可以在任何PHP项目中重用的随机函数和类。

1.6.3 2022-09-14 13:42 UTC

This package is auto-updated.

Last update: 2024-09-14 22:40:17 UTC


README

此包是我们许多项目中目前正在使用的可重用类和函数的集合。

目录

  1. 需求
  2. 安装
  3. 使用

需求

为了使用此包,必须满足以下要求

  • credentials.ini 文件必须存在于根目录的 2 级 以上
    • 例如:如果代码部署到以下目录: /var/www/html/test/,则文件必须位于以下目录: /var/www/credentials/credentials.ini
  • Composer
  • PHP版本 7.0+
  • SSH访问
    • 按照以下教程设置SSH密钥
    • 您可以通过在具有SSH密钥的机器上运行以下命令来测试它:ssh -T git@github.com

安装

要安装此包,必须遵循以下步骤(按顺序)

  • 通过运行以下命令在您的项目中创建一个 composer.json 文件:composer init
  • 在遍历选项后,将以下代码添加到 composer.json 文件中
    "minimum-stability": "dev",
    "require": {
      "3bg-supply-co/php-functions": "^1.6"
    }
  • 运行 composer update -n

使用

要使用此代码集合,确定您的需求。此包中找到的所有类都将使用基本 命名空间 Common3BG

要使用日志记录函数 writeLog()/tmp/ 目录必须由执行脚本的用户具有 写入 权限。

例如,如果您只需要访问 src/GenericFunctions.php 文件中找到的函数,那么只需在您的脚本中调用此函数即可。

如果您需要使用其中一个类(例如 ApiResponse),则必须包含正确的 命名空间。例如,以下行代码是使用 ApiResponse 类所需的

  use Common3BG\ApiResponse as ApiResponse;

ApiResponse 类

此类不接收任何参数。

以下方法

PHP代码示例
require (__DIR__) . '/vendor/autoload.php';
use Common3BG\ApiResponse as ApiResponse;

$apiResponse = new ApiResponse();

// Sample #1: simple one liner being added to message then returning the response
$apiResponse->addMessage('Testing a custom message');
$apiResponse->sendResponse();

// Sample #2: adding another message then returning the response
$apiResponse->addMessage('Testing a second custom message');
$apiResponse->sendResponse();

// Sample #3: sending a fail response without any parameters
$apiResponse->sendFailResponse();


// Sample #4: adding a string error message with no data, then returning the fail response
$apiResponse->sendFailResponse('Custom Error Message with no $data given');

// Sample $5: adding an error message with data to the errors, then returning the fail response
$apiResponse->sendFailResponse(['message' => 'Original error message from $data', 'test' => 'Some error sample here (test key is not needed)']);
$apiResponse->sendFailResponse();

// Sample #6: adding multiple errors to the errors, then returning the fail response
$apiResponse->addError(['message' => 'Custom Error Message #2', 'test' => 'Sample Error #2']);
$apiResponse->addError(['message' => 'Custom Error Message #3', 'test' => 'Sample Error #3']);
$apiResponse->sendFailResponse();
成功响应示例
{
    "success": true,
    "message": "",
    "data": [
        "Testing a success message",
        {
            "sample": "Success #1 Message"
        }
    ],
    "errors": []
}
失败响应示例
{
    "success": false,
    "message": "",
    "data": [],
    "errors": [
        {
            "message": "Custom Error Message #1",
            "test": "Sample Error #1"
        },
        {
            "message": "Custom Error Message #2",
            "test": "Sample Error #2"
        },
        {
            "message": "Custom Error Message #3",
            "test": "Sample Error #3"
        },
        {
            "message": "Error Message.",
            "test": "Testing an object being added to the errors"
        }
    ]
}

HttpPost 类

文档即将推出!

通用函数

文档即将推出!