Created
September 13, 2013 03:54
-
-
Save abcarroll/6546631 to your computer and use it in GitHub Desktop.
A simple one-time use function to generate the inside part of a PHP class based on functions from a text file. Pulls and uses the function's prototype/synopsis from the PHP manual, as well as adds the full prototype/synopsis and description in a comment. This makes it easy to convert procedural style PHP extensions into classes. Written specific…
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
<?php | |
/* | |
* Copyright (c) 2013, Armond B. Carroll III, [email protected] | |
* This 'gist' (file) is distributed under the BSD 2-Clause License. | |
* | |
* This code will turn a function list into the inside part of a class, with | |
* the synopsis, and each member calling the original procedural style function. | |
* It was written to turn ncurses_* functions into a class. | |
* | |
* TO USE, load a text file into a file (ncurses.txt in this revision), with | |
* a one function name per line (no () or other characters, just the function | |
* name, like 'foo' not 'foo()' or otherwise). IF you copied directly from | |
* manual, and it has a — followed by the description, there is code in | |
* there that will remove the &mdash. | |
* | |
* Now download the PHP manual, the HTML "many files" version and untar -xzvf | |
* the file into the same directory as this script. The function synopsis and | |
* description will be pulled from there. | |
* | |
* It is assumed that for each member you want to chop off everything before | |
* the first underscore. so ncurses_getyx() becomes $yourOO->getyx(). | |
* If this isn't the case, modify lines dealing with $inner_function | |
* | |
* KNOWN BUGS: | |
* - Doesn't unescape HTML entities from the manual. It's a one line fix. | |
* - Dosn't handle optional parameters. In the code generated you will have, | |
* e.g. fuction foo($bar, [$spaz]) if $spaz was optional in the synopsis. | |
* ncurses only had one function like this, so I left it out. Additionally, | |
* it might be useful to ensure you don't miss those (they will need manual | |
* handling) | |
*/ | |
$f = file('ncurses.txt'); | |
foreach($f as $l) { | |
$l = trim($l); | |
if(empty($l)) { | |
continue; | |
} | |
//If you copy from the manual and have the — in there, this will remove it: | |
//list($function,) = explode('—', $l); | |
// else, use this: | |
$function = $line; | |
$function = trim($function); | |
$inner_function = explode('_', $function, 2); | |
$function_d = str_replace("_", '-', $function); | |
$f = file_get_contents("php-chunked-xhtml/function.$function_d.html"); | |
preg_match('/<div class="methodsynopsis dc-description">(.*)<\/div>/ismU', $f, $synopsis); | |
$synopsis = strip_tags($synopsis[1]); | |
$synopsis = str_replace("\r", "", $synopsis); | |
$synopsis = str_replace("\n", "", $synopsis); | |
$synopsis = trim(preg_replace("/ +/", " ", $synopsis)); | |
$synopsis = str_replace("( ", "(", $synopsis); | |
$synopsis = str_replace(" )", ")", $synopsis); | |
// machine synopsis, doesn't handle void error conditions, throws some warnings, but it works | |
preg_match('/\((.*)\)/', $synopsis, $msynopsis_m); | |
$msynopsis = @explode(',', $msynopsis_m[1]); | |
foreach($msynopsis as &$sy) { | |
$sy = trim($sy); | |
$sy_p = explode(' ', $sy, 2); | |
$sy = trim($sy_p[1]); | |
} | |
$msynopsis = implode(', ', $msynopsis); | |
preg_match('/<span class="dc-title">(.*)<\/span>/ismU', $f, $desc); | |
$desc = trim($desc[1]); | |
$msynopsis_no_ref = str_replace("&", "", $msynopsis); | |
echo "\t\t// $synopsis - $desc\n"; | |
echo "\t\tpublic function " . $inner_function[1] . "($msynopsis) {\n\t\t\treturn $function($msynopsis_no_ref);\n\t\t} "; | |
echo "\n\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment