Skip to content

Commit

Permalink
Implement custom autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Jan 23, 2024
1 parent cb9f678 commit ae575d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
7 changes: 3 additions & 4 deletions comment-hacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

use EmiliaProjects\WP\Comment\Inc\Autoload;
use EmiliaProjects\WP\Comment\Inc\Hacks;

/**
Expand All @@ -45,9 +46,7 @@
define( 'EMILIA_COMMENT_HACKS_PATH', plugin_dir_path( __FILE__ ) );
}

/* ***************************** CLASS AUTOLOADING *************************** */
if ( file_exists( EMILIA_COMMENT_HACKS_PATH . 'vendor/autoload.php' ) ) {
require EMILIA_COMMENT_HACKS_PATH . 'vendor/autoload.php';
}
require_once EMILIA_COMMENT_HACKS_PATH . 'inc/autoload.php';
new Autoload();

new Hacks();
6 changes: 0 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"classmap": [
"admin/",
"inc/"
]
},
"autoload-dev": {
"classmap": [
"tests/"
Expand Down
46 changes: 46 additions & 0 deletions inc/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace EmiliaProjects\WP\Comment\Inc;

/**
* Clean the emails.
*/
class Autoload {

/**
* Classmap. Key is the class name, value is the file path.
*
* @var array
*/
private static array $classmap = [
'EmiliaProjects\WP\Comment\Admin\Admin' => 'admin/admin.php',
'EmiliaProjects\WP\Comment\Admin\Comment_Parent' => 'admin/comment-parent.php',
'EmiliaProjects\WP\Comment\Inc\Clean_Emails' => 'inc/clean-emails.php',
'EmiliaProjects\WP\Comment\Inc\Email_Links' => 'inc/email-links.php',
'EmiliaProjects\WP\Comment\Inc\Forms' => 'inc/forms.php',
'EmiliaProjects\WP\Comment\Inc\Hacks' => 'inc/hacks.php',
'EmiliaProjects\WP\Comment\Inc\Length' => 'inc/length.php',
'EmiliaProjects\WP\Comment\Inc\Notifications' => 'inc/notifications.php',
];

/**
* Constructor.
*
* Register the autoloader.
*/
public function __construct() {
\spl_autoload_register( [ __CLASS__, 'autoload' ] );
}

/**
* Autoload the classes.
*
* @param string $class_name The class name.
*/
public static function autoload( string $class_name ): void {
if ( ! isset( static::$classmap[ $class_name ] ) ) {
return;
}
require_once EMILIA_COMMENT_HACKS_PATH . static::$classmap[ $class_name ];
}
}

0 comments on commit ae575d9

Please sign in to comment.