<?php
namespace App\Helpers;
class Uid
{
// You can simply use this function :
// you have to stick to using single quotation (')
public static function setConfig($config,$key,$value,$path)
{
file_put_contents(
$path,
str_replace("'$key'". ' => ' ."'" . config($config)."'",
"'$key'". ' => ' ."'" . $value ."'",
file_get_contents($path))
);
// clear config cache
Artisan::call('cache:clear');
}
// Or use these. It makes no difference if you use a single or double quotation
public static function setConfig($config,$key,$value,$path)
{
file_put_contents(
$path,
str_replace(["'$key'". ' => ' ."'" . config($config)."'","\"$key\"". ' => ' ."\"" . config($config)."\""],
"'$key'". ' => ' ."'" . $value ."'",
file_get_contents($path))
);
// clear config cache
Artisan::call('cache:clear');
}
/*
* $name => Config Name
* $key => The key whose value you want to change; Example : 'redis.client'
* $value => The new value, you can add an array
* $implodeCharacter => spacers between keys
*/
public static function putConfig($name,$key,$value,$implodeCharacter = '.' )
{
$path = base_path('config/'.$name.'.php');
$config = config($name);
$data = self::findAndReplace($key, $config,$value,$implodeCharacter);
file_put_contents($path,'<?php
return '.var_export($data,true).';');
Artisan::call('cache:clear');
}
public static function findAndReplace($key, $config,$value,$implodeCharacter) {
$parts = explode($implodeCharacter, $key);
for($i=0;$i < count($parts);$i++) {
if($i == count($parts)-1){
$config[$parts[$i]] = $value;
}else{
$config= $config[$parts[$i]];
}
}
return $config;
}
}