<?php

namespace App\Providers;

use App\Services\HisSoap\Client;
use Illuminate\Support\Facades\Route;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register(): void
    {
        $this->cross();
        $this->registerHisService();
    }

    /**
     * Bootstrap any application services.
     *
     * @param UrlGenerator $url
     * @return void
     */
    public function boot(UrlGenerator $url): void
    {
        Schema::defaultStringLength(191);

        // Route参数表正则绑定
        Route::pattern('patient_id', '[a-zA-Z0-9]+');
        Route::pattern('serial_no', '[a-zA-Z0-9]+');

        // 配置https
        if(env('REDIRECT_HTTPS')) {
            $url->forceScheme('https');
        }
    }

    /**
     * 允许跨域
     * @return void
     */
    protected function cross(): void
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: GET,PUT,POST,PATCH,DELETE");
        header('Access-Control-Allow-Headers:x-requested-with,content-type,Authorization');
    }

    protected function registerHisService(): void
    {
        // His平台服务
        $this->app->singleton('HisSoapService', function () {
            return new Client();
        });
    }
}