Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: manage third party libs via composer #8240

Draft
wants to merge 16 commits into
base: release-3.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
->in(__DIR__)
// Don't touch libraries.
->exclude([
'vendor',
'cache',
'other',
'Packages',
'Smileys',
'Sources/minify',
'Sources/random_compat',
'Sources/ReCaptcha',
'Themes',
])
// Skip all index.php files and ssi_example.php.
Expand Down
8 changes: 1 addition & 7 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
filter:
excluded_paths:
- '*.min.js'
- 'Sources/minify/'
- 'Sources/random_compat/'
- 'Sources/ReCaptcha/'
dependency_paths:
- 'Sources/minify/'
- 'Sources/random_compat/'
- 'Sources/ReCaptcha/'
- 'vendor/' # Just to be sure
checks:
php:
variable_existence: true
Expand Down
2 changes: 0 additions & 2 deletions Sources/Actions/Admin/ACP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,6 @@ public static function getFileVersions(array &$versionOptions): array
);

$ignore_sources = [
Config::$sourcedir . '/minify/*',
Config::$sourcedir . '/ReCaptcha/*',
Config::$sourcedir . '/Tasks/*',
];

Expand Down
79 changes: 19 additions & 60 deletions Sources/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,32 @@

namespace SMF;

/*
* An autoloader for certain classes.
*
* @param string $class The fully-qualified class name.
*/
spl_autoload_register(function ($class) {
static $hook_value = '';

static $class_map = [
// Some special cases.
'ReCaptcha\\' => 'ReCaptcha/',
'MatthiasMullie\\Minify\\' => 'minify/src/',
'MatthiasMullie\\PathConverter\\' => 'minify/path-converter/src/',
$loader = require Config::$vendordir . '/autoload.php';
$third_party_mappers = [];

// In general, the SMF namespace maps to $sourcedir.
'SMF\\' => '',
];
// Ensure $sourcedir is set to something valid.
if (class_exists(Config::class, false) && isset(Config::$sourcedir)) {
$sourcedir = Config::$sourcedir;
}

// Ensure $sourcedir is set to something valid.
if (class_exists(Config::class, false) && isset(Config::$sourcedir)) {
$sourcedir = Config::$sourcedir;
}
if (empty($sourcedir) || !is_dir($sourcedir)) {
$sourcedir = __DIR__;
}

if (empty($sourcedir) || !is_dir($sourcedir)) {
$sourcedir = __DIR__;
// Do any third-party scripts want in on the fun?
if (!defined('SMF_INSTALLING') && class_exists(Config::class, false)) {
if (!class_exists(IntegrationHook::class, false) && is_file($sourcedir . '/IntegrationHook.php')) {
require_once $sourcedir . '/IntegrationHook.php';
}

// Do any third-party scripts want in on the fun?
if (!defined('SMF_INSTALLING') && class_exists(Config::class, false) && $hook_value !== (Config::$modSettings['integrate_autoload'] ?? '')) {
if (!class_exists(IntegrationHook::class, false) && is_file($sourcedir . '/IntegrationHook.php')) {
require_once $sourcedir . '/IntegrationHook.php';
}

if (class_exists(IntegrationHook::class, false)) {
$hook_value = Config::$modSettings['integrate_autoload'];
IntegrationHook::call('integrate_autoload', [&$class_map]);
}
if (class_exists(IntegrationHook::class, false)) {
IntegrationHook::call('integrate_autoload', [&$third_party_mappers]);
}
}

foreach ($class_map as $prefix => $dirname) {
// Does the class use the namespace prefix?
$len = strlen($prefix);

if (strncmp($prefix, $class, $len) !== 0) {
continue;
}

// Get the relative class name.
$relative_class = substr($class, $len);

// Replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$filename = $dirname . strtr($relative_class, '\\', '/') . '.php';

// Failsafe: Never load a file named index.php.
if (basename($filename) === 'index.php') {
return;
}

// If the file exists, require it.
if (file_exists($filename = $sourcedir . '/' . $filename)) {
require $filename;

return;
}
}
});
foreach ($third_party_mappers as $prefix => $dirname) {
$loader->addPsr4($prefix, $dirname);
}

?>
6 changes: 6 additions & 0 deletions Sources/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ class Config
* Path to the Sources directory.
*/
public static string $sourcedir;
/**
* Path to where our dependencies are located.
*/
public static string $vendordir;
/**
* Path to the Packages directory.
*
Expand Down Expand Up @@ -949,6 +953,8 @@ public static function set(array $settings): void
// As of 3.0, this is no longer changeable.
self::$tasksdir = self::$sourcedir . '/Tasks';

self::$vendordir = self::$boarddir . '/vendor';

if ((empty(self::$packagesdir) || !is_dir(realpath(self::$packagesdir))) && is_dir(self::$boarddir . '/Packages')) {
self::$packagesdir = self::$boarddir . '/Packages';
}
Expand Down
Loading