Esempio n. 1
0
 /**
  * Load from an input stream, e.g. a file
  *
  * @param   io.streams.InputStream|io.Channel|string $in
  * @param   string $charset the charset the stream is encoded in or NULL to trigger autodetection by BOM
  * @return  self
  * @throws  io.IOException
  * @throws  lang.FormatException
  */
 public function load($in, $charset = null) : self
 {
     $reader = new TextReader($in, $charset);
     $expansion = $this->expansion ?: self::$env;
     $this->_data = [];
     $section = null;
     while (null !== ($t = $reader->readLine())) {
         $trimmedToken = trim($t);
         if ('' === $trimmedToken) {
             continue;
         }
         // Empty lines
         $c = $trimmedToken[0];
         if (';' === $c || '#' === $c) {
             // One line comments
             continue;
         } else {
             if ('[' === $c) {
                 if (false === ($p = strrpos($trimmedToken, ']'))) {
                     throw new FormatException('Unclosed section "' . $trimmedToken . '"');
                 }
                 $section = substr($trimmedToken, 1, $p - 1);
                 $this->_data[$section] = [];
             } else {
                 if (false !== ($p = strpos($t, '='))) {
                     $key = trim(substr($t, 0, $p));
                     $value = ltrim(substr($t, $p + 1));
                     if ('' === $value) {
                         // OK
                     } else {
                         if ('"' === $value[0]) {
                             // Quoted strings
                             $quoted = substr($value, 1);
                             while (false === ($p = strrpos($quoted, '"'))) {
                                 if (null === ($line = $reader->readLine())) {
                                     break;
                                 }
                                 $quoted .= "\n" . $line;
                             }
                             $value = $expansion->in(substr($quoted, 0, $p));
                         } else {
                             // unquoted string
                             if (false !== ($p = strpos($value, ';'))) {
                                 // Comments at end of line
                                 $value = substr($value, 0, $p);
                             }
                             $value = $expansion->in(rtrim($value));
                         }
                     }
                     // Arrays and maps: key[], key[0], key[assoc]
                     if (']' === substr($key, -1)) {
                         if (false === ($p = strpos($key, '['))) {
                             throw new FormatException('Invalid key "' . $key . '"');
                         }
                         $offset = substr($key, $p + 1, -1);
                         $key = substr($key, 0, $p);
                         if (!isset($this->_data[$section][$key])) {
                             $this->_data[$section][$key] = [];
                         }
                         if ('' === $offset) {
                             $this->_data[$section][$key][] = $value;
                         } else {
                             $this->_data[$section][$key][$offset] = $value;
                         }
                     } else {
                         $this->_data[$section][$key] = $value;
                     }
                 } else {
                     if ('' !== trim($t)) {
                         throw new FormatException('Invalid line "' . $t . '"');
                     }
                 }
             }
         }
     }
     return $this;
 }
Esempio n. 2
0
 private function file_contents($name, $type)
 {
     $filename = $name . '.' . $type;
     //read file and return content
     self::$file_content = file_get_contents(MEDIA_DIR . '/' . $filename);
 }
 public function resetUnseekable()
 {
     $r = new TextReader(newinstance('io.streams.InputStream', array(), '{
     public function read($size= 8192) { return NULL; }
     public function available() { return 0; }
     public function close() { }
   }'));
     $r->reset();
 }