| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace Dcat\Admin\Support;
- use Composer\Autoload\ClassLoader;
- class Composer
- {
- /**
- * @var array
- */
- protected static $files = [];
- /**
- * @var ClassLoader
- */
- protected static $loader;
- /**
- * 获取 composer 类加载器.
- *
- * @return ClassLoader
- */
- public static function loader()
- {
- if (! static::$loader) {
- static::$loader = include base_path().'/vendor/autoload.php';
- }
- return static::$loader;
- }
- /**
- * @param $path
- * @return ComposerProperty
- */
- public static function parse(?string $path)
- {
- return new ComposerProperty(static::fromJson($path));
- }
- /**
- * @param null|string $packageName
- * @param null|string $lockFile
- * @return null
- */
- public static function getVersion(?string $packageName, ?string $lockFile = null)
- {
- if (! $packageName) {
- return null;
- }
- $lockFile = $lockFile ?: base_path('composer.lock');
- $content = collect(static::fromJson($lockFile)['packages'] ?? [])
- ->filter(function ($value) use ($packageName) {
- return $value['name'] == $packageName;
- })->first();
- return $content['version'] ?? null;
- }
- /**
- * @param null|string $path
- * @return array
- */
- public static function fromJson(?string $path)
- {
- if (isset(static::$files[$path])) {
- return static::$files[$path];
- }
- if (! $path || ! is_file($path)) {
- return static::$files[$path] = [];
- }
- try {
- return static::$files[$path] = (array) json_decode(app('files')->get($path), true);
- } catch (\Throwable $e) {
- }
- return static::$files[$path] = [];
- }
- }
|