Depends.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace ba;
  3. use think\Exception;
  4. /**
  5. * 依赖管理
  6. */
  7. class Depends
  8. {
  9. /**
  10. * 类型
  11. * @value npm | composer
  12. */
  13. protected $type = '';
  14. /**
  15. * json 文件完整路径
  16. */
  17. protected $json = null;
  18. /**
  19. * json 文件内容
  20. */
  21. protected $jsonContent = null;
  22. public function __construct(string $json, string $type = 'npm')
  23. {
  24. $this->json = $json;
  25. $this->type = $type;
  26. }
  27. /**
  28. * 获取 json 文件内容
  29. * @param bool $realTime 获取实时内容
  30. * @throws Exception
  31. */
  32. public function getContent(bool $realTime = false)
  33. {
  34. if (!file_exists($this->json)) {
  35. throw new Exception($this->json . ' file does not exist!');
  36. }
  37. if ($this->jsonContent && !$realTime) return $this->jsonContent;
  38. $content = @file_get_contents($this->json);
  39. $this->jsonContent = json_decode($content, true);
  40. if (!$this->jsonContent) {
  41. throw new Exception($this->json . ' file read failure!');
  42. }
  43. return $this->jsonContent;
  44. }
  45. /**
  46. * 设置 json 文件内容
  47. * @param array $content
  48. * @throws Exception
  49. */
  50. public function setContent(array $content = [])
  51. {
  52. if (!$content) $content = $this->jsonContent;
  53. if (!isset($content['name'])) {
  54. throw new Exception('Depend content file content is incomplete');
  55. }
  56. $content = json_encode($content, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
  57. $result = @file_put_contents($this->json, $content . PHP_EOL);
  58. if (!$result) {
  59. throw new Exception('File has no write permission:' . $this->json);
  60. }
  61. }
  62. /**
  63. * 获取依赖项
  64. * @param bool $devEnv 是否是获取开发环境依赖
  65. * @throws Exception
  66. */
  67. public function getDepends(bool $devEnv = false)
  68. {
  69. try {
  70. $content = $this->getContent();
  71. } catch (Exception $e) {
  72. return [];
  73. }
  74. if ($this->type == 'npm') {
  75. return $devEnv ? $content['devDependencies'] : $content['dependencies'];
  76. } else {
  77. return $devEnv ? $content['require-dev'] : $content['require'];
  78. }
  79. }
  80. /**
  81. * 是否存在某个依赖
  82. * @param string $name 依赖名称
  83. * @param bool $devEnv 是否是获取开发环境依赖
  84. * @throws Exception
  85. */
  86. public function hasDepend(string $name, bool $devEnv = false)
  87. {
  88. $depends = $this->getDepends($devEnv);
  89. return $depends[$name] ?? false;
  90. }
  91. /**
  92. * 添加依赖
  93. * @param array $depends 要添加的依赖数组["xxx" => ">=7.1.0",]
  94. * @param bool $devEnv 是否添加为开发环境依赖
  95. * @param bool $cover 覆盖模式
  96. * @throws Exception
  97. */
  98. public function addDepends(array $depends, bool $devEnv = false, bool $cover = false)
  99. {
  100. $content = $this->getContent(true);
  101. $dKey = $devEnv ? ($this->type == 'npm' ? 'devDependencies' : 'require-dev') : ($this->type == 'npm' ? 'dependencies' : 'require');
  102. if (!$cover) {
  103. foreach ($depends as $key => $item) {
  104. if (isset($content[$dKey][$key])) {
  105. throw new Exception($key . ' dependencie already exists!');
  106. }
  107. }
  108. }
  109. $content[$dKey] = array_merge($content[$dKey], $depends);
  110. $this->setContent($content);
  111. }
  112. /**
  113. * 删除依赖
  114. * @param array $depends 要删除的依赖数组["php", "w7corp/easywechat"]
  115. * @param bool $devEnv 是否为开发环境删除依赖
  116. * @throws Exception
  117. */
  118. public function removeDepends(array $depends, bool $devEnv = false)
  119. {
  120. $content = $this->getContent(true);
  121. $dKey = $devEnv ? ($this->type == 'npm' ? 'devDependencies' : 'require-dev') : ($this->type == 'npm' ? 'dependencies' : 'require');
  122. foreach ($depends as $item) {
  123. if (isset($content[$dKey][$item])) {
  124. unset($content[$dKey][$item]);
  125. }
  126. }
  127. $this->setContent($content);
  128. }
  129. }