PHPackages                             purc/autoloader-class-map - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. purc/autoloader-class-map

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

purc/autoloader-class-map
=========================

Autoloader class map generator and autoloader for usage in composer or PHP projects

0.2.1(3y ago)011GPL-2.0-or-laterPHPPHP &gt;=7.0

Since Jan 29Pushed 3y ago1 watchersCompare

[ Source](https://github.com/muratpurc/mpAutoloaderClassMap)[ Packagist](https://packagist.org/packages/purc/autoloader-class-map)[ RSS](/packages/purc-autoloader-class-map/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)DependenciesVersions (3)Used By (0)

AutoloaderClassMap
==================

[](#autoloaderclassmap)

A PHP based class map generator and autoloader for usage with [composer](https://getcomposer.org/) or directly in PHP projects.

Is able to parse several defined directories/files for existing `class`/`interface`/`trait` definitions and to generate a class map configuration file which is usable for an autoloader implementation.

---

Installation
------------

[](#installation)

### Install using composer

[](#install-using-composer)

```
composer require purc/autoloader-class-map
```

**NOTE:** You may need to update your `minimum-stability` definition to `dev` in your `composer.json`in order to install this package.

```
{
    "minimum-stability": "dev"
}
```

### Install via git clone

[](#install-via-git-clone)

Open your command line tool, clone the repository, change to the directory, and run composer.

```
# Clone from GitHub
$ git clone https://github.com/muratpurc/mpAutoloaderClassMap

# Change to the package folder
$ cd mpAutoloaderClassMap

# Run composer
$ composer install
```

### Download from GitHub with bash

[](#download-from-github-with-bash)

Open bash, download the latest version (see the latest tag) from GitHub, extract the archive, change to the directory, and run composer.

```
# Download release
$ curl -s -L https://github.com/muratpurc/mpAutoloaderClassMap/archive/refs/tags/0.2.1.tar.gz -o mpAutoloaderClassMap-0.2.1.tar.gz

# Extract archive
$ tar -xzf mpAutoloaderClassMap-0.2.1.tar.gz

# Change to the extracted folder
$ cd mpAutoloaderClassMap-0.2.1

# Run composer
$ composer install
```

### Manually download from GitHub

[](#manually-download-from-github)

Download the zip package from [GitHub](https://github.com/muratpurc/mpAutoloaderClassMap) and extract it to a folder.

Open your command line tool, change to the extracted folder and run composer.

```
$ composer install
```

---

Description
-----------

[](#description)

Loading required class/interface/trait files, in PHP can be done via different ways.

A project should ideally use the autoloading standard as defined in the [PSR-4](https://www.php-fig.org/psr/psr-4/), but this is not possible in some cases, e.g. when you have to deal with legacy code, which was implemented way before the existence of the PHP Standard Recommendation ([PSR](https://www.php-fig.org/psr/)).

If the project is not PSR-4 compatible and/or there is no way to map automatically required `class`/`interface`/`trait` names to file system location, and you want to get rid of all `require`/`include`statements in your PHP files, then using a class map configuration is probably the convenient solution.

The AutoloaderClassMap parses specific folders for `class`, `interface`, and `trait` definitions, and generates a class map file from the parse results. The class map file can be used with a custom autoloader implementation, which deals with loading the required source files, the first time you use one of the class, interface or trait names in your PHP scripts.

---

Class Map Generator Options
---------------------------

[](#class-map-generator-options)

There a some options to control class map creation described as follows:

### `excludeDirs`

[](#excludedirs)

(`string[]`) List of directories to ignore (note: is case-insensitive)

Default value is `['.svn', '.cvs']`.

### `excludeFiles`

[](#excludefiles)

(`string[]`) List of files to ignore, regexp pattern is also accepted (note: is case insensitive)

Default value is `['/^~*.\.php$/', '/^~*.\.inc$/']`

### `extensionsToParse`

[](#extensionstoparse)

(`string[]`) List of file extensions to parse (note: is case-insensitive)

Default value is `['.php', '.inc']`

### `enableDebug`

[](#enabledebug)

(`bool`) Flag to enable debugging, collects some helpful state information's

Default value is `false`

---

Setting an Environment Variable or PHP $GLOBAL
----------------------------------------------

[](#setting-an-environment-variable-or-php-global)

The AutoloaderClassMap comes with a build-in `Autoloader` implementation, see [Autoloader.php](./src/Autoloader.php).

You need to define the path to your class map file in order to use the build-in `Autoloader`. This can be done by defining an environment variable or by setting the PHP superglobal `$GLOBAL` variable.

### Environment Variable

[](#environment-variable)

There are different ways to set an environment variable, use the solution which fits the best for your needs.

1. Setting the environment variable in a `.env` file, in case your project supports dotenv.

    ```
    PURC_AUTOLOADER_CLASS_MAP_FILE=/path/to/your/class_map_file.php

    ```
2. Setting the environment variable in a `.htaccess` file, in case your project is served via Apache.

    ```
    SetEnv PURC_AUTOLOADER_CLASS_MAP_FILE /path/to/your/class_map_file.php

    ```
3. Setting the environment variable in a PHP script, e.g. at the beginning of the application bootstrap process. This must be done before using an entry defined in the class map file!

    ```
