/** * Locate a source either if it a file or a directory and normalize it. Return an array of SplFileInfo. * * @param mixed $source * @return SplFileInfo[] * @throws Exception */ public function locate($source) { $sources = []; $normalizedSources = []; // if the wildcard is at the end of the string, it means that we look for a directory $endingWithWilCard = substr($source, strlen($source) - 2) === '/*'; if ($endingWithWilCard) { $source = substr($source, 0, strlen($source) - 2); } if (is_dir($source) || $endingWithWilCard) { $finder = new Finder(); $finder->in($source); foreach ($finder as $file) { $sources[] = $file; } } else { if (strstr($source, '*') !== false) { // if the source contains a wildcard, we use it with the finder component $sources = $this->getSourcesFromFinder($source); } else { $sources[] = $source; } } // each found sources should be normalized foreach ($sources as $source) { $normalizedSources[] = $this->normalizer->normalize($source); } return $normalizedSources; }
public function testNormalize() { $normalizer = new Normalizer($this->getCacheDir()); // string normalization $file = $this->createFile('test.css'); $normalizedFile = $normalizer->normalize($file); // it MUST return a instance of SplFileInfo representing the file $this->assertInstanceOf(SplFileInfo::class, $normalizedFile); $this->assertEquals($file, $normalizedFile->getRealPath()); $this->assertExceptionThrown(function () use($normalizer) { $normalizer->normalize('assets.missing.css'); }, 'File assets.missing.css not found, searched in assets.missing.css, /tmp/jk-spam-assets/assets.missing.css'); // SplFileInfo normalization $splFileInfo = $normalizer->normalize($normalizedFile); $this->assertEquals($splFileInfo->getRealPath(), $normalizedFile->getRealPath()); $this->assertExceptionThrown(function () use($normalizer) { $normalizer->normalize(new SplFileInfo('missing.css')); }, 'Unable to find missing.css during normalization process'); // other type should fail $this->assertExceptionThrown(function () use($normalizer) { $normalizer->normalize(42); }, 'The source should be a string if it is not an instance of SplInfo (instead of integer)'); // application path completion mkdir($this->getCacheDir() . '/test'); touch($this->getCacheDir() . '/test/test.css'); $normalizedFile = $normalizer->normalize('test/test.css'); $this->assertEquals($this->getCacheDir() . '/test/test.css', $normalizedFile->getRealPath()); }