Is there any way to determine if the current file is the “root” file (requested by apache, or ran via the CLI) or if it is an included file?
basename($_SERVER['SCRIPT_FILENAME']) != basename(__FILE__)
Would work, but it only works based on filenames, not the whole path, so kinda hacky. I’d prefer if there was a nicer, and more elegent way to do this.
Bit of context
I’m writing a PHP 5.4+ API framework named Scaffold, which is extremely useful for the api first methodology. The current issue is, is that many people who write an api first application, use their api via a class in the application.
$user = Scaffold::get('/users/nathaniel');
A nice way to do this, is if that was the actual api, rather than subsequently sending a web request to the server. That way, you avoid the overhead of the HTTP protocol.
I have a pretty good idea on how to do that, but the problem is, I need to know if I should send the response to the browser (encoded and all) or wait for a manual call of Scaffold::request (or any of it’s aliases, Scaffold::get being just one of them.
The way I’ve decided to do this, is to check if the root file of the application (the file usually requested via apache) was actually requested, or was included by a different file.
I found something that works by myself. This is PHP 5.4+ only (because of function back referencing)