andrevanzuydam/tina4php

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进行自动模板化
  • 自动包含和项目结构
  • Open 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(app TWIG文件)

.Env配置

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

[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注解,只需访问/swagger端点即可看到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