private function fixFile(\SplFileInfo $file, $dryRun)
 {
     $new = $old = file_get_contents($file->getRealpath());
     $appliedFixers = [];
     Tokens::clearCache();
     try {
         foreach ($this->fixers as $fixer) {
             if (!$fixer->supports($file)) {
                 continue;
             }
             $newest = $fixer->fix($file, $new);
             if ($newest !== $new) {
                 $appliedFixers[] = $fixer->getName();
             }
             $new = $newest;
         }
     } catch (\Exception $e) {
         if ($this->errorsManager) {
             $this->errorsManager->report(ErrorsManager::ERROR_TYPE_EXCEPTION, $this->getFileRelativePathname($file), $e->__toString());
         }
         return;
     }
     if ($new !== $old) {
         if (!$dryRun) {
             file_put_contents($file->getRealpath(), $new);
         }
     }
     return $appliedFixers;
 }
Example #2
0
 public function processFile(\SplFileInfo $file)
 {
     try {
         $parseResult = $this->parser->parse(new SourceFile($file->getRealpath()));
         if ($parseResult->hasRedeclarations() && !$this->tolerantMode) {
             throw new CollectorException(sprintf("Duplicate (potentially conditional) definitions of the following unit(s) found:\n\n\tUnit(s): %s\n\tFile: %s", join(', ', $parseResult->getRedeclarations()), $file->getRealPath()), CollectorException::InFileRedeclarationFound);
         }
         $this->collectorResult->addParseResult($file, $parseResult);
     } catch (ParserException $e) {
         throw new CollectorException(sprintf("Could not process file '%s' due to parse errors", $file->getRealPath()), CollectorException::ParseErrror, $e);
     } catch (CollectorResultException $e) {
         throw new CollectorException($e->getMessage(), CollectorException::RedeclarationFound);
     }
 }
Example #3
0
/**
 * Get the size of a directory or a file
 *
 * @param SplFileInfo $file SplFileInfo instance
 *
 * @return int The calculated size
 */
function get_size(\SplFileInfo $file)
{
    $size = 0;
    try {
        if ($file->isFile()) {
            $size += $file->getSize();
        } else {
            foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($file->getRealpath())) as $f) {
                $size += $f->getSize();
            }
        }
    } catch (\RuntimeException $e) {
    }
    return $size;
}
Example #4
0
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun)
 {
     $new = $old = file_get_contents($file->getRealpath());
     $appliedFixers = array();
     foreach ($fixers as $fixer) {
         if (!$fixer->supports($file)) {
             continue;
         }
         $new1 = $fixer->fix($file, $new);
         if ($new1 != $new) {
             $appliedFixers[] = $fixer->getName();
         }
         $new = $new1;
     }
     if ($new != $old) {
         if (!$dryRun) {
             file_put_contents($file->getRealpath(), $new);
         }
         return $appliedFixers;
     }
 }
 /**
  * This method will create reflection class instances for all interfaces
  * and classes that can be found in the given source code files.
  *
  * @param string $pathnames The source directory that is the target of the
  *        class search.
  *
  * @return Iterator
  */
 public function find(array $paths)
 {
     $classes = array();
     foreach ($paths as $path) {
         if (file_exists($path) === false || !is_file($path)) {
             throw new \LogicException('Invalid or nonexistent file ' . $path);
         }
         $fileInfo = new \SplFileInfo($path);
         $fileClasses = $this->parseFile($fileInfo->getRealpath());
         $classes = array_merge($classes, $fileClasses);
     }
     return new \ArrayIterator($classes);
 }
 /**
  * Get the total filesize for a given file or directory
  *
  * If $file is a file then just return the result of `filesize()`.
  * If $file is a directory then schedule a recursive filesize scan.
  *
  * @param \SplFileInfo $file The file or directory you want to know the size of
  * @param bool $skip_excluded_files Skip excluded files when calculating a directories total size
  *
  * @return int                        The total of the file or directory
  */
 public function filesize(\SplFileInfo $file, $skip_excluded_files = false)
 {
     // Skip missing or unreadable files
     if (!file_exists($file->getPathname()) || !$file->getRealpath() || !$file->isReadable()) {
         return false;
     }
     // If it's a file then just pass back the filesize
     if ($file->isFile() && $file->isReadable()) {
         return $file->getSize();
     }
     // If it's a directory then pull it from the cached filesize array
     if ($file->isDir()) {
         // If we haven't calculated the site size yet then kick it off in a thread
         $directory_sizes = get_transient('hmbkp_directory_filesizes');
         if (!is_array($directory_sizes)) {
             if (!$this->is_site_size_being_calculated()) {
                 // Mark the filesize as being calculated
                 set_transient('hmbkp_directory_filesizes_running', true, HOUR_IN_SECONDS);
                 // Schedule a Backdrop task to trigger a recalculation
                 $task = new \HM\Backdrop\Task(array($this, 'recursive_filesize_scanner'));
                 $task->schedule();
             }
             return;
         }
         if ($this->backup->get_root() === $file->getPathname()) {
             return $directory_sizes[$file->getPathname()];
         }
         $current_pathname = trailingslashit($file->getPathname());
         $root = trailingslashit($this->backup->get_root());
         foreach ($directory_sizes as $path => $size) {
             // Remove any files that aren't part of the current tree
             if (false === strpos($path, $current_pathname)) {
                 unset($directory_sizes[$path]);
             }
         }
         if ($skip_excluded_files) {
             $excludes = $this->backup->exclude_string('regex');
             foreach ($directory_sizes as $path => $size) {
                 // Skip excluded files if we have excludes
                 if ($excludes && preg_match('(' . $excludes . ')', str_ireplace($root, '', Backup::conform_dir($path)))) {
                     unset($directory_sizes[$path]);
                 }
             }
         }
         // Directory size is now just a sum of all files across all sub directories
         return array_sum($directory_sizes);
     }
 }
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun, $diff, FileCacheManager $fileCacheManager)
 {
     $new = $old = file_get_contents($file->getRealpath());
     if (!$fileCacheManager->needFixing($this->getFileRelativePathname($file), $old)) {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_SKIPPED));
         }
         return;
     }
     if ($this->lintManager && !$this->lintManager->createProcessForFile($file->getRealpath())->isSuccessful()) {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_INVALID));
         }
         return;
     }
     $appliedFixers = array();
     // we do not need Tokens to still caching previously fixed file - so clear the cache
     Tokens::clearCache();
     try {
         foreach ($fixers as $fixer) {
             if (!$fixer->supports($file)) {
                 continue;
             }
             $newest = $fixer->fix($file, $new);
             if ($newest !== $new) {
                 $appliedFixers[] = $fixer->getName();
             }
             $new = $newest;
         }
     } catch (\Exception $e) {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_EXCEPTION));
         }
         if ($this->errorsManager) {
             $this->errorsManager->report(ErrorsManager::ERROR_TYPE_EXCEPTION, $this->getFileRelativePathname($file), $e->__toString());
         }
         return;
     }
     $fixInfo = null;
     if ($new !== $old) {
         if ($this->lintManager) {
             $lintProcess = $this->lintManager->createProcessForSource($new);
             if (!$lintProcess->isSuccessful()) {
                 if ($this->eventDispatcher) {
                     $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_LINT));
                 }
                 if ($this->errorsManager) {
                     $this->errorsManager->report(ErrorsManager::ERROR_TYPE_LINT, $this->getFileRelativePathname($file), $lintProcess->getOutput());
                 }
                 return;
             }
         }
         if (!$dryRun) {
             file_put_contents($file->getRealpath(), $new);
         }
         $fixInfo = array('appliedFixers' => $appliedFixers);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
     }
     $fileCacheManager->setFile($this->getFileRelativePathname($file), $new);
     if ($this->eventDispatcher) {
         $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus($fixInfo ? FixerFileProcessedEvent::STATUS_FIXED : FixerFileProcessedEvent::STATUS_NO_CHANGES));
     }
     return $fixInfo;
 }
Example #8
0
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun, $diff)
 {
     $new = $old = file_get_contents($file->getRealpath());
     $appliedFixers = array();
     foreach ($fixers as $fixer) {
         if (!$fixer->supports($file)) {
             continue;
         }
         $new1 = $fixer->fix($file, $new);
         if ($new1 != $new) {
             $appliedFixers[] = $fixer->getName();
         }
         $new = $new1;
     }
     if ($new != $old) {
         if (!$dryRun) {
             file_put_contents($file->getRealpath(), $new);
         }
         $fixInfo = array('appliedFixers' => $appliedFixers);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
         return $fixInfo;
     }
 }
Example #9
0
 /**
  * Uploads a file or directory to an FTP connection.
  *
  * @param \Ftp         $ftp  An active FTP connection.
  * @param \SplFileInfo $file A local file to upload.
  */
 protected function upload(\Ftp $ftp, \SplFileInfo $file)
 {
     // enter into passive mode
     $ftp->pasv(true);
     // move to the file's parent directory
     $ftp->chdir($this->targetDirectory . '/' . $file->getRelativePath());
     // check if the file exists
     $fileExists = in_array($file->getBasename(), $ftp->nlist('.'));
     // check if the file is a directory
     if ($file->isDir()) {
         // create the directory if it does not exist
         if (!$fileExists) {
             $this->printTaskInfo('Creating directory: ' . $file->getRelativePathname());
             // create directory
             $ftp->mkdir($file->getBasename());
         }
     } else {
         // if the file already exists, check our skip options
         if ($fileExists) {
             // skip the file if the file sizes are equal
             if ($this->skipSizeEqual && $ftp->size($file->getBasename()) === $file->getSize()) {
                 return;
             }
             // skip the file if modified time is same or newer than source
             if ($this->skipUnmodified && $ftp->mdtm($file->getBasename()) >= $file->getMTime()) {
                 return;
             }
         }
         // try to upload the file
         $this->printTaskInfo('Uploading: ' . $file->getRelativePathname());
         if (!$ftp->put($file->getBasename(), $file->getRealpath(), FTP_BINARY)) {
             // something went wrong
             return Result::error($this, 'Failed while uploading file ' . $file->getRelativePathname());
         }
     }
 }
Example #10
0
 public function conVertFile(\SplFileInfo $file, array $converter, $dryRun, $diff, $outputExt)
 {
     $new = $old = file_get_contents($file->getRealpath());
     $appliedConverters = array();
     foreach ($converter as $convert) {
         if (!$convert->supports($file)) {
             continue;
         }
         $new1 = $convert->convert($file, $new);
         if ($new1 != $new) {
             $appliedConverters[] = $convert->getName();
         }
         $new = $new1;
     }
     if ($new != $old) {
         if (!$dryRun) {
             $filename = $file->getRealpath();
             $ext = strrchr($filename, '.');
             if ($outputExt) {
                 $filename = rtrim($filename, $ext) . '.' . trim($outputExt, '.');
             }
             file_put_contents($filename, $new);
         }
         $fixInfo = array('appliedConverters' => $appliedConverters);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
         return $fixInfo;
     }
 }
Example #11
0
 /**
  * @param  \Yosmanyga\Resource\Resource $resource
  * @param  \SplFileInfo                 $file
  * @return \Yosmanyga\Resource\Resource
  */
 private function convertResource(Resource $resource, \SplFileInfo $file)
 {
     return new Resource(array_merge($resource->getMetadata(), array('file' => $file->getRealpath())), $resource->getMetadata('type'));
 }
 private function _tryToBuildAddonFromFile(SplFileInfo $infoFile)
 {
     $manifestFilePath = realpath("{$infoFile}");
     $infoFileContents = @json_decode(file_get_contents($manifestFilePath), true);
     $shouldLog = $this->_logger->isHandling(ehough_epilog_Logger::DEBUG);
     if ($infoFileContents === null || $infoFileContents === false || empty($infoFileContents)) {
         if ($shouldLog) {
             $this->_logger->debug('Could not parse add-on manifest file at ' . $manifestFilePath);
         }
         return null;
     }
     try {
         return $this->_constructAddonFromArray($infoFileContents, $manifestFilePath);
     } catch (Exception $e) {
         if ($shouldLog) {
             $this->_logger->warn('Caught exception when parsing info file at ' . $infoFile->getRealpath() . ': ' . $e->getMessage());
         }
         return null;
     }
 }
Example #13
0
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun, $diff, FileCacheManager $fileCacheManager)
 {
     $new = $old = file_get_contents($file->getRealpath());
     if ('' === $old || !$fileCacheManager->needFixing($this->getFileRelativePathname($file), $old) || PHP_VERSION_ID >= 50306 && PHP_VERSION_ID < 50400 && false !== stripos($old, '__halt_compiler()')) {
         $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_SKIPPED));
         return;
     }
     try {
         $this->linter->lintFile($file->getRealpath());
     } catch (LintingException $e) {
         $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_INVALID));
         $this->errorsManager->report(new Error(Error::TYPE_INVALID, $this->getFileRelativePathname($file)));
         return;
     }
     $old = file_get_contents($file->getRealpath());
     $appliedFixers = array();
     // we do not need Tokens to still caching previously fixed file - so clear the cache
     Tokens::clearCache();
     $tokens = Tokens::fromCode($old);
     $newHash = $oldHash = $tokens->getCodeHash();
     try {
         foreach ($fixers as $fixer) {
             if (!$fixer->supports($file) || !$fixer->isCandidate($tokens)) {
                 continue;
             }
             $fixer->fix($file, $tokens);
             if ($tokens->isChanged()) {
                 $tokens->clearEmptyTokens();
                 $tokens->clearChanged();
                 $appliedFixers[] = $fixer->getName();
             }
         }
     } catch (\Exception $e) {
         $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_EXCEPTION));
         $this->errorsManager->report(new Error(Error::TYPE_EXCEPTION, $this->getFileRelativePathname($file)));
         return;
     }
     $fixInfo = null;
     if (!empty($appliedFixers)) {
         $new = $tokens->generateCode();
         $newHash = $tokens->getCodeHash();
     }
     // We need to check if content was changed and then applied changes.
     // But we can't simple check $appliedFixers, because one fixer may revert
     // work of other and both of them will mark collection as changed.
     // Therefore we need to check if code hashes changed.
     if ($oldHash !== $newHash) {
         try {
             $this->linter->lintSource($new);
         } catch (LintingException $e) {
             $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_LINT));
             $this->errorsManager->report(new Error(Error::TYPE_LINT, $this->getFileRelativePathname($file)));
             return;
         }
         if (!$dryRun && false === @file_put_contents($file->getRealpath(), $new)) {
             $error = error_get_last();
             if ($error) {
                 throw new IOException(sprintf('Failed to write file "%s", "%s".', $file->getRealpath(), $error['message']), 0, null, $file->getRealpath());
             }
             throw new IOException(sprintf('Failed to write file "%s".', $file->getRealpath()), 0, null, $file->getRealpath());
         }
         $fixInfo = array('appliedFixers' => $appliedFixers);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
     }
     $fileCacheManager->setFile($this->getFileRelativePathname($file), $new);
     $this->dispatchEvent(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus($fixInfo ? FixerFileProcessedEvent::STATUS_FIXED : FixerFileProcessedEvent::STATUS_NO_CHANGES));
     return $fixInfo;
 }
Example #14
0
 public function fixFile(\SplFileInfo $file, array $fixers, $dryRun, $diff, FileCacheManager $fileCacheManager)
 {
     $new = $old = file_get_contents($file->getRealpath());
     if (!$fileCacheManager->needFixing($this->getFileRelativePathname($file), $old)) {
         if ($this->eventDispatcher) {
             $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus(FixerFileProcessedEvent::STATUS_SKIPPED));
         }
         return;
     }
     $appliedFixers = array();
     // we do not need Tokens to still caching previously fixed file - so clear the cache
     Tokens::clearCache();
     foreach ($fixers as $fixer) {
         if (!$fixer->supports($file)) {
             continue;
         }
         $newest = $fixer->fix($file, $new);
         if ($newest !== $new) {
             $appliedFixers[] = $fixer->getName();
         }
         $new = $newest;
     }
     $fixInfo = null;
     if ($new !== $old) {
         if (!$dryRun) {
             file_put_contents($file->getRealpath(), $new);
         }
         $fixInfo = array('appliedFixers' => $appliedFixers);
         if ($diff) {
             $fixInfo['diff'] = $this->stringDiff($old, $new);
         }
     }
     $fileCacheManager->setFile($this->getFileRelativePathname($file), $new);
     if ($this->eventDispatcher) {
         $this->eventDispatcher->dispatch(FixerFileProcessedEvent::NAME, FixerFileProcessedEvent::create()->setStatus($fixInfo ? FixerFileProcessedEvent::STATUS_FIXED : FixerFileProcessedEvent::STATUS_NO_CHANGES));
     }
     return $fixInfo;
 }
Example #15
0
 public function updateContentDir(DOMElement $parent, SplFileInfo $directory, $role)
 {
     $dir = $parent->ownerDocument->createElement('dir');
     $dir->setAttribute('name', $directory->getFilename());
     if ($this->basedir) {
         $dir->setAttribute('baseinstalldir', $this->basedir);
     }
     $parent->appendChild($dir);
     foreach (new DirectoryIterator($directory->getRealpath()) as $file) {
         if ($file->isDot()) {
             continue;
         } else {
             if ($file->isDir()) {
                 $this->updateContentDir($dir, $file, $role);
             } else {
                 $this->updateContentFile($dir, $file, $role);
             }
         }
     }
     return $parent;
 }
 /**
  * @param SplFileInfo $value1
  * @param SplFileInfo $value2
  * @return int
  */
 protected function compare($value1, $value2)
 {
     return strcasecmp($value2->getRealpath(), $value1->getRealpath());
 }
Example #17
0
 public function _callbackGetAcceptData(SplFileInfo $a, SplFileInfo $b)
 {
     return strcmp($a->getRealpath(), $b->getRealpath());
 }
Example #18
0
 public function _callbackTestSort(SplFileInfo $a, SplFileInfo $b)
 {
     return strcmp($a->getRealpath(), $b->getRealpath());
 }
 /**
  * Get the total filesize for a given file or directory. Aware of exclusions.
  *
  * If $file is a file then return the result of `filesize()` or 0 if it's excluded.
  * If $file is a directory then recursively calculate the size without
  * the size of excluded files/directories.
  *
  * @param \SplFileInfo   $file The file or directory you want to know the size of.
  *
  * @return int           The total filesize of the file or directory without
  *                       the size of excluded files/directories.
  */
 public function filesize(\SplFileInfo $file)
 {
     // Skip missing or unreadable files.
     if (!file_exists($file->getPathname()) || !$file->getRealpath() || !$file->isReadable()) {
         return 0;
     }
     // If it's a file then return its filesize or 0 if it's excluded.
     if ($file->isFile()) {
         if ($this->excludes && $this->excludes->is_file_excluded($file)) {
             return 0;
         } else {
             return $file->getSize();
         }
     }
     // If it's a directory then pull it from the cached filesize array.
     if ($file->isDir()) {
         return $this->directory_filesize($file);
     }
 }
Example #20
0
 /**
  * Get the total filesize for a given file or directory
  *
  * If $file is a file then just return the result of `filesize()`.
  * If $file is a directory then recursively calculate the size.
  *
  * @param \SplFileInfo   $file The file or directory you want to know the size of
  *
  * @return int           The total filesize of the file or directory
  */
 public function filesize(\SplFileInfo $file)
 {
     // Skip missing or unreadable files
     if (!file_exists($file->getPathname()) || !$file->getRealpath() || !$file->isReadable()) {
         return 0;
     }
     // If it's a file then just pass back the filesize
     if ($file->isFile()) {
         return $file->getSize();
     }
     // If it's a directory then pull it from the cached filesize array
     if ($file->isDir()) {
         return $this->directory_filesize($file);
     }
 }
Example #21
0
 private function wasModifiedSinceLastRun(SplFileInfo $file)
 {
     return filemtime($file->getRealpath()) > self::$lastTime;
 }