WordPress.org

Make WordPress Core

Changeset 37636


Ignore:
Timestamp:
06/06/2016 03:23:38 AM (4 years ago)
Author:
rmccue
Message:

Autoload: Introduce shim for SPL autoloading.

For PHP 5.2, SPL can be disabled. As SPL provides the support for multiple autoloaders, this needs to be shimmed if not available.

Fixes #36926.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-simplepie.php

    r34348 r37636  
    3030}
    3131
    32 if ( function_exists( 'spl_autoload_register' ) ) {
    33     /**
    34      * We autoload classes we may not need.
    35      *
    36      * If SPL is disabled, we load all of SimplePie manually.
    37      *
    38      * Core.php is not loaded manually, because SimplePie_Core (a deprecated class)
    39      * was never included in WordPress core.
    40      */
    41     spl_autoload_register( 'wp_simplepie_autoload' );
    42 } else {
    43     require ABSPATH . WPINC . '/SimplePie/Cache/Base.php';
    44     require ABSPATH . WPINC . '/SimplePie/Cache/DB.php';
    45     require ABSPATH . WPINC . '/SimplePie/Cache/File.php';
    46     require ABSPATH . WPINC . '/SimplePie/Cache/Memcache.php';
    47     require ABSPATH . WPINC . '/SimplePie/Cache/MySQL.php';
    48     require ABSPATH . WPINC . '/SimplePie/Caption.php';
    49     require ABSPATH . WPINC . '/SimplePie/Category.php';
    50     require ABSPATH . WPINC . '/SimplePie/Copyright.php';
    51     require ABSPATH . WPINC . '/SimplePie/Credit.php';
    52     require ABSPATH . WPINC . '/SimplePie/Decode/HTML/Entities.php';
    53     require ABSPATH . WPINC . '/SimplePie/Enclosure.php';
    54     require ABSPATH . WPINC . '/SimplePie/gzdecode.php';
    55     require ABSPATH . WPINC . '/SimplePie/HTTP/Parser.php';
    56     require ABSPATH . WPINC . '/SimplePie/Net/IPv6.php';
    57     require ABSPATH . WPINC . '/SimplePie/Rating.php';
    58     require ABSPATH . WPINC . '/SimplePie/Restriction.php';
    59     require ABSPATH . WPINC . '/SimplePie/Source.php';
    60 }
     32/**
     33 * We autoload classes we may not need.
     34 */
     35spl_autoload_register( 'wp_simplepie_autoload' );
    6136
    6237/**
  • trunk/src/wp-includes/compat.php

    r36490 r37636  
    435435    require ABSPATH . WPINC . '/random_compat/random.php';
    436436}
     437
     438// SPL can be disabled on PHP 5.2
     439if ( ! function_exists( 'spl_autoload_register' ) ):
     440    $_wp_spl_autoloaders = array();
     441
     442    /**
     443     * Autoloader compatibility callback.
     444     *
     445     * @param string $classname Class to attempt autoloading.
     446     */
     447    function __autoload( $classname ) {
     448        global $_wp_spl_autoloaders;
     449        foreach ( $_wp_spl_autoloaders as $autoloader ) {
     450            if ( ! is_callable( $autoloader ) ) {
     451                // Avoid the extra warning if the autoloader isn't callable.
     452                continue;
     453            }
     454
     455            call_user_func( $autoloader, $classname );
     456
     457            // If it has been autoloaded, stop processing.
     458            if ( class_exists( $classname, false ) ) {
     459                return;
     460            }
     461        }
     462    }
     463
     464    /**
     465     * Register a function to be autoloaded.
     466     *
     467     * @param callable $autoload_function The function to register.
     468     * @param boolean $throw Should the function throw an exception if the function isn't callable?
     469     * @param boolean $prepend Should we prepend the function to the stack?
     470     */
     471    function spl_autoload_register( $autoload_function, $throw = true, $prepend = false ) {
     472        if ( $throw && ! is_callable( $autoload_function ) ) {
     473            // String not translated to match PHP core.
     474            throw new Exception( 'Function not callable' );
     475        }
     476
     477        global $_wp_spl_autoloaders;
     478
     479        // Don't allow multiple registration.
     480        if ( in_array( $autoload_function, $_wp_spl_autoloaders ) ) {
     481            return;
     482        }
     483
     484        if ( $prepend ) {
     485            array_unshift( $_wp_spl_autoloaders, $autoload_function );
     486        } else {
     487            $_wp_spl_autoloaders[] = $autoload_function;
     488        }
     489    }
     490
     491    /**
     492     * Unregister an autoloader function.
     493     *
     494     * @param callable $function The function to unregister.
     495     * @return boolean True if the function was unregistered, false if it could not be.
     496     */
     497    function spl_autoload_unregister( $function ) {
     498        global $_wp_spl_autoloaders;
     499        foreach ( $_wp_spl_autoloaders as &$autoloader ) {
     500            if ( $autoloader === $function ) {
     501                unset( $autoloader );
     502                return true;
     503            }
     504        }
     505
     506        return false;
     507    }
     508
     509    /**
     510     * Get the registered autoloader functions.
     511     *
     512     * @return array List of autoloader functions.
     513     */
     514    function spl_autoload_functions() {
     515        return $GLOBALS['_wp_spl_autoloaders'];
     516    }
     517endif;
Note: See TracChangeset for help on using the changeset viewer.