Skip to content

Commit

Permalink
Implemented classmap pruning method (#7)
Browse files Browse the repository at this point in the history
* Implemented classmap pruning method

* Fixed some typos, improved the readme
  • Loading branch information
BelleNottelling authored Oct 3, 2023
1 parent 9d2e5d4 commit ce5aeeb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# AntLoader


[![Packagist Downloads](https://img.shields.io/packagist/dt/antcms/antloader)](https://packagist.org/packages/antcms/antloader)
[![PHP Tests](https://github.com/AntCMS-org/AntLoader/actions/workflows/tests.yml/badge.svg)](https://github.com/AntCMS-org/AntLoader/actions/workflows/tests.yml)
[![PHPStan](https://github.com/AntCMS-org/AntLoader/actions/workflows/phpstan.yml/badge.svg)](https://github.com/AntCMS-org/AntLoader/actions/workflows/phpstan.yml)

A small, simple, and highly performant autoloader for PHP applications.

- Supports at least PHP 8.0
Expand Down Expand Up @@ -31,8 +36,8 @@ $loader->resetClassMap(); // Reset the classmap, clearing the existing one out f
```

## Configuration
AntLoader accepts uses an array to configure it's available options.
None of the configuration options are required, however at a minimum it is recommended to specify a path unless you know APCu will be usable in all enviroments for your application.
AntLoader accepts an array to configure it's available options.
None of the configuration options are required, however at a minimum it is recommended to specify a path unless you know APCu will be usable in all environments for your application.
**Note**: Please read the "Classmap Caching Options" section of this document, as that covers the strengths and weaknesses of each caching approach.

```PHP
Expand Down Expand Up @@ -64,7 +69,7 @@ This feature allows AntLoader to achieve optimal performance by persisting the c
Here are a few things to note about the APCu mode:

- AntLoader generates a random key based on the directory it resides in. This ensures a unique key name to avoid accidentally overwriting APCu keys. The generated key remains static throughout the lifespan of the application.
- As long as you aren't running two seperate PHP applications & using the same copy of AntLoader (which you shouldn't be), this is sufficient to prevent issues.
- As long as you aren't running two separate PHP applications & using the same copy of AntLoader (which you shouldn't be), this is sufficient to prevent issues.
- Depending on your web server configuration, using APCu may allow the classmap to be accessed by other PHP applications. However, in the case of AntLoader, this information only includes the namespaces/classes within your application and their respective paths.
- By default, AntLoader stores the classmap with APCu using a Time-to-Live (TTL) of 7 days.

Expand All @@ -75,7 +80,7 @@ Here are some details about the filesystem caching method:

- By default, AntLoader saves the classmap to the system's temporary directory, which may not survive through multiple sessions. It is recommended to override the default path and specify a more persistent location.
- The classmap file stored in the filesystem has no lifespan limit imposed by AntLoader. It will be retained until either you delete the file or call the `resetClassMap` function.
- Clearing or resetting the classmap is generally easier to perform outside of calling the resetClassMap function provided by AntLoader.
- Clearing or resetting the classmap is generally easier to perform outside of calling the `resetClassMap` function provided by AntLoader.

## Notes

Expand All @@ -85,15 +90,19 @@ Here are some details about the filesystem caching method:
- Depending on the setup, prepending AntLoader may speed up the performance of your application.
- For example, if you are using composer's autoloader and have a lot of composer packages, that may delay the time it takes to load classes within your application.
- In this example, the classes inside of your application will load slightly faster and classes loaded through composer will be slightly slower.
- Potential improvements are highly dependent on the specific application and enviroment. In most situations, the difference is likely to be very minimal.
- Potential improvements are highly dependent on the specific application and environment. In most situations, the difference is likely to be very minimal.

So, we encourage you to take advantage of the classmap feature to get the best performance out of your application.

### Maintaining AntLoader
AntLoader is generally hands-off, except that we highly recomend clearing out / resetting the classmap after updating your application.
AntLoader is generally hands-off, except that we highly recommend clearing out / resetting the classmap after updating your application.
AntLoader will **never** remove outdated classes / paths from the classmap, so never allowing it to be rebuilt can negatively affect the performance of your application if classes are renamed or moved.
The best way to do this is simply to call the `resetClassMap` function that AntLoader provides. This will automatically reset the classmap for the current Cache method.

#### Pruning the classmap
If you have an application where the class list may periodically change, you can prune the classmap periodically to ensure it's not filling up with classes that no longer exist.
To do so, simply call `pruneClassmap`. This function will return the number of pruned classes.

## License

AntLoader is distributed with no warranty under the [Apache License 2.0](https://github.com/AntCMS-org/AntLoader/blob/main/LICENSE)
28 changes: 25 additions & 3 deletions src/AntLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class AntLoader
{
private string $classMapPath = '';

/** @var array<string,array<string>> **/
/** @var array<string,array<string>> */
private array $psr0 = [];

/** @var array<string,array<string>> **/
/** @var array<string,array<string>> */
private array $psr4 = [];

/** @var array<string,string> **/
/** @var array<string,string> */
private array $classMap = [];

private int $cacheType = 0;
Expand Down Expand Up @@ -251,6 +251,28 @@ public function autoload(string $class): void
}
}

/**
* Prunes the classmap cache of any non-existant classes
*
* @return int The number of classes that was pruned.
*/
public function pruneClassmap(): int
{
$pruned = 0;
foreach ($this->classMap as $class => $path) {
if (!file_exists($path)) {
$pruned++;
unset($this->classMap[$class]);
}
}

if ($pruned > 0) {
$this->saveMap();
}

return $pruned;
}

/**
* @param string $class Classname, after having any specialized handling performed
* @param string $path The path associated with the namespace
Expand Down
14 changes: 14 additions & 0 deletions tests/RandomClassesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,17 @@
$loader->resetClassMap();
deleteRandomClasses();
});

test('Prune classmap', function () {
// Test class loading with class map
deleteRandomClasses();
createRandomClasses(10);

$loader = setupLoader('filesystem');
$loader->resetClassMap();
$loader->checkClassMap();

deleteRandomClasses();

expect($loader->pruneClassmap())->toEqual(10);
});

0 comments on commit ce5aeeb

Please sign in to comment.