{"id":19450840,"url":"https://github.com/morning-train/php-loader","last_synced_at":"2025-10-09T00:37:10.336Z","repository":{"id":41305622,"uuid":"509048900","full_name":"Morning-Train/php-loader","owner":"Morning-Train","description":"A simple class for loading PHP files and optionally initializing PHP classes","archived":false,"fork":false,"pushed_at":"2022-09-02T09:06:19.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-11T21:40:03.305Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Morning-Train.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2022-06-30T11:17:33.000Z","updated_at":"2022-06-30T12:45:40.000Z","dependencies_parsed_at":"2022-08-25T18:03:04.715Z","dependency_job_id":null,"html_url":"https://github.com/Morning-Train/php-loader","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fphp-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fphp-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fphp-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morning-Train%2Fphp-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morning-Train","download_url":"https://codeload.github.com/Morning-Train/php-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250748094,"owners_count":21480778,"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-10T16:39:14.560Z","updated_at":"2025-10-09T00:37:05.293Z","avatar_url":"https://github.com/Morning-Train.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Loader\n\nA simple PHP File or class loader for PHP. Built with PHP.\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Getting Started](#getting-started)\n    - [Installation](#installation)\n- [Dependencies](#dependencies)\n    - [symfony/finder](#symfonyfinder)\n- [Usage](#usage)\n    - [Multiple Paths](#multiple-paths)\n- [Filename Constraints](#filename-constraints)\n    - [Using Multiple file names](#using-multiple-file-names)\n- [Loading Classes](#loading-classes)\n    - [Has Method](#has-method)\n    - [Call Static](#call-static)\n    - [Class or inheritance requirement](#class-or-inheritance-requirement)\n    - [Constructing, invoking or calling](#constructing-invoking-or-calling)\n- [Credits](#credits)\n- [Testing](#testing)\n- [License](#license)\n\n## Introduction\n\nThis package is a tool to help you initialize parts of your projects by loading in all files that match certain filename\nrules in a defined directory.\n\nYou may even initialize the classes contained in these files as long as they are PSR-4 compliant.\n\nFor instance, you may use this tool to load all files in a \"/routes\" directory or all files ending with \"Block.php\" and\ninitialize all found classes that extend `Block` and then call `init` on them.\n\nMore specifically, this tool is made for, but not dependent on, the WP-Framework. Here it is useful for loading in all\nroutes, registering all blocks and initializing all Hooks.\n\n## Getting Started\n\nTo get started install the package as described below in [Installation](#installation).\n\nTo use the tool have a look at [Usage](#usage)\n\n### Installation\n\n```bash\ncomposer require morningtrain/php-loader\n```\n\n## Dependencies\n\n### symfony/finder\n\n[Finder](https://symfony.com/doc/current/components/finder.html) is used to find files in the directory\n\n## Usage\n\nFirst create a Loader using `Loader::create`. This takes an absolute path to the directory you wish to load from as an\nargument and returns a Loader. The Loader is further configured by chaining.\n\nIn its simples form the Loader only needs a path. This will tell it to load all .php files in that directory\nusing `require`\n\n```php\n    // Loading all PHP files in ./MyDir\n    use Morningtrain\\PHPLoader\\Loader;\n    \n    Loader::create(__DIR__ . '/MyDir');\n```\n\n### Multiple Paths\n\nYou may supply an array of full paths to `Loader::create` if you need to handle multiple directories;\n\n```php\n    // Loading all PHP files in ./MyDir and ./MyOtherDir\n    use Morningtrain\\PHPLoader\\Loader;\n    \n    Loader::create([__DIR__ . '/MyDir',__DIR__ . '/MyOtherDir']);\n```\n\n## Filename Constraints\n\nTo limit the loader to only load files with a given name use `fileName(string|array $filename)`\nSee [Symfoni Finder: File Name](https://symfony.com/doc/current/components/finder.html#file-name) for options.\n\nBy default `$fileName` is `*.php`\n\n```php\n    // Loading all PHP files that end with \"Foo\" in ./MyDir\n    use Morningtrain\\PHPLoader\\Loader;\n    \n    Loader::create(__DIR__ . '/MyDir')\n        -\u003efileName('*Foo.php');\n```\n\n### Using Multiple file names\n\nIf you need to allow multiple filename formats then supply an array for `Loader::fileName`\n\n## Loading Classes\n\nAs long as no class related options are set on the Loader it will simply load the files.\n\nThis is useful for route files and similar.\n\nIf you have classes that you wish to load and initialize then read on!\n\n**Note:**\nAll files will be loaded even if the class requirements are not fulfilled. The Loader has no knowledge of its classes\nbefore they are loaded.\n\n### Has Method\n\nAborts handling a found class if it does not have a specific method.\n\n**Note:** it is not necessary to specify `hasMethod` if `call` or `callStatic` is used.\n\n```php\n    // Loading all PHP files in ./MyDir and invoke them if they have the method myMethod\n    use Morningtrain\\PHPLoader\\Loader;\n    \n    Loader::create(__DIR__ . '/MyDir')\n        -\u003ehasMethod('myMethod')\n        -\u003einvoke();\n```\n\n### Call Static\n\nTo call a static method on all loaded classes specify the method using `Loader::callStatic($methodName)`\n\nThis will call said method on every loaded class that has it. You do not need to check using `Loader::hasMethod`\nbeforehand\n\n```php\n    // Loading all PHP files in ./MyDir and call a static method on it if it is of the class Foo\n    use Morningtrain\\PHPLoader\\Loader;\n    \n    Loader::create(__DIR__ . '/MyDir')\n        -\u003eisA(\\Foo::class)\n        -\u003ecallStatic('myMethod');\n```\n\n### Class or inheritance requirement\n\nTo only call methods on classes that are of a given class or extended from it use `Loader::isA($className)`. This works\nthe same way is `ia_a($obj,$class)` where $obj is the found class.\n\nIf the found class does not match the required class then the Loader will stop handling the current class and the class\nwill never be constructed or called.\n\n```php\n    // Loading all PHP files in ./MyDir and call a static method on it if it is of the class Foo\n    use Morningtrain\\PHPLoader\\Loader;\n    \n    Loader::create(__DIR__ . '/MyDir')\n        -\u003eisA(\\Foo::class)\n        -\u003ecallStatic('myMethod');\n```\n\n### Constructing, invoking or calling\n\nYou can also construct an instance from the loaded classes, call a method on an instance or invoke an instance using the\nLoader.\n\nIf you use `Loader::invoke` or `Loader::call` then it is not necessary to use `Loader::construct` as well\n\n```php\n    // Loading all PHP files in ./MyDir, construct them and then call 'myMethod'\n    use Morningtrain\\PHPLoader\\Loader;\n    \n    Loader::create(__DIR__ . '/MyDir')\n        -\u003ecall('myMethod');\n```\n\n## Credits\n\n- [Mathias Munk](https://github.com/mrmoeg)\n- [All Contributors](../../contributors)\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorning-train%2Fphp-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorning-train%2Fphp-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorning-train%2Fphp-loader/lists"}