Git Product home page Git Product logo

classloader's Introduction

ClassLoader Component

Class loader component loads your PHP files automatically as long as they follow some standard naming convention. The following standards are supported:

Install the component

The best way to install the component is using Composer.

composer require webiny/class-loader

For additional versions of the package, visit the Packagist page.

Usage

To use the ClassLoader, get its instance by calling ClassLoader::getInstance() method.

    require_once 'Webiny/Component/ClassLoader/ClassLoader.php'

    use Webiny\Component\ClassLoader;

    ClassLoader::getInstance();

Once you have the ClassLoader instance, you can register map rules. The ClassLoader automatically detects if you are registering a namespace or a PEAR rule. PEAR rules are identified by having a underline '_' at the end of the prefix. If PSR is not defined, the component will use PSR-4 standard. All paths should be absolute.

    ClassLoader::getInstance()->registerMap([
    										// a namespace rule (PSR-4 - default)
    										'Webiny' => realpath(dirname(__FILE__)).'/library/Webiny',
    										// a namespace rule (PSR-0)
    										'Symfony' => [
    										    'Path' => '/var/vendors/Symfony',
    										    'Psr' => 0
    										],
    										// a PEAR rule
    										'Swift_' => realpath(dirname(__FILE__)).'/library/Swift',
    										]);

As you can see the registerMap method takes an array of multiple rules. Each rule consists of a prefix and a location.

For better performance you can provide a Cache component to ClassLoader. Doing so, ClassLoader will cache the paths and files resulting in a faster performance.

    ClassLoader::getLoader()->registerCacheDriver($instanceOfCacheInterface);

Non-standardized libraries

If you have a library that is not following neither the PSR naming convention nor the PEAR naming convention, you'll have to manually define some of the settings.

Let's take a look at this example:

    ClassLoader::getInstance()->registerMap([
        'Smarty_' => [
                        'Path'      => '/var/www/Vendors/Smarty/libs/sysplugins',
                        'Normalize' => false,
                        'Case'      => lower
                     ]
    ]);

You can see that the Smarty_ library is defined as an array that has Path, Normalize and Case parameter.

Path

Defines the path to the library.

Normalize

The Normalize parameter tells the autoloader if he should to change the _, on the class name, into directory separators. For example if you have a class namesSmarty_Internal_Compile the normalized path would be Smarty/Internal/Compiler. If you set the Normalize parameter to false, the original class name will be used.

Case

By default the autoloader transfers all the class names to CamelCase, you can set the Case parameter to lower if you wish that the class names are used in lower case inside the class path.

Resources

To run unit tests, you need to use the following command:

$ cd path/to/Webiny/Component/ClassLoader/
$ composer.phar install
$ phpunit

classloader's People

Contributors

svenalhamad 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.