hyperf/nano

将 Hyperf 应用程序规模缩小到单个文件

维护者

详细信息

github.com/hyperf/nano

源代码

问题

安装次数: 12,167

依赖项: 1

建议者: 0

安全性: 0

星标: 394

关注者: 11

分支: 26

开放问题: 9

类型:项目

v2.0.1 2023-10-08 08:49 UTC

README

英文 | 中文

Financial Contributors on Open Collective Php Version Swoole Version Nano License

Nano,由 Hyperf 提供

Nano 是一个零配置、无骨架、最小化的 Hyperf 发行版,允许您只需一个 PHP 文件即可快速构建 Hyperf 应用程序。

目的

Svelte 的作者曾经说过,“框架不是组织代码的工具,它们是组织思维的工具”。Nano 的最大优点是它不会打断您的思维。Nano 很擅长自我声明,您无需了解框架的细节。您可以快速阅读代码并了解其用途。用最少的代码声明编写完整的 Hyperf 应用程序。

特性

  • 无骨架。
  • 快速启动。
  • 零配置。
  • 闭包风格。
  • 支持所有 Hyperf 特性(除注解外)。
  • 与所有 Hyperf 组件兼容。

示例

创建一个单个 PHP 文件,例如 index.php

<?php
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create('0.0.0.0', 9051);

$app->get('/', function () {

    $user = $this->request->input('user', 'nano');
    $method = $this->request->getMethod();

    return [
        'message' => "hello {$user}",
        'method' => $method,
    ];

});

$app->run();

运行服务器

php index.php start

这就是您需要的一切。

更多示例

路由

$app 继承了 Hyperf 路由的所有方法。

<?php
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create();

$app->addGroup('/nano', function () use ($app) {
    $app->addRoute(['GET', 'POST'], '/{id:\d+}', function($id) {
        return '/nano/'.$id;
    });
    $app->put('/{name:.+}', function($name) {
        return '/nano/'.$name;
    });
});

$app->run();

DI 容器

<?php
use Hyperf\Nano\ContainerProxy;
use Hyperf\Nano\Factory\AppFactory;

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

class Foo {
    public function bar() {
        return 'bar';
    }   
}

$app = AppFactory::create();
$app->getContainer()->set(Foo::class, new Foo());

$app->get('/', function () {
    /** @var ContainerProxy $this */
    $foo = $this->get(Foo::class);
    return $foo->bar();
});

$app->run();

按照惯例,nano 管理的所有闭包中的 $this 都绑定到 ContainerProxy,包括中间件、异常处理器等。

中间件

<?php
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create();

$app->get('/', function () {
    return $this->request->getAttribute('key');
});

$app->addMiddleware(function ($request, $handler) {
    $request = $request->withAttribute('key', 'value');
    return $handler->handle($request);
});

$app->run();

除了闭包之外,所有 $app->addXXX() 方法也接受类名作为参数。您可以通过任何相应的 hyperf 类传递。

异常处理器

<?php
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create();

$app->get('/', function () {
    throw new \Exception();
});

$app->addExceptionHandler(function ($throwable, $response) {
    return $response->withStatus('418')
        ->withBody(new SwooleStream('I\'m a teapot'));
});

$app->run();

自定义命令

<?php
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create();

$app->addCommand('echo {--name=Nano}', function($name){
    $this->output->info("Hello, {$name}");
})->setDescription('The echo command.');

$app->run();

要运行此命令,请执行

php index.php echo

事件监听器

<?php
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Framework\Event\BootApplication;
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create();

$app->addListener(BootApplication::class, function($event){
    $this->get(StdoutLoggerInterface::class)->info('App started');
});

$app->run();

自定义进程

<?php
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create();

$app->addProcess(function(){
    while (true) {
        sleep(1);
        $this->container->get(StdoutLoggerInterface::class)->info('Processing...');
    }
})->setName('nano-process')->setNums(1);

$app->addProcess(function(){
    $this->container->get(StdoutLoggerInterface::class)->info('Determine whether the process needs to be started based on env...');
})->setName('nano-process')->setNums(1)->setEnable(\Hyperf\Support\env('PROCESS_ENABLE', true))));

$app->run();

Crontab

<?php
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create();

$app->addCrontab('* * * * * *', function(){
    $this->get(StdoutLoggerInterface::class)->info('execute every second!');
})->setName('nano-crontab')->setOnOneServer(true)->setMemo('Test crontab.');

$app->run();

使用 Hyperf 组件

<?php
use Hyperf\DB\DB;
use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::create();

$app->config([
    'db.default' => [
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', 3306),
        'database' => env('DB_DATABASE', 'hyperf'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
    ]
]);

$app->get('/', function(){
    return DB::query('SELECT * FROM `user` WHERE gender = ?;', [1]);
});

$app->run();

如何使用 Swow

  • require swow 引擎
composer require "hyperf/engine-swow:^2.0"
  • 运行代码
<?php

declare(strict_types=1);

use Hyperf\Nano\Factory\AppFactory;

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

$app = AppFactory::createSwow();

$app->get('/', function () {
    return 'Hello World';
});

$app->run();