<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;

class MakeApacheConfCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    //部署文件目录名称，需手动输入
    protected $signature = 'make:apache-conf {dir}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动生成apache配置文件';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $url = config('app.web_url');
        $parsedUrl = parse_url($url);
        $host = $parsedUrl['host'];
        $dir = $this->argument('dir');
        $project = base_path();
        $project = explode('/', $project);
        array_pop($project);
        $project[] = $dir;
        $project = implode('/', $project);
        $conf =
'<VirtualHost *:80>
   DocumentRoot "'.$project.'/public/"
   ServerName '.$host.'
   Alias "/resources/" "'.$project.'/resources/upload/"
   Alias "/admin" "'.$project.'/public/dist"
   Alias "/static/" "'.$project.'/public/dist/static/"
   ErrorLog logs/mp-website-admin-error_log
   CustomLog logs/mp-website-admin-access_log combined
   <Directory "'.$project.'/public/">
       AllowOverride All
       Require all granted
   </Directory>
   <Directory "'.$project.'/resources/upload/">
    RewriteEngine On
        RewriteCond %{QUERY_STRING} responseContentType=application%2Foctet-stream [NC]
        RewriteRule .* - [E=DOWNLOAD:yes]
        Header set Content-Disposition "attachment" env=DOWNLOAD
    AllowOverride All
    Require all granted
   </Directory>
   <Directory "'.$project.'/public/dist">
    AllowOverride All
    DirectoryIndex index.html
    Require all granted
   </Directory>
        <Directory "'.$project.'/public/dist/static/">
            AllowOverride All
                Require all granted
        </Directory>
</VirtualHost>
<VirtualHost *:443>
    DocumentRoot "'.$project.'/public/"
    ServerName '.$host.'
        Alias "/resources/" "'.$project.'/resources/upload/"
        Alias "/admin" "'.$project.'/public/dist"
        Alias "/static/" "'.$project.'/public/dist/static/"
    LimitRequestLine 40940
    LimitRequestFieldSize 40940

    SSLEngine on
    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    SSLCertificateFile "/etc/httpd/cert/matchexpo.cn/matchexpo.cn.crt"
    SSLCertificateKeyFile "/etc/httpd/cert/matchexpo.cn/matchexpo.cn.key"
    SSLCertificateChainFile "/etc/httpd/cert/matchexpo.cn/CA-Bundle.crt"
    #SSLCACertificateFile "/etc/httpd/cert/call/TencentQQAuthCA.crt"
    #SSLVerifyClient require

    <FilesMatch "\.(cgi|shtml|pl|asp|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>

    BrowserMatch ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

    <Directory "'.$project.'/public/">
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
        <Directory "'.$project.'/resources/upload/">
         RewriteEngine On
        RewriteCond %{QUERY_STRING} responseContentType=application%2Foctet-stream [NC]
        RewriteRule .* - [E=DOWNLOAD:yes]
        Header set Content-Disposition "attachment" env=DOWNLOAD
            AllowOverride All
        Require all granted
        </Directory>
        <Directory "'.$project.'/public/dist">
                AllowOverride All
                DirectoryIndex index.html
                Require all granted
        </Directory>
        <Directory "'.$project.'/public/dist/static/">
                AllowOverride All
                Require all granted
        </Directory>
</VirtualHost>
';
        $path = resource_path('conf/');
        if (!is_dir($path)) {
            mkDirs($path);
        }
        $fileName = $path . $dir .'_apache.conf';
        file_put_contents($fileName, $conf);
        echo 'echo IncludeOptional '.$fileName.' >> /etc/httpd/conf/httpd.conf'.PHP_EOL;
    }
}
