/** * @param $file * @return $this */ public function setFile($file) { $this->file = new \SplFileObject($file, "r"); if (!$this->file->isFile()) { throw new \Exception(sprintf("Invalid file for parsing %s", $file)); } return $this; }
protected function buildQueue() { $projectDir = $this->getProjectDir(); $outputDir = $this->getOutputDir(); $item = new \SplFileObject($this->getSingleFile()); if ($item->isFile()) { try { $factory = new View\Factory($item->getRealPath()); $view = $factory->create(); $view->setSiteConfig($this->getSiteConfig())->setOutputDir($outputDir); $this->addView($view); } catch (\Exception $e) { $this->getOutputter()->stderr(str_replace($projectDir, '', $item->getRealPath()) . ': ' . $e->getMessage()); } } return $this; }
/** * Used to clear a directory of matching files. * * @param string $path The path to search. * @param integer $now The current timestamp * @param integer $threshold Any file not modified after this value will be deleted. * @return void */ protected function _clearDirectory($path, $now, $threshold) { $prefixLength = strlen($this->settings['prefix']); if (!is_dir($path)) { return; } $dir = dir($path); while (($entry = $dir->read()) !== false) { if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) { continue; } $filePath = $path . $entry; if (!file_exists($filePath) || is_dir($filePath)) { continue; } $file = new SplFileObject($path . $entry, 'r'); if ($threshold) { $mtime = $file->getMTime(); if ($mtime > $threshold) { continue; } $expires = (int) $file->current(); if ($expires > $now) { continue; } } if ($file->isFile()) { $filePath = $file->getRealPath(); $file = null; //@codingStandardsIgnoreStart @unlink($filePath); //@codingStandardsIgnoreEnd } } }
<?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);
/** * 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; } } } }
<?php $s = new SplFileObject(__FILE__); var_dump($s->isFile()); var_dump($s->isDir()); var_dump($s->isLink());