Created
February 8, 2024 22:36
-
-
Save sbassett29/4ec8caf090fc02bfe2506449c39e9ecf to your computer and use it in GitHub Desktop.
php cli ast generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
declare(strict_types=1); | |
define( "AST_PHP_VERSION", 80 ); | |
if( PHP_SAPI !== 'cli' ) { | |
exit("Please run as a PHP CLI!"); | |
} | |
if( ! isset($argv[1]) ) { | |
exit("Please provide a file name or string of PHP code as an argument!"); | |
} | |
if( ! in_array( "ast", get_loaded_extensions() ) ) { | |
exit("Please ensure the php/ast module is installed!"); | |
} | |
$ast = null; | |
if( is_file( $argv[1] ) ) { | |
$ast = ast\parse_file( $argv[1], $version=AST_PHP_VERSION ); | |
} | |
else if( is_string( $argv[1] ) ) { | |
$ast = ast\parse_code( $argv[1], $version=AST_PHP_VERSION ); | |
} | |
else { | |
exit("A file name or valid PHP code string were not provided as an argument!"); | |
} | |
if( $ast !== null ) { | |
echo $ast; | |
} | |
else { | |
exit("AST unable to be generated!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment