| 1 | <?php |
||
| 9 | class Locator |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var Normalizer |
||
| 13 | */ |
||
| 14 | protected $normalizer; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Locator constructor. |
||
| 18 | * |
||
| 19 | * @param Normalizer $normalizer |
||
| 20 | */ |
||
| 21 | 1 | public function __construct(Normalizer $normalizer) |
|
| 25 | |||
| 26 | /** |
||
| 27 | * Locate a source either if it a file or a directory and normalize it. Return an array of SplFileInfo. |
||
| 28 | * |
||
| 29 | * @param $source |
||
| 30 | * @return SplFileInfo[] |
||
| 31 | * @throws Exception |
||
| 32 | */ |
||
| 33 | 1 | public function locate($source) |
|
| 34 | { |
||
| 35 | 1 | $sources = []; |
|
| 36 | 1 | $normalizedSources = []; |
|
| 37 | |||
| 38 | 1 | if (is_dir($source)) { |
|
| 39 | 1 | $finder = new Finder(); |
|
| 40 | 1 | $finder->in($source); |
|
| 41 | |||
| 42 | 1 | foreach ($finder as $file) { |
|
| 43 | 1 | $sources[] = $file; |
|
| 44 | } |
||
| 45 | 1 | } else if (strstr($source, '*') !== false) { |
|
| 46 | // if the source contains a wildcard, we use it with the finder component |
||
| 47 | $sources = $this->getSourcesFromFinder($source); |
||
| 48 | } else { |
||
| 49 | 1 | $sources[] = $source; |
|
| 50 | } |
||
| 51 | |||
| 52 | // each found sources should be normalized |
||
| 53 | 1 | foreach ($sources as $source) { |
|
| 54 | 1 | $normalizedSources[] = $this |
|
| 55 | 1 | ->normalizer |
|
| 56 | 1 | ->normalize($source); |
|
| 57 | } |
||
| 58 | |||
| 59 | 1 | return $normalizedSources; |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return Normalizer |
||
| 64 | */ |
||
| 65 | 1 | public function getNormalizer() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param $source |
||
| 72 | * @return SplFileInfo[] |
||
| 73 | */ |
||
| 74 | protected function getSourcesFromFinder($source) |
||
| 99 | } |
||
| 100 |