Skip to content

Commit 3cc81a0

Browse files
committed
Feature: Add the AutoLoad utility class
1 parent 10a508e commit 3cc81a0

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-17
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ This parameter accepts an array holding options.
7070

7171
- `output_buffer` : (boolean) whether output buffer should be printed.
7272
- `exclude_classes` : (array) an array holding class names to exclude.
73-
- `base_dir_var` : (string) the variable or constant name that is prefixed before the inclusion path.
73+
- `base_dir_var` : (string) the variable or constant name that is prefixed before the inclusion path. Default: `__DIR__`.
7474
- `output_var_name` : (string) the variable string that the map array is assigned to. Default: `$aClassMap`. If `return` is set, the variable will not be set but the file just returns the generated map array.
7575
- `do_in_constructor` : (boolean) whether to perform the action in the constructor. Default: `true`.
7676
- `structure` : (string) either `CLASS` or `PATH`. When `CLASS` is set, the generated array keys consist of class names. When `PATH` is set, the generated array keys consist of paths. Default: `CLASS`.
@@ -143,5 +143,23 @@ print_r( $_oGenerator->get() );
143143
```
144144
To find JavaScript files, change the `'css'` part to `'js'` in the `allowed_extensions` search argument.
145145

146+
## Bundled Utility Autoloader
147+
This package includes an autoloader.
148+
149+
### Examples
150+
#### Including an array of class list
151+
```
152+
$_aClassMap = [
153+
"PHPClassMapGenerator\\PHPClassMapGenerator" => __DIR__ . "/PHPClassMapGenerator.php",
154+
"PHPClassMapGenerator\\Autoload" => __DIR__ . "/autoload.php",
155+
];
156+
PHPClassMapGenerator\Utility\Autoload::set( $_aClassMap );
157+
```
158+
159+
#### Including the class map file that returns an array of class list
160+
```
161+
PHPClassMapGenerator\Utility\Autoload::set( include( __DIR__ . '/class-map.php' ) );
162+
```
163+
146164
## License
147165
Licensed under [MIT](./LICENSE).

source/Utility/AutoLoad.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace PHPClassMapGenerator\Utility;
4+
5+
/**
6+
*
7+
* ### Examples
8+
* #### Including an array of class list
9+
* ```
10+
* $_aClassMap = [
11+
* "PHPClassMapGenerator\\PHPClassMapGenerator" => __DIR__ . "/PHPClassMapGenerator.php",
12+
* "PHPClassMapGenerator\\Autoload" => __DIR__ . "/autoload.php",
13+
* ];
14+
* PHPClassMapGenerator\Utility\Autoload::set( $_aClassMap );
15+
* ```
16+
* #### Including the class map file that returns an array of class list
17+
* ```
18+
* PHPClassMapGenerator\Utility\Autoload::set( include( __DIR__ . '/class-map.php' ) );
19+
* ```
20+
* @since 1.3.0
21+
*/
22+
class AutoLoad {
23+
24+
public $aClasses = [];
25+
26+
/**
27+
* @param array $aClasses
28+
*/
29+
public function __construct( array $aClasses ) {
30+
$this->aClasses = $aClasses;
31+
spl_autoload_register( array( $this, 'replyToIncludeClass' ), false );
32+
}
33+
34+
/**
35+
* @param string $sUnknownClassName
36+
* @callback sql_autoload_register()
37+
*/
38+
public function replyToIncludeClass( $sUnknownClassName ) {
39+
if ( ! isset( $this->aClasses[ $sUnknownClassName ] ) ) {
40+
return;
41+
}
42+
include( $this->aClasses[ $sUnknownClassName ] );
43+
}
44+
45+
/**
46+
* @param array $aClasses
47+
*/
48+
static public function set( array $aClasses ) {
49+
$_sSelfClass = __CLASS__;
50+
new $_sSelfClass( $aClasses );
51+
}
52+
53+
}

source/autoload.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,5 @@
22

33
namespace PHPClassMapGenerator;
44

5-
Autoload::set( include( __DIR__ . '/class-map.php' ) );
6-
7-
class Autoload {
8-
static public function set( array $aClasses ) {
9-
self::$aAutoLoadClasses = $aClasses + self::$aAutoLoadClasses;
10-
spl_autoload_register( array( __CLASS__, 'replyToIncludeClass' ), false );
11-
}
12-
static public function replyToIncludeClass( $sUnknownClassName ) {
13-
if ( ! isset( self::$aAutoLoadClasses[ $sUnknownClassName ] ) ) {
14-
return;
15-
}
16-
include( self::$aAutoLoadClasses[ $sUnknownClassName ] );
17-
}
18-
static public $aAutoLoadClasses = [];
19-
}
5+
include_once( __DIR__ . '/Utility/AutoLoad.php' );
6+
Utility\Autoload::set( include( __DIR__ . '/class-map.php' ) );

source/class-map.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
*/
88
return array(
99
"PHPClassMapGenerator\\PHPClassMapGenerator" => __DIR__ . "/PHPClassMapGenerator.php",
10-
"PHPClassMapGenerator\\Autoload" => __DIR__ . "/autoload.php",
1110
"PHPClassMapGenerator\\interfacePHPClassMapGenerator" => __DIR__ . "/interfacePHPClassMapGenerator.php",
1211
"PHPClassMapGenerator\\FileSystem\\FileListGenerator" => __DIR__ . "/FileSystem/FileListGenerator.php",
1312
"PHPClassMapGenerator\\FileSystem\\traitFileSearch" => __DIR__ . "/FileSystem/traitFileSearch.php",
1413
"PHPClassMapGenerator\\Header\\HeaderGenerator" => __DIR__ . "/Header/HeaderGenerator.php",
14+
"PHPClassMapGenerator\\Utility\\AutoLoad" => __DIR__ . "/Utility/AutoLoad.php",
1515
"PHPClassMapGenerator\\Utility\\traitCodeParser" => __DIR__ . "/Utility/traitCodeParser.php",
1616
"PHPClassMapGenerator\\Utility\\traitPath" => __DIR__ . "/Utility/traitPath.php",
1717
);

0 commit comments

Comments
 (0)