Giter Club home page Giter Club logo

php-writer's Introduction

php-writer

Build Status Coverage Status Gitter

Update PHP scripts from their AST by using php-parser for reading and php-unparser for generating back the php.

How does it works?

You must include the module to your script and feed to it a php file string.

var fs = require('fs');

// Load the writer
var writer = require('php-writer');

// Read file content
var contents = fs.readFileSync('my-php-file.php', 'utf8');

// All the possible options
var options = {
  writer: {
    indent: true,
    dontUseWhitespaces: false,
    shortArray: true,
    forceNamespaceBrackets: false
  },
  parser: {
    debug: false, 
    locations: false,
    extractDoc: false,
    suppressErrors: false
  },
  lexer: {
    all_tokens: false,
    comment_tokens: false,
    mode_eval: false,
    asp_tags: false,
    short_tags: false
  },
  ast: {
    withPositions: true
  }
};

// Init the writer
var myPhpFile = new writer(contents, options);

// Print the file AST object
console.log(myPhpFile);

Options

You can pass to the writer an options object to customize your writer instance:

  • writer: The same options you would pass to php-unparser
  • parser: The same options you would pass to php-parser
  • ast: The same options you would pass to php-parser

If you want to learn more about AST you can read the definitions.

Examples

All the examples assume that you have already loaded the file like in the example.

Find a function and set its body value:
var myPhpFile = new writer(contents, options);
myPhpFile.findFunction('foo').setBody('return $a + $b');
Find a class and set a property value:
var myPhpFile = new writer(contents, options);
myPhpFile.findClass('Foo_Class').setProperty('bar', 'foo-bar');

php-writer's People

Contributors

ichiriac avatar sevavietl avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.