npm = root_path() . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'package.json'; $this->composer = root_path() . DIRECTORY_SEPARATOR . 'composer.json'; } /** * 获取 composer.json 内容 * @param bool $realTime 获取实时内容 * @throws Exception */ public function getComposer(bool $realTime = false) { if (!file_exists($this->composer)) { throw new Exception('composer.json file does not exist!'); } if ($this->composerContent && !$realTime) return $this->composerContent; $composerContent = @file_get_contents($this->composer); $this->composerContent = json_decode($composerContent, true); if (!$this->composerContent) { throw new Exception('composer.json file read failure!'); } return $this->composerContent; } /** * 获取 composer 依赖 * @param bool $devEnv 是否是获取开发环境依赖 * @throws Exception */ public function getComposerRequire(bool $devEnv = false) { $composerContent = $this->getComposer(); return $devEnv ? $composerContent['require-dev'] : $composerContent['require']; } /** * 是否存在某个 composer 依赖 * @param string $requireName 依赖名称 * @param bool $devEnv 是否是获取开发环境依赖 * @throws Exception */ public function hasComposerRequire(string $requireName, bool $devEnv = false) { $require = $this->getComposerRequire($devEnv); return $require[$requireName] ?? false; } /** * 添加 composer 依赖 * @param array $require 要添加的依赖数组["php" => ">=7.1.0",] * @param bool $devEnv 是否添加为开发环境依赖 * @param bool $cover 覆盖模式 * @throws Exception */ public function addComposerRequire(array $require, bool $devEnv = false, bool $cover = false) { $composerContent = $this->getComposer(true); if ($devEnv) { $composerRequire = $composerContent['require-dev']; $composerContent['require-dev'] = array_merge($composerContent['require-dev'], $require); } else { $composerRequire = $composerContent['require']; $composerContent['require'] = array_merge($composerContent['require'], $require); } if (!$cover) { foreach ($require as $key => $item) { if (isset($composerRequire[$key])) { throw new Exception($key . ' require already exists!'); } } } $this->setComposer($composerContent); } /** * 删除 composer 依赖 * @param array $require 要删除的依赖数组["php", "w7corp/easywechat"] * @param bool $devEnv 是否为开发环境删除依赖 * @throws Exception */ public function removeComposerRequire(array $require, bool $devEnv = false) { $composerContent = $this->getComposer(true); if ($devEnv) { foreach ($require as $item) { if (isset($composerContent['require-dev'][$item])) { unset($composerContent['require-dev'][$item]); } } } else { foreach ($require as $item) { if (isset($composerContent['require'][$item])) { unset($composerContent['require'][$item]); } } } $this->setComposer($composerContent); } /** * 设置 composer.json 内容 * @param array $content * @throws Exception */ public function setComposer(array $content) { if (!isset($content['require']) || !isset($content['name'])) { throw new Exception('composer.json file content is incomplete'); } $content = json_encode($content, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); $result = @file_put_contents($this->composer, $content . PHP_EOL); if (!$result) { throw new Exception('File has no write permission:composer.json'); } } /** * 获取 package.json 内容 * @param bool $realTime 获取实时内容 * @throws Exception */ public function getNpm(bool $realTime = false) { if (!file_exists($this->npm)) { throw new Exception(DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'package.json file does not exist!'); } if ($this->npmContent && !$realTime) return $this->npmContent; $npmContent = @file_get_contents($this->npm); $this->npmContent = json_decode($npmContent, true); if (!$this->npmContent) { throw new Exception(DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'package.json file read failure!'); } return $this->npmContent; } /** * 获取 npm 依赖 * @param bool $devEnv 是否是获取开发环境依赖 * @throws Exception */ public function getNpmDependencies(bool $devEnv = false) { $npmContent = $this->getNpm(); return $devEnv ? $npmContent['devDependencies'] : $npmContent['dependencies']; } /** * 是否存在某个 npm 依赖 * @param string $dependenciesName 依赖名称 * @param bool $devEnv 是否是获取开发环境依赖 * @throws Exception */ public function hasNpmDependencies(string $dependenciesName, bool $devEnv = false) { $dependencies = $this->getNpmDependencies($devEnv); return $dependencies[$dependenciesName] ?? false; } /** * 添加 npm 依赖 * @param array $dependencies 要添加的依赖数组["xxx" => ">=7.1.0",] * @param bool $devEnv 是否添加为开发环境依赖 * @param bool $cover 覆盖模式 * @throws Exception */ public function addNpmDependencies(array $dependencies, bool $devEnv = false, bool $cover = false) { $npmContent = $this->getNpm(true); if ($devEnv) { $npmDependencies = $npmContent['devDependencies']; $npmContent['devDependencies'] = array_merge($npmContent['devDependencies'], $dependencies); } else { $npmDependencies = $npmContent['dependencies']; $npmContent['dependencies'] = array_merge($npmContent['dependencies'], $dependencies); } if (!$cover) { foreach ($dependencies as $key => $item) { if (isset($npmDependencies[$key])) { throw new Exception($key . ' dependencie already exists!'); } } } $this->setNpm($npmContent); } /** * 删除 npm 依赖 * @param array $dependencies 要删除的依赖数组["xxx", "vue-i18n"] * @param bool $devEnv 是否添加为开发环境删除依赖 * @throws Exception */ public function removeNpmDependencies(array $dependencies, bool $devEnv = false) { $npmContent = $this->getNpm(true); if ($devEnv) { foreach ($dependencies as $dependency) { if (isset($npmContent['devDependencies'][$dependency])) { unset($npmContent['devDependencies'][$dependency]); } } } else { foreach ($dependencies as $dependency) { if (isset($npmContent['dependencies'][$dependency])) { unset($npmContent['dependencies'][$dependency]); } } } $this->setNpm($npmContent); } /** * 设置 package.json 内容 * @param array $content * @throws Exception */ public function setNpm(array $content) { if (!isset($content['dependencies']) || !isset($content['name'])) { throw new Exception('package.json file content is incomplete'); } $content = json_encode($content, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); $result = @file_put_contents($this->npm, $content . PHP_EOL); if (!$result) { throw new Exception('File has no write permission:package.json'); } } }