Giter VIP home page Giter VIP logo

aura.autoload's Introduction

Aura.Autoload

Provides a full PSR-4 and limited PSR-0 autoloader. Although it is installable via Composer, its best use is probably outside a Composer-oriented project.

For a full PSR-0 only autoloader, please see Aura.Autoload v1.

Foreword

Installation

This library requires PHP 5.3 or later; we recommend using the latest available version of PHP as a matter of principle. It has no userland dependencies.

It is installable and autoloadable via Composer as aura/autoload.

Alternatively, download a release or clone this repository, then require or include its autoload.php file.

Quality

Scrutinizer Code Quality Code Coverage Build Status

To run the unit tests at the command line, issue phpunit at the package root. (This requires PHPUnit to be available as phpunit.)

This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Community

To ask questions, provide feedback, or otherwise communicate with the Aura community, please join our Google Group, follow @auraphp on Twitter, or chat with us on #auraphp on Freenode.

Getting Started

To use the autoloader, first instantiate it, then register it with SPL autoloader stack:

<?php
// instantiate
$loader = new \Aura\Autoload\Loader;

// append to the SPL autoloader stack; use register(true) to prepend instead
$loader->register();
?>

PSR-4 Namespace Prefixes

To add a namespace conforming to PSR-4 specifications, point to the base directory for that namespace. Multiple base directories are allowed, and will be searched in the order they are added.

<?php
$loader->addPrefix('Foo\Bar', '/path/to/foo-bar/src');
$loader->addPrefix('Foo\Bar', '/path/to/foo-bar/tests');
?>

To set several namespaces prefixes at once, overriding all previous prefix settings, use setPrefixes().

<?php
$loader->setPrefixes(array(
    'Foo\Bar' => array(
        '/path/to/foo-bar/src',
        '/path/to/foo-bar/tests',
    ),

    'Baz\Dib' => array(
        '/path/to/baz.dib/src',
        '/path/to/baz.dib/tests',
    ),
));
?>

PSR-0 Namespaces

To add a namespace conforming to PSR-0 specifications, one that uses only namespace separators in the class names (no underscores allowed!), point to the directory containing classes for that namespace. Multiple directories are allowed, and will be searched in the order they are added.

<?php
$loader->addPrefix('Baz\Dib', '/path/to/baz-dib/src/Baz/Dib');
$loader->addPrefix('Baz\Dib', '/path/to/baz-dib/tests/Baz/Dib');
?>

To set several namespaces prefixes at once, as with PSR-4, use setPrefixes().

Explicit Class-to-File Mappings

To map a class explictly to a file, use the setClassFile() method.

<?php
$loader->setClassFile('Foo\Bar\Baz', '/path/to/Foo/Bar/Baz.php');
?>

To set several class-to-file mappings at once, overriding all previous mappings, use setClassFiles(). (Alternatively, use addClassFiles() to append to the existing mappings.)

<?php
$loader->setClassFiles(array(
    'Foo\Bar\Baz'  => '/path/to/Foo/Bar/Baz.php',
    'Foo\Bar\Qux'  => '/path/to/Foo/Bar/Qux.php',
    'Foo\Bar\Quux' => '/path/to/Foo/Bar/Quux.php',
));
?>

Inspection and Debugging

These methods are available to inspect the Loader:

  • getPrefixes() returns all the added namespace prefixes and their base directories

  • getClassFiles() returns all the explicit class-to-file mappings

  • getLoadedClasses() returns all the class names loaded by the Loader and the file names for the loaded classes

If a class file cannot be loaded for some reason, review the debug information using getDebug(). This will show a log of information for the most-recent autoload attempt involving the Loader.

<?php
// set the wrong path for Foo\Bar classes
$loader->addPrefix('Foo\Bar', '/wrong/path/to/foo-bar/src');

// this will fail
$baz = new \Foo\Bar\Baz;

// examine the debug information
var_dump($loader->getDebug());
// array(
//     'Loading Foo\\Bar\\Baz',
//     'No explicit class file',
//     'Foo\\Bar\\: /path/to/foo-bar/Baz.php not found',
//     'Foo\\: no base dirs',
//     'Foo\\Bar\\Baz not loaded',
// )
?>

aura.autoload's People

Contributors

dhaaker avatar harikt avatar koriym avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aura.autoload's Issues

Note about v1 and v2 in README.md

@pmjones I am seeing another issue with the v1 and v2 repos. Most of the people may not know we have 2 versions. Consider someone coming to README.md of develop-2 branch of aura/autoload . They read

Provides a PSR-4 (and limited PSR-0) autoloading facility

which is a bad thing I guess. For we already have good support in v1 .

I am thinking whether it is good to keep the branch information also in README.md mentioning develop is v1 , develop-2 is v2 .

What are your thoughts on this ?

Document setMode

In Exact Class Usage @nelsond in #irc was having problem resolving the class .

The problem was though it was having require for other classes it was having a class_exists method which try to load and throws exception .

Static Class loading problem!

How can I load a static class?
Like I wanna use

use \Config;
Config::Value('TEST'); //Which will give a value of TEST

currently it giving following error

Fatal error:  Uncaught Error: Class 'Config' not found in ..\system.php:4
Stack trace:
#0 ..\index.php(4): require()
#1 {main}
  thrown in ..\system.php on line 4

Path has been truncated.

Question about directories and namespaces

Hello guys,

I use this autoloader for a long period of time, but now i have a file with static class in directory like this:
ext/pok4/the_tickets.php

I have a namespace:
namespace ext\pok4;
in file.

Autoloader is:
$loader->setClassFile('ext\pok4\the_tickets', 'ext/pok4/the_tickets/ext.php');

And the router is:

$collection->attachRoute(new PHPRouter\Route('/ajax/ext/:id/:id', [
'_controller' => 'ext\pok4\the_tickets::ajax',

Everything is worknig fine, but i have a question.
Can i use autoloader more global ? Like this:
$loader->addPrefix('ext\\', 'ext/');

i try but is not working...

file_exists and is_readable

I guess is_readable() is a better choice than file_exists() for some have no read permission . Will it makes slow or can we add a require_one in try catch and throw the appropriate exception ? Like FileNotReadable something ?

How to autoload self directory

How can we autoload several name spaces in current folder
Example
$loader->addPrefix('./', DIR . '/');
Is it even possible like it's done in composer?
Thanks

RFC splclassloader

Hi @pmjones ,

Accoding to RFC at php.net , https://wiki.php.net/rfc/splclassloader the method is add , for the naming consistency is that ok that method name is add() than anything ?

public function add($resourceName, $resourcePath = null);

Is that ok that we also make add , than addPrefix , addPrefixes etc ?

please add support for underscore notation

Hello.

I'd like to load a package (Twig) that internally uses the underscore notation (eg Twig_Loader_Filesystem; Twig_LoaderInterface).

Could you please make this work in your loader ? That would be great as I cannot run Composer on my shared host provider. Or do you know any (php only) autoload alternative that does ?

Thanks for considering it.
B.

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.