kollapsminoriteten/wp-includes/SimplePie/autoloader.php

87 lines
2.0 KiB
PHP
Raw Normal View History

2019-11-02 10:38:58 +01:00
<?php
2025-12-12 13:15:55 +01:00
// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
// SPDX-License-Identifier: BSD-3-Clause
2025-02-28 08:42:11 +01:00
2019-11-02 10:38:58 +01:00
/**
2025-02-28 08:42:11 +01:00
* PSR-4 implementation for SimplePie.
2019-11-02 10:38:58 +01:00
*
2025-02-28 08:42:11 +01:00
* After registering this autoload function with SPL, the following line
* would cause the function to attempt to load the \SimplePie\SimplePie class
* from /src/SimplePie.php:
2019-11-02 10:38:58 +01:00
*
2025-02-28 08:42:11 +01:00
* new \SimplePie\SimplePie();
2019-11-02 10:38:58 +01:00
*
2025-02-28 08:42:11 +01:00
* @param string $class The fully-qualified class name.
* @return void
2019-11-02 10:38:58 +01:00
*/
2025-02-28 08:42:11 +01:00
spl_autoload_register(function ($class) {
2019-11-02 10:38:58 +01:00
2025-02-28 08:42:11 +01:00
// project-specific namespace prefix
$prefix = 'SimplePie\\';
2019-11-02 10:38:58 +01:00
2025-02-28 08:42:11 +01:00
// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/';
2019-11-02 10:38:58 +01:00
2025-02-28 08:42:11 +01:00
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
2019-11-02 10:38:58 +01:00
2025-02-28 08:42:11 +01:00
// get the relative class name
$relative_class = substr($class, $len);
2019-11-02 10:38:58 +01:00
2025-02-28 08:42:11 +01:00
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
2020-09-15 14:29:22 +02:00
2025-02-28 08:42:11 +01:00
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
// autoloader
spl_autoload_register(array(new SimplePie_Autoloader(), 'autoload'));
if (!class_exists('SimplePie'))
{
exit('Autoloader not registered properly');
}
/**
* Autoloader class
*/
class SimplePie_Autoloader
{
protected $path;
2019-11-02 10:38:58 +01:00
/**
2025-02-28 08:42:11 +01:00
* Constructor
2019-11-02 10:38:58 +01:00
*/
2025-02-28 08:42:11 +01:00
public function __construct()
2019-11-02 10:38:58 +01:00
{
2025-02-28 08:42:11 +01:00
$this->path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'library';
2019-11-02 10:38:58 +01:00
}
/**
2025-02-28 08:42:11 +01:00
* Autoloader
2019-11-02 10:38:58 +01:00
*
2025-02-28 08:42:11 +01:00
* @param string $class The name of the class to attempt to load.
2019-11-02 10:38:58 +01:00
*/
2025-02-28 08:42:11 +01:00
public function autoload($class)
2019-11-02 10:38:58 +01:00
{
2025-02-28 08:42:11 +01:00
// Only load the class if it starts with "SimplePie"
if (strpos($class, 'SimplePie') !== 0)
2019-11-02 10:38:58 +01:00
{
2025-02-28 08:42:11 +01:00
return;
2019-11-02 10:38:58 +01:00
}
2020-09-15 14:29:22 +02:00
2025-02-28 08:42:11 +01:00
$filename = $this->path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
include $filename;
2019-11-02 10:38:58 +01:00
}
}