/** * Set the path to the file * * @param string $path * @param string|null $contentType */ public function setPath($path, $contentType = null) { if (($test = File::isReadable($path)) !== true) { throw new InvalidArgumentException("invalid value provided for 'path'; {$test}"); } $this->path = $path; if ($contentType === null) { $ext = pathinfo($path, PATHINFO_EXTENSION); $contentType = Mime::getTypeFromExtension($ext); } $this->addHeader("Content-Type", $contentType); }
/** * Create a new log reader * * @param string $path The absolute path to the log file to read */ public function __construct($path) { if (!is_string($path)) { throw new InvalidArgumentException("invalid value provided for 'path'; " . "expecting an absolute file path as string"); } else { if (($test = File::isReadable($path)) !== true) { throw new Exception("failed to create log file reader; {$test}"); } else { $this->path = $path; $this->fh = fopen($path, 'r'); } } }
/** * Attempt to decode a file that contains JSON * * Note: only use this method on file that contains JSON arrays or objects; * any other data type will result in an exception being thrown * * @param string $path The Absolute path to the json file to decode * @param boolean $assoc Whether or not to return an associative array * @param integer $opts Options to pass to json_decode * @param integer $depth The max recursion depth to parse * @return object|array|string * @return object|array The read and decode were successful * @return string An error message describing the failure * @throws Exception If the resulting data is not an object or array */ public static function decodeFile($path, $assoc = false, $opts = 0, $depth = 512) { if (($test = File::isReadable($path)) !== true) { $err = $test; } else { if (($json = @file_get_contents($path)) === false) { $err = "read operation failed on '{$path}'"; } else { if (($ret = json_decode($json, $assoc, $depth, $opts)) === null && json_last_error() !== JSON_ERROR_NONE) { $err = "decode operation failed on '{$path}'"; } else { if (!is_array($ret) && !is_object($ret)) { throw new Exception("Invalid JSON value type in '{$path}'; " . "only use sndsgd\\Json::decodeFile() on files that " . "contain an array or an object"); } else { return $ret; } } } } return "failed to decode JSON file; {$err}"; }
/** * Set the path to a file to read into stdin * * @param string $path An absolute file path * @throws InvalidArgumentException If $path is not a readable file */ public function setStdinFile($path) { if (($test = File::isReadable($path)) !== true) { throw new InvalidArgumentException("invalid value provided for 'path'; {$test}"); } $this->spec[self::STDIN] = ['file', $path, 'r']; $this->stdin = ''; }