Install.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Utility;
  3. use Dotenv\Dotenv;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Storage;
  7. class Install
  8. {
  9. const INSTALL_FILE = '.' . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'install.lock';
  10. public static function hasLock()
  11. {
  12. return file_exists(self::getInstallLockPath());
  13. }
  14. public static function lock()
  15. {
  16. touch(self::getInstallLockPath());
  17. }
  18. public static function getEnvFileDic()
  19. {
  20. $contents = new Collection(file(self::getEnvFilePath()));
  21. $contentDic = $contents->reduce(function ($contentDic, $content) {
  22. if (is_string($content) && $content !== PHP_EOL) {
  23. $explodeArr = explode('=', str_replace(PHP_EOL, '', $content));
  24. if (count($explodeArr) > 1) {
  25. list($key, $value) = $explodeArr;
  26. } else {
  27. $key = $explodeArr[0];
  28. $value = '';
  29. }
  30. $contentDic[$key] = $value;
  31. }
  32. return $contentDic;
  33. }, []);
  34. return new Collection($contentDic);
  35. }
  36. public static function saveEnvFileDic(Collection $contents)
  37. {
  38. $contentArray = $contents->map(function ($value, $index) {
  39. return "{$index}={$value}";
  40. })->toArray();
  41. $content = implode($contentArray, PHP_EOL);
  42. file_put_contents(self::getEnvFilePath(), $content);
  43. }
  44. private static function getEnvFilePath()
  45. {
  46. return Container::getInstance()->environmentPath() . DIRECTORY_SEPARATOR .
  47. Container::getInstance()->environmentFile();
  48. }
  49. private static function getInstallLockPath()
  50. {
  51. return Container::getInstance()
  52. ->make('path.storage') . DIRECTORY_SEPARATOR . self::INSTALL_FILE;
  53. }
  54. }