| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace TypistTech\Imposter; |
| 6 |
|
| 7 |
class ConfigCollectionFactory |
| 8 |
{ |
| 9 |
public static function forProject( |
| 10 |
ProjectConfigInterface $projectConfig, |
| 11 |
Filesystem $filesystem |
| 12 |
): ConfigCollectionInterface { |
| 13 |
return static::addRequiredPackageConfigsRecursively( |
| 14 |
new ConfigCollection(), |
| 15 |
$projectConfig, |
| 16 |
$projectConfig, |
| 17 |
$filesystem |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
private static function addRequiredPackageConfigsRecursively( |
| 22 |
ConfigCollectionInterface $configCollection, |
| 23 |
ProjectConfigInterface $projectConfig, |
| 24 |
ConfigInterface $config, |
| 25 |
Filesystem $filesystem |
| 26 |
): ConfigCollectionInterface { |
| 27 |
$filteredRequires = static::getFilteredPackages($projectConfig, $config); |
| 28 |
|
| 29 |
foreach ($filteredRequires as $package) { |
| 30 |
$packageConfig = ConfigFactory::build( |
| 31 |
$projectConfig->getVendorDir() . $package . '/composer.json', |
| 32 |
$filesystem |
| 33 |
); |
| 34 |
|
| 35 |
$configCollection->add($packageConfig); |
| 36 |
|
| 37 |
static::addRequiredPackageConfigsRecursively( |
| 38 |
$configCollection, |
| 39 |
$projectConfig, |
| 40 |
$packageConfig, |
| 41 |
$filesystem |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
return $configCollection; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param ProjectConfigInterface $projectConfig |
| 50 |
* @param ConfigInterface $config |
| 51 |
* |
| 52 |
* @return string[] |
| 53 |
*/ |
| 54 |
private static function getFilteredPackages(ProjectConfigInterface $projectConfig, ConfigInterface $config): array |
| 55 |
{ |
| 56 |
$requiredPackages = array_filter($config->getRequires(), function (string $package) { |
| 57 |
return (false !== strpos($package, '/')); |
| 58 |
}); |
| 59 |
|
| 60 |
$nonComposerPackages = array_filter($requiredPackages, function (string $package) { |
| 61 |
return (false === strpos($package, 'composer/')); |
| 62 |
}); |
| 63 |
|
| 64 |
return array_filter($nonComposerPackages, function (string $package) use ($projectConfig) { |
| 65 |
return ! in_array($package, $projectConfig->getExcludes(), true); |
| 66 |
}); |
| 67 |
} |
| 68 |
} |
| 69 |
|