Skip to content

Commit ec9df2b

Browse files
Updated class to work more powerful with classmaps.
1 parent 91875b0 commit ec9df2b

File tree

1 file changed

+65
-54
lines changed

1 file changed

+65
-54
lines changed

src/Autoloader.php

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ class Autoloader {
7373
*
7474
* @var bool
7575
*/
76-
protected $classmaps = false;
76+
protected $classmap = false;
77+
78+
/**
79+
* Stores All Classmaps.
80+
*
81+
* @var array
82+
* @static
83+
*/
84+
protected static $classmaps = array();
7785

7886
/**
7987
* Autoloader constructor.
@@ -104,22 +112,26 @@ protected function set_options( $options ) {
104112
'prepend' => false,
105113
'mapping' => false,
106114
'search_folders' => false,
107-
'classmaps' => false,
115+
'classmap' => false,
108116
), $options );
109117
$this->options['exclude'] = ( ! is_array( $this->options['exclude'] ) ) ? array_filter( array( $this->options['exclude'] ) ) : $this->options['exclude'];
110118
$this->options['mapping'] = ( ! is_array( $this->options['mapping'] ) ) ? array() : $this->options['mapping'];
111119
$this->mappings = $this->options['mapping'];
112120

113-
if ( ! empty( $this->options['classmaps'] ) ) {
114-
if ( is_array( $this->options['classmaps'] ) ) {
115-
$this->classmaps = $this->options['classmaps'];
116-
unset( $this->options['classmaps'] );
117-
} elseif ( is_string( $this->options['classmaps'] ) && file_exists( $this->options['classmaps'] ) ) {
118-
$this->classmaps = include $this->options['classmaps'];
121+
if ( ! empty( $this->options['classmap'] ) && is_string( $this->options['classmap'] ) && file_exists( $this->options['classmap'] ) ) {
122+
$this->classmap = $this->options['classmap'];
123+
if ( ! isset( self::$classmaps[ $this->options['classmap'] ] ) ) {
124+
$data = include $this->options['classmap'];
125+
foreach ( $data as $key => $value ) {
126+
unset( $data[ $key ] );
127+
$data[ strtolower( $key ) ] = $value;
128+
}
129+
self::$classmaps[ $this->options['classmap'] ] = $data;
119130
}
120131
}
121132

122133
unset( $this->options['mapping'] );
134+
unset( $this->options['classmaps'] );
123135
return $this;
124136
}
125137

@@ -185,69 +197,68 @@ protected function slashit( $string ) {
185197
*/
186198
public function autoload( $class ) {
187199
if ( true === $this->is_valid_lookup( $class ) ) {
200+
$lclass = strtolower( $class );
201+
$ckey = $this->classmap;
188202
$filenames = null;
189203
$folders = null;
190204
$is_loaded = false;
191205

192206
/**
193207
* @uses varunsridharan/php-classmap-generator script which provides location along with class name
194208
*/
195-
if ( isset( $this->classmaps[ $class ] ) ) {
196-
$is_loaded = $this->load_file( $this->classmaps[ $class ], $class );
197-
if ( false === $is_loaded ) {
198-
$is_loaded = $this->load_file( $this->base_path . $this->classmaps[ $class ], $class );
209+
if ( isset( self::$classmaps[ $ckey ][ $lclass ] ) && file_exists( $this->base_path . self::$classmaps[ $ckey ][ $lclass ] ) ) {
210+
$this->load_file( $this->base_path . self::$classmaps[ $ckey ][ $lclass ], $class );
211+
} else {
212+
/**
213+
* Checks and loads file if given class exists in mapping array.
214+
*
215+
* @example array(
216+
* '\somenamespace\someclass' => 'your-path/file.php'
217+
* '\somenamespace\someclass2' => 'your-path/file2.php'
218+
* )
219+
*/
220+
if ( false === $is_loaded && isset( $this->mappings[ $class ] ) ) {
221+
$is_loaded = $this->load_file( $this->mappings[ $class ], $class );
222+
if ( false === $is_loaded ) {
223+
$is_loaded = $this->load_file( $this->base_path . $this->mappings[ $class ], $class );
224+
}
199225
}
200-
}
201226

202-
/**
203-
* Checks and loads file if given class exists in mapping array.
204-
*
205-
* @example array(
206-
* '\somenamespace\someclass' => 'your-path/file.php'
207-
* '\somenamespace\someclass2' => 'your-path/file2.php'
208-
* )
209-
*/
210-
if ( false === $is_loaded && isset( $this->mappings[ $class ] ) ) {
211-
$is_loaded = $this->load_file( $this->mappings[ $class ], $class );
227+
/**
228+
* Checks and loads class based on the files & folder found.
229+
*/
212230
if ( false === $is_loaded ) {
213-
$is_loaded = $this->load_file( $this->base_path . $this->mappings[ $class ], $class );
214-
}
215-
}
216-
217-
/**
218-
* Checks and loads class based on the files & folder found.
219-
*/
220-
if ( false === $is_loaded ) {
221-
$filenames = $this->get_possible_filenames( $class );
222-
$folders = $this->get_possible_foldernames( $class );
223-
224-
if ( is_array( $filenames ) && is_array( $folders ) && ! empty( $filenames ) && ! empty( $folders ) ) {
225-
foreach ( $folders as $folder ) {
226-
foreach ( $filenames as $file ) {
227-
$is_loaded = $this->load_file( $this->slashit( $this->base_path . $folder ) . $file, $class );
231+
$filenames = $this->get_possible_filenames( $class );
232+
$folders = $this->get_possible_foldernames( $class );
233+
234+
if ( is_array( $filenames ) && is_array( $folders ) && ! empty( $filenames ) && ! empty( $folders ) ) {
235+
foreach ( $folders as $folder ) {
236+
foreach ( $filenames as $file ) {
237+
$is_loaded = $this->load_file( $this->slashit( $this->base_path . $folder ) . $file, $class );
238+
if ( $is_loaded ) {
239+
break;
240+
}
241+
}
228242
if ( $is_loaded ) {
229243
break;
230244
}
231245
}
232-
if ( $is_loaded ) {
233-
break;
234-
}
235246
}
236247
}
237-
}
238-
239-
/**
240-
* Checks and loads class based on all subfolder in the given path
241-
*/
242-
if ( false === $is_loaded && true === $this->option( 'search_folders' ) ) {
243-
$folders = $this->get_folders( $this->base_path );
244248

245-
if ( is_array( $folders ) && is_array( $filenames ) && ! empty( $filenames ) ) {
246-
foreach ( $folders as $folder ) {
247-
foreach ( $filenames as $file ) {
248-
$is_loaded = $this->load_file( $this->slashit( $folder ) . $file, $class );
249-
if ( $is_loaded ) {
250-
break;
249+
/**
250+
* Checks and loads class based on all subfolder in the given path
251+
*/
252+
if ( false === $is_loaded && true === $this->option( 'search_folders' ) ) {
253+
$folders = $this->get_folders( $this->base_path );
254+
255+
if ( is_array( $folders ) && is_array( $filenames ) && ! empty( $filenames ) ) {
256+
foreach ( $folders as $folder ) {
257+
foreach ( $filenames as $file ) {
258+
$is_loaded = $this->load_file( $this->slashit( $folder ) . $file, $class );
259+
if ( $is_loaded ) {
260+
break;
261+
}
251262
}
252263
}
253264
}

0 commit comments

Comments
 (0)