strukt / framework
Strukt 框架
Requires
- php: ^8.1
- filp/whoops: ^2.14
- nunomaduro/collision: v7.x-dev
- phplucidframe/console-table: ^1.2
- psy/psysh: ^v0.11.21
- strukt/console: 1.0.0
- strukt/generator: v1.1.1-alpha
- strukt/process: v1.1.0
- strukt/router: v1.1.5-alpha
- symfony/http-foundation: ^4.3
Suggests
This package is auto-updated.
Last update: 2024-09-29 19:53:03 UTC
README
这是统一框架下所有 strukt-strukt 组件的包。
几乎没有人会单独使用这个包。
入门
echo {"minimum-stability":"dev"} > composer.json composer require "strukt/framework:1.1.7-alpha" --prefer-dist
设置、缓存、配置和环境
缓存
务必记得在必要时清除并重新加载缓存
./xcli cache:clear ./xcli cache:make cli ./xcli cache:make idx
Shell
进入Shell
./xcli shell:exec
设置应用程序类型
config("app.type", "App:Idx")// for index.php, alternative App:Cli for console
配置
config("facet.middlewares") config("facet.providers")
环境设置
此类默认位于 strukt-commons
Strukt\Env::withFile();//default .env file in your root folder Strukt\Env::withFile(".env-dev"); Strukt\Env::set("root_dir", getcwd());//custom environment variable Strukt\Env::get("root_dir");
设置包注册表
文件位置 ./cfg/repo.ini
repos(); //list all repositories repos("published");//list all published strukt packages repos("installed");//list all installed strukt packages
包
默认包
$core = new Strukt\Package\Core();//implements Strukt\Package\Pkg //returns array of middlewares, commands and providers $core->getSettings($type);//type is "App:Idx" or "App:Cli" $core->getName();//core $core->getCmdName();//null $core->getFiles();//null $core->getModules();//null $core->isPublished();//true by default $core->getRequirements();//null or array
上述方法在抽象类 Strukt\Package\Pkg 中,你可以使用它们来创建自己的包。
构建包
在开发你的包的过程中,你的第一步是安装 strukt-framework 并执行 composer exec strukt-cfg 命令,这将创建你的文件夹结构。你需要创建 src 和 package 文件夹。
以下为包的结构。
├── bootstrap.php ├── cfg ├── console ├── index.php ├── lib ├── tpl ├── vendor ├── composer.json ├── LICENSE ├── package #Place all your packages files here ├── README.md └── src └── Strukt └── Package └── Pkg{{Package Name}}.php #Identify your package resources here
你的包类在 src/Strukt/Package/Pkg<Package Name>.php 将会有在 默认包 部分列出的方法,它应该实现接口 Strukt\Contract\Package
包自动加载
你可能需要从你的根目录和包资源中自动加载库。
$loader = require "vendor/autoload.php"; ... ... $loader->addPsr4("App\\", [ __DIR__."/lib/App", __DIR__."/package/lib/App" ]); return $loader;
注意
对于需要安装到你的 app/src/{{AppName}} 文件夹的包,在构建你的包时你可以使用一些技巧。`publish:package` 命令接受 package 参数,用于发布当前正在开发的包,因为你的源代码将位于根文件夹下的一个名为 package 的子文件夹中。
这需要你进入你的 cfg/repo.php(参见 设置包注册表),并使用密钥/关键字 package 指示你目前正在开发的包,这将允许发布者将文件安装到你的应用程序源文件夹 app/src 中。
验证器
示例
class User extends \Strukt\Contract\Form{ /** * @IsNotEmpty() * @IsAlpha() */ public string $username; /** * @IsNotEmpty() */ public string $password; }
验证器注解
/** * @IsNotEmpty() * @IsAlpha() * @IsAlphaNum() * @IsNumeric() * @IsEmail() * @IsDate(Y-m-d) * @IsIn(a,b,c) * @EqualTo(xyz) * @IsLen(10) */
添加验证器
新的验证器可以添加到你的 lib/App/Validator.php 中。在那里你可以找到一个示例 App\Validator::isLenGt
/** * @IsLenGt(10) */