exorg/autoloader

Expansible Universal Autoloader.

2.0.0 2024-05-18 16:34 UTC

This package is not auto-updated.

Last update: 2024-11-02 18:39:47 UTC


README

PHP workflow

Expansible Universal Autoloader.

Getting Started

Prerequisities

The instruction assumes using the Linux operating system or compatible tools for other operating systems.

Installation

php8.*-cli, Git and Composer required

The recommended way to install Autoloader into the source code of the project is to handle it as code dependency by Composer. Git is needed to fetch packages from version control repositories.

The php8.*-cli is needed for installing Composer.

Autoloader installation

Add to your composer.json file appropriate entry by running the following command

composer require exorg/autoloader

If it's project set-up stage and no one dependency have been installed yet, run

composer install

If another dependencies have been intalled previously, run

composer update

Usage

The simplest way to autoload all needed files in executable file is attaching autoload.php file generated by Composer (after running composer install or composer update command) like in following example

require_once (__DIR__ . '/vendor/autoload.php');

PSR-4 autoloader

.
└── src
    └── Subject
        └── Core
            └── SomeComponent.php
<?php

namespace Vendor\Package\Subject\Core;

class SomeComponent
{

}
<?php

declare(strict_types=1);

require_once (__DIR__ . '/vendor/autoload.php');

use ExOrg\Autoloader\Autoloader;
use ExOrg\Autoloader\Psr4AutoloadingStrategy;

$psr4Strategy = new Psr4AutoloadingStrategy();
$psr4Strategy->registerNamespacePath('Vendor\Package', './src');

$autoloader = new Autoloader();
$autoloader->setAutoloadingStrategy($psr4Strategy);
$autoloader->register();

$component = new Vendor\Package\Subject\Core\SomeComponent();

$autoloader->unregister();

PSR-0 autoloader

.
└── src
    └── Vendor
        └── Package
            └── Subject
                └── Core
                    └── SomeComponent.php
<?php

namespace Vendor\Package\Subject\Core;

class SomeComponent
{

}
<?php

declare(strict_types=1);

require_once (__DIR__ . '/vendor/autoload.php');

use ExOrg\Autoloader\Autoloader;
use ExOrg\Autoloader\Psr0AutoloadingStrategy;

$psr0Strategy = new Psr0AutoloadingStrategy();
$psr0Strategy->registerNamespacePath('Vendor\Package', './src');

$autoloader = new Autoloader();
$autoloader->setAutoloadingStrategy($psr0Strategy);
$autoloader->register();

$component = new Vendor\Package\Subject\Core\SomeComponent();

$autoloader->unregister();

PEAR autoloader

.
└── src
    └── Subject
        └── Core
            └── SomeComponent.php
<?php

class Subject_Core_SomeComponent
{

}
<?php

declare(strict_types=1);

require_once (__DIR__ . '/vendor/autoload.php');

use ExOrg\Autoloader\Autoloader;
use ExOrg\Autoloader\PearAutoloadingStrategy;

$pearStrategy = new PearAutoloadingStrategy();
$pearStrategy->registerPseudonamespacePath('Subject', './src');

$autoloader = new Autoloader();
$autoloader->setAutoloadingStrategy($pearStrategy);
$autoloader->register();

$component = new Subject_Core_SomeComponent();

$autoloader->unregister();

Fixed autoloader

.
└── src
    └── Subject
        └── Core
            └── SomeComponent.php
<?php

namespace Subject\Core;

class SomeComponent
{

}
<?php

declare(strict_types=1);

require_once (__DIR__ . '/vendor/autoload.php');

use ExOrg\Autoloader\Autoloader;
use ExOrg\Autoloader\FixedAutoloadingStrategy;

$fixedStrategy = new FixedAutoloadingStrategy();
$fixedStrategy->registerClassPath('Subject\Core\SomeComponent', './src/Subject/Core/SomeComponent.php');

$autoloader = new Autoloader();
$autoloader->setAutoloadingStrategy($fixedStrategy);
$autoloader->register();

$component = new \Subject\Core\SomeComponent();

$autoloader->unregister();

Recursive autoloader

.
└── src
    └── Subject
        └── Core
            └── SomeComponent.php
<?php

namespace Subject\Core;

class SomeComponent
{

}
<?php

declare(strict_types=1);

require_once (__DIR__ . '/vendor/autoload.php');

use ExOrg\Autoloader\Autoloader;
use ExOrg\Autoloader\RecursiveAutoloadingStrategy;

$recursiveStrategy = new RecursiveAutoloadingStrategy();
$recursiveStrategy->registerPath('./src');

$autoloader = new Autoloader();
$autoloader->setAutoloadingStrategy($recursiveStrategy);
$autoloader->register();

$component = new \Subject\Core\SomeComponent();

$autoloader->unregister();

Tests

Unit tests

This project has unit tests, which has been built with PHPUnit framework and run on Linux operating system.

To run tests, write the following command in your command line inside the main Autoloader project directory

vendor/bin/phpunit tests/

or use a Composer script

composer test

Code style tests

This code follows PSR-1 and PSR-12 standards.

To run tests for code style write the following command in your command line inside the main Autoloader project directory

vendor/bin/phpcs tests/ src/

or use a Composer script

composer sniff

You can also use a Composer script for running both tests and check code style

composer check

Built with

Versioning

This project is versioning according to SemVer versioning standars. All available releases are tagged.

Contributing

Please read CONTRIBUTING.md for details on the code of conduct, and the process for submitting pull requests.

Author

License

This project is licensed under the MIT License - see the LICENSE.md file for details.