/** * @param FileInterface $file * * @return bool */ public function deleteFile(FileInterface $file) { if (@unlink($file->getPath())) { return true; } return false; }
/** * @param FileInterface $file * @return string[] */ private function getClassFromFile(FileInterface $file) : array { $classes = []; $tokens = token_get_all($file->getContents()); $count = count($tokens); $namespace = ''; for ($i = 2; $i < $count; $i++) { // Detect the namespace if ($tokens[$i - 2][0] === T_NAMESPACE && $tokens[$i - 1][0] === T_WHITESPACE && $tokens[$i][0] === T_STRING) { while ($tokens[$i][0] === T_STRING || $tokens[$i][0] === T_NS_SEPARATOR) { $namespace .= $tokens[$i][1]; $i += 1; } } if ($tokens[$i - 2][0] === T_CLASS && $tokens[$i - 1][0] === T_WHITESPACE && $tokens[$i][0] === T_STRING) { $classes[] = $tokens[$i][1]; } } if ($namespace) { return array_map(function ($class) use($namespace) { return "{$namespace}\\{$class}"; }, $classes); } return $classes; }
public function imageInterface($file) { if (get_class($file) != 'FileInterface') { $file = new FileInterface($file); } $this->file = $file; if (!($this->image = image::init($file->getPath()))) { return false; } }
private function buildSheet(FileInterface $file) : SheetInterface { $csv = tmpfile(); $stream = $file->content()->rewind(); while (!$stream->isEof()) { fwrite($csv, $stream->read(8192)); } rewind($csv); $sheet = new Sheet(basename((string) $file->name(), '.csv')); $position = 1; $firstLine = fgetcsv($csv, 0, $this->delimiter); if (!$this->useFirstLineAsColumnIdentifier) { rewind($csv); } while (($line = fgetcsv($csv, 0, $this->delimiter)) !== false) { foreach ($line as $key => $value) { $sheet = $sheet->add(new Cell(new Position($this->useFirstLineAsColumnIdentifier ? $firstLine[$key] : $key, $position), $value)); } ++$position; } fclose($csv); return $sheet; }
/** * Create the wished file at the given absolute path * * @param string $path * @param FileInterface $file * * @return void */ private function createFileAt(string $path, FileInterface $file) { if ($file instanceof DirectoryInterface) { $folder = $path . '/' . (string) $file->name(); if ($this->files->contains($folder) && $this->files->get($folder) === $file) { return; } $this->filesystem->mkdir($folder); $file->recordedEvents()->foreach(function ($event) use($folder) { if ($this->handledEvents->contains($event)) { return; } switch (true) { case $event instanceof FileWasRemoved: $this->filesystem->remove($folder . '/' . $event->file()); break; case $event instanceof FileWasAdded: $this->createFileAt($folder, $event->file()); break; } $this->handledEvents = $this->handledEvents->add($event); }); $this->files = $this->files->put($folder, $file); return; } $path .= '/' . (string) $file->name(); if ($this->files->contains($path) && $this->files->get($path) === $file) { return; } $stream = $file->content(); $stream->rewind(); $handle = fopen($path, 'w'); while (!$stream->isEof()) { fwrite($handle, $stream->read(8192)); } $this->files = $this->files->put($path, $file); }
/** * Create the specified file. * I dont understand why this exists with the provided $file. Surely if we want to * create a file we should be calling the file's constructor or one of its static * creation methods? * @param FileInterface $file - the file to be created? * @param FolderInterface $parent - the parent folder to stick the file within. * * @return FileInterface */ public function createFile(FileInterface $file, FolderInterface $parent) { $filepath = $file->getPath() . '/' . $file->getName(); $gridFs = ConnectionHandler::getInstance()->getConnection(); $timeNow = time(); $mongoFolder = MongoFolder::loadFromFolderInterface($parent); $metadata = array('creation_time' => $file->getCreatedTime(), 'modification_time' => $file->getModifiedTime(), 'parent' => $mongoFolder->getMongoId(), 'type' => 'file'); # This is another hack where the getPath() method here is returning the path to the # file as it is locally stored in the linux filesystem, rather than the path of where # it will be stored withing the mongo based filesystem. $mongoId = $gridFs->storeFile($file->getPath(), $metadata); $mongoFile = MongoFile::loadFromMongoId($mongoId); return $mongoFile; }
/** * @param FileInterface $file * @return array */ private function getValidateFileResult(FileInterface $file) { $check = array('File name not specified' => !$file->getName(), 'Filesize not specified' => is_null($file->getSize()), 'Created time not specified' => !$file->getCreatedTime(), 'File path already exists' => $this->fileExists($file->getPath())); return $check; }