From 15dc362ba5357f6281d7e735f18957ef42b5e25f Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 24 Sep 2020 13:40:30 -0400 Subject: [PATCH] Add a $files->fileGetContents() method to accompany existing filePutContents() method --- wire/core/FileValidatorModule.php | 4 ++-- wire/core/ProcessWire.php | 1 + wire/core/WireFileTools.php | 30 +++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/wire/core/FileValidatorModule.php b/wire/core/FileValidatorModule.php index 8e671ff5..fd5ad166 100644 --- a/wire/core/FileValidatorModule.php +++ b/wire/core/FileValidatorModule.php @@ -94,7 +94,7 @@ abstract class FileValidatorModule extends WireData implements Module { protected $_pagefile = null; /** - * Is the given file valid? (this is the method mdoules should implement) + * Is the given file valid? (this is the method modules should implement) * * This method should return: * - boolean TRUE if file is valid @@ -118,7 +118,7 @@ abstract class FileValidatorModule extends WireData implements Module { * FileValidator modules should not implement this method, as it only serves as a front-end to isValid() * for logging purposes. * - * @param $filename + * @param string $filename * @return bool|int Returns TRUE if valid, FALSE if not, or integer 1 if valid as a result of sanitization. * */ diff --git a/wire/core/ProcessWire.php b/wire/core/ProcessWire.php index 2a6984e1..00ba9de6 100644 --- a/wire/core/ProcessWire.php +++ b/wire/core/ProcessWire.php @@ -24,6 +24,7 @@ require_once(__DIR__ . '/boot.php'); * ====================== * @property AdminTheme|AdminThemeFramework|null $adminTheme * @property WireCache $cache + * @property WireClassLoader $classLoader * @property Config $config * @property WireDatabasePDO $database * @property WireDateTime $datetime diff --git a/wire/core/WireFileTools.php b/wire/core/WireFileTools.php index 37e52bd6..9fbcc68c 100644 --- a/wire/core/WireFileTools.php +++ b/wire/core/WireFileTools.php @@ -5,7 +5,7 @@ * * #pw-summary Helpers for working with files and directories. * - * ProcessWire 3.x, Copyright 2018 by Ryan Cramer + * ProcessWire 3.x, Copyright 2020 by Ryan Cramer * https://processwire.com * * @method bool include($filename, array $vars = array(), array $options = array()) @@ -825,6 +825,7 @@ class WireFileTools extends Wire { * - `LOCK_EX` (constant): Acquire exclusive lock to file while writing. * @return int|bool Number of bytes written or boolean false on fail * @throws WireException if given invalid $filename (since 3.0.118) + * @see WireFileTools::fileGetContents() * */ public function filePutContents($filename, $contents, $flags = 0) { @@ -834,6 +835,33 @@ class WireFileTools extends Wire { return $result; } + /** + * Get contents of file + * + * This is the same as PHP’s `file_get_contents()` except that the arguments are simpler and + * it may be preferable to use this in ProcessWire for future cases where the file system may be + * abstracted from the installation. + * + * @param string $filename Full path and filename to read + * @param int $offset The offset where the reading starts on the original stream. Negative offsets count from the end of the stream. + * @param int $maxlen Maximum length of data read. The default is to read until end of file is reached. + * @return bool|string Returns the read data (string) or boolean false on failure. + * @since 3.0.167 + * @see WireFileTools::filePutContents() + * + */ + public function fileGetContents($filename, $offset = 0, $maxlen = 0) { + if($offset && $maxlen) { + return file_get_contents($filename, false, null, $offset, $maxlen); + } else if($offset) { + return file_get_contents($filename, false, null, $offset); + } else if($maxlen) { + return file_get_contents($filename, false, null, 0, $maxlen); + } else { + return file_get_contents($filename); + } + } + /** * Given a filename, render it as a ProcessWire template file *