{"id":22104686,"url":"https://github.com/riimu/kit-classloader","last_synced_at":"2025-07-06T02:38:19.331Z","repository":{"id":4425477,"uuid":"5563410","full_name":"Riimu/Kit-ClassLoader","owner":"Riimu","description":"PSR-0 and PSR-4 compatible class autoloader","archived":false,"fork":false,"pushed_at":"2017-07-16T09:49:54.000Z","size":104,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-13T16:52:41.935Z","etag":null,"topics":["autoloader","php","php-library","psr-0","psr-4"],"latest_commit_sha":null,"homepage":"http://kit.riimu.net","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Riimu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-26T20:28:55.000Z","updated_at":"2020-06-02T08:20:03.000Z","dependencies_parsed_at":"2022-09-15T05:40:42.709Z","dependency_job_id":null,"html_url":"https://github.com/Riimu/Kit-ClassLoader","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-ClassLoader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-ClassLoader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-ClassLoader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-ClassLoader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Riimu","download_url":"https://codeload.github.com/Riimu/Kit-ClassLoader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227501358,"owners_count":17781412,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autoloader","php","php-library","psr-0","psr-4"],"created_at":"2024-12-01T06:34:10.018Z","updated_at":"2024-12-01T06:34:11.106Z","avatar_url":"https://github.com/Riimu.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PSR-0 and PSR-4 Class Autoloader #\n\n*Classloader* is a PHP library for autoloading classes. Class autoloading means\nthat classes are loaded only when they are actually needed instead of having to\ninclude each class file on every execution. This reduces the page loading\noverhead especially on larger websites, as only some of the class files need to\nbe loaded. Usually the classes are also loaded dynamically from files with file\nnames based on the namespace and class name. This also makes it easier to\nmanage a large number of class files.\n\nThis library supports two of the current standards for autoloading classes,\nnamely the [PSR-0](http://www.php-fig.org/psr/psr-0/) and [PSR-4](http://www.php-fig.org/psr/psr-4/).\nThe basic idea behind these standards is that class files reside in directories\nbased on their namespace and in files named after the class. The key difference\nbetween these two standards is that PSR-4 does not require the entire namespace\nto be present in the directory hierarchy.\n\nHowever, since the operation of finding the actual class files tends to be\nrelatively costly, this library also provides basic caching mechanisms that\nallow caching the class file locations in a PHP file. With caching, the \nperformance difference between autoloading and loading the files manually\nbecomes negligible.\n\nThe API documentation, which can be generated using Apigen, can be read online\nat: http://kit.riimu.net/api/classloader/\n\n[![Travis](https://img.shields.io/travis/Riimu/Kit-ClassLoader.svg?style=flat-square)](https://travis-ci.org/Riimu/Kit-ClassLoader)\n[![Scrutinizer](https://img.shields.io/scrutinizer/g/Riimu/Kit-ClassLoader.svg?style=flat-square)](https://scrutinizer-ci.com/g/Riimu/Kit-ClassLoader/)\n[![Scrutinizer Coverage](https://img.shields.io/scrutinizer/coverage/g/Riimu/Kit-ClassLoader.svg?style=flat-square)](https://scrutinizer-ci.com/g/Riimu/Kit-ClassLoader/)\n[![Packagist](https://img.shields.io/packagist/v/riimu/kit-classloader.svg?style=flat-square)](https://packagist.org/packages/riimu/kit-classloader)\n\n## Requirements ##\n\n  * The minimum supported PHP version is 5.6\n\n## Installation ##\n\n### Installation with Composer ###\n\nThe easiest way to install this library is to use Composer to handle your\ndependencies. In order to install this library via Composer, simply follow\nthese two steps:\n\n  1. Acquire the `composer.phar` by running the Composer\n     [Command-line installation](https://getcomposer.org/download/)\n     in your project root.\n\n  2. Once you have run the installation script, you should have the `composer.phar`\n     file in you project root and you can run the following command:\n\n     ```\n     php composer.phar require \"riimu/kit-classloader:^4.4\"\n     ```\n\nAfter installing this library via Composer, you can load the library by\nincluding the `vendor/autoload.php` file that was generated by Composer during\nthe installation.\n\n### Adding the library as a dependency ###\n\nIf you are already familiar with how to use Composer, you may alternatively add\nthe library as a dependency by adding the following `composer.json` file to your\nproject and running the `composer install` command:\n\n```json\n{\n    \"require\": {\n        \"riimu/kit-classloader\": \"^4.4\"\n    }\n}\n```\n\n### Manual installation ###\n\nIf you do not wish to use Composer to load the library, you may also download\nthe library manually by downloading the [latest release](https://github.com/Riimu/Kit-ClassLoader/releases/latest)\nand extracting the `src` folder to your project. You may then include the\nprovided `src/autoload.php` file to load the library classes.\n\n## Usage ##\n\nThe `ClassLoader` supports autoloading as defined by the PSR-0 and PSR-4\nstandards via the methods `addBasePath()` and `addPrefixPath()` respectively.\nYou do not need to understand these standards to use this library, simply use\nthe method that works best for you.\n\n### PSR-0 class autoloading ###\n\nPSR-0 class autoloading defines that class files must be placed in a directory\ntree that reflects their namespace. For example, the class 'Foo\\Bar\\Baz' could\nbe located in a file '/path/to/classes/Foo/Bar/Baz.php'. This method is usually\nthe simplest way to place your class files.\n\nUsing the `addBasePath()` method, you can define the base directories where to\nlook for classes. To load the class mentioned above, you could use the\nfollowing code:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n$loader = new Riimu\\Kit\\ClassLoader\\ClassLoader();\n$loader-\u003eaddBasePath('/path/to/classes/');\n$loader-\u003eregister();\n\n$obj = new Foo\\Bar\\Baz();\n```\n\nIf a specific directory only applies to a specific namespace, you can use the\nsecond parameter to define the namespace as well. However, The directory still\nneeds to point to the base directory for the namespace. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n$loader = new Riimu\\Kit\\ClassLoader\\ClassLoader();\n$loader-\u003eaddBasePath('/path/to/classes/', 'Foo\\Bar');\n$loader-\u003eregister();\n\n$obj = new Foo\\Bar\\Baz();\n```\n\nNote that PSR-0 also states that underscores in the class name are treated as\nnamespace separators (but not underscores in namespaces). So, even if your class\nwas called 'Foo\\Bar_Baz', both of the above examples would still work.\nEven if your class namespaces are defined using underscores in the class name,\nyou need to use backslashes in the namespace argument.\n\n### PSR-4 class autoloading ###\n\nUnlike PSR-0, the PSR-4 class autoloading standard does not require classes to\nbe placed in a directory tree that reflects their entire namespace. Instead,\npart of the namespace can be replaced by a specific path.\n\nFor example, if your class 'Foo\\Bar\\Baz' was located in the file\n'/path/to/Library/Baz.php', you could register the path using `addPrefixPath()`\nand specifying the namespace as demonstrated in the following example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n$loader = new Riimu\\Kit\\ClassLoader\\ClassLoader();\n$loader-\u003eaddPrefixPath('/path/to/Library/', 'Foo\\Bar');\n$loader-\u003eregister();\n\n$obj = new Foo\\Bar\\Baz();\n```\n\nThis allows much shorter directory structures as the entire namespace does not\nneed to be reflected in the directory tree. It's also possible to omit the\nnamespace argument, in which case the path will work the same as paths added via\n`addBasePath()`, except for the fact that underscores in the class name will not\nbe treated as namespace separators.\n\n### Adding multiple paths ###\n\nWhile you could simply call the path adding methods multiple times, it's\npossible to add multiple paths using an array. This usually makes configuration\nmuch easier. For example, you could add multiple base paths by providing an\narray:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n$loader = new Riimu\\Kit\\ClassLoader\\ClassLoader();\n$loader-\u003eaddPrefixPath([\n    '/path/to/classes/',\n    '/other/path/',\n]);\n$loader-\u003eregister();\n```\n\nOr you could add namespace specific paths by providing an associative array that\ndefines the namespaces using the array keys:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n$loader = new Riimu\\Kit\\ClassLoader\\ClassLoader();\n$loader-\u003eaddPrefixPath([\n    'Foo\\Bar' =\u003e '/path/to/classes/',\n    'Other\\Namesapace' =\u003e ['/other/path/', '/some/other'],\n]);\n$loader-\u003eregister();\n```\n\nAs shown in the example above, you can also provide an list of paths for\nspecific namespace.\n\n### Caching ###\n\nLooking for classes in the filesystem on each request is a costly affair. It is\nhighly recommended to cache the file locations so that they do not need to be\nsearched on every request. After all, the class files do not tend to move around\nin the file system.\n\nThis library provides a very simple caching system via `FileCacheClassLoader`.\nThe class stores the file locations in a single PHP file which is loaded on\nevery request instead of searching for the files manually.\n\nThe usage of the `FileCacheClassLoader` does not differ much from using the\n`ClassLoader`. You simply need to provide the path to a writable cache file in\nthe constructor. The file will be used to store the class locations and will be\nrewritten when new class files are discovered. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n$loader = new Riimu\\Kit\\ClassLoader\\FileCacheClassLoader(__DIR__ . '/cache.php');\n$loader-\u003eaddBasePath('/path/to/classes/');\n$loader-\u003eregister();\n```\n\n## Credits ##\n\nThis library is Copyright (c) 2012-2017 Riikka Kalliomäki.\n\nSee LICENSE for license and copying information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friimu%2Fkit-classloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Friimu%2Fkit-classloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friimu%2Fkit-classloader/lists"}