用Json配置文件来驱动Php Laravel框架
把Laravel5.5 的 php配置文件 改为 用json配置文件来驱动
记录一下自己学习过程(简易版未完成)
前因: 吾公司项目是基于 Laravel5.5 开发的、 项目业务的配置比较多,我们有python 和其他 程序。其实配置可以抽出来公共管理的。json 也是一个很好的格式。所以就尝试用 json文件 来驱动Laravel框架运行。把配置文件json化。
源配置文件

先大概截图下过程有空再改
这一步是把Laravel默认加载配置驱动配置换成我创建的驱动来完成

新建一个目录 \Facades\Facade
创建一个文件。 这是加载json驱动程序代码 (仿照原来加载php的代码改的)
1 <?php 2 3 namespace App\Facades\Facade; 4 5 use Exception; 6 use SplFileInfo; 7 use Illuminate\Config\Repository; 8 use Symfony\Component\Finder\Finder; 9 use Illuminate\Contracts\Foundation\Application; 10 use Illuminate\Contracts\Config\Repository as RepositoryContract; 11 12 class LoadJsonConfiguration 13 { 14 /** 15 * Bootstrap the given application. 16 * @param Application $app 17 * @throws Exception 18 */ 19 public function bootstrap(Application $app) 20 { 21 $items = []; 22 23 // First we will see if we have a cache configuration file. If we do, we'll load 24 // the configuration items from that file so that it is very quick. Otherwise 25 // we will need to spin through every configuration file and load them all. 26 if (file_exists($cached = $app->getCachedConfigPath())) { 27 $items = require $cached; 28 29 $loadedFromCache = true; 30 } 31 32 // Next we will spin through all of the configuration files in the configuration 33 // directory and load each one into the repository. This will make all of the 34 // options available to the developer for use in various parts of this app. 35 $app->instance('config', $config = new Repository($items)); 36 37 if (! isset($loadedFromCache)) { 38 $this->loadConfigurationFiles($app, $config); 39 } 40 41 // Finally, we will set the application's environment based on the configuration 42 // values that were loaded. We will pass a callback which will be used to get 43 // the environment in a web context where an "--env" switch is not present. 44 $app->detectEnvironment(function () use ($config) { 45 return $config->get('app.env', 'production'); 46 }); 47 48 date_default_timezone_set($config->get('app.timezone', 'UTC')); 49 50 mb_internal_encoding('UTF-8'); 51 } 52 53 /** 54 * Load the configuration items from all of the files. 55 * 56 * @param \Illuminate\Contracts\Foundation\Application $app 57 * @param \Illuminate\Contracts\Config\Repository $repository 58 * @return void 59 * @throws \Exception 60 */ 61 protected function loadConfigurationFiles(Application $app, RepositoryContract $repository) 62 { 63 $files = $this->getConfigurationFiles($app); 64 65 if (! isset($files['app'])) { 66 throw new Exception('Unable to load the "app" configuration file.'); 67 } 68 69 foreach ($files as $key => $path) { 70 $repository->set($key, $this->configFormat(json_decode(file_get_contents($path), true))); 71 } 72 } 73 74 /** 75 * Get all of the configuration files for the application. 76 * 77 * @param \Illuminate\Contracts\Foundation\Application $app 78 * @return array 79 */ 80 protected function getConfigurationFiles(Application $app) 81 { 82 $files = []; 83 84 $configPath = realpath($app->configPath()); 85 foreach (Finder::create()->files()->name('*.json')->in($configPath) as $file) { 86 $directory = $this->getNestedDirectory($file, $configPath); 87 88 $files[$directory.basename($file->getRealPath(), '.json')] = $file->getRealPath(); 89 } 90 91 ksort($files, SORT_NATURAL); 92 93 return $files; 94 } 95 96 /** 97 * Get the configuration file nesting path. 98 * 99 * @param \SplFileInfo $file 100 * @param string $configPath 101 * @return string 102 */ 103 protected function getNestedDirectory(SplFileInfo $file, $configPath) 104 { 105 $directory = $file->getPath(); 106 107 if ($nested = trim(str_replace($configPath, '', $directory), DIRECTORY_SEPARATOR)) { 108 $nested = str_replace(DIRECTORY_SEPARATOR, '.', $nested).'.'; 109 } 110 111 return $nested; 112 } 113 114 protected function configFormat(&$array){ 115 static $i = 0; 116 foreach($array as $key => &$value){ 117 if (empty($value)){ 118 continue; 119 } elseif (is_array($value)){ 120 $i++; 121 $this->configFormat($value); 122 } elseif (preg_match_all('/getEnv\(([0-9a-zA-Z-_,:\/.\'" ]+)\)/', $value, $matches)){ 123 $this->valuePregStrReplace($value, $matches); 124 } elseif (preg_match_all('/getEnvStorage\(([0-9a-zA-Z-_,:\/.\'" ]+)\)/', $value, $matches)){ 125 $value = storage_path(current($matches[1])); 126 } elseif (preg_match_all('/getEnvResource\(([0-9a-zA-Z-_,:\/.\'" ]+)\)/', $value, $matches)){ 127 $value = resource_path(current($matches[1])); 128 } elseif (preg_match_all('/getEnvExpload\(([0-9a-zA-Z-_,:\/.\'" ]+)\)/', $value, $matches)){ 129 $this->valuePregStrReplace($value, $matches); 130 $value = explode(",", $value); 131 } 132 } 133 return $array; 134 } 135 136 protected function valuePregStrReplace(&$value, $matches){ 137 for ($i = 0; $i < count($matches[0]); $i++) { 138 $originStr = $matches[0][$i]; 139 $params = explode(',', trim($matches[1][$i])); 140 $newStr = env($params[0],isset($params[1]) ? $this->stringFormat(trim($params[1])) : null); 141 $value = str_replace($originStr, $newStr, $value); 142 } 143 } 144 145 protected function stringFormat($string){ 146 if ($string === 'false'){ 147 return false; 148 } elseif (empty($string) || trim($string) === '\'\'' || trim($string) === '""'){ 149 return ''; 150 } elseif (trim($string) === 'null' || trim($string) === '"null"'){ 151 return null; 152 } else { 153 return $string; 154 } 155 } 156 }
完成后配置截图 这时候 .php 配置已经没作用了。只是没有删除

数据样子 数据暂时是正则替换 实现 env 配置

生命在于过程, 每天一点点.

浙公网安备 33010602011771号