/** * Recursively adds files from a directory to the archive. * * The `$regex` is used as a whitelist filter. Any path in the directory * that matches the regular expression will be added to the archive. Any * non-matching paths will be excluded. * * @param string $path The path to the directory. * @param string $filter The regular expression filter. * * @return array An associative array mapping archive paths to file system paths. */ public function buildFromDirectory($path, $filter = null) { if (null === $this->dispatcher) { return parent::buildFromDirectory($path, $filter); } $event = new PreBuildFromDirectoryEvent($this, $path, $filter); $this->dispatcher->dispatch(Events::PRE_BUILD_FROM_DIRECTORY, $event); $map = array(); if (!$event->isSkipped()) { /* * By using `Phar::buildFromDirectory()` directly, it eliminates * any possibility of making changes to the files as they are * passed to the archive. To work around this issue, the data is * used to create an iterator achieving the same purpose, but using * `Builder::buildFromIterator()` instead. */ $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($event->getPath(), RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS)); if (null !== $filter) { $iterator = new RegexIterator($iterator, $event->getFilter()); } $map = $this->buildFromIterator($iterator, $event->getPath()); $event = new PostBuildFromDirectoryEvent($this, $event->getPath(), $event->getFilter()); $this->dispatcher->dispatch(Events::POST_BUILD_FROM_DIRECTORY, $event); } return $map; }
/** * Logs when a directory is added. * * @param PostBuildFromDirectoryEvent $event The event arguments. */ public function onPostBuildFromDirectory(PostBuildFromDirectoryEvent $event) { $this->logger->info(sprintf('The directory "%s" has been added.', basename($event->getPath())), array('filter' => $event->getFilter(), 'path' => $event->getPath())); }