Making WordPress.org

Changeset 2762


Ignore:
Timestamp:
03/17/2016 06:42:01 PM (9 years ago)
Author:
obenland
Message:

Plugin Directory: Account for dashes in folder names when loading a class.

Example:
Admin\List_Table => /admin/list-table/.

Also adds some property/method docs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-autoloader.php

    r2621 r2762  
    88 */
    99class Autoloader {
     10
     11    /**
     12     * Namespace separator.
     13     */
    1014    const NS_SEPARATOR = '\\';
     15
     16    /**
     17     * The prefix to compare classes against.
     18     *
     19     * @var string
     20     * @access protected
     21     */
    1122    protected $prefix;
     23
     24    /**
     25     * Length of the prefix string.
     26     *
     27     * @var int
     28     * @access protected
     29     */
    1230    protected $prefix_length;
     31
     32    /**
     33     * Path to the file to be loaded.
     34     *
     35     * @var string
     36     * @access protected
     37     */
    1338    protected $path;
    1439
     40    /**
     41     * Constructor.
     42     *
     43     * @param string $prefix Prefix all classes have in common.
     44     * @param string $path   Path to the files to be loaded.
     45     */
    1546    public function __construct( $prefix, $path ) {
    1647        $this->prefix        = $prefix;
     
    1950    }
    2051
     52    /**
     53     * Loads a class if it starts with `$this->prefix`.
     54     *
     55     * @param string $class The class to be loaded.
     56     */
    2157    public function load( $class ) {
    2258        if ( strpos( $class, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
     
    3167        if ( false !== ( $last_ns_pos = strripos( $class, self::NS_SEPARATOR ) ) ) {
    3268            $namespace = substr( $class, 0, $last_ns_pos );
     69            $namespace = str_replace( '_', '-', $namespace );
    3370            $class     = substr( $class, $last_ns_pos + 1 );
    3471            $file      = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
     
    4582}
    4683
     84/**
     85 * Registers Autoloader's autoload function.
     86 *
     87 * @param string $prefix
     88 * @param string $path
     89 */
    4790function register_class_path( $prefix, $path ) {
    4891    $loader = new Autoloader( $prefix, $path );
Note: See TracChangeset for help on using the changeset viewer.