Git Product home page Git Product logo

autoloader's Introduction

Fedora Autoloader

Build Status

Static PSR-4, PSR-0, and classmap autoloader. Includes loader for required and optional dependencies.

Autoloader

PSR-4

\Fedora\Autoloader\Autoload::addPsr4($prefix, $path, $prepend = false)

PSR-0

\Fedora\Autoloader\Autoload::addPsr0($prefix, $path, $prepend = false)

Classmap

\Fedora\Autoloader\Autoload::addClassMap(array $classMap, $path)

phpab Template

Template: res/phpab/fedora.php.tpl

For an example of its' usage, see tests/genclassmap.sh.

Dependencies loader

Loops through provided array of dependencies:

  • If dependency is not an array:
    • If dependency is required, it is only required/loaded if it exists, otherwise a \RuntimeException is thrown.
    • If dependency is not required, it is only required/loaded if it exists.
  • If dependency is an array:
    • Loops through all items until the first item that exists is found and then it is required/loaded. If no item is found and the dependency is required, the last item will be required/loaded if it exists, otherwise a \RuntimeException is thrown.

Required dependencies

\Fedora\Autoloader\Dependencies::required(array $requiredDependencies)

Example 1

\Fedora\Autoloader\Dependencies::required(array(
    '/usr/share/php/RequiredFoo/autoload.php',
    '/usr/share/php/RequiredBar/autoload.php',
));

Equates to:

if (
    is_file('/usr/share/php/RequiredFoo/autoload.php')
    && is_readable('/usr/share/php/RequiredFoo/autoload.php')
) {
    require_once '/usr/share/php/RequiredFoo/autoload.php';
} else {
    throw new \RuntimeException("File not found: '/usr/share/php/RequiredFoo/autoload.php'");
}

if (
    is_file('/usr/share/php/RequiredBar/autoload.php')
    && is_readable('/usr/share/php/RequiredBar/autoload.php')
) {
    require_once '/usr/share/php/RequiredBar/autoload.php';
} else {
    throw new \RuntimeException("File not found: '/usr/share/php/RequiredBar/autoload.php'");
}

Example 2

\Fedora\Autoloader\Dependencies::required(array(
    array(
        '/usr/share/php/RequiredFooVersion1/autoload.php',
        '/usr/share/php/RequiredFooVersion2/autoload.php',
        '/usr/share/php/RequiredFooVersion3/autoload.php',
    ),
));

Equates to:

if (
    is_file('/usr/share/php/RequiredFooVersion1/autoload.php')
    && is_readable('/usr/share/php/RequiredFooVersion1/autoload.php')
) {
    require_once '/usr/share/php/RequiredFooVersion1/autoload.php';
} elseif (
    is_file('/usr/share/php/RequiredFooVersion2/autoload.php')
    && is_readable('/usr/share/php/RequiredFooVersion2/autoload.php')
) {
    require_once '/usr/share/php/RequiredFooVersion2/autoload.php';
} elseif (
    is_file('/usr/share/php/RequiredFooVersion3/autoload.php')
    && is_readable('/usr/share/php/RequiredFooVersion3/autoload.php')
) {
    require_once '/usr/share/php/RequiredFooVersion3/autoload.php';
} else {
    throw new \RuntimeException("Files not found: "
        . "'/usr/share/php/RequiredFooVersion1/autoload.php'"
        . "|| '/usr/share/php/RequiredFooVersion2/autoload.php'"
        . "|| '/usr/share/php/RequiredFooVersion3/autoload.php'"
    );
}

Optional dependencies

\Fedora\Autoloader\Dependencies::optional(array $optionalDependencies)

Example 1

\Fedora\Autoloader\Dependencies::optional(array(
    '/usr/share/php/OptionalFoo/autoload.php',
    '/usr/share/php/OptionalBar/autoload.php',
));

Equates to:

if (
    is_file('/usr/share/php/OptionalFoo/autoload.php')
    && is_readable('/usr/share/php/OptionalFoo/autoload.php')
) {
    require_once '/usr/share/php/OptionalFoo/autoload.php';
}

if (
    is_file('/usr/share/php/OptionalBar/autoload.php')
    && is_readable('/usr/share/php/OptionalBar/autoload.php')
) {
    require_once '/usr/share/php/OptionalBar/autoload.php';
}

Example 2

\Fedora\Autoloader\Dependencies::optional(array(
    array(
        '/usr/share/php/OptionalFooVersion1/autoload.php',
        '/usr/share/php/OptionalFooVersion2/autoload.php',
        '/usr/share/php/OptionalFooVersion3/autoload.php',
    ),
));

Equates to:

if (
    is_file('/usr/share/php/OptionalFooVersion1/autoload.php')
    && is_readable('/usr/share/php/OptionalFooVersion1/autoload.php')
) {
    require_once '/usr/share/php/OptionalFooVersion1/autoload.php';
} elseif (
    is_file('/usr/share/php/OptionalFooVersion2/autoload.php')
    && is_readable('/usr/share/php/OptionalFooVersion2/autoload.php')
) {
    require_once '/usr/share/php/OptionalFooVersion2/autoload.php';
} elseif (
    is_file('/usr/share/php/OptionalFooVersion3/autoload.php')
    && is_readable('/usr/share/php/OptionalFooVersion3/autoload.php')
) {
    require_once '/usr/share/php/OptionalFooVersion3/autoload.php';
}

License

The MIT License (MIT)

autoloader's People

Contributors

remicollet avatar siwinski avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

autoloader's Issues

multi autoloader with alternative versions

We are going to have need for this when multiple components are required and available as multiple versions, to avoid mixing component from different versions

We cannot use:

     \Fedora\Autoloader\Dependencies::required(array(
    array(
        "/usr/share/php/Symfony3/Component/Console/autoload.php",
        "/usr/share/php/Symfony3/Component/DependencyInjection/autoload.php",
    ),
    array(
        "/usr/share/php/Symfony/Component/Console/autoload.php",
        "/usr/share/php/Symfony/Component/DependencyInjection/autoload.php",
    )));

Workaround:

     if (!file_exists($dir = '/usr/share/php/Symfony3') $dir =  '/usr/share/php/Symfony';
     \Fedora\Autoloader\Dependencies::required(array(
        "$dir/Component/Console/autoload.php",
        "$dir/Component/DependencyInjection/autoload.php",
    ));

BTW, we perhaps have to stick with a single version in this case, as we also don't have any way to describe such requirement on RPM side.

Optional dep dependencies improvment

Discover in owncloud stack:

Owncloud pulls Monolog

  • Monolog requires aws/sdk either v2 or v3
  • aws/ask requires guzzle either v5 or v6
  • owncloud requires guzzle v5 only

For now, the workaround is to load autoloader in correct order (Guzzle v5, Aws v2, Monolog)

BTW, we don't have any way in a package (e.g. Monolog) to NOT load aws sdk if already loaded.

I mean

\Fedora\Autoloader\Dependencies::madatory(array(
        '/usr/share/php/Aws/autoload.php',
));

Will load v2 autoloader

\Fedora\Autoloader\Dependencies::optional(array(
    array(
        '/usr/share/php/Aws3/autoload.php',
        '/usr/share/php/Aws/autoload.php',
)));

Will load v3, even if v2 already loaded by a previous call.

Improving this will require to manage a static list of already included files.

How to handle composer's files array?

Hi,
I have searched far and wide for documentation on the fedora autoloader but can't find any good one for a first-time-php-packager on Fedora.
I am having this particular issue: the software I am packaging has a files array in it's composer.json under autoload. How do I handle this with fedora autoloader or for packaging on Fedora in general? These files belong to no specific namespace as far as I can tell.

Project in question: Kopano kdav

autoload without namespace

Hi there !
I used autoloader to package composer in gentoo (gentoo/gentoo#2905),
it went well and it is merged in the main tree now, so thank you for making this lib it helped me a lot.

I am now working on phpunit and some package like https://github.com/sebastianbergmann/php-timer didn't have namespace declaration in the lib, so I am wondering what could be the best usage, to load it with the autoloader (for consistency sake).

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.