ArrayUtil.php
3 years ago
Config.php
3 years ago
ConfigCollection.php
3 years ago
ConfigCollectionFactory.php
3 years ago
ConfigCollectionInterface.php
3 years ago
ConfigFactory.php
3 years ago
ConfigInterface.php
3 years ago
Filesystem.php
3 years ago
FilesystemInterface.php
3 years ago
Imposter.php
3 years ago
ImposterFactory.php
3 years ago
ImposterInterface.php
3 years ago
ProjectConfig.php
3 years ago
ProjectConfigInterface.php
3 years ago
StringUtil.php
3 years ago
Transformer.php
3 years ago
TransformerInterface.php
3 years ago
Config.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace TypistTech\Imposter; |
| 6 | |
| 7 | class Config implements ConfigInterface |
| 8 | { |
| 9 | /** |
| 10 | * @var string |
| 11 | */ |
| 12 | protected $packageDir; |
| 13 | |
| 14 | /** |
| 15 | * @var array |
| 16 | */ |
| 17 | private $config; |
| 18 | |
| 19 | public function __construct(string $packageDir, array $config) |
| 20 | { |
| 21 | $this->packageDir = StringUtil::addTrailingSlash($packageDir); |
| 22 | $this->config = $config; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return string[] |
| 27 | */ |
| 28 | public function getAutoloads(): array |
| 29 | { |
| 30 | return array_map(function (string $autoload): string { |
| 31 | return $this->packageDir . $autoload; |
| 32 | }, array_unique($this->getAutoloadPaths())); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @return string[] |
| 37 | */ |
| 38 | private function getAutoloadPaths(): array |
| 39 | { |
| 40 | $autoloads = $this->get('autoload'); |
| 41 | unset($autoloads['exclude-from-classmap']); |
| 42 | |
| 43 | return ArrayUtil::flattenMap(function ($autoloadConfig): array { |
| 44 | return $this->normalizeAutoload($autoloadConfig); |
| 45 | }, $autoloads); |
| 46 | } |
| 47 | |
| 48 | protected function get(string $key): array |
| 49 | { |
| 50 | return $this->config[$key] ?? []; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param $autoloadConfigs |
| 55 | * |
| 56 | * @return string[] |
| 57 | */ |
| 58 | private function normalizeAutoload($autoloadConfigs): array |
| 59 | { |
| 60 | if (! is_array($autoloadConfigs)) { |
| 61 | return [$autoloadConfigs]; |
| 62 | } |
| 63 | |
| 64 | return ArrayUtil::flattenMap(function ($autoloadConfig): array { |
| 65 | return $this->normalizeAutoload($autoloadConfig); |
| 66 | }, $autoloadConfigs); |
| 67 | } |
| 68 | |
| 69 | public function getPackageDir(): string |
| 70 | { |
| 71 | return $this->packageDir; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return string[] |
| 76 | */ |
| 77 | public function getRequires(): array |
| 78 | { |
| 79 | return array_keys($this->get('require')); |
| 80 | } |
| 81 | } |
| 82 |