/** * Adds files using an iterator. * * @param Traversable $iterator The iterator. * @param string $message The message to announce. * @param boolean $binary Should the adding be binary-safe? * * @throws RuntimeException If a file is not readable. */ private function add(Traversable $iterator = null, $message = null, $binary = false) { static $count = 0; if ($iterator) { if ($message) { $this->putln('?', $message); } $box = $binary ? $this->box->getPhar() : $this->box; $baseRegex = $this->config->getBasePathRegex(); $mapper = $this->config->getMapper(); /** @var $file SplFileInfo */ foreach ($iterator as $file) { if (0 === ++$count % 100) { gc_collect_cycles(); } $relative = preg_replace($baseRegex, '', $file->getPathname()); if (null !== ($mapped = $mapper($relative))) { $relative = $mapped; } if ($this->isVerbose()) { if (false === $file->isReadable()) { throw new RuntimeException(sprintf('The file "%s" is not readable.', $file->getPathname())); } $this->putln('+', $file); if (null !== $mapped) { $this->putln('>', $relative); } } $box->addFile($file, $relative); } } }
public function testGetMapper() { $this->setConfig(array('map' => array(array('first/test/path' => 'a'), array('' => 'b/')))); $ds = DIRECTORY_SEPARATOR; $mapper = $this->config->getMapper(); $this->assertEquals("a{$ds}sub{$ds}path{$ds}file.php", $mapper('first/test/path/sub/path/file.php')); $this->assertEquals("b{$ds}second{$ds}test{$ds}path{$ds}sub{$ds}path{$ds}file.php", $mapper('second/test/path/sub/path/file.php')); }