{"id":18750641,"url":"https://github.com/webiny/classloader","last_synced_at":"2025-04-12T23:32:20.764Z","repository":{"id":20203670,"uuid":"23475084","full_name":"webiny/ClassLoader","owner":"webiny","description":"[READ-ONLY] PHP class loader (autoloader) that supports PEAR, PSR-0 and PSR-4 standards.  (master at Webiny/Framework)","archived":false,"fork":false,"pushed_at":"2017-11-26T21:24:24.000Z","size":26,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-26T18:05:36.186Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.webiny.com/","language":"PHP","has_issues":false,"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/webiny.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.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":"2014-08-29T19:20:16.000Z","updated_at":"2023-05-21T17:35:14.000Z","dependencies_parsed_at":"2022-09-03T21:24:13.522Z","dependency_job_id":null,"html_url":"https://github.com/webiny/ClassLoader","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FClassLoader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FClassLoader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FClassLoader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FClassLoader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webiny","download_url":"https://codeload.github.com/webiny/ClassLoader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647257,"owners_count":21139081,"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":[],"created_at":"2024-11-07T17:12:40.299Z","updated_at":"2025-04-12T23:32:16.753Z","avatar_url":"https://github.com/webiny.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"ClassLoader Component\n=====================\nClass loader component loads your PHP files automatically as long as they follow some standard naming convention.\nThe following standards are supported:\n- PEAR\n- [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)\n- [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md)\n\nInstall the component\n---------------------\nThe best way to install the component is using Composer.\n\n```bash\ncomposer require webiny/class-loader\n```\nFor additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/class-loader).\n\n## Usage\n\nTo use the ClassLoader, get its instance by calling ClassLoader::getInstance() method.\n\n```php\n    require_once 'Webiny/Component/ClassLoader/ClassLoader.php'\n\n    use Webiny\\Component\\ClassLoader;\n\n    ClassLoader::getInstance();\n```\n\nOnce you have the ClassLoader instance, you can register map rules. The ClassLoader automatically detects if you are\nregistering a namespace or a PEAR rule. PEAR rules are identified by having a underline '_' at the end of the prefix.\nIf PSR is not defined, the component will use **PSR-4** standard. All paths should be absolute.\n\n```php\n    ClassLoader::getInstance()-\u003eregisterMap([\n    \t\t\t\t\t\t\t\t\t\t// a namespace rule (PSR-4 - default)\n    \t\t\t\t\t\t\t\t\t\t'Webiny' =\u003e realpath(dirname(__FILE__)).'/library/Webiny',\n    \t\t\t\t\t\t\t\t\t\t// a namespace rule (PSR-0)\n    \t\t\t\t\t\t\t\t\t\t'Symfony' =\u003e [\n    \t\t\t\t\t\t\t\t\t\t    'Path' =\u003e '/var/vendors/Symfony',\n    \t\t\t\t\t\t\t\t\t\t    'Psr' =\u003e 0\n    \t\t\t\t\t\t\t\t\t\t],\n    \t\t\t\t\t\t\t\t\t\t// a PEAR rule\n    \t\t\t\t\t\t\t\t\t\t'Swift_' =\u003e realpath(dirname(__FILE__)).'/library/Swift',\n    \t\t\t\t\t\t\t\t\t\t]);\n```\n\nAs you can see the registerMap method takes an array of multiple rules. Each rule consists of a prefix and a location.\n\nFor better performance you can provide a Cache component to ClassLoader. Doing so, ClassLoader will cache the paths and\nfiles resulting in a faster performance.\n\n```php\n    ClassLoader::getLoader()-\u003eregisterCacheDriver($instanceOfCacheInterface);\n```\n\n## Non-standardized libraries\n\nIf you have a library that is not following neither the PSR naming convention nor the PEAR naming convention, you'll\nhave to manually define some of the settings.\n\nLet's take a look at this example:\n```php\n    ClassLoader::getInstance()-\u003eregisterMap([\n        'Smarty_' =\u003e [\n                        'Path'      =\u003e '/var/www/Vendors/Smarty/libs/sysplugins',\n                        'Normalize' =\u003e false,\n                        'Case'      =\u003e lower\n                     ]\n    ]);\n```\n\nYou can see that the `Smarty_` library is defined as an array that has `Path`, `Normalize` and `Case` parameter.\n\n### `Path`\n\nDefines the path to the library.\n\n### `Normalize`\n\nThe `Normalize` parameter tells the autoloader if he should to change the `_`, on the class name, into directory separators.\nFor example if you have a class names`Smarty_Internal_Compile` the normalized path would be `Smarty/Internal/Compiler`.\nIf you set the `Normalize` parameter to `false`, the original class name will be used.\n\n### `Case`\n\nBy default the autoloader transfers all the class names to CamelCase, you can set the `Case` parameter to `lower` if\nyou wish that the class names are used in lower case inside the class path.\n\nResources\n---------\nTo run unit tests, you need to use the following command:\n\n    $ cd path/to/Webiny/Component/ClassLoader/\n    $ composer.phar install\n    $ phpunit","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fclassloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebiny%2Fclassloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fclassloader/lists"}