Nette\Neon\Decoder::decode PHP Method

decode() public method

Decodes a NEON string.
public decode ( $input ) : mixed
return mixed
    public function decode($input)
    {
        if (!is_string($input)) {
            throw new \InvalidArgumentException(sprintf('Argument must be a string, %s given.', gettype($input)));
        } elseif (substr($input, 0, 3) === "") {
            // BOM
            $input = substr($input, 3);
        }
        $this->input = "\n" . str_replace("\r", '', $input);
        // \n forces indent detection
        $pattern = '~(' . implode(')|(', self::PATTERNS) . ')~Amix';
        $this->tokens = preg_split($pattern, $this->input, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_DELIM_CAPTURE);
        $last = end($this->tokens);
        if ($this->tokens && !preg_match($pattern, $last[0])) {
            $this->pos = count($this->tokens) - 1;
            $this->error();
        }
        $this->pos = 0;
        $res = $this->parse(NULL);
        while (isset($this->tokens[$this->pos])) {
            if ($this->tokens[$this->pos][0][0] === "\n") {
                $this->pos++;
            } else {
                $this->error();
            }
        }
        return $res;
    }

Usage Example

コード例 #1
0
 /**
  * @param string $path
  * @param string $find
  * @param int $depth
  */
 public function addAutoloadConfig($path, $find = 'config.neon', $depth = -1)
 {
     // Development
     if (!$this->cacheConfig && $this->isDevelopment()) {
         foreach (Finder::find($find)->from($path)->limitDepth($depth) as $file) {
             $this->addConfig((string) $file);
         }
         return;
     }
     // Production
     $directory = $this->parameters['tempDir'] . '/cache/configs';
     $cachePath = $directory . '/' . Strings::webalize(str_replace(dirname($this->parameters['appDir']), '', $path)) . '.neon';
     if (file_exists($cachePath)) {
         $this->addConfig($cachePath);
         return;
     }
     $encoder = new Encoder();
     $decoder = new Decoder();
     @mkdir($directory);
     $content = [];
     foreach (Finder::find($find)->from($path)->limitDepth($depth) as $file) {
         $content = Helpers::merge($content, $decoder->decode(file_get_contents($file)));
     }
     file_put_contents($cachePath, $encoder->encode($content));
     $this->addConfig($cachePath);
 }
All Usage Examples Of Nette\Neon\Decoder::decode