Пример #1
0
 /**
  * @param string[] $endOfLineMap  Associative array of file names and line
  *                                endings. Shell style globs using "*" are
  *                                allowed.
  * @param string[] $paths         List of paths to search for files to
  *                                convert. If empty uses current/present
  *                                working directory from getcwd().
  * @param string[] $excludedFiles List of files that should be excluded from
  *                                having line endings changed.
  *
  * @throws \DomainException
  * @throws \InvalidArgumentException
  * @throws PeolFileException
  * @throws PeolPathException
  * @return self
  */
 public function convertFiles(array $endOfLineMap, array $paths = array(), array $excludedFiles = array())
 {
     $paths = $paths ?: array(getcwd());
     /**
      * @var string $path
      */
     foreach ($paths as $path) {
         $path = $this->normalizePath($path);
         if (!is_readable($path) || !is_dir($path) || !is_writable($path)) {
             continue;
         }
         foreach ($endOfLineMap as $glob => $eol) {
             $filterItr = $this->getNewGlobFilter($path, $glob, $excludedFiles);
             if (empty($filterItr)) {
                 return $this;
             }
             /**
              * @var \FilesystemIterator $fsi
              */
             foreach ($filterItr as $fsi) {
                 $fullName = $fsi->getRealPath();
                 if (!$fsi->isWritable()) {
                     $mess = 'File is NOT writable: ' . $fullName;
                     throw new PeolFileException($mess);
                 }
                 $file = $fsi->openFile('rb+', false);
                 if (!$file->flock(LOCK_EX)) {
                     $mess = 'Could NOT get write lock on ' . $fullName;
                     throw new PeolFileException($mess);
                 }
                 try {
                     $tempFile = new \SplTempFileObject();
                     foreach ($file as $line) {
                         $this->convertString($line, $eol);
                         $tempFile->fwrite($line);
                     }
                     $tempFile->fflush();
                     $tempFile->rewind();
                     $file->ftruncate(0);
                     $file->fflush();
                     $file->rewind();
                     foreach ($tempFile as $line) {
                         $file->fwrite($line);
                     }
                     $file->fflush();
                 } catch (\RuntimeException $exp) {
                     $mess = 'File I/O failed on temp file for ' . $fullName;
                     throw new PeolFileException($mess, 0, $exp);
                 }
                 unset($tempFile, $file);
             }
         }
     }
     return $this;
 }