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
Imposter.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace TypistTech\Imposter; |
| 6 | |
| 7 | class Imposter implements ImposterInterface |
| 8 | { |
| 9 | /** |
| 10 | * @var string[] |
| 11 | */ |
| 12 | private $autoloads; |
| 13 | |
| 14 | /** |
| 15 | * @var string[] |
| 16 | */ |
| 17 | private $invalidAutoloads; |
| 18 | |
| 19 | /** |
| 20 | * @var ConfigCollectionInterface |
| 21 | */ |
| 22 | private $configCollection; |
| 23 | |
| 24 | /** |
| 25 | * @var TransformerInterface |
| 26 | */ |
| 27 | private $transformer; |
| 28 | |
| 29 | /** |
| 30 | * @var FilesystemInterface |
| 31 | */ |
| 32 | private $filesystem; |
| 33 | |
| 34 | /** |
| 35 | * Imposter constructor. |
| 36 | * |
| 37 | * @param ConfigCollectionInterface $configCollection |
| 38 | * @param TransformerInterface $transformer |
| 39 | * @param FilesystemInterface $filesystem |
| 40 | */ |
| 41 | public function __construct( |
| 42 | ConfigCollectionInterface $configCollection, |
| 43 | TransformerInterface $transformer, |
| 44 | FilesystemInterface $filesystem |
| 45 | ) { |
| 46 | $this->configCollection = $configCollection; |
| 47 | $this->transformer = $transformer; |
| 48 | $this->filesystem = $filesystem; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return ConfigCollectionInterface |
| 53 | */ |
| 54 | public function getConfigCollection(): ConfigCollectionInterface |
| 55 | { |
| 56 | return $this->configCollection; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return TransformerInterface |
| 61 | */ |
| 62 | public function getTransformer(): TransformerInterface |
| 63 | { |
| 64 | return $this->transformer; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Transform all autoload files. |
| 69 | * |
| 70 | * @return void |
| 71 | */ |
| 72 | public function run() |
| 73 | { |
| 74 | $autoloads = $this->getAutoloads(); |
| 75 | array_walk($autoloads, [$this, 'transform']); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get all valid (exist) autoload paths. |
| 80 | * |
| 81 | * @return string[] |
| 82 | */ |
| 83 | public function getAutoloads(): array |
| 84 | { |
| 85 | if ($this->autoloads === null) { |
| 86 | $this->setAutoloads(); |
| 87 | } |
| 88 | |
| 89 | return $this->autoloads; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get all autoload paths which defined in composer.json but not exist. |
| 94 | * |
| 95 | * @return string[] |
| 96 | */ |
| 97 | public function getInvalidAutoloads(): array |
| 98 | { |
| 99 | if ($this->invalidAutoloads === null) { |
| 100 | $this->setAutoloads(); |
| 101 | } |
| 102 | |
| 103 | return $this->invalidAutoloads; |
| 104 | } |
| 105 | |
| 106 | protected function setAutoloads(): void |
| 107 | { |
| 108 | $this->autoloads = []; |
| 109 | $this->invalidAutoloads = []; |
| 110 | |
| 111 | $autoloads = $this->configCollection->getAutoloads(); |
| 112 | |
| 113 | foreach ($autoloads as $autoload) { |
| 114 | $isValid = $this->filesystem->isFile($autoload) || $this->filesystem->isDir($autoload); |
| 115 | |
| 116 | if ($isValid) { |
| 117 | $this->autoloads[] = $autoload; |
| 118 | } else { |
| 119 | $this->invalidAutoloads[] = $autoload; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Transform a file or directory recursively. |
| 126 | * |
| 127 | * @param string $target Path to the target file or directory. |
| 128 | * |
| 129 | * @return void |
| 130 | */ |
| 131 | public function transform(string $target) |
| 132 | { |
| 133 | $this->transformer->transform($target); |
| 134 | } |
| 135 | } |
| 136 |