本文整理汇总了PHP中PhpParser\PrettyPrinter\Standard类的典型用法代码示例。如果您正苦于以下问题:PHP Standard类的具体用法?PHP Standard怎么用?PHP Standard使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Standard类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: lint
/**
* @param $code
* @param bool $autoFix - In case of true - pretty code will be generated (avaiable by getPrettyCode method)
* @return Logger
*/
public function lint($code, $autoFix = false)
{
$this->autoFix = $autoFix;
$this->prettyCode = '';
$this->logger = new Logger();
try {
$stmts = $this->parser->parse($code);
} catch (Error $e) {
$this->logger->addRecord(new LogRecord('', '', Logger::LOGLEVEL_ERROR, $e->getMessage(), ''));
return $this->logger;
}
$traverser = new NodeTraverser();
$rulesVisitor = new RulesVisitor($this->rules, $this->autoFix);
$traverser->addVisitor($rulesVisitor);
$traverser->traverse($stmts);
$messages = $rulesVisitor->getLog();
foreach ($messages as $message) {
$this->logger->addRecord(new LogRecord($message['line'], $message['column'], $message['level'], $message['message'], $message['name']));
}
if ($autoFix) {
$prettyPrinter = new PrettyPrinter\Standard();
$this->prettyCode = $prettyPrinter->prettyPrint($stmts);
}
$sideEffectVisitor = new SideEffectsVisitor();
$traverser->addVisitor($sideEffectVisitor);
$traverser->traverse($stmts);
if ($sideEffectVisitor->isMixed()) {
$this->logger->addRecord(new LogRecord('', '', Logger::LOGLEVEL_ERROR, 'A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side' . 'effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both.', ''));
}
return $this->logger;
}
示例2: generate
public function generate($schemaFilePath, $name, $namespace, $directory)
{
$context = $this->createContext($schemaFilePath, $name, $namespace, $directory);
if (!file_exists($directory . DIRECTORY_SEPARATOR . 'Model')) {
mkdir($directory . DIRECTORY_SEPARATOR . 'Model', 0755, true);
}
if (!file_exists($directory . DIRECTORY_SEPARATOR . 'Normalizer')) {
mkdir($directory . DIRECTORY_SEPARATOR . 'Normalizer', 0755, true);
}
$prettyPrinter = new Standard();
$modelFiles = $this->modelGenerator->generate($context->getRootReference(), $name, $context);
$normalizerFiles = $this->normalizerGenerator->generate($context->getRootReference(), $name, $context);
$generated = [];
foreach ($modelFiles as $file) {
$generated[] = $file->getFilename();
file_put_contents($file->getFilename(), $prettyPrinter->prettyPrintFile([$file->getNode()]));
}
foreach ($normalizerFiles as $file) {
$generated[] = $file->getFilename();
file_put_contents($file->getFilename(), $prettyPrinter->prettyPrintFile([$file->getNode()]));
}
if ($this->fixer !== null) {
$config = Config::create()->setRiskyAllowed(true)->setRules(array('@Symfony' => true, 'empty_return' => false, 'concat_without_spaces' => false, 'double_arrow_multiline_whitespaces' => false, 'unalign_equals' => false, 'unalign_double_arrow' => false, 'align_double_arrow' => true, 'align_equals' => true, 'concat_with_spaces' => true, 'newline_after_open_tag' => true, 'ordered_use' => true, 'phpdoc_order' => true, 'short_array_syntax' => true))->finder(DefaultFinder::create()->in($directory));
$resolver = new ConfigurationResolver();
$resolver->setDefaultConfig($config);
$resolver->resolve();
$this->fixer->fix($config);
}
return $generated;
}
示例3: transpile
public function transpile($srcPath, $dstPath)
{
// transpile based on target version
$code = file_get_contents($srcPath);
// Parse into statements
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$stmts = $parser->parse($code);
if (!$stmts) {
throw new ParseException(sprintf("Unable to parse PHP file '%s'\n", $srcPath));
}
// Custom traversal does the actual transpiling
$traverser = self::getTraverser();
$stmts = $traverser->traverse($stmts);
$prettyPrinter = new Standard();
if ($dstPath == '-') {
// Output directly to stdout
$this->getIo()->output($this->getStub() . $prettyPrinter->prettyPrint($stmts));
} else {
$dir = dirname($dstPath);
if (!is_dir($dir) || !is_writeable($dir)) {
@mkdir($dir, 0777, true);
if (!is_dir($dir) || !is_writeable($dir)) {
throw new InvalidOptionException(sprintf('Destination directory "%s" does not exist or is not writable or could not be created.', $dir));
}
}
file_put_contents($dstPath, $this->getStub() . $prettyPrinter->prettyPrint($stmts));
}
}
示例4: compile
/**
* @return string
* @throws Exception\DomainException
*/
public function compile()
{
$class = $this->compileClass();
$node = $this->builderFactory->namespace($this->namespace)->addStmt($this->builderFactory->use('zdi\\Container'))->addStmt($this->builderFactory->use('zdi\\Container\\CompiledContainer'))->addStmt($class)->getNode();
$prettyPrinter = new PrettyPrinter\Standard();
return $prettyPrinter->prettyPrintFile(array($node));
}
示例5: testPrettyPrintExpr
public function testPrettyPrintExpr()
{
$prettyPrinter = new Standard();
$expr = new Expr\BinaryOp\Mul(new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')), new Expr\Variable('c'));
$this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
$expr = new Expr\Closure(array('stmts' => array(new Stmt\Return_(new String_("a\nb")))));
$this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
}
示例6: testRessources
/**
* @dataProvider resourceProvider
*/
public function testRessources($expected, $swaggerSpec, $name)
{
$swagger = JaneSwagger::build();
$printer = new Standard();
$files = $swagger->generate($swaggerSpec, 'Joli\\Jane\\Swagger\\Tests\\Expected', 'dummy');
// Resource + NormalizerFactory
$this->assertCount(2, $files);
$resource = $files[1];
$this->assertEquals($resource->getFilename(), 'dummy/Resource/TestResource.php');
$this->assertEquals(trim($expected), trim($printer->prettyPrintFile([$resource->getNode()])));
}
示例7: compile
/**
* Compile the view with devise code in it
*
* @param string $view
* @return string
*/
public function compile($view)
{
ini_set('xdebug.max_nesting_level', env('XDEBUG_MAX_NESTING_LEVEL', 3000));
$this->parser = new DeviseParser();
$prettyPrinter = new Standard();
$pristine = $this->pristine($view);
$modified = $this->modified($view);
$pristine[0]->stmts = $modified;
$result = $prettyPrinter->prettyPrintFile($pristine);
return $result;
}
示例8: createClass
private function createClass($commande, $description)
{
$output = $this->_output;
$root = ROOT . DS;
$app = $root . 'app' . DS . "Application";
$lib = $root . 'library' . DS . "commands.php";
// create command name
$name = S::create($commande)->replace(':', ' ')->toTitleCase()->replace(' ', '')->append("Command")->__toString();
// create FQN
$fqn = "Application\\Commands\\" . $name;
// check avaibality
// load commands.php file
$code = file_get_contents($lib);
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP5);
$prettyPrinter = new PrettyPrinter\Standard();
$stmts = $parser->parse($code);
foreach ($stmts[0]->expr as $express) {
$tmp = $express[0]->value->value;
if (S::create($tmp)->humanize()->__toString() == S::create($fqn)->humanize()->__toString()) {
$output->writeln("This command already exists in commands.php");
die;
}
}
// commands not exists add it to commands.php
$nb = count($stmts[0]->expr->items);
$ligne = 4 + $nb;
$attributes = array("startLine" => $ligne, "endLine" => $ligne, "kind" => 2);
$obj = new \PhpParser\Node\Expr\ArrayItem(new \PhpParser\Node\Scalar\String_($fqn, $attributes), null, false, $attributes);
array_push($stmts[0]->expr->items, $obj);
$code = $prettyPrinter->prettyPrint($stmts);
$code = "<?php \r\n" . $code;
$output->writeln("Create FQN commande " . $fqn);
$path = $app . DS . "Commands" . DS . $name . ".php";
$arg1 = new \PhpParser\Node\Arg(new \PhpParser\Node\Scalar\String_($commande));
$arg2 = new \PhpParser\Node\Arg(new \PhpParser\Node\Scalar\String_($description));
$arg3 = new \PhpParser\Node\Arg(new \PhpParser\Node\Scalar\String_('Start process'));
$arg4 = new \PhpParser\Node\Arg(new \PhpParser\Node\Scalar\String_('Finished'));
$factory = new BuilderFactory();
$node = $factory->namespace('Application\\Commands')->addStmt($factory->use('Symfony\\Component\\Console\\Command\\Command'))->addStmt($factory->use('Symfony\\Component\\Console\\Input\\InputArgument'))->addStmt($factory->use('Symfony\\Component\\Console\\Input\\InputInterface'))->addStmt($factory->use('Symfony\\Component\\Console\\Input\\InputOption'))->addStmt($factory->use('Symfony\\Component\\Console\\Output\\OutputInterface'))->addStmt($factory->class($name)->extend('Command')->addStmt($factory->method('configure')->makeProtected()->addStmt(new Node\Expr\MethodCall(new Node\Expr\Variable('this'), "setName", array($arg1)))->addStmt(new Node\Expr\MethodCall(new Node\Expr\Variable('this'), "setDescription", array($arg2))))->addStmt($factory->method('execute')->makeProtected()->addParam($factory->param('input')->setTypeHint('InputInterface'))->addParam($factory->param('output')->setTypeHint('OutputInterface'))->addStmt(new Node\Expr\MethodCall(new Node\Expr\Variable('output'), "writeln", array($arg3)))->addStmt(new Node\Expr\MethodCall(new Node\Expr\Variable('output'), "writeln", array($arg4)))))->getNode();
$stmts = array($node);
$prettyPrinter = new PrettyPrinter\Standard();
$php = $prettyPrinter->prettyPrintFile($stmts);
file_put_contents($path, $php);
$fs = new Filesystem();
// if file exists add command to commands.php
if ($fs->exists($path)) {
$output->writeln("File saved in " . $path);
$output->writeln("Register command to console");
file_put_contents($lib, $code);
} else {
$output->writeln("File not created");
}
}
示例9: pp
function pp($nodes)
{
static $prettyPrinter;
if (!$prettyPrinter) {
$prettyPrinter = new PrettyPrinter\Standard();
}
if (!is_array($nodes)) {
$nodes = func_get_args();
}
echo $prettyPrinter->prettyPrint($nodes) . "\n";
die(1);
}
示例10: generateFileContentForArray
/**
* @param array $items
* @return string
*/
private function generateFileContentForArray(array $items)
{
$nodes = [];
foreach ($items as $key => $value) {
$key = is_int($key) ? new LNumber($key) : new String_($key);
$value = new String_($value);
$node = new ArrayItem($value, $key);
array_push($nodes, $node);
}
$statements = [new Return_(new Array_($nodes))];
$printer = new Standard();
return $printer->prettyPrintFile($statements);
}
示例11: generateFromNeonFile
/**
* @param string $path
*/
public function generateFromNeonFile($path)
{
$definition = Neon::decode(file_get_contents($path));
assert(isset($definition['class']));
assert(isset($definition['type']));
assert($definition['type'] === 'in-place');
$data = $definition['data'];
$output = $this->configuration->getDir() . DIRECTORY_SEPARATOR . $this->configuration->getOutputFolder() . DIRECTORY_SEPARATOR . $definition['class'] . '.php';
$consts = Helper::createStringConstants($data);
$node = $this->createClassFromData($definition['class'], $this->configuration->getNamespace(), $consts);
$prettyPrinter = new PrettyPrinter\Standard();
file_put_contents($output, $prettyPrinter->prettyPrintFile([$node]));
}
示例12: saveCode
public function saveCode($stmts)
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$prettyPrinter = new PrettyPrinter\Standard();
try {
$code = $prettyPrinter->prettyPrintFile($stmts);
} catch (Error $e) {
echo 'Parse Error: ', $e->getMessage();
}
if (!file_exists('src\\' . $this->restDir)) {
mkdir('src\\' . $this->restDir, '755', true);
}
file_put_contents($this->restPath, $code);
}
示例13: updateCache
/**
* @param AnnotationManager $annotationManager
* @param ServiceManager $serviceManager
* @param RouteManager $routeManager
* @param Standard $prettyPrinter
*/
public function updateCache(AnnotationManager $annotationManager, ServiceManager $serviceManager, RouteManager $routeManager, Standard $prettyPrinter)
{
$classInfos = $annotationManager->buildClassInfosBasedOnPaths($this->paths);
$code = "<?php\n\n";
foreach ($classInfos as $classInfo) {
$code .= $prettyPrinter->prettyPrint($serviceManager->generateCode($classInfo));
$code .= "\n\n";
}
foreach ($classInfos as $classInfo) {
$code .= $prettyPrinter->prettyPrint($routeManager->generateCode($classInfo));
$code .= "\n\n";
}
file_put_contents($this->cacheFile, $code);
}
示例14: generate
/**
* @param EntityMapping $modelMapping
* @param string $namespace
* @param string $path
* @param bool $override
* @return void
*/
public function generate(EntityMapping $modelMapping, $namespace, $path, $override = false)
{
$abstractNamespace = $namespace . '\\Base';
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$abstractPath = $path . DIRECTORY_SEPARATOR . 'Base';
if (!is_dir($abstractPath)) {
mkdir($abstractPath, 0777, true);
}
$abstracClassName = 'Abstract' . $modelMapping->getName();
$classPath = $path . DIRECTORY_SEPARATOR . $modelMapping->getName() . '.php';
$abstractClassPath = $abstractPath . DIRECTORY_SEPARATOR . $abstracClassName . '.php';
$nodes = array();
$nodes = array_merge($nodes, $this->generatePropertyNodes($modelMapping));
$nodes = array_merge($nodes, $this->generateConstructNodes($modelMapping));
$nodes = array_merge($nodes, $this->generateMethodNodes($modelMapping));
$nodes = array_merge($nodes, $this->generateMetadataNodes($modelMapping));
$nodes = array(new Node\Stmt\Namespace_(new Name($abstractNamespace), array(new Class_('Abstract' . $modelMapping->getName(), array('type' => 16, 'stmts' => $nodes)))));
$abstractClassCode = $this->phpGenerator->prettyPrint($nodes);
file_put_contents($abstractClassPath, '<?php' . PHP_EOL . PHP_EOL . $abstractClassCode);
if (file_exists($classPath) && !$override) {
return;
}
$nodes = array(new Node\Stmt\Namespace_(new Name($namespace), array(new Class_($modelMapping->getName(), array('extends' => new FullyQualified($abstractNamespace . '\\' . $abstracClassName))))));
$classCode = $this->phpGenerator->prettyPrint($nodes);
file_put_contents($classPath, '<?php' . PHP_EOL . PHP_EOL . $classCode);
}
示例15: __invoke
/**
* @param ClassReflection $reflection
*
* @return string
*/
public function __invoke(ClassReflection $reflection)
{
$stubCode = ClassGenerator::fromReflection($reflection)->generate();
$isInterface = $reflection->isInterface();
if (!($isInterface || $reflection->isTrait())) {
return $stubCode;
}
return $this->prettyPrinter->prettyPrint($this->replaceNodesRecursively($this->parser->parse('<?php ' . $stubCode), $isInterface));
}