Skip to content

Commit

Permalink
Revert changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wppunk committed Oct 18, 2020
1 parent 3fd4d8d commit 9dbc776
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
32 changes: 28 additions & 4 deletions src/class-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,24 @@ class Autoload {
* @var string
*/
private $folder;
/**
* Cache
*
* @var Cache
*/
private $cache;

/**
* Autoload constructor.
*
* @param string $prefix Prefix for your namespace.
* @param string $folder Path to folder.
* @param Cache $cache Cache.
*/
public function __construct( $prefix, $folder ) {
public function __construct( $prefix, $folder, Cache $cache ) {
$this->prefix = ltrim( $prefix, '\\' );
$this->folder = $folder;
$this->cache = $cache;
spl_autoload_register( [ $this, 'autoload' ] );
}

Expand All @@ -60,12 +68,23 @@ public function __construct( $prefix, $folder ) {
*
* @throws Exception Class not found.
*/
public function autoload( $class ) {
private function autoload( $class ) {
if ( 0 !== strpos( $class, $this->prefix ) ) {
return;
}

require_once $this->file_path( $class );
$path = $this->cache->get( $class );
if ( $path ) {
require_once $path;

return;
}

$path = $this->file_path( $class );

require_once $path;

$this->cache->update( $class, $path );
}

/**
Expand All @@ -74,6 +93,8 @@ public function autoload( $class ) {
* @param string $class Full class name.
*
* @return string
*
* @throws Exception Class not found.
*/
private function file_path( $class ) {
$class = str_replace( $this->prefix, '', $class );
Expand All @@ -86,8 +107,11 @@ private function file_path( $class ) {
$local_path = strtolower( str_replace( [ '\\', '_' ], [ '/', '-' ], $local_path ) );

$path = $this->folder . '/' . $local_path;
if ( file_exists( $path ) ) {
return $path;
}

return $path;
throw new Exception( $class, $path );
}

}
6 changes: 4 additions & 2 deletions wpautoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

use Composer\Factory;
use WPPunk\Autoload\Cache;
use Composer\Json\JsonFile;
use WPPunk\Autoload\Autoload;

Expand All @@ -19,8 +20,9 @@
return;
}

$dir = dirname( Factory::getComposerFile() ) . '/';
$dir = dirname( Factory::getComposerFile() ) . '/';
$cache = new Cache();

foreach ( $content['extra']['wp-autoload'] as $prefix => $folder ) {
new Autoload( $prefix, $dir . $folder );
new Autoload( $prefix, $dir . $folder, $cache );
}

0 comments on commit 9dbc776

Please sign in to comment.