Git Product home page Git Product logo

class-loader's Introduction

Class loader

PHP class loader that implements both PSR-0 and PSR-4 autoloading.

image

Features

  • PSR-0 and PSR-4 autoloading
  • class maps
  • custom suffixes
  • composer bridge
  • debug mode

Requirements

  • PHP 7.1.0+

Usage examples

Registering prefixes

<?php

use Kuria\ClassLoader\ClassLoader;

// load the class loader manually
require '/path/to/src/ClassLoader.php';

// create an instance
$debug = true; // true during development, false in production

$classLoader = new ClassLoader($debug);

// register the autoloader
$classLoader->register();

// PSR-4 prefix
$classLoader->addPrefix('Foo\\Bar\\', 'vendor/foo/bar/src');

$classLoader->addPrefixes([
    'Kuria\\Error\\' => 'vendor/kuria/error/src',
    'Foo\\Baz\\' => 'example/foo/baz',
]);

// PSR-0 prefix
$classLoader->addPrefix('Example\\FooBar\\', 'vendor/example/foobar', ClassLoader::PSR0);

$classLoader->addPrefixes([
    'Kuria\\Error\\' => 'vendor/kuria/error/src',
    'Foo_' => 'example/foo',
], ClassLoader::PSR0);

// PSR-4 fallback (empty prefix)
$classLoader->addPrefix('', 'src');

// PSR-0 fallback (empty prefix)
$classLoader->addPrefix('', 'old-code/example', ClassLoader::PSR0);

// single class
$classLoader->addClass('Foo', 'path/to/foo.class.php');

// class map
$classLoader->addClassMap([
    'Bar' => 'path/to/bar.class.php',
    'Baz' => 'path/to/baz.class.php',
]);

Using the composer bridge

The ComposerBridge class can be used to initialize autoloading for packages managed by Composer.

<?php

use Kuria\ClassLoader\ClassLoader;
use Kuria\ClassLoader\ComposerBridge;

require __DIR__ . '/vendor/kuria/class-loader/src/ClassLoader.php';
require __DIR__ . '/vendor/kuria/class-loader/src/ComposerBridge.php';

$classLoader = new ClassLoader();

ComposerBridge::configure($classLoader, __DIR__ . '/vendor');

$classLoader->register();

Disabling prefixes

If you are using an optimized autoloader, you can pass an optional third parameter to configure() to disable prefixes completely. Only the class maps and files will be loaded.

<?php

ComposerBridge::configure($classLoader, __DIR__ . '/vendor', false);

Debug mode

If debug mode is enabled, a check is performed after a file has been loaded to determine whether the class, interface or trait was defined in that file.

This helps detect wrong or misspelled namespaces or class names.

To enable debug mode, call $classLoader->setDebug(true) or pass true to the appropriate constructor argument.

class-loader's People

Contributors

shira-3749 avatar shiranai7 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.