PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Laravel教程 > 正文

    关于Lumen / Laravel .env 文件中的环境变量加载原理

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读: .env 文件可自定义其他任何有效的环境变量,并可通过 调用 env() 或 $_SERVER 或 $_ENV 来获取该变量。那么env()是如何加载到这...
     .env 文件可自定义其他任何有效的环境变量,并可通过  调用 env() 或 $_SERVER 或 $_ENV  来获取该变量。那么env()是如何加载到这些变量的呢?在Lumen的vendor/laravel/lumen-framework/src/helpers.php中,我们可以发现env函数是这样被定义的:

    1. if (! function_exists('env')) { 
    2.     /** 
    3.      * Gets the value of an environment variable. Supports boolean, empty and null. 
    4.      * 
    5.      * @param  string  $key 
    6.      * @param  mixed   $default 
    7.      * @return mixed 
    8.      */ 
    9.     function env($key$default = null) 
    10.     { 
    11.         $value = getenv($key); 
    12.  
    13.         if ($value === false) { 
    14.             return value($default); 
    15.         } 
    16.  
    17.         switch (strtolower($value)) { 
    18.             case 'true'
    19.             case '(true)'
    20.                 return true; 
    21.  
    22.             case 'false'
    23.             case '(false)'
    24.                 return false; 
    25.  
    26.             case 'empty'
    27.             case '(empty)'
    28.                 return ''
    29.  
    30.             case 'null'
    31.             case '(null)'
    32.                 return
    33.         } 
    34.  
    35.         if (Str::startsWith($value'"') && Str::endsWith($value'"')) { 
    36.             return substr($value, 1, -1); 
    37.         } 
    38.  
    39.         return $value
    40.     } 

    可见,env函数中调用了 getenv() 来读取环境变量。我们又知道getenv()是PHP原生提供可读取   $_SERVER 或 $_ENV   全局变量的函数API,.env文件中的环境变量为何可以通过getenv()来获取呢?vlucas/phpdotenv 就是这个幕后功臣,在Lumen 或 Laravel 的vendor下可以找到她,如果要单独下载她,去这里 :https://github.com/vlucas/phpdotenv 。

    在vlucas/phpdotenv/src/Loader.php文件中,我们可以看到.env被加载进一个数组,然后把每一行看作setter并调用setEnvironmentVariable()方法:

     

    1. /** 
    2.      * Load `.env` file in given directory. 
    3.      * 
    4.      * @return array 
    5.      */ 
    6.     public function load() 
    7.     { 
    8.         $this->ensureFileIsReadable(); 
    9.  
    10.         $filePath = $this->filePath; 
    11.         $lines = $this->readLinesFromFile($filePath); 
    12.         foreach ($lines as $line) { 
    13.             if (!$this->isComment($line) && $this->looksLikeSetter($line)) { 
    14.                 $this->setEnvironmentVariable($line); 
    15.             } 
    16.         } 
    17.  
    18.         return $lines
    19.     } 
    20.  
    21. /** 
    22.      * Read lines from the file, auto detecting line endings. 
    23.      * 
    24.      * @param string $filePath 
    25.      * 
    26.      * @return array 
    27.      */ 
    28.     protected function readLinesFromFile($filePath
    29.     { 
    30.         // Read file into an array of lines with auto-detected line endings 
    31.         $autodetect = ini_get('auto_detect_line_endings'); 
    32.         ini_set('auto_detect_line_endings''1'); 
    33.         $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 
    34.         ini_set('auto_detect_line_endings'$autodetect); 
    35.  
    36.         return $lines
    37.     } 

     Loader::setEnvironmentVariable($name, $value = null) 的定义如下:

     

    1. /** 
    2.      * Set an environment variable. 
    3.      * 
    4.      * This is done using: 
    5.      * - putenv, 
    6.      * - $_ENV, 
    7.      * - $_SERVER. 
    8.      * 
    9.      * The environment variable value is stripped of single and double quotes. 
    10.      * 
    11.      * @param string      $name 
    12.      * @param string|null $value 
    13.      * 
    14.      * @return void 
    15.      */ 
    16.     public function setEnvironmentVariable($name$value = null) 
    17.     { 
    18.         list($name$value) = $this->normaliseEnvironmentVariable($name$value); 
    19.  
    20.         // Don't overwrite existing environment variables if we're immutable 
    21.         // Ruby's dotenv does this with `ENV[key] ||= value`. 
    22.         if ($this->immutable && $this->getEnvironmentVariable($name) !== null) { 
    23.             return
    24.         } 
    25.  
    26.         // If PHP is running as an Apache module and an existing 
    27.         // Apache environment variable exists, overwrite it 
    28.         if (function_exists('apache_getenv') && function_exists('apache_setenv') && apache_getenv($name)) { 
    29.             apache_setenv($name$value); 
    30.         } 
    31.  
    32.         if (function_exists('putenv')) { 
    33.             putenv("$name=$value"); 
    34.         } 
    35.  
    36.         $_ENV[$name] = $value
    37.         $_SERVER[$name] = $value
    38.     } 

    PHP dotenv 她是什么?

    Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically.

    This is a PHP version of the original Ruby dotenv.


      Why .env?

    You should never store sensitive credentials in your code. Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments – such as database credentials or credentials for 3rd party services – should be extracted from the code into environment variables.

    Basically, a .env file is an easy way to load custom configuration variables that your application needs without having to modify .htaccess files or Apache/nginx virtual hosts. This means you won't have to edit any files outside the project, and all the environment variables are always set no matter how you run your project - Apache, Nginx, CLI, and even PHP 5.4's built-in webserver. It's WAY easier than all the other ways you know of to set environment variables, and you're going to love it.

    . NO editing virtual hosts in Apache or Nginx
    . NO adding php_value flags to .htaccess files
    . EASY portability and sharing of required ENV values
    . COMPATIBLE with PHP's built-in web server and CLI runner

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-153-5874-1.html
    相关热词搜索: