tina4stack/tina4php

Tina4 PHP

v2.0.79 2024-09-05 12:15 UTC

This package is auto-updated.

Last update: 2024-09-24 11:31:32 UTC


README

Tina4 是一个轻量级的路由和基于 Twig 的模板系统,允许您快速编写网站和 API 应用程序。目前,完整部署的大小不到 8MB,我们的目标是成为具有最小碳足迹的 PHP 框架。由于代码非常紧凑,且所有功能都是从零开始设计的,我们相信您会找到一个愉快的体验。

加入我们Discord,共同踏上这段旅程。

项目的宗旨是让您成为开发者,而 PHP 成为英雄!

PHP Composer

安装

我们目前正在测试最新的 PHP 8.2,请报告您可能遇到的任何问题。

  • 安装 PHP7.4 > 确保以下扩展已启用:fileinfo、mbstring、curl、gd、xml。
  • 安装 Composer * Windows 用户必须安装 openssl 以确保 JWT 密钥正确生成
  • 创建一个您想要工作的项目文件夹
  • 在您的项目文件夹终端 / 控制台

使用 composer 在终端中安装

composer require tina4stack/tina4php

使用以下命令开始您的 Tina4 项目

composer exec tina4 initialize:run

在项目文件夹中启动带有 PHP 的 web 服务器

composer start

在浏览器中访问https://:7145,您应该能看到文档页面

如果您想将 webservice 运行在特定的端口上

composer start 8080

数据库支持

ORM 和数据库模块都已提取到各自的 Packagist 模块中。ORM 和数据库元数据现在使用更统一的机制。服务模块现在在 bin 下创建,当它们的校验和更改时,tina4 服务和 tina4 bin 可执行文件将被替换。

数据库支持表格

特性

  • 使用 TWIG 自动模板化
  • 自动包含和项目结构
  • 开放 API 注释,快速生成 Swagger 文档和安全功能
  • 注释驱动测试,编写单元测试就像编写代码一样
  • Tina4 ORM
  • 服务运行器
  • 异步触发器和事件
  • 开箱即支持 Swoole
  • 模块化编程,每个项目都是潜在模块。

运行测试

composer test

启动服务

composer start-service

Tina4 菜单

composer tina4

注意 上述命令似乎仅在 Linux 和 Mac 上运行

在 Windows 上执行以下操作

php bin\tina4

使用 Docker 工作

这要求您已经运行了您的 Docker 环境

我们假设 /app 是当前项目的内部路径 安装

docker run -v $(pwd):/app tina4stack/php:latest composer require tina4stack/tina4php
docker run -v $(pwd):/app tina4stack/php:latest composer exec tina4 initialise:run

升级

docker run -v $(pwd):/app -p7145:7145 tina4stack/php:latest composer upgrade 

运行

docker run -v $(pwd):/app -p7145:7145 tina4stack/php:latest composer start 

例如,在 8080 端口等其他端口上

docker run -v $(pwd):/app -p8080:8080 tina4stack/php:latest composer start 8080

快速参考

文件夹布局如下,可以通过定义 PHP 常量 TINA4_TEMPLATE_LOCATIONSTINA4_ROUTE_LOCATIONSTINA4_INCLUDE_LOCATIONS 来覆盖

  • src
    • app (辅助工具、PHP 类)
    • public (系统 Twig 文件、图片、CSS、JS)
    • orm (ORM 对象 - 扩展 \Tina4\ORM)
    • routes (路由)
    • scss - 样式表模板
    • services (服务进程 - 扩展 \Tina4\Process)
    • templates (应用 Twig 文件)

.Env 配置

Tina4 使用 .env 文件来设置项目常量,当系统首次运行时将为您创建一个 .env 文件。如果您在您的操作系统上指定了名为 ENVIRONMENT 的环境变量,则将加载 .env.ENVIRONMENT。

[Section]           <-- Group section
MY_VAR=Test         <-- Example declaration, no quotes required or escaping, quotes will be treated as part of the variable
# A comment        <-- This is a comment
[Another Section]
VERSION=1.0.0

如果您在 .env 文件中包含敏感信息(如密码),请勿将其包含在项目中,而是创建一个示例,说明它应该是什么样子。

路由示例

在Tina4中创建API端点和路由非常简单,如下所示。如果您要添加swagger注解,只需访问/sswagger端点即可查看OpenApi渲染。

/**
* @description Swagger Description
* @tags Example,Route
*/
\Tina4\Get::add("/hello-world", function(\Tina4\Response $response){
    return $response ("Hello World!");
});

路由还可以映射到类方法,静态方法更适合路由,但您可以根据需要混合使用,例如如果您想将所有功能整齐地放在一起。

/**
 * Example of route calling class , method
 * Note the swagger annotations will go in the class
 */
\Tina4\Get::add("/test/class", ["Example", "route"]);

Example.php

class Example
{
    public function someThing() {
        return "Yes!";
    }
    
    /**
     * @param \Tina4\Response $response
     * @return array|false|string
     * @description Hello Normal -> see Example.php route
     */
    public function route (\Tina4\Response $response) {
        return $response ("OK!");
    }

}

SQLite3数据库连接示例

您可以使用tina4工具添加此类行,或者将下面的示例粘贴到您的index.php文件中。

global $DBA;
$DBA = new \Tina4\DataSQLite3("test.db");
  

ORM对象关系示例

class Address extends \Tina4\ORM
{
    public $id;
    public $address;
    public $customerId;

    //Link up customerId => Customer object
    public $hasOne = [["Customer" => "customerId"]];
}

class Customer extends \Tina4\ORM
{
    public $primaryKey = "id";
    public $id;
    public $name;

    //Primary key id maps to customerId on Address table
    public $hasMany = [["Address" => "customerId"]];
}

使用上述对象的一些代码

$customer = (new Customer());
$customer->id = 1;
$customer->name = "Test";
$customer->save();

$address = (new Address());
$address->address = "1 Street";
$address->customerId = 1;
$address->save();

$customer = (new Customer());
$customer->addresses[0]->address = "Another Address";
$customer->addresses[0]->address->save(); //Save the address
$customer->load("id = 1");

$address = new Address();
$address->load("id = 1");
$address->address = "New Street Address";
$address->customer->name = "New Name for customer"
$address->customer->save(); //save the customer
$address->save();

从命令行运行测试

试试看会发生什么

composer test

编写单元测试很容易,可以作为一个注释在您的代码注释中完成

/**
 * Some function to add numbers
 * @tests
 *   assert (1,1) === 2, "1 + 1 = 2"
 *   assert is_integer(1,1) === true, "This should be an integer"
 */
function add ($a,$b) {
    return $a+$b;
}

触发器和事件

Tina4Php通过使用popen执行并“线程”触发代码支持非常有限的线程或事件触发。有一些限制,因为代码中不能有注释,只能使用简单的变量。除此之外,几乎可以完成任何事情。

触发器和触发的示例

//Example of the triggered event, notice the sleep timer which should shut down most code on windows or linux making PHP wait for the result.

\Tina4\Thread::addTrigger("me", static function($name, $sleep=1, $hello="OK"){
    $iCount = 0;
    while ($iCount < 10) {
        file_put_contents("./log/event.log", "Hello {$name} {$hello}!\n", FILE_APPEND);
        sleep($sleep);
        $iCount++;
    }
});

在这里,触发器在2个路由上触发,在浏览器中点击每个路由以查看event.log中的输出

\Tina4\Get::add("/test", function(\Tina4\Response $response){
    
    \Tina4\Thread::trigger("me", ["Again", 1, "Moo!"]);

    return $response("OK!");
});

\Tina4\Get::add("/test/slow", function(\Tina4\Response $response){

    \Tina4\Thread::trigger("me", ["Hello", 3]);

    return $response("OK!");
});

输出到event.log文件应该是异步发生的,同时路由立即返回给浏览的用户

使用git web hooks触发部署

系统内置了一个路径,可以从github webhook触发系统上的部署

https://<site-name>/git/deploy

此操作需要以下内容在.env中;您将需要生成一个在系统之间共享的秘密。此外,您可以使用GIT_DEPLOYMENT_DIRS指定要包含在部署中的仓库目录。如果您使用私有仓库,请确保为部署系统上的git赋予权限。

[DEPLOYMENT]
GIT_TINA4_PROJECT_ROOT=.
GIT_BRANCH=master
GIT_REPOSITORY=https://github.com/tina4stack/tina4-php.git
GIT_SECRET=0123456789
GIT_DEPLOYMENT_STAGING=..\staging
GIT_DEPLOYMENT_PATH=deploy-test
GIT_DEPLOYMENT_DIRS=["branding", "bin"]
SLACK_NOTIFICATION_CHANNEL="general"

PhpDocs

docker run --rm -v %cd%:/data phpdoc/phpdoc:3 -d Tina4

构建docker

docker build . -t tina4stack/php:7.4

部署docker

 docker push tina4stack/php:7.4

Jquery validate cheat sheet

https://gist.github.com/rhacker/3550309

待办事项

  • 添加健康检查
  • 为每个请求添加GUID,将其传递到代码的其余部分

如果由于某种原因在运行pecl install ext后homebrew损坏 - zsh: killed php

sudo chown -R "$(id -un)":"$(id -gn)" /opt/homebrew

PHP信息

如果需要,这是一个PHP info路由的示例。

Route::get("/phpinfo", function(Response $response){
  ob_start();
  phpinfo();
  $data = ob_get_contents();
  ob_clean();
  return $response($data, HTTP_OK, TEXT_HTML);
});

MacOS的PHP扩展

安装IMAP扩展的示例

brew tap kabel/php-ext
brew install php-imap