Skip to content

Commit beccf6e

Browse files
committed
Improved autoload performances
1 parent 52abd1b commit beccf6e

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/autoload.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616
define('PFC_BIN_DIR', __DIR__ . '/../bin/');
1717

1818
/**
19-
* Register Autoload
19+
* Register PhpFastCache Autoload
20+
*/
21+
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . DIRECTORY_SEPARATOR);
22+
spl_autoload_register();
23+
24+
/**
25+
* Register PhpFastCache
26+
* embedded-libraries Autoload
2027
*/
2128
spl_autoload_register(function ($entity) {
22-
$module = explode('\\', $entity, 2);
23-
if (!in_array($module[ 0 ], ['phpFastCache', 'Psr'])) {
24-
/**
25-
* Not a part of phpFastCache file
26-
* then we return here.
27-
*/
28-
return;
29-
} else if (strpos($entity, 'Psr\Cache') === 0) {
29+
/**
30+
* Attempting to load Psr\Cache
31+
*/
32+
if (strpos($entity, 'Psr\Cache') === 0) {
3033
$path = PFC_BIN_DIR . 'legacy/Psr/Cache/src/' . substr(strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT;
3134

3235
if (is_readable($path)) {
@@ -35,7 +38,12 @@
3538
trigger_error('Cannot locate the Psr/Cache files', E_USER_ERROR);
3639
}
3740
return;
38-
} else if (strpos($entity, 'Psr\SimpleCache') === 0) {
41+
}
42+
43+
/**
44+
* Attempting to load Psr\SimpleCache
45+
*/
46+
if (strpos($entity, 'Psr\SimpleCache') === 0) {
3947
$path = PFC_BIN_DIR . 'legacy/Psr/SimpleCache/src/' . substr(strrchr($entity, '\\'), 1) . '.' . PFC_PHP_EXT;
4048

4149
if (is_readable($path)) {
@@ -45,13 +53,6 @@
4553
}
4654
return;
4755
}
48-
49-
$entity = str_replace('\\', '/', $entity);
50-
$path = __DIR__ . '/' . $entity . '.' . PFC_PHP_EXT;
51-
52-
if (is_readable($path)) {
53-
require_once $path;
54-
}
5556
});
5657

5758
if ((!defined('PFC_IGNORE_COMPOSER_WARNING') || !PFC_IGNORE_COMPOSER_WARNING) && class_exists('Composer\Autoload\ClassLoader')) {

tests/Autoload.test.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
$testHelper->printPassText('Autoload successfully found the CacheManager');
2121
}
2222

23+
/**
24+
* Testing Psr autoload
25+
*/
26+
if (!class_exists('phpFastCache\Drivers\Files\Driver')) {
27+
$testHelper->printFailText('Autoload failed to find the "files" driver');
28+
}else{
29+
$testHelper->printPassText('Autoload successfully found the "files" driver');
30+
}
31+
2332
/**
2433
* Testing Psr autoload
2534
*/

0 commit comments

Comments
 (0)