xp-framework/rfc

Advertise scripts as entry point

thekid opened this issue · 4 comments

Scope of Change

This RFC suggests advertising scripts as the preferred way over classes as entry point.

Rationale

Brevity: Scripts do not have an enclosing class, but rather just start with the code right away (after some optional use statements).

Functionality

  • Change README on front page - the example will be changed to a script in the file ageindays.php. The option to use classes with a static main() method will be mentioned "if one prefers reuse".
  • Check execution - Currently, the whole script is loaded into memory using file_get_contents() and rewritten using string replacements. See if this needs to be optimized.

Example

<?php
use util\{Date, DateUtil};
use util\cmd\Console;

$span= DateUtil::timespanBetween(new Date($argv[1]), Date::now());
Console::writeLine('Hey, you are ', $span->getDays(), ' days old');

Security considerations

Speed impact

Dependencies

Related documents

xp-framework/core#127 - Enable running scripts from files

Should we advertise a separate file extension, e.g. .script.php, .sxp or .sphp? It would show the things can't just be executed by the PHP processor. Note the latter will most probably break lots of IDEs' highlighting, so we might choose to go the same way as with .class.php...

What's the reason (target) for this change?

I think it's a bad idea since it can cause security problems:
Changing the file extension prevents the execution of the script by the interpreter but can expose source code if the web server isn't configured to deny access to this files (the .inc problem all over again).
And as you mentioned, it will break IDEs and other tools expecting this extension.

It the reason is only the simplification while executing the scripts, a central registry with an alias for every script could be a solution. Something like the Symfony console component:
http://symfony.com/doc/current/components/console/introduction.html

What's the reason (target) for this change?

Stated in the "Rationale" section: Scripts are shorter than classes.

Class
use util\cmd\Console;
class HelloWorld {
  public static function main($args) {
    Console::writeLine('Hello World');
  }
}
Script
use util\cmd\Console;
Console::writeLine('Hello World');

Changing the file extension [...] can expose source

You're right. Therefore suggesting .script.php to be advertised as default.