PHP code example of on1kel / hyperf-lighty

1. Go to this page and download the library: Download on1kel/hyperf-lighty library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

on1kel / hyperf-lighty example snippets


return [
    \On1kel\HyperfLighty\ConfigProvider::class,
];


declare(strict_types=1);

use Hyperf\Di\ReflectionManager;
use On1kel\HyperfLighty\Attributes\Process\DeployRoles;

$rolesEnv = env('APP_ROLES', 'api');
$enabledRoles = array_values(array_filter(array_map('trim', explode(',', $rolesEnv))))
    ?: ['api'];

$registry =    continue;
    }

    // ===== Вариант 2: Класс с атрибутом или универсальный =====
    $class = $value;

    $ref = ReflectionManager::reflectClass($class);
    $attrs = $ref->getAttributes(DeployRoles::class);

    // Нет атрибута → универсальный процесс
    if ($attrs === []) {
        $enabled[] = $class;
        continue;
    }

    /** @var DeployRoles $roles */
    $roles = $attrs[0]->newInstance();
    if (array_intersect($enabledRoles, $roles->roles)) {
        $enabled[] = $class;
    }
}

return $enabled;



namespace On1kel\HyperfLighty\Process;

use Hyperf\Crontab\Process\CrontabDispatcherProcess;
use On1kel\HyperfLighty\Attributes\Process\DeployRoles;

#[DeployRoles(['cron'])]
final class CronProcess extends CrontabDispatcherProcess {}

#[DeployRoles(['cron'])]
final class CronProcess extends CrontabDispatcherProcess {}

#[DeployRoles(['queue'])]
final class QueueConsumerProcess extends ConsumerProcess {}


declare(strict_types=1);

use Cubekit\Kafka\Hyperf\Process\InboxKafkaConsumerProcess;
use Cubekit\Kafka\Hyperf\Process\OutboxKafkaConsumerProcess;
use On1kel\HyperfLighty\Process\CronProcess;
use On1kel\HyperfLighty\Process\QueueConsumerProcess;

return [
    // Явное назначение ролей (final / внешние пакеты)
    InboxKafkaConsumerProcess::class => ['kafka'],
    OutboxKafkaConsumerProcess::class => ['kafka'],

    // Процессы с атрибутами DeployRoles
    CronProcess::class,
    QueueConsumerProcess::class,
];

return [
    \Hyperf\Contract\StdoutLoggerInterface::class
        => \On1kel\HyperfLighty\Logger\StdoutLoggerFactory::class,
];


declare(strict_types=1);

use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Level;

return [
    'default' => [
        'handler' => [
            'class' => StreamHandler::class,
            'constructor' => [
                'stream' => 'php://stdout',
                'level' => Level::Info,
            ],
        ],
        'formatter' => [
            'class' => JsonFormatter::class,
            'constructor' => [
                'appendNewline' => true,
            ],
        ],
    ],
];

class UserResource extends JsonResource
{
    public function toArray($request): array
    {
        $data = [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
        ];

        return $this->filterByRequestedFields($data);
    }
}
bash
php bin/hyperf.php vendor:publish on1kel/hyperf-lighty
bash
php bin/hyperf.php lighty:generate User V1_0
bash
php bin/hyperf.php ide-helper:model

_ide_helper_models.php
json
> {
>   "scripts": {
>       "post-install-cmd": [
>           "@php bin/hyperf.php ide-helper:model"
>       ]
>   }
> }
> 
bash
php bin/hyperf.php ide-helper:model
bash
php bin/hyperf.php vendor:publish on1kel/hyperf-lighty
json
{
    "limit": 10,
    "page": 1,
    "paginate": true
}