xp-framework / core

The XP Framework is an all-purpose, object oriented PHP framework.
Other
19 stars 6 forks source link

Use Composer autoloader when executing scripts #303

Closed thekid closed 2 years ago

thekid commented 2 years ago

This pull request uses Composer's autoloading mechanism for XP scripts. The following now works:

<?php

use Smalot\PdfParser\Parser from 'smalot/pdfparser';
use util\cmd\Console;

// See See https://github.com/smalot/pdfparser
$parser= new Parser();
$pdf= $parser->parseFile($argv[1]);

Console::writeLine($pdf->getText());

Unlike #302, which reimplements this mechanism, this pull request ensures the XP core is not loaded twice.

This means it only works for up-to-date framework versions. If an older XP version is inside the vendor directory, scripts will yield Compile error (Cannot redeclare __error() (previously declared in .../lang.base.php:104). To get around this, we should ship the following diff as bugfix release:

diff --git a/src/main/php/__xp.php b/src/main/php/__xp.php
index e0dfafe79..2b18fb619 100755
--- a/src/main/php/__xp.php
+++ b/src/main/php/__xp.php
@@ -4,6 +4,8 @@ if (PHP_VERSION_ID < 70000) {
   throw new \Exception('This version of the XP Framework requires PHP 7.0.0+, have PHP '.PHP_VERSION);
 }

+if (class_exists(xp::class, false)) return;
+
 $p= max(strrpos(__FILE__, DIRECTORY_SEPARATOR), strrpos(__FILE__, '?'));
 require_once substr(__FILE__, 0, $p + 1).'lang.base.php';
thekid commented 2 years ago

Made obsolete by xp-runners/reference#85