Git Product home page Git Product logo

pyphp's Introduction

pyphp

Php parser, compiler and interpreter for python

Usage

###Executing php code You can execute php code using the pyphp.executer module. The execute_file reads a php file and executes it, while execute_php executes given php code.

  • To run a php script:
import pyphp.executer
pyphp.executer.execute_file('my_php_code.php')

my_php_code.php contents:

Hello <?php echo "World\n";?>
  • To run php code
import pyphp.executer

phpcode = r'Hello <?php echo "World.\n"; ?>';

pyphp.executer.execute_php(phpcode)

output:

Hello World.
  • You can redirect the script's output to any file-like object, such as StringIO

    import pyphp.executer
    import StringIO
    
    stdout = StringIO.StringIO()
    phpcode = r'Hello <?php echo "World.\n"; ?>';
    
    pyphp.executer.execute_php(phpcode, stdout=stdout)
    
    print repr(`stdout.getvalue())

    output:

    'Hello World.\n'
    
  • You can run the php file directly by using the module as a script:

   python pyphph/executer.py phpscript.php arg1 arg2 ...

###Using the parser The parser breaks up the php code into a list of tokens.

  • This will parse a php file in the same directory and return it as a list of tokens.
import pyphp.parser
token_list = pyphp.parser.parse_file("my_php_code.php")
  • You can also parse the php code directly:
import pyphp.parser

phpcode = r'Hello <?php echo "World.\n";?>';

token_list = pyphp.parser.parse_php(phpcode)
  • token_list would be a list like this:
[Token['DIRECT_OUTPUT', 'Hello '],
 Token['WS'],
 Token['IDENTIFIER', 'echo'],
 Token['WS'],
 Token['STRING', 'World\n'],
 Token[';']]

this token list can then be fed to the compiler to generate a syntax tree.

###Using the compiler The compiler takes a list of tokens a creates a syntax tree representing the code

  • This will compile a php file in the same directory and return it as a syntax tree.
import pyphp.compiler
syntax_tree = pyphp.compiler.compile_file("my_php_code.php")
  • You can also compile the php code :
import pyphp.compiler

phpcode = r'Hello <?php echo "World.\n";?>';

syntax_tree = pyphp.compiler.compile_php(phpcode)
  • Or compile the list of tokens directly:
import pyphp.parser
import pyphp.compiler

phpcode = r'Hello <?php echo "World.\n";?>';

token_list = pyphp.parser.parse_php(phpcode)

syntax_tree = pyphp.compiler.compile_php(token_list)
  • syntax_tree would be a TreeNode object like this:
TreeNode<php_file>[
    TreeNode<direct_output>['Hello '],
    TreeNode<echo>[
        TreeNode<primitive>[
            Token['STRING', 'World\n']
        ]
    ]
]
This `TreeNode` object can then be fed to `execute_php` to run the code.

###PHP Library support Currently, only a minimal set of PHP's language features and built in library is supported, but the plan is to support as most of it as possible.

Language features not supported yet are namespaces, anonymous functions, lots of other stuff.

Below is the official subset of PHP's built in features currently supported.

name description supported
true true literal yes
false false literal yes
null null literal yes
E_ERROR error constant yes
E_WARNING error constant yes
E_PARSE error constant yes
E_NOTICE error constant yes
E_CORE_ERROR error constant yes
E_CORE_WARNING error constant yes
E_COMPILE_ERROR error constant yes
E_COMPILE_WARNING error constant yes
E_USER_ERROR error constant yes
E_USER_WARNING error constant yes
E_USER_NOTICE error constant yes
E_STRICT error constant yes
E_RECOVERABLE_ERROR error constant yes
E_DEPRECATED error constant yes
E_USER_DEPRECATED error constant yes
E_ALL error constant yes
isset Checks if a variable is set yes
empty checks if a variable is empty yes
die outputs to stdout and halts execution yes
defined returns whether a constant is defined or not yes
require_once requires a php script once yes
require requires a php script yes
include_once includes a php script once yes
include includes a php script yes
var_dump dumps a variable to stdout yes
ini_set modifies settings derived from php.ini no-op
error_reporting modifies error reporting level yes
date_default_timezone_set sets default datetime zone no-op

pyphp's People

Contributors

g-i-o- avatar

Watchers

James Cloos avatar 周子豪, Zhou Robot ZiHao 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.