PHP Class Loader
PSR-0, PSR-4 and PEAR class loader.



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

To learn what you can do with Webiny Framework, checkout Webiny Platform - open-source content management system.

Explore Webiny Framework components:

  • Amazon currently supports implementation of Amazon S3
  • Annotations component for parsing annotations from a class, method or a property
  • Cache provides several caching libraries like Apc, Couchbase, Memcache and Redis
  • ClassLoader a PSR-0, PSR-4 and PEAR class loader
  • Config a very handy library for parsing YAML, INI, JSON and PHP configuration files
  • Crypt library for encoding, decoding and validating hashes
  • Entity an ODM layer (currently only supports MongoDB)
  • EventManager want to do event-based development, this is a library for you
  • Http library for parsing HTTP requests and creating HTTP responses
  • Image library for image manipulation
  • Logger PSR-3 log components for logging code execution in runtime
  • Mailer component for sending emails
  • Mongo MongoDB class wrapper
  • OAuth2 library for working with OAuth2, currently supports Facebook, LinkedIn and Google+
  • REST fully featured REST library with caching, security and rate control
  • Router handles defining, parsing, creating and matching url routes
  • Security provides authorization and authentication layer
  • ServiceManager want to write truly service based, loosely-coupled code, this library provides that
  • StdLib this component provides object wrappers for Arrays, Strings, Urls and DateTime data types
  • Storage storage abstraction layer that simplifies the way you work with files and directories
  • TemplateEngine provides a layer for rendering view templates and defining template plugins and manipulators
  • TwitterOAuth library for working with Twitter API using Twitter OAuth

SIGN UP FOR EARLY BETA ACCESS:

Lastest from our blog:

The Power of the Community

In the focus of recent feedback that I got, I think there is a need for me to justify some of the decisions we made while developing Webiny Framework.

Share & subscribe: