/** * @return void */ public function testInit() { $filePath = __DIR__ . '/example_read.txt'; $fileObject = new FileReader($filePath, 'r', true); $this->assertEquals($filePath, $fileObject->getPathname()); $this->assertTrue($fileObject->isReadable()); $fileObject = new \SplFileObject($filePath, 'r'); $this->assertEquals($filePath, $fileObject->getPathname()); $this->assertTrue($fileObject->isReadable()); }
/** * Add input argument * * @param string $filePath * Input file path * * @return Identify * @throws \InvalidArgumentException */ public function in($filePath) { if (!file_exists($filePath)) { $message = 'The input file path (' . $filePath . ') is invalid or the file could not be located.'; throw new \InvalidArgumentException($message); } $file = new \SplFileObject($filePath); if ($file->isReadable()) { $this->inputFile = '"' . $file->getPathname() . '"'; } $this->getQuery()->dirty(); return $this; }
<?php $regex = '/^([a-zA-Z0-9\\/]?)+[a-zA-Z0-9_]+\\.{1}[a-z]+$/'; /// test for file name validity (not complete) $delimieter = array('|', "\t"); if (preg_match($regex, $argv[1]) === 1 && preg_match($regex, $argv[2]) === 1) { try { $values = array(); $s1 = new SplFileObject($argv[1], 'r'); $s1->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE); if ($s1->isFile() && $s1->isReadable()) { $s1->seek(1); while ($row = $s1->fgetcsv($delimieter[0])) { $data = array($row[7], $row[8]); if (!in_array($data, $values)) { $values[] = $data; } } unset($row); usort($values, function ($a, $b) { return $a[0] > $b[0]; }); try { $s2 = new SplFileObject($argv[2], 'w'); $s2->fputcsv(array('id', 'code'), $delimieter[1]); foreach ($values as $data) { $s2->fputcsv($data, "\t"); } } catch (RuntimeException $e) { echo $e->getMessage(); exit($e->severity);
/** * Validate the current installation directory against an existing checksum file * This reports any changes to your installation directory - added, removed or changed files * * @author Steve Kenow <*****@*****.**> * */ public static function validateChecksum() { $validationFile = new SplFileObject($checkfile); if ($validationFile->isReadable()) { $currentHash = $currentPerms = array(); $cache_dir = preg_replace('#[\\|/]#', DIRECTORY_SEPARATOR, ICMS_CACHE_PATH); $templates_dir = preg_replace('#[\\|/]#', DIRECTORY_SEPARATOR, ICMS_COMPILE_PATH); foreach (new RecursiveIteratorIterator($dir) as $name => $item) { $itemPath = $item->getPath(); $itemFilename = $item->getBasename(); $itemPerms = $item->getPerms(); /* exclude cache and templates_c directories */ if ($itemPath != $cache_dir && $itemPath != $templates_dir) { $fileHash = sha1_file($name); $currentHash[$name] = $fileHash; $currentPerms[$name] = $itemPerms; } } echo _CORE_CHECKSUM_CHECKFILE . $checkfile . '<br />'; $validHash = $validPerms = array(); while (!$validationFile->eof()) { list($filename, $checksum, $filePermissions) = $validationFile->fgetcsv(';'); $validHash[$filename] = $checksum; $validPerms[$filename] = $filePermissions; } $hashVariations = array_diff_assoc($validHash, $currentHash); // changed or removed files $addedFiles = array_diff_key($currentHash, $validHash); $missingFiles = array_diff_key($validHash, $currentHash); $permVariations = array_diff_assoc($validPerms, $currentPerms); // changed permissions or removed files echo '<br /><strong>' . count($hashVariations) . _CORE_CHECKSUM_ALTERED_REMOVED . '</strong><br />'; foreach ($hashVariations as $file => $check) { echo $file . '<br />'; } echo '<br /><strong>' . count($addedFiles) . _CORE_CHECKSUM_FILES_ADDED . '</strong><br />'; foreach ($addedFiles as $file => $hash) { echo $file . '<br />'; } echo '<br /><strong>' . count($missingFiles) . _CORE_CHECKSUM_FILES_REMOVED . '</strong><br />'; foreach ($missingFiles as $file => $hash) { echo $file . '<br />'; } echo '<br /><strong>' . count($permVariations) . _CORE_CHECKSUM_PERMISSIONS_ALTERED . '</strong><br />'; foreach ($permVariations as $file => $perms) { echo $file . '<br />'; } } else { echo _CORE_CHECKSUM_CHECKFILE_UNREADABLE; } unset($validationFile); unset($item); unset($dir); }
/** * Reads an dune import file containing dune data and returns a site object. * * @param string $filePath The file path location for the * @param bool $errorOnBadData If true and exception will be thrown due to a line of bad data. If false the * line is skipped silently. * * @return Site A Site object populated with dunes. * @throws InvalidOperationException If database credentials have not been set (required for dune creation) * @throws MyInvalidArgumentException If the import path is invalid. */ public final function importDunes($filePath, $errorOnBadData = TRUE) { if (is_null(self::$databaseCredentials)) { throw new InvalidOperationException('Database credentials must be set at the class level to allow this action to take place.'); } Dune::setDatabaseCredentials(self::$databaseCredentials); $fileHandle = new SplFileObject($filePath); if (!$fileHandle->isFile() && !$fileHandle->isReadable() && $fileHandle->getExtension() != 'txt') { throw new MyInvalidArgumentException('The specified import file path does not point to a valid readable text (.txt) file.'); } while (!$fileHandle->eof()) { $duneData = $fileHandle->fgetcsv(' '); if ($duneData[0] == NULL || substr($duneData[0], 0, 1) == '%') { continue; // Skip the line } if (count($duneData) != 6) { if ($errorOnBadData) { $line = $fileHandle->key() + 1; throw new MyInvalidArgumentException('Import failed. Line ' . $line . ' does not contain sufficient data or is improperly formatted.'); } else { continue; } } try { $dune = new Dune(new LatLng($duneData[3], $duneData[2]), $this->siteName, $duneData[5], $duneData[4]); $this->dunes[] = $dune; } catch (Exception $e) { if ($errorOnBadData) { $line = $fileHandle->key() + 1; throw new MyInvalidArgumentException('Import failed. Line ' . $line . ' contains invalid data.', $e); } else { continue; } } } }
/** * Obtain test case tags * * @return Array */ function getTags() { if (empty($this->_tagsMap)) { $testCaseFileObj = new SplFileObject($this->_testCaseFile); $line = ""; $inTags = false; if ($testCaseFileObj->isReadable()) { while ($testCaseFileObj->valid() && $line != "#--- End tags\n") { $line = $testCaseFileObj->fgets(); if ($inTags && $line == "#--- End tags\n") { $inTags = false; } if ($inTags) { $this->_tagsMap[] = trim(str_replace("#", "", $line)); } if (!$inTags && $line == "#--- Start tags\n") { $inTags = true; } } } } return $this->_tagsMap; }