Creating a PHP extension extending another PHP extension? - php

I searched over the Internet several documentation about how to create PHP extensions, but unfortunately, there is nothing about linking to another extensions (and making a requirement for having that extension loaded prior to the new it is being created).
I guess I could simply #include necessary header files into my source code, but not sure about linking.
As an example, and to play with extension creation, the first I want to create is a solution I implemented to allow namespaces in memcached github but wanted to know how to use other extensions' code from my custom extension one for other usages as well.

I'm not sure how to reply to the thread with StormByte, but it sounds like you need to do some load balancing or caching, not extending PHP.
If you really want to do this at the code level, you could use exec() to call a Python script, which gets compiled into byte code automatically.

Related

Running WikiMedia lua script or expanding API module output

I have two specific questions and I hope someone can answer either of them:
Is there a way to provide MediaWiki's mw library to a stand-alone lua script?
Is there an API command or property that exposes the output of a dynamic module?
Background: I am trying to figure out how to access the output of a wiktionary module (in this case, pron-th). This is a module that can be dynamically inserted by editors to show transliteration (pronunciation) of Thai words. For example, whenever an editor has added this line:
{{th-pron|ไคฺร่}}
...the server will run the Lua script documented found here and outputs a table showing the various transliterations (example). However, this output is specifically excluded when doing API requests (example) and I cannot find an endpoint that includes this data. And running the lua script directly fails because it is missing several imports, such as mw.ustring, mw.text, etc., which I believe are defined in a PHP include higher up their software stack. I have significant PHP experience but none with Lua, so I am sort of at a loss here.
Short of calling up each page directly and scraping the data, I can't think of a way to do this.
The MediaWiki mw library is part of the Scribunto extension (see https://github.com/wikimedia/mediawiki-extensions-Scribunto/tree/master/includes/engines/LuaCommon/lualib), and has a lot of dependencies back to MEdiawikis php core, so it won't be easy to just import that.
You could render that wikitext via API like https://en.wiktionary.org/w/api.php?action=parse&text=%20{{th-pron|%E0%B9%84%E0%B8%84%E0%B8%BA%E0%B8%A3%E0%B9%88}}&contentmodel=wikitext and parse the output, or you could try and replace the MediaWiki specific function calls with other function calls to some native Lua library.

Dependencies graph for large PHP application

I've recently inherited a large PHP application with NO objects/modules/namespaces...only a lot of files containing functions.
Of course, there is a LOT of dependencies (and all files and almost always included).
I'm looking for a tool that could analyse the files and generate a dependencies graph. It would then be easier to detect independent files/set of files and re-factor the whole thing.
So far the best solution I've found would be to write a CodeSniffer sniff to detect all functions calls and then use that to generate the graph.
It seems something useful for other, so I'm sure tools already exists for it.
What would you recommend ?
I think that the best solution is use a doc generat + grapviz, PHPDocumentor looks to have a Grapviz extension at https://github.com/phpDocumentor/GraphViz
This is a example made with PHPDocumentor:
http://demo.phpdoc.org/Clean/graphs/classes.svg
Too you can use a hierarchical profiler like xhprof (https://github.com/facebook/xhprof), this can draw a tree of all call to functions from a execution.
A example form xhprof draw done by Graphviz
I could recommend a lightweight project I wrote few days ago. Basically I had a 300+ files PHP project and I wanted to detect what files do these files require/include and vice-versa. Moreover, I wanted to check for each individual file what files does this file requires/includes (directly or indirectly, ie. via file inheritance) and vice-versa: what are the files that include this particular file. For any combination of these I wanted an interactive dependency graph (base on file inclusion and not on class/function calls/usage).
Check out the project's sandbox and its source code.
Note that the whole thing was written in only 2 days so don't judge it
too harsh. What's important is that it's doing its job!

Detect during compilation whether PHP is being compiled with a given extension

I am trying to build an extension for PHP. After following Sara Golemon's book I have a basic extension which I can compile as a shared module and, in addition, I can compile it statically along PHP itself.
Now I want to modify the PHP interpreter in order to intercept particular internal function invocations and communicate these calls to my extension. I want to do this only when my extension is statically compiled with PHP---the interpreter build process should otherwise generate an unmodified PHP binary. My understanding is that I should use the C preprocessor. However, to achieve my goal I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension (i.e. ./configure --enable-myextension). Unfortunately, I cannot find such a flag nor one seems to be set by the configure script.
I should say here that I have tried setting preprossessor flags within my extension's code but this will not work. My extension is first touched late in the build process (i.e. roughly after the core of the interpreter) and the flags I set there are not active when the bulk of interpreter code is being compiled.
Any thoughts? Do the above sound reasonable?
My understanding is that I should use the C preprocessor.
Nope, you don't need that.
I need a preprocessor flag that will only be raised when PHP is configured to compile with my extension
Why would you want that? It would basically limit the functionality of your extension artificially, although it's possible to hook function calls no matter how your extension is compiled.
Do the above sound reasonable?
In my opinion, it's not reasonable. Please have a look at how AOP hooks function calls: https://github.com/AOP-PHP/AOP
If you need to hook more than just function calls, you need to reach down at the lowest level, the opcodes, by using zend_set_user_opcode_handler(). Please use lxr.php.net or similar tools (fgrep, etc) to find out where and how such handlers are used. I know laruence was working hard on an interesting extension last year here: http://svn.php.net/viewvc/pecl/taint/trunk/taint.c?view=markup so I would take that as the most "up to date" way of doing things as a reference, if anything has changed in the meanwhile.

Put PHP files toghether

Is there a tool to take a PHP file, with all its dependencies to other external PHP files, and create one, huge, final PHP file that includes them all?
Thanks
You don't really want to take a whole library and put it in a single file, because you end up loading a bunch of class definitions that might not even be needed by your script (i.e.: script A might need it, but not script B, however they end up loading it anyway).
PHP 5.3 (and a PECL extension for 5.2) introduced PHARs (PHP Archives), which works a little like JARs (Java equivalent):
$phar = new Phar('myLibrary.phar');
$phar->addFile('myClass.php');
Then you can do:
include_once('phar://myLibrary.phar/myClass.php');
I use it often and it is indeed very useful for quick software updates on client/production servers.
I've never heard of anything like that before, but maybe this could help... not really sure how it works http://webscripts.softpedia.com/script/PHP-Clases/Split-and-merge-files-19891.html
It is a new extension built-in PHP 5.3. You can read more about it here
http://www.php.net/manual/en/intro.phar.php

ZipArchive php Class - Is it built-in to PHP?

Ok, just wondering on the versions of PHP that this class is built into. And if they are built into all platforms (OS's). I'm wanting an approach to search through a zip file and place files using file_put_contents in different filepaths within the webroot. In any case, I'm familiar with how to do this with the ZipArchive class, but I'm wondering if using this class would be a good solution and support MOST, if not ALL servers?? I mean, I'd rather not use a method that requires the Server to have it installed. I'm looking for a solution to this that will support at least MOST servers without having to install the class...
Thanks :)
Also, I'd like to support opening tar.gz and/or .tgz files if possible, but I don't think the ZipArchive class supports this, but perhaps a different built-in php class does??
Tar support is not built into PHP, but if you have a look at the PEAR library you should be able to find some classes that support creating/extracting tarballs (amongst others). Have a look at http://pear.php.net/package/Archive_Tar or http://pear.php.net/package/File_Archive. The last one should be a generic interface to multiple archiving formats (including ZIP and TAR).
Whether or not ZIP support is built-in may vary, though I guess most packagers will include it. Then again, you could always test it by checking if the ZipArchive class exists by calling class_exists('ZipArchive'); and show a nice error message or fall back to a more generic approach...

Categories