diff --git a/readme.md b/readme.md index 74e9874..6572e90 100644 --- a/readme.md +++ b/readme.md @@ -6,9 +6,10 @@ A php-package for parsing and evaluating the [yarn.lock](https://yarnpkg.com/lang/en/docs/yarn-lock/) format. ## Basic Usage + ```php getDependencies(); ``` ## Package Depth -If you maybe don't just want all packages but only the direct dependencies plus one level of indirection, you have to go a little extra mile: + +You can also query packages by depth: + ```php -dependencies; -// get these packages from the yarn lock-file -$rootDependencies = array_map(function($packageName, $packageVersion) use ($yarnLock) { - return $yarnLock->getPackage($packageName, $packageVersion); -}, array_keys($packageDependencies), array_values($packageDependencies)); -// some of our dependencies might be used by other dependencies deeper down the tree so -// they wouldn't appear in the top levels, if we wouldn't explicitly set them there. -$yarnLock->calculateDepth($rootDependencies); - -// get the first two levels; the second argument is the exclusive upper limit -$yarnLock->getPackagesByDepth(0, 2); -``` \ No newline at end of file +// Get just the direct dependencies. +$directDependencies = $yarnLock->getPackagesByDepth(0); +// Get the first two levels of dependencies. +$firstTwoLevels = $yarnLock->getPackagesByDepth(0, 2); +``` diff --git a/src/Package.php b/src/Package.php index dd28001..6cc1062 100644 --- a/src/Package.php +++ b/src/Package.php @@ -59,7 +59,7 @@ class Package * * @var Package[] */ - protected $resolves; + protected $resolves = []; /** * Depth in the dependency tree. Only initialized once the YarnLock computes @@ -208,9 +208,6 @@ public function getResolves() */ public function addResolves(Package $package) { - if ($this->resolves === null) { - $this->resolves = []; - } if (in_array($package, $this->resolves)) { return; } diff --git a/src/YarnLock.php b/src/YarnLock.php index 25ca7e5..4b8c060 100644 --- a/src/YarnLock.php +++ b/src/YarnLock.php @@ -133,6 +133,7 @@ public function getPackagesByName($packageName) */ public function getPackagesByDepth($start, $end = 0) { + $this->calculateDepth(); if ($end === 0 || ($end !== null && $end < $start)) { $end = $start + 1; }