<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | Route cache compiler
// +----------------------------------------------------------------------

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * 路由缓存编译命令
 * 将所有应用路由文件合并为单个缓存文件，用于生产环境优化
 * @package app\command
 */
class RouteCache extends Command
{
    /**
     * 配置命令参数
     */
    protected function configure(): void
    {
        $this->setName('route:cache')
            ->setDescription('Compile all application route files into a single cache for production');
    }

    /**
     * 执行路由编译
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output): void
    {
        require_once dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'bootstrap' . DIRECTORY_SEPARATOR . 'package_loader.php';

        $rootPath = dirname(__DIR__, 2);
        $count = \onezan_compile_routes($rootPath);

        $output->writeln("<info>Route cache compiled: {$count} files</info>");

        if (function_exists('opcache_reset')) {
            opcache_reset();
            $output->writeln('<info>OPcache cleared</info>');
        }
    }
}
