Improved hasPackage() performance

main
Michael Thessel 5 years ago
parent 6ffd3eb67c
commit f8010d5220

@ -28,6 +28,7 @@ class ArrayRepository extends BaseRepository
{ {
/** @var PackageInterface[] */ /** @var PackageInterface[] */
protected $packages; protected $packages;
protected $packageMap;
public function __construct(array $packages = array()) public function __construct(array $packages = array())
{ {
@ -121,15 +122,13 @@ class ArrayRepository extends BaseRepository
*/ */
public function hasPackage(PackageInterface $package) public function hasPackage(PackageInterface $package)
{ {
$packageId = $package->getUniqueName(); if (empty($this->packageMap)) {
foreach ($this->getPackages() as $repoPackage) {
foreach ($this->getPackages() as $repoPackage) { $this->packageMap[$repoPackage->getUniqueName()] = $repoPackage;
if ($packageId === $repoPackage->getUniqueName()) {
return true;
} }
} }
return false; return isset($this->packageMap[$package->getUniqueName()]);
} }
/** /**

Loading…
Cancel
Save