예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function validate()
 {
     $path = Path::normalize($this->value);
     $test = $this->isDir === true ? Dir::isWritable($path) : File::isWritable($path);
     if ($test === true) {
         $this->value = $path;
         return true;
     }
     $this->message = $test;
     return false;
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function write()
 {
     $path = LogFile::getPathFromName($this->record->getName());
     if (($test = File::prepare($path, 0775)) !== true) {
         throw new Exception("failed to write log file '{$path}'; {$test}");
     }
     $line = LogFile::encodeRecord($this->record);
     if (!@file_put_contents($path, "{$line}\n", FILE_APPEND)) {
         throw new Exception("failed to write log '{$path}': {$line}");
     }
 }
예제 #3
0
 /**
  * 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);
 }
예제 #4
0
 /**
  * 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');
         }
     }
 }
예제 #5
0
 /**
  * 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}";
 }
예제 #6
0
 /**
  * Create a temp file
  * 
  * @param string $name A name for the file name
  * @param string|null $contents Optional contents for the file
  * @return string The path to the newly created temp file
  */
 public static function file($name = "temp", $contents = null)
 {
     $tmpdir = self::getDir();
     $name = File::sanitizeName($name);
     list($name, $ext) = File::splitName($name, "");
     if ($ext !== "") {
         $ext = ".{$ext}";
     }
     do {
         $rand = substr(md5(microtime(true) . mt_rand()), 10, 10);
         $path = $tmpdir . DIRECTORY_SEPARATOR . "{$name}-{$rand}{$ext}";
     } while (file_exists($path));
     touch($path);
     if ($contents) {
         file_put_contents($path, $contents);
     }
     self::registerPath($path, false);
     return $path;
 }
예제 #7
0
 /**
  * Set an output file for either stdout or stderr
  *
  * @param integer $stream The index of the relevant stream
  * @param string $path An absolute file path
  * @param boolean $append Whether or not to append to the file
  */
 private function setOutputFile($stream, $path, $append)
 {
     if (($test = File::isWritable($path)) !== true) {
         throw new InvalidArgumentException("invalid value provided for 'path'; {$test}");
     }
     $this->spec[$stream] = ['file', $path, $append === true ? 'a' : 'w'];
 }
예제 #8
0
 /**
  * Show usage statistics
  *
  * @return void
  */
 public static function showUsageStats()
 {
     $time = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
     $time = number_format($time, 4);
     $memory = File::formatSize(memory_get_peak_usage(), 2);
     $message = "processed in {$time} seconds using {$memory} of memory\n";
     $verboseLevel = Env::getVerboseLevel();
     Env::setVerboseLevel(Env::NORMAL);
     Env::log($message);
     Env::setVerboseLevel($verboseLevel);
 }