samagtech / bit-framework

v1.3.1 2023-11-10 14:18 UTC

This package is auto-updated.

Last update: 2024-09-10 15:58:01 UTC


README

通过 Serverless FramekworkBref 来管理小型函数 lambda 的微框架。

安装

安装通过以下命令完成:

    composer require samagtech/bit-framework

快速开始

首先需要创建用于通过 Bref 运行脚本的 Handler 文件。例如:

<?php

require __DIR__ . '/vendor/autoload.php';

use Bref\Context\Context;

class Handler implements \Bref\Event\Handler
{
    public function handle($event, Context $context)
    {
        return 'Hello ' . $event['name'];
    }
}

随后需要创建 Bit 应用程序

// bootstrap.php

<?php

use SamagTech\BitFramework\Application;

require __DIR__.'/vendor/autoload.php';

$app = Application::getInstance(__DIR__);

$app->bootProviders();

// $app->register(Provider::class) Funzione per registrare dei provider

// $app->bootDb() Da utilizzare nel caso di DB

return $app;

最后,需要创建应用程序的启动点

// index.php

try {

    $app = require __DIR__.'/bootstrap.php'

    $app->setHandler(ProductHandler::class);

    return $app->run();

}
catch (Error|Exception $e) {

    log_message('error', $e->getMessage(), ['exception' => $e]);
}

创建服务提供商

要创建服务提供商,需要创建一个实现 \SamagTech\BitFramework\Contracts\Provider 的类。例如。

// FooProvider.php

class FooProvider implements \SamagTech\BitFramework\Contracts\Provider {

    public function register () : void {

        app()->bind(Foo::class, new Foo());

        // Or

        app()->bind(Foo::class, function () {
            return new Foo();
        })
    }
}

创建模型

对于数据库管理,可以使用 Laravel 的模型。

class Foo extends Illuminate\Database\Eloquent\Model {}

环境

以下变量用于配置环境:


<!-- Configurazione per il DB -->
DB_DRIVER=mysql
DB_HOST=
DB_NAME=
DB_USER=
DB_PASSWORD=
DB_PREFIX=

<!-- Configura la tipologia di ambiente -->
ENV=local

<!-- Attiva/Disattiva il debug -->
DEBUG=true

数据库配置

要添加更多数据库连接,需要创建包含以下内容的 config/database.php 文件:

<?php

return [
    'default'   => [
        'driver'    => env('DB_DRIVER', 'mysql'),
        'host'      => env('DB_HOST', '127.0.0.1'),
        'database'  => env('DB_NAME', ''),
        'username'  => env('DB_USER', ''),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => env('DB_PREFIX', ''),
        'port'      => env('DB_PORT', '3306'),
    ]

];

辅助函数

现有辅助函数列表

  • app() -> 返回应用程序的实例
  • debug() -> 执行 print_r() 列出传递的参数,并以 die() 结束
  • d() -> 使用 kint 进行调试
  • dd() -> 使用 kint 和 die() 进行调试
  • log_message() -> 写入日志