1010use Composer \Package \PackageInterface ;
1111use Yiisoft \Config \ConfigPaths ;
1212use Yiisoft \Config \Options ;
13+ use Yiisoft \Strings \WildcardPattern ;
1314
1415use function dirname ;
16+ use function is_string ;
1517use function realpath ;
1618use function str_replace ;
1719
@@ -25,6 +27,11 @@ final class ProcessHelper
2527 private Options $ rootPackageOptions ;
2628 private array $ rootPackageExtra ;
2729
30+ /**
31+ * @psalm-var array<string, CompletePackage>
32+ */
33+ private array $ packages ;
34+
2835 /**
2936 * @param Composer $composer The composer instance.
3037 */
@@ -45,16 +52,53 @@ public function __construct(Composer $composer)
4552 $ this ->composer = $ composer ;
4653 $ this ->rootPackageOptions = new Options ($ this ->rootPackageExtra );
4754 $ this ->paths = new ConfigPaths ($ rootPath , $ this ->rootPackageOptions ->sourceDirectory ());
55+ $ this ->packages = (new PackagesListBuilder ($ this ->composer ))->build ();
56+ }
57+
58+ /**
59+ * Returns all vendor packages.
60+ *
61+ * @psalm-return array<string, CompletePackage>
62+ */
63+ public function getPackages (): array
64+ {
65+ return $ this ->packages ;
4866 }
4967
5068 /**
51- * Builds and returns packages.
69+ * Returns vendor packages without packages from the vendor override sublayer .
5270 *
53- * @return array<string, CompletePackage>
71+ * @psalm- return array<string, CompletePackage>
5472 */
55- public function buildPackages (): array
73+ public function getVendorPackages (): array
5674 {
57- return (new PackagesListBuilder ($ this ->composer ))->build ();
75+ $ vendorPackages = [];
76+
77+ foreach ($ this ->packages as $ name => $ package ) {
78+ if (!$ this ->isVendorOverridePackage ($ name )) {
79+ $ vendorPackages [$ name ] = $ package ;
80+ }
81+ }
82+
83+ return $ vendorPackages ;
84+ }
85+
86+ /**
87+ * Returns vendor packages only from the vendor override sublayer.
88+ *
89+ * @psalm-return array<string, CompletePackage>
90+ */
91+ public function getVendorOverridePackages (): array
92+ {
93+ $ vendorOverridePackages = [];
94+
95+ foreach ($ this ->packages as $ name => $ package ) {
96+ if ($ this ->isVendorOverridePackage ($ name )) {
97+ $ vendorOverridePackages [$ name ] = $ package ;
98+ }
99+ }
100+
101+ return $ vendorOverridePackages ;
58102 }
59103
60104 /**
@@ -84,6 +128,19 @@ public function getRelativePackageFilePath(PackageInterface $package, string $fi
84128 return str_replace ("{$ this ->getPackageRootDirectoryPath ($ package )}/ " , '' , $ filePath );
85129 }
86130
131+ /**
132+ * Returns the relative path to the package file including the package name.
133+ *
134+ * @param PackageInterface $package The package instance.
135+ * @param string $filePath The absolute path to the package file.
136+ *
137+ * @return string The relative path to the package file including the package name.
138+ */
139+ public function getRelativePackageFilePathWithPackageName (PackageInterface $ package , string $ filePath ): string
140+ {
141+ return "{$ package ->getPrettyName ()}/ {$ this ->getRelativePackageFilePath ($ package , $ filePath )}" ;
142+ }
143+
87144 /**
88145 * Returns the package filename excluding the source directory {@see Options::sourceDirectory()}.
89146 *
@@ -184,4 +241,26 @@ private function getPackageRootDirectoryPath(PackageInterface $package): string
184241 {
185242 return $ this ->composer ->getInstallationManager ()->getInstallPath ($ package );
186243 }
244+
245+ /**
246+ * Checks whether the package is in the vendor override sublayer.
247+ *
248+ * @param string $package The package name.
249+ *
250+ * @return bool Whether the package is in the vendor override sublayer.
251+ */
252+ private function isVendorOverridePackage (string $ package ): bool
253+ {
254+ foreach ($ this ->rootPackageOptions ->vendorOverrideLayerPackages () as $ pattern ) {
255+ if (!is_string ($ pattern )) {
256+ continue ;
257+ }
258+
259+ if ($ package === $ pattern || (new WildcardPattern ($ pattern ))->match ($ package )) {
260+ return true ;
261+ }
262+ }
263+
264+ return false ;
265+ }
187266}
0 commit comments