/** * Converts data to JSON and writes/appends this to specified filename. * * @param string $filename Name of file to write to. * @param mixed $data Data to write to file. */ function writeToFile($filename, $data) { $builder = new JsonBuilder(); $builder->setJsonEncodeOptions(JSON_UNESCAPED_SLASHES); $builder->setValues($data); $json = $builder->build(); file_put_contents($filename, $json . PHP_EOL, FILE_APPEND); }
public function testBuildWithJsonEncodeOptions() { $this->jsonBuilder->setJsonEncodeOptions(JSON_FORCE_OBJECT); $this->assertSame(JSON_FORCE_OBJECT, $this->jsonBuilder->getJsonEncodeOptions()); $this->assertSame('{}', $this->jsonBuilder->build()); }
$sourcePath = $argv[1]; $targetPath = $argv[2]; $finder->files()->in($sourcePath)->name('*.php'); $start = time(); foreach ($finder as $file) { $hintVisitor = new PhpCodeHints\HintVisitor(); $parser = new Parser(new PhpParser\Lexer()); $traverser = new NodeTraverser(); $traverser->addVisitor(new NodeVisitor\NameResolver()); $traverser->addVisitor($hintVisitor); $code = $file->getContents(); try { $stmts = $parser->parse($code); $stmts = $traverser->traverse($stmts); $jsonContent = ""; $builder->setJsonEncodeOptions(JSON_PRETTY_PRINT); $builder->setValues($hintVisitor->fileStmts); $jsonContent = $builder->build(); if (!$fs->exists($targetPath . $file->getRelativePath())) { echo "created directory: " . $targetPath . $file->getRelativePath() . "\n"; $fs->mkdir($targetPath . $file->getRelativePath()); } $outputFilename = $targetPath . $file->getRelativePath() . "/" . $file->getBasename('.php') . '.json'; file_put_contents($outputFilename, $jsonContent); echo "wrote file: " . $outputFilename . "\n"; $builder->reset(); } catch (PhpParser\Error $e) { echo 'Parse Error: ', $e->getMessage(); } } echo "\nGenerated json hint files in: " . (time() - $start) . "secs\n";