Skip to content
This repository has been archived by the owner on Jul 20, 2018. It is now read-only.

Commit

Permalink
Merge pull request #226 from ockham/autoloader
Browse files Browse the repository at this point in the history
Replace list of require_once statements by an autoloader
  • Loading branch information
joshbetz committed Feb 4, 2015
2 parents 10fb692 + 428bdf7 commit f94e3a0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
1 change: 0 additions & 1 deletion tests/checks/test-BaseScanner.php
Expand Up @@ -5,7 +5,6 @@ class BaseScannerTest extends WP_UnitTestCase {

public function setUp() {
parent::setUp();
require_once VIP_SCANNER_SCANNERS_DIR . '/class-base-scanner.php';

$this->resetBaseScanner();
}
Expand Down
60 changes: 35 additions & 25 deletions vip-scanner/vip-scanner.php
Expand Up @@ -6,36 +6,46 @@
define( 'VIP_SCANNER_DIR', dirname( __FILE__ ) );
define( 'VIP_SCANNER_CHECKS_DIR', VIP_SCANNER_DIR . '/checks' );
define( 'VIP_SCANNER_ANALYZERS_DIR', VIP_SCANNER_DIR . '/analyzers' );
define( 'VIP_SCANNER_RENDERERS_DIR', VIP_SCANNER_DIR . '/renderers' );
define( 'VIP_SCANNER_SCANNERS_DIR', VIP_SCANNER_DIR . '/scanners' );
define( 'VIP_SCANNER_BIN_DIR', VIP_SCANNER_DIR . '/bin' );

require_once( VIP_SCANNER_DIR . '/config-vip-scanner.php' );
require_once( VIP_SCANNER_DIR . '/class-base-check.php' );
require_once( VIP_SCANNER_DIR . '/class-preg-file.php' );
require_once( VIP_SCANNER_DIR . '/class-analyzed-file.php' );
require_once( VIP_SCANNER_DIR . '/class-analyzed-php-file.php' );
require_once( VIP_SCANNER_DIR . '/class-analyzed-css-file.php' );
require_once( VIP_SCANNER_DIR . '/class-base-analyzer.php' );

require_once( VIP_SCANNER_SCANNERS_DIR . '/class-base-scanner.php' );
require_once( VIP_SCANNER_SCANNERS_DIR . '/class-directory-scanner.php' );
require_once( VIP_SCANNER_SCANNERS_DIR . '/class-zip-scanner.php' );
require_once( VIP_SCANNER_SCANNERS_DIR . '/class-theme-scanner.php' );
require_once( VIP_SCANNER_SCANNERS_DIR . '/class-content-scanner.php' );
require_once( VIP_SCANNER_SCANNERS_DIR . '/class-diff-scanner.php' );

require_once( VIP_SCANNER_RENDERERS_DIR . '/class-analyzer-renderer.php' );
require_once( VIP_SCANNER_RENDERERS_DIR . '/class-resource-renderer.php' );
require_once( VIP_SCANNER_RENDERERS_DIR . '/class-renderer-group.php' );
require_once( VIP_SCANNER_RENDERERS_DIR . '/class-file-renderer.php' );
require_once( VIP_SCANNER_RENDERERS_DIR . '/class-namespace-renderer.php' );
require_once( VIP_SCANNER_RENDERERS_DIR . '/class-class-renderer.php' );
require_once( VIP_SCANNER_RENDERERS_DIR . '/class-function-renderer.php' );

spl_autoload_register( function( $class_name ) {

// Class names that are in files that aren't found by our scheme below.
$other = array(
'VIP_PregFile' => 'class-preg-file.php',
'VIP_Scanner_Async' => 'vip-scanner-async.php',
'AnalyzedPHPFile' => 'class-analyzed-php-file.php',
'AnalyzedCSSFile' => 'class-analyzed-css-file.php',
'RendererGroup' => 'renderers/class-renderer-group.php',
);

if ( array_key_exists( $class_name, $other ) ) {
require VIP_SCANNER_DIR . '/' . $other[ $class_name ];
return;
}

// Example: $class_name === 'ThemeScanner'
$hyphenated_name = strtolower( preg_replace('/([a-z])([A-Z])/', '$1-$2', $class_name ) ); // Example: theme-scanner
$category = substr( strrchr( $hyphenated_name, '-' ), 1 ); // Example: scanner
$plural = substr( $category, -1 ) === 'y' ? substr( $category, 0, -1 ) . 'ies' : $category . 's'; // Example: scanners

$possible_locations = array(
VIP_SCANNER_DIR . '/' . $plural . '/class-' . $hyphenated_name . '.php', // Example: vip-scanner/scanners/class-theme-scanner.php
VIP_SCANNER_DIR . '/class-' . $hyphenated_name . '.php', // Example: vip-scanner/class-theme-scanner.php
);

foreach( $possible_locations as $location ) {
if ( file_exists( $location ) ) {
require $location ;
return;
}
}
} );

if ( is_admin() ) {
require_once( VIP_SCANNER_SCANNERS_DIR . '/class-async-directory-scanner.php' );
require_once( VIP_SCANNER_DIR . '/vip-scanner-async.php' );
VIP_Scanner_Async::get_instance();
}

class VIP_Scanner {
Expand Down

0 comments on commit f94e3a0

Please sign in to comment.